← 返回标签列表

#leetcode

9 篇文章

链表反转

leetcode 206 单链表反转12输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL 迭代方法首先设置pre,cur,lat三个指针 12pre cur latnull 1 -&gt...

516.最长回文子序列

题目 给定字符串s,求其最长回文子序列(可以非连续)的长度 DP 当已知一个序列是回文时,添加首尾元素后的序列存在两种情况,一种是首尾元素相等,则最长回文的长度加2,当首尾元素不相等,则最长回文序列为仅添加首元素时的最长回文与仅添加尾元素时的最长回文之间的最大值。我们可以用$dp[i][j]...

53.最大子序列和

对于给定序列,得到最大和的子序列 Example: 123>Input: [-2,1,-3,4,-1,2,1,-5,4],>Output: 6>Explanation: [4,-1,2,1] has the largest sum = 6. brute force 遍历所有的...

122.Best Time to Buy and Sell Stock II

与121不同的在于,121只能操作一次,而这个是可以操作任意次。 123Input: [7,1,5,3,6,4]Output: 7Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = ...

70. climbing stairs

描述 n阶楼梯,每次一步或者两步,一共有多少种方法 Solutions brute_force $f(n)=f(n-1)+f(n-2)$ 显然有,到第n阶楼梯有两种方法,从n-1过去,和n-2过去。即到n阶的方法等于这两种方法的和 1234def brute_force(n): if ...

93. Restore IP Address

Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example: 12Input: "25525511135"Ou...