上一页 1 2 3 4 5 6 7 8 ··· 10 下一页
摘要: 暴力法 思路: 递归枚举出所有的可能。 class Solution: def climbStairs(self, n: int) -> int: def process(i,n): if i == n: return 1 if i > n: return 0 return process(i+1, 阅读全文
posted @ 2020-06-10 12:30 nil_f 阅读(148) 评论(0) 推荐(0)
摘要: 二分法 思路: 从0到x不断二分尝试。 代码: class Solution: def mySqrt(self, x: int) -> int: l, r, ans = 0, x, -1 while l <= r: mid = (l + r) // 2 if mid * mid <= x: ans 阅读全文
posted @ 2020-06-09 23:22 nil_f 阅读(175) 评论(0) 推荐(0)
摘要: 一维转二维再转一维 思路: 先根据最大长度条件把一维数组转换为二维数组,二维数组中的每个数组是结果中每个字符串包含的所有单词。再对二维数组中每个数组进行加空格处理,这里要注意的是,要对最后一行单独处理。 代码: class Solution: def fullJustify(self, words: 阅读全文
posted @ 2020-06-09 22:25 nil_f 阅读(155) 评论(0) 推荐(0)
摘要: 逐位计算 思路: 遍历字符串,逐位加和,用一个变量记录是否产生进位。 class Solution: def addBinary(self, a: str, b: str) -> str: res = '' if len(a)<len(b): a,b = b,a temp = 0 for i in 阅读全文
posted @ 2020-06-08 18:07 nil_f 阅读(107) 评论(0) 推荐(0)
摘要: 一次遍历 思路: 从后向前遍历数组,把相应位置数字加一,同时对10取余,如果不为0,则直接返回加1后的数组,如果为0,代表当前位置的数为9,继续遍历。如果遍历结束都没有返回结果,代表每一位数字均为9,则加一后的结果为原数组长度加一,第一位为1,其余为0。 代码: class Solution: de 阅读全文
posted @ 2020-06-08 17:36 nil_f 阅读(151) 评论(0) 推荐(0)
摘要: DFA法 思路: 确定的有穷自动机,相关参考8. 字符串转换整数 (atoi) 遍历字符串,遇到的字符总共有6种。(空格,正负号,数字,小数点,字符e,无效字符) 可能出现的状态有11种: start:初始状态 signed:符号态 integer:整数态 sDot:特殊的初始小数点态(小数点之前为 阅读全文
posted @ 2020-06-07 16:20 nil_f 阅读(176) 评论(0) 推荐(0)
摘要: 动态规划 思路: dp[i][j]表示走完 i,j 位置所需的最短路径,由于只能向下或者向右,所以第一行和第一列中每个值为当前值加上前一个值,即dp[0][j]=dp[0] [j]+dp[0][j-1],dp[i][0] = dp[i][0]+dp[i-1][0]。非第一行和第一列的值为当前值加上其 阅读全文
posted @ 2020-06-07 13:53 nil_f 阅读(110) 评论(0) 推荐(0)
摘要: 动态规划 思路: 参考62. 不同路径 代码: class Solution: def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: m = len(obstacleGrid) n = len(obstac 阅读全文
posted @ 2020-06-07 13:35 nil_f 阅读(123) 评论(0) 推荐(0)
摘要: 找规律 思路: 由题意可知,由于只能向下或向右移动,所以总共只需要走(m-1)+(n-1)= m+n-2步。而只需要确定m-1步向右的步数或者确定向下的n-1步,即可确定路径,所以共有或者种可能。 代码: class Solution: def uniquePaths(self, m: int, n 阅读全文
posted @ 2020-06-06 15:21 nil_f 阅读(119) 评论(0) 推荐(0)
摘要: 先闭环后开环 思路: 先找到链表最后一个非空节点tail,通过tail.next=head使链表形成闭环。接着从头开始找到第n-k%n 个节点当作new_tail,new_tail.next为new_head,接着再令new_tail.next为null进行开环,返回new_head即可。 代码: 阅读全文
posted @ 2020-06-06 13:24 nil_f 阅读(106) 评论(0) 推荐(0)
上一页 1 2 3 4 5 6 7 8 ··· 10 下一页