Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 19 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
568a569
添加(1143.最长公共子序列.md):增加typescript版本
xiaofei-2020 May 19, 2022
b3335f7
添加 763.划分字母区间 补充思路的Java代码实现
May 19, 2022
30c1be1
添加(1035.不相交的线.md):增加typescript版本
xiaofei-2020 May 19, 2022
4d2b80c
添加(0053.最大子序和动态规划.md):增加typescript版本
xiaofei-2020 May 19, 2022
8759349
添加(0392.判断子序列.md):增加typescript版本
xiaofei-2020 May 19, 2022
4eefc54
添加(0115.不同的子序列.md):增加typescript版本
xiaofei-2020 May 20, 2022
58c4ad4
添加(0583.两个字符串的删除操作.md):增加typescript版本
xiaofei-2020 May 20, 2022
fa3a45c
添加(0072.编辑距离.md):增加typescript版本
xiaofei-2020 May 20, 2022
be7d6e1
添加 0226.翻转二叉树.md Scala版本
wzqwtt May 20, 2022
81d4685
添加 0101.对称二叉树.md Scala版本
wzqwtt May 20, 2022
7163601
Merge pull request #1358 from xiaofei-2020/dp44
youngyangyang04 Jun 12, 2022
a054544
Merge pull request #1359 from zhouchaoyu1/master
youngyangyang04 Jun 13, 2022
6b8692c
Merge pull request #1361 from xiaofei-2020/dp45
youngyangyang04 Jun 13, 2022
3071e3b
Merge pull request #1362 from xiaofei-2020/dp46
youngyangyang04 Jun 13, 2022
7886da3
Merge pull request #1364 from xiaofei-2020/dp47
youngyangyang04 Jun 13, 2022
9727003
Merge pull request #1366 from xiaofei-2020/dp48
youngyangyang04 Jun 13, 2022
6d130e7
Merge pull request #1367 from xiaofei-2020/dp49
youngyangyang04 Jun 13, 2022
f9aeae5
Merge pull request #1368 from xiaofei-2020/dp50
youngyangyang04 Jun 13, 2022
e9cdb3e
Merge pull request #1369 from wzqwtt/tree06
youngyangyang04 Jun 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions problems/0053.最大子序和(动态规划).md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ const maxSubArray = nums => {
};
```

TypeScript:

```typescript
function maxSubArray(nums: number[]): number {
/**
dp[i]:以nums[i]结尾的最大和
*/
const dp: number[] = []
dp[0] = nums[0];
let resMax: number = 0;
for (let i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
resMax = Math.max(resMax, dp[i]);
}
return resMax;
};
```



-----------------------
Expand Down
37 changes: 37 additions & 0 deletions problems/0072.编辑距离.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,42 @@ const minDistance = (word1, word2) => {
};
```

TypeScript:

```typescript
function minDistance(word1: string, word2: string): number {
/**
dp[i][j]: word1前i个字符,word2前j个字符,最少操作数
dp[0][0]=0:表示word1前0个字符为'', word2前0个字符为''
*/
const length1: number = word1.length,
length2: number = word2.length;
const dp: number[][] = new Array(length1 + 1).fill(0)
.map(_ => new Array(length2 + 1).fill(0));
for (let i = 0; i <= length1; i++) {
dp[i][0] = i;
}
for (let i = 0; i <= length2; i++) {
dp[0][i] = i;
}
for (let i = 1; i <= length1; i++) {
for (let j = 1; j <= length2; j++) {
if (word1[i - 1] === word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(
dp[i - 1][j],
dp[i][j - 1],
dp[i - 1][j - 1]
) + 1;
}
}
}
return dp[length1][length2];
};
```



