Leet code —Jump Game】的更多相关文章

问题叙述性说明: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For exa…
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 推断一个数整数是不是回文?比如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 只是这里要考虑翻转后,数值…
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母.字母区分大小写,因此"a"和"A"是不同类型的石头. 示例 1: 输入: J = "aA", S = "aAA…
描述: 要求相邻数2进制差一位 先获得n-1的列表表示小于 2^(n-1) 的符合要求的列表,加上最高位的加成 2^(n-1) 就是大于等于 2^(n-1) 的符合要求的列表,后者翻转一下就能够与前者连接上了 代码: class Solution: # @return a list of integers def grayCode(self, n): if n == 0: return [0] s1 = self.grayCode(n - 1) s2 = [item + 2 ** (n - 1)…
描述: 输出全排列 代码: class Solution: # @param num, a list of integer # @return a list of lists of integers def doSth(self, num): result = self.permute(num[1:]) for lst in result: for i in range(len(lst) + 1): yield lst[:i] + num[:1] + lst[i:] def permute(se…
描述: 使用了递归,有些计算是重复的,用了额外的空间,Version 1是m*n Bonus:一共走了m+n步,例如 m = 2, n = 3 [#, @, @, #, @],所以抽象成数学问题,解是C(m + n, m) 代码: class Solution: # @return an integer def __init__(self): self.record = {} def uniquePaths(self, m, n): if m == 0 or n == 0: return 0 i…
描述:注意需要先self.connect(right)再self.connect(left),否则会有case通不过,原因是左边递归执行时依赖与右边的next已经建立,而先执行connect(left)的话右边还没有完成关系的建立. 代码: class Solution: # @param root, a tree node # @return nothing def doSth(self, nextNode, conNode): while nextNode is not None: if n…
描述:log(n) 代码: class Solution: # @param x, an integer # @return an integer def getVal(self, begin, end, x): if end == begin : return begin if end == begin + 1: return begin while True: mid = (begin + end) / 2 tmp = mid * mid if tmp == x: return mid el…
描述:数组 A,对于 i < j, 找到最大的 A[j] - A[i] 代码: class Solution: # @param prices, a list of integer # @return an integer def maxProfit(self, prices): if len(prices) == 0 or len(prices) == 1: return 0 cur_min = prices[0] max_minus = 0 for i in range(1, len(pri…
描述:递归 代码: class Solution: # @param num, a list of integers # @return a tree node def sortedArrayToBST(self, num): if len(num) == 0: return None mid_index = len(num) / 2 tmp_tree = TreeNode(num[mid_index]) tmp_tree.left = self.sortedArrayToBST(num[:mi…