mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 05:20:59 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -174,6 +174,7 @@ const maxSubArray = nums => {
|
||||
// 数组长度,dp初始化
|
||||
const len = nums.length;
|
||||
let dp = new Array(len).fill(0);
|
||||
dp[0] = nums[0];
|
||||
// 最大值初始化为dp[0]
|
||||
let max = dp[0];
|
||||
for (let i = 1; i < len; i++) {
|
||||
|
@ -89,27 +89,26 @@ class Solution {
|
||||
private:
|
||||
bool backtracking (const string& s,
|
||||
const unordered_set<string>& wordSet,
|
||||
vector<int>& memory,
|
||||
vector<bool>& memory,
|
||||
int startIndex) {
|
||||
if (startIndex >= s.size()) {
|
||||
return true;
|
||||
}
|
||||
// 如果memory[startIndex]不是初始值了,直接使用memory[startIndex]的结果
|
||||
if (memory[startIndex] != -1) return memory[startIndex];
|
||||
if (!memory[startIndex]) return memory[startIndex];
|
||||
for (int i = startIndex; i < s.size(); i++) {
|
||||
string word = s.substr(startIndex, i - startIndex + 1);
|
||||
if (wordSet.find(word) != wordSet.end() && backtracking(s, wordSet, memory, i + 1)) {
|
||||
memory[startIndex] = 1; // 记录以startIndex开始的子串是可以被拆分的
|
||||
return true;
|
||||
}
|
||||
}
|
||||
memory[startIndex] = 0; // 记录以startIndex开始的子串是不可以被拆分的
|
||||
memory[startIndex] = false; // 记录以startIndex开始的子串是不可以被拆分的
|
||||
return false;
|
||||
}
|
||||
public:
|
||||
bool wordBreak(string s, vector<string>& wordDict) {
|
||||
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
|
||||
vector<int> memory(s.size(), -1); // -1 表示初始化状态
|
||||
vector<bool> memory(s.size(), 1); // -1 表示初始化状态
|
||||
return backtracking(s, wordSet, memory, 0);
|
||||
}
|
||||
};
|
||||
|
@ -325,6 +325,20 @@ var lowestCommonAncestor = function(root, p, q) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
|
||||
if (root === null || root === p || root === q) return root;
|
||||
const left = lowestCommonAncestor(root.left, p, q);
|
||||
const right = lowestCommonAncestor(root.right, p, q);
|
||||
if (left !== null && right !== null) return root;
|
||||
if (left !== null) return left;
|
||||
if (right !== null) return right;
|
||||
return null;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -37,7 +37,7 @@
|
||||
|
||||
1. dp[i]的定义
|
||||
|
||||
**dp[i]表示i之前包括i的最长上升子序列的长度**。
|
||||
**dp[i]表示i之前包括i的以nums[i]结尾最长上升子序列的长度**
|
||||
|
||||
2. 状态转移方程
|
||||
|
||||
|
@ -148,23 +148,19 @@ java版本1中创建了String数组,多次使用Integer.parseInt了方法,
|
||||
版本2
|
||||
class Solution {
|
||||
public int monotoneIncreasingDigits(int n) {
|
||||
if (n==0)return 0;
|
||||
char[] chars= Integer.toString(n).toCharArray();
|
||||
int start=Integer.MAX_VALUE;//start初始值设为最大值,这是为了防止当数字本身是单调递增时,没有一位数字需要改成9的情况
|
||||
for (int i=chars.length-1;i>0;i--){
|
||||
if (chars[i]<chars[i-1]){
|
||||
chars[i-1]--;
|
||||
start=i;
|
||||
String s = String.valueOf(n);
|
||||
char[] chars = s.toCharArray();
|
||||
int start = s.length();
|
||||
for (int i = s.length() - 2; i >= 0; i--) {
|
||||
if (chars[i] > chars[i + 1]) {
|
||||
chars[i]--;
|
||||
start = i+1;
|
||||
}
|
||||
}
|
||||
StringBuilder res=new StringBuilder();
|
||||
for (int i=0;i<chars.length;i++){
|
||||
if (chars[i]=='0'&&i==0)continue;//防止出现09这样的情况
|
||||
if (i>=start){
|
||||
res.append('9');
|
||||
}else res.append(chars[i]);
|
||||
for (int i = start; i < s.length(); i++) {
|
||||
chars[i] = '9';
|
||||
}
|
||||
return Integer.parseInt(res.toString());
|
||||
return Integer.parseInt(String.valueOf(chars));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user