-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
20 changes: 20 additions & 0 deletions problems/0101.对称二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -725,5 +725,25 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
}
```

## Scala

递归:
```scala
object Solution {
def isSymmetric(root: TreeNode): Boolean = {
if (root == null) return true // 如果等于空直接返回true
def compare(left: TreeNode, right: TreeNode): Boolean = {
if (left == null && right == null) return true // 如果左右都为空,则为true
if (left == null && right != null) return false // 如果左空右不空,不对称,返回false
if (left != null && right == null) return false // 如果左不空右空,不对称,返回false
// 如果左右的值相等,并且往下递归
left.value == right.value && compare(left.left, right.right) && compare(left.right, right.left)
}
// 分别比较左子树和右子树
compare(root.left, root.right)
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
30 changes: 30 additions & 0 deletions problems/0115.不同的子序列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,36 @@ const numDistinct = (s, t) => {
};
```

TypeScript:

```typescript
function numDistinct(s: string, t: string): number {
/**
dp[i][j]: s前i个字符,t前j个字符,s子序列中t出现的个数
dp[0][0]=1, 表示s前0个字符为'',t前0个字符为''
*/
const sLen: number = s.length,
tLen: number = t.length;
const dp: number[][] = new Array(sLen + 1).fill(0)
.map(_ => new Array(tLen + 1).fill(0));
for (let m = 0; m < sLen; m++) {
dp[m][0] = 1;
}
for (let i = 1; i <= sLen; i++) {
for (let j = 1; j <= tLen; j++) {
if (s[i - 1] === t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[sLen][tLen];
};
```




-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
48 changes: 48 additions & 0 deletions problems/0226.翻转二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,53 @@ func invertTree(_ root: TreeNode?) -> TreeNode? {
}
```

### Scala

深度优先遍历(前序遍历):
```scala
object Solution {
def invertTree(root: TreeNode): TreeNode = {
if (root == null) return root
// 递归
def process(node: TreeNode): Unit = {
if (node == null) return
// 翻转节点
val curNode = node.left
node.left = node.right
node.right = curNode
process(node.left)
process(node.right)
}
process(root)
root
}
}
```

广度优先遍历(层序遍历):
```scala
object Solution {
import scala.collection.mutable
def invertTree(root: TreeNode): TreeNode = {
if (root == null) return root
val queue = mutable.Queue[TreeNode]()
queue.enqueue(root)
while (!queue.isEmpty) {
val len = queue.size
for (i <- 0 until len) {
var curNode = queue.dequeue()
if (curNode.left != null) queue.enqueue(curNode.left)
if (curNode.right != null) queue.enqueue(curNode.right)
// 翻转
var tmpNode = curNode.left
curNode.left = curNode.right
curNode.right = tmpNode
}
}
root
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
25 changes: 25 additions & 0 deletions problems/0392.判断子序列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,32 @@ const isSubsequence = (s, t) => {
};
```

TypeScript:

```typescript
function isSubsequence(s: string, t: string): boolean {
/**
dp[i][j]: s的前i-1个,t的前j-1个,最长公共子序列的长度
*/
const sLen: number = s.length,
tLen: number = t.length;
const dp: number[][] = new Array(sLen + 1).fill(0)
.map(_ => new Array(tLen + 1).fill(0));
for (let i = 1; i <= sLen; i++) {
for (let j = 1; j <= tLen; j++) {
if (s[i - 1] === t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[sLen][tLen] === s.length;
};
```

Go:

```go
func isSubsequence(s string, t string) bool {
dp := make([][]int,len(s)+1)
Expand Down
61 changes: 61 additions & 0 deletions problems/0583.两个字符串的删除操作.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,67 @@ const minDistance = (word1, word2) => {
};
```

TypeScript:

> dp版本一:

```typescript
function minDistance(word1: string, word2: string): number {
/**
dp[i][j]: word1前i个字符,word2前j个字符,所需最小步数
dp[0][0]=0: word1前0个字符为'', word2前0个字符为''
*/
const length1: number = word1.length,
length2: number = word2.length;
const dp: number[][] = new Array(length1 + 1).fill(0)
.map(_ => new Array(length2 + 1).fill(0));
for (let i = 0; i <= length1; i++) {
dp[i][0] = i;
}
for (let i = 0; i <= length2; i++) {
dp[0][i] = i;
}
for (let i = 1; i <= length1; i++) {
for (let j = 1; j <= length2; j++) {
if (word1[i - 1] === word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + 1;
}
}
}
return dp[length1][length2];
};
```

> dp版本二:

```typescript
function minDistance(word1: string, word2: string): number {
/**
dp[i][j]: word1前i个字符,word2前j个字符,最长公共子序列的长度
dp[0][0]=0: word1前0个字符为'', word2前0个字符为''
*/
const length1: number = word1.length,
length2: number = word2.length;
const dp: number[][] = new Array(length1 + 1).fill(0)
.map(_ => new Array(length2 + 1).fill(0));
for (let i = 1; i <= length1; i++) {
for (let j = 1; j <= length2; j++) {
if (word1[i - 1] === word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
}
}
}
const maxLen: number = dp[length1][length2];
return length1 + length2 - maxLen * 2;
};
```




-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
65 changes: 65 additions & 0 deletions problems/0763.划分字母区间.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,71 @@ class Solution {
return list;
}
}

class Solution{
/*解法二: 上述c++补充思路的Java代码实现*/

public int[][] findPartitions(String s) {
List<Integer> temp = new ArrayList<>();
int[][] hash = new int[26][2];//26个字母2列 表示该字母对应的区间

for (int i = 0; i < s.length(); i++) {
//更新字符c对应的位置i
char c = s.charAt(i);
if (hash[c - 'a'][0] == 0) hash[c - 'a'][0] = i;

hash[c - 'a'][1] = i;

//第一个元素区别对待一下
hash[s.charAt(0) - 'a'][0] = 0;
}


List<List<Integer>> h = new LinkedList<>();
//组装区间
for (int i = 0; i < 26; i++) {
//if (hash[i][0] != hash[i][1]) {
temp.clear();
temp.add(hash[i][0]);
temp.add(hash[i][1]);
//System.out.println(temp);
h.add(new ArrayList<>(temp));
// }
}
// System.out.println(h);
// System.out.println(h.size());
int[][] res = new int[h.size()][2];
for (int i = 0; i < h.size(); i++) {
List<Integer> list = h.get(i);
res[i][0] = list.get(0);
res[i][1] = list.get(1);
}

return res;

}

public List<Integer> partitionLabels(String s) {
int[][] partitions = findPartitions(s);
List<Integer> res = new ArrayList<>();
Arrays.sort(partitions, (o1, o2) -> Integer.compare(o1[0], o2[0]));
int right = partitions[0][1];
int left = 0;
for (int i = 0; i < partitions.length; i++) {
if (partitions[i][0] > right) {
//左边界大于右边界即可纪委一次分割
res.add(right - left + 1);
left = partitions[i][0];
}
right = Math.max(right, partitions[i][1]);

}
//最右端
res.add(right - left + 1);
return res;

}
}
```

### Python
Expand Down
24 changes: 24 additions & 0 deletions problems/1035.不相交的线.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,30 @@ const maxUncrossedLines = (nums1, nums2) => {
};
```

TypeScript:

```typescript
function maxUncrossedLines(nums1: number[], nums2: number[]): number {
/**
dp[i][j]: nums1前i-1个,nums2前j-1个,最大连线数
*/
const length1: number = nums1.length,
length2: number = nums2.length;
const dp: number[][] = new Array(length1 + 1).fill(0)
.map(_ => new Array(length2 + 1).fill(0));
for (let i = 1; i <= length1; i++) {
for (let j = 1; j <= length2; j++) {
if (nums1[i - 1] === nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[length1][length2];
};
```




Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /