原文链接: https://leetcode-cn.com/problems/bracket-lcci 英文原文Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n pairs of parentheses. Note: The result set should not contain duplicated subsets. For example, give…
标签详情
动态规划 分类
原文链接: https://leetcode-cn.com/problems/pile-box-lcci 英文原文You have a stack of n boxes, with widths wi, depths di, and heights hi. The boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger tha…
原文链接: https://leetcode-cn.com/problems/reverse-bits-lcci 英文原文You have an integer and you can flip exactly one bit from a 0 to a 1. Write code to find the length of the longest sequence of 1s you could create. Example 1: Input: num = 1775(110111011112…
原文链接: https://leetcode-cn.com/problems/coin-lcci 英文原文Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent), write code to calculate the number of ways of representing n cents. (The result may be la…
原文链接: https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof 中文题目写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。 答案需要取模 1e9+7(1000000007),如…
原文链接: https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof 中文题目一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。 示例 1: 输入:n = 2 输出:2 示例 2: 输入:n = 7 输出:21 示例 3: 输入:n = 0 输出:1 提示: 0 <= n …
原文链接: https://leetcode-cn.com/problems/jian-sheng-zi-lcof 中文题目给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]...k[m-1] 。请问 k[0]*k[1]*...*k[m-1] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。 示例 1: 输入: 2 输出: 1 解释: 2 = 1 …
原文链接: https://leetcode-cn.com/problems/jian-sheng-zi-ii-lcof 中文题目给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]...k[m - 1] 。请问 k[0]*k[1]*...*k[m - 1] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。 答案需要取模 1e9+7(10000000…
原文链接: https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof 中文题目请实现一个函数用来匹配包含'. '和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但与"aa.a"和"ab*a"均不匹配。 示例 1: 输入: s = "aa" p = "a"…
原文链接: https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof 中文题目输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。 要求时间复杂度为O(n)。 示例1: 输入: nums = [-2,1,-3,4,-1,2,1,-5,4] 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 提示: 1 <= arr.length <= 10^5 -100 <= …
原文链接: https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof 中文题目输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数。 例如,输入12,1~12这些整数中包含1 的数字有1、10、11和12,1一共出现了5次。 示例 1: 输入:n = 12 输出:5 示例 2: 输入:n = 13 输出:6 限制: 1
原文链接: https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof 中文题目在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物? 示例 1: 输入: [ [1,3,1], [1,5,1], [4,2,1] ] 输出: 12 解释: 路径 1…
原文链接: https://leetcode-cn.com/problems/chou-shu-lcof 中文题目我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 说明: 1 是丑数。 n 不超过1690。 注意:本题与主站 264 题相同:https://leetcode-cn.com/probl…
原文链接: https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof 中文题目把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。 你需要用一个浮点数数组返回答案,其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。 示例 1: 输入: 1 输出: [0.16667,0.16667,0.16667,0.16667,0.16667,0.16667] 示例 2: 输入…
原文链接: https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof 中文题目假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少? 示例 1: 输入: [7,1,5,3,6,4] 输出: 5 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。 示例…
原文链接: https://leetcode-cn.com/problems/contiguous-sequence-lcci 英文原文You are given an array of integers (both positive and negative). Find the contiguous sequence with the largest sum. Return the sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4] Output: 6 …
原文链接: https://leetcode-cn.com/problems/bst-sequences-lcci 英文原文A binary search tree was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays …
原文链接: https://leetcode-cn.com/problems/robot-in-a-grid-lcci 英文原文Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can only move in two directions, right and down, but certain cells are "off limits" such tha…
原文链接: https://leetcode-cn.com/problems/re-space-lcci 英文原文Oh, no! You have accidentally removed all spaces, punctuation, and capitalization in a lengthy document. A sentence like "I reset the computer. It still didn't boot!" became "iresetthecomputeri…
原文链接: https://leetcode-cn.com/problems/the-masseuse-lcci 英文原文A popular masseuse receives a sequence of back-to-back appointment requests and is debating which ones to accept. She needs a break between appointments and therefore she cannot accept any …
原文链接: https://leetcode-cn.com/problems/boolean-evaluation-lcci 英文原文Given a boolean expression consisting of the symbols 0 (false), 1 (true), & (AND), | (OR), and ^ (XOR), and a desired boolean result value result, implement a function to count the nu…
原文链接: https://leetcode-cn.com/problems/number-of-2s-in-range-lcci 英文原文Write a method to count the number of 2s that appear in all the numbers between 0 and n (inclusive). Example: Input: 25 Output: 9 Explanation: (2, 12, 20, 21, 22, 23, 24, 25)(Note …
原文链接: https://leetcode-cn.com/problems/circus-tower-lcci 英文原文A circus is designing a tower routine consisting of people standing atop one anoth­er's shoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than…
原文链接: https://leetcode-cn.com/problems/get-kth-magic-number-lcci 英文原文Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. Note that 3, 5, and 7 do not have to be factors, but it should not have any other prime …
原文链接: https://leetcode-cn.com/problems/max-black-square-lcci 英文原文Imagine you have a square matrix, where each cell (pixel) is either black or white Design an algorithm to find the maximum subsquare such that all four borders are filled with black pix…
原文链接: https://leetcode-cn.com/problems/max-submatrix-lcci 英文原文Given an NxM matrix of positive and negative integers, write code to find the submatrix with the largest possible sum. Return an array [r1, c1, r2, c2], where r1, c1 are the row number and…
原文链接: https://leetcode-cn.com/problems/qie-fen-shu-zu 英文原文 中文题目给定一个整数数组 nums ,小李想将 nums 切割成若干个非空子数组,使得每个子数组最左边的数和最右边的数的最大公约数大于 1 。为了减少他的工作量,请求出最少可以切成多少个子数组。 示例 1: 输入:nums = [2,3,3,2,3,3] 输出:2 解释:最优切割为 [2,3,3,2] 和 [3,3] 。第一个子数组头尾数字的最大公约数为 2 ,第二个子数组头尾数…
原文链接: https://leetcode-cn.com/problems/er-cha-shu-ren-wu-diao-du 英文原文 中文题目任务调度优化是计算机性能优化的关键任务之一。在任务众多时,不同的调度策略可能会得到不同的总体执行时间,因此寻求一个最优的调度方案是非常有必要的。 通常任务之间是存在依赖关系的,即对于某个任务,你需要先完成他的前导任务(如果非空),才能开始执行该任务。我们保证任务的依赖关系是一棵二叉树,其中 root 为根任务,root.left 和 root.rig…
原文链接: https://leetcode-cn.com/problems/chuan-di-xin-xi 英文原文 中文题目小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下: 有 n 名玩家,所有玩家编号分别为 0 ~ n-1,其中小朋友 A 的编号为 0 每个玩家都有固定的若干个可传信息的其他玩家(也可能没有)。传信息的关系是单向的(比如 A 可以向 B 传信息,但 B 不能向 A 传信息)。 每轮信息必须需要传递给另一个人,且信息可重复经过同一个人 给定总玩家数 n,以及按 …
原文链接: https://leetcode-cn.com/problems/zui-xiao-tiao-yue-ci-shu 英文原文 中文题目为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机。游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1。初始有一个小球在编号 0 的弹簧处。若小球在编号为 i 的弹簧处,通过按动弹簧,可以选择把小球向右弹射 jump[i] 的距离,或者向左弹射到任意左侧弹簧的位置。也就是说,在编号为 i 弹簧处按动弹簧,小球可以弹向 0 到 i-1 中任意弹…