Merge branch 'youngyangyang04:master' into master

This commit is contained in:
xsduan98
2021-07-23 16:05:00 +08:00
committed by GitHub
6 changed files with 87 additions and 11 deletions

View File

@ -9,6 +9,8 @@
## 24. 两两交换链表中的节点
https://leetcode-cn.com/problems/swap-nodes-in-pairs/
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

View File

@ -188,15 +188,10 @@ Java
```java
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] != o2[0]) {
return Integer.compare(o2[0],o1[0]);
} else {
return Integer.compare(o1[1],o2[1]);
}
}
// 身高从大到小排身高相同k小的站前面
Arrays.sort(people, (a, b) -> {
if (a[0] == b[0]) return a[1] - b[1];
return b[0] - a[0];
});
LinkedList<int[]> que = new LinkedList<>();

View File

@ -97,7 +97,18 @@ public:
Java:
Python:
```python3
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
dp = [-1] * len(nums)
stack = []
for i in range(len(nums)*2):
while(len(stack) != 0 and nums[i%len(nums)] > nums[stack[-1]]):
dp[stack[-1]] = nums[i%len(nums)]
stack.pop()
stack.append(i%len(nums))
return dp
```
Go:
JavaScript:

View File

@ -214,7 +214,29 @@ func longestPalindromeSubseq(s string) int {
}
```
Javascript
```javascript
const longestPalindromeSubseq = (s) => {
const strLen = s.length;
let dp = Array.from(Array(strLen), () => Array(strLen).fill(0));
for(let i = 0; i < strLen; i++) {
dp[i][i] = 1;
}
for(let i = strLen - 1; i >= 0; i--) {
for(let j = i + 1; j < strLen; j++) {
if(s[i] === s[j]) {
dp[i][j] = dp[i+1][j-1] + 2;
} else {
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][strLen - 1];
};
```
-----------------------

View File

@ -199,6 +199,52 @@ func replaceSpace(s string) string {
python
```python
class Solution(object):
def replaceSpace(self, s):
"""
:type s: str
:rtype: str
"""
list_s = list(s)
# 记录原本字符串的长度
original_end = len(s)
# 将空格改成%20 使得字符串总长增长 2nn为原本空格数量。
# 所以记录空格数量就可以得到目标字符串的长度
n_space = 0
for ss in s:
if ss == ' ':
n_space += 1
list_s += ['0'] * 2 * n_space
# 设置左右指针位置
left, right = original_end - 1, len(list_s) - 1
# 循环直至左指针越界
while left >= 0:
if list_s[left] == ' ':
list_s[right] = '0'
list_s[right - 1] = '2'
list_s[right - 2] = '%'
right -= 3
else:
list_s[right] = list_s[left]
right -= 1
left -= 1
# 将list变回str输出
s = ''.join(list_s)
return s
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)