Skip to content

Commit c72e90c

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents ae12518 + 5675e7b commit c72e90c

22 files changed

+607
-27
lines changed

problems/0031.下一个排列.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,50 @@ class Solution {
120120
```
121121

122122
## Python
123-
123+
>直接使用sorted()不符合题意
124+
```python
125+
class Solution:
126+
def nextPermutation(self, nums: List[int]) -> None:
127+
"""
128+
Do not return anything, modify nums in-place instead.
129+
"""
130+
for i in range(len(nums)-1, -1, -1):
131+
for j in range(len(nums)-1, i, -1):
132+
if nums[j] > nums[i]:
133+
nums[j], nums[i] = nums[i], nums[j]
134+
nums[i+1:len(nums)] = sorted(nums[i+1:len(nums)])
135+
return
136+
nums.sort()
137+
```
138+
>另一种思路
124139
```python
140+
class Solution:
141+
'''
142+
抛砖引玉:因题目要求“必须原地修改,只允许使用额外常数空间”,python内置sorted函数以及数组切片+sort()无法使用。
143+
故选择另一种算法暂且提供一种python思路
144+
'''
145+
def nextPermutation(self, nums: List[int]) -> None:
146+
"""
147+
Do not return anything, modify nums in-place instead.
148+
"""
149+
length = len(nums)
150+
for i in range(length-1, 0, -1):
151+
if nums[i-1] < nums[i]:
152+
for j in range(length-1, 0, -1):
153+
if nums[j] > nums[i-1]:
154+
nums[i-1], nums[j] = nums[j], nums[i-1]
155+
break
156+
self.reverse(nums, i, length-1)
157+
break
158+
else:
159+
# 若正常结束循环,则对原数组直接翻转
160+
self.reverse(nums, 0, length-1)
161+
162+
def reverse(self, nums: List[int], low: int, high: int) -> None:
163+
while low < high:
164+
nums[low], nums[high] = nums[high], nums[low]
165+
low += 1
166+
high -= 1
125167
```
126168

127169
## Go

problems/0053.最大子序和.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ class Solution {
161161
}
162162
```
163163

164+
```java
165+
// DP 方法
166+
class Solution {
167+
public int maxSubArray(int[] nums) {
168+
int ans = Integer.MIN_VALUE;
169+
int[] dp = new int[nums.length];
170+
dp[0] = nums[0];
171+
ans = dp[0];
172+
173+
for (int i = 1; i < nums.length; i++){
174+
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
175+
ans = Math.max(dp[i], ans);
176+
}
177+
178+
return ans;
179+
}
180+
}
181+
```
182+
164183
Python:
165184
```python
166185
class Solution:

problems/0106.从中序与后序遍历序列构造二叉树.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -792,30 +792,27 @@ func findRootIndex(target int,inorder []int) int{
792792

793793
```javascript
794794
var buildTree = function(inorder, postorder) {
795-
if (!postorder.length) return null
796-
797-
let root = new TreeNode(postorder[postorder.length - 1])
798-
799-
let index = inorder.findIndex(number => number === root.val)
800-
801-
root.left = buildTree(inorder.slice(0, index), postorder.slice(0, index))
802-
root.right = buildTree(inorder.slice(index + 1, inorder.length), postorder.slice(index, postorder.length - 1))
803-
804-
return root
795+
if (!preorder.length) return null;
796+
const rootVal = postorder.pop(); // 从后序遍历的数组中获取中间节点的值, 即数组最后一个值
797+
let rootIndex = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标
798+
const root = new TreeNode(rootVal); // 创建中间节点
799+
root.left = buildTree(inorder.slice(0, rootIndex), postorder.slice(0, rootIndex)); // 创建左节点
800+
root.right = buildTree(inorder.slice(rootIndex + 1), postorder.slice(rootIndex)); // 创建右节点
801+
return root;
805802
};
806803
```
807804

808805
从前序与中序遍历序列构造二叉树
809806

810807
```javascript
811808
var buildTree = function(preorder, inorder) {
812-
if(!preorder.length)
813-
return null;
814-
let root = new TreeNode(preorder[0]);
815-
let mid = inorder.findIndex((number) => number === root.val);
816-
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
817-
root.right = buildTree(preorder.slice(mid + 1, preorder.length), inorder.slice(mid + 1, inorder.length));
818-
return root;
809+
if (!preorder.length) return null;
810+
const rootVal = preorder.shift(); // 从前序遍历的数组中获取中间节点的值, 即数组第一个值
811+
const index = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标
812+
const root = new TreeNode(rootVal); // 创建中间节点
813+
root.left = buildTree(preorder.slice(0, index), inorder.slice(0, index)); // 创建左节点
814+
root.right = buildTree(preorder.slice(index), inorder.slice(index + 1)); // 创建右节点
815+
return root;
819816
};
820817
```
821818

problems/0108.将有序数组转换为二叉搜索树.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,25 @@ var sortedArrayToBST = function (nums) {
375375
};
376376
```
377377

378+
## C
379+
递归
380+
```c
381+
struct TreeNode* traversal(int* nums, int left, int right) {
382+
if (left > right)
383+
return NULL;
384+
int mid = left + ((right - left) / 2);
385+
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
386+
root->val = nums[mid];
387+
root->left = traversal(nums, left, mid - 1);
388+
root->right = traversal(nums, mid + 1, right);
389+
return root;
390+
}
378391

379-
392+
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
393+
struct TreeNode* root = traversal(nums, 0, numsSize - 1);
394+
return root;
395+
}
396+
```
380397
381398
-----------------------
382399
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0122.买卖股票的最佳时机II(动态规划).md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ class Solution:
200200

201201
Go:
202202

203+
```go
204+
func maxProfit(prices []int) int {
205+
//创建数组
206+
dp:=make([][]int,len(prices))
207+
for i:=0;i<len(prices);i++{
208+
dp[i]=make([]int,2)
209+
}
210+
dp[0][0]=-prices[0]
211+
dp[0][1]=0
212+
for i:=1;i<len(prices);i++{
213+
dp[i][0]=max(dp[i-1][0],dp[i-1][1]-prices[i])
214+
dp[i][1]=max(dp[i-1][1],dp[i-1][0]+prices[i])
215+
}
216+
return dp[len(prices)-1][1]
217+
}
218+
func max(a,b int)int{
219+
if a<b{
220+
return b
221+
}
222+
return a
223+
}
224+
```
225+
226+
227+
203228
Javascript:
204229
```javascript
205230
// 方法一:动态规划(dp 数组)

problems/0123.买卖股票的最佳时机III.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,38 @@ class Solution:
278278
return dp[4]
279279
```
280280

281+
Go:
282+
283+
```go
284+
func maxProfit(prices []int) int {
285+
dp:=make([][]int,len(prices))
286+
for i:=0;i<len(prices);i++{
287+
dp[i]=make([]int,5)
288+
}
289+
dp[0][0]=0
290+
dp[0][1]=-prices[0]
291+
dp[0][2]=0
292+
dp[0][3]=-prices[0]
293+
dp[0][4]=0
294+
for i:=1;i<len(prices);i++{
295+
dp[i][0]=dp[i-1][0]
296+
dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i])
297+
dp[i][2]=max(dp[i-1][2],dp[i-1][1]+prices[i])
298+
dp[i][3]=max(dp[i-1][3],dp[i-1][2]-prices[i])
299+
dp[i][4]=max(dp[i-1][4],dp[i-1][3]+prices[i])
300+
}
301+
return dp[len(prices)-1][4]
302+
}
303+
func max(a,b int)int{
304+
if a>b{
305+
return a
306+
}
307+
return b
308+
}
309+
```
310+
311+
312+
281313
JavaScript:
282314

283315
> 版本一:

problems/0151.翻转字符串里的单词.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,94 @@ class Solution {
300300
}
301301
```
302302

303+
```java
304+
//解法二:创建新字符数组填充。时间复杂度O(n)
305+
class Solution {
306+
public String reverseWords(String s) {
307+
//源字符数组
308+
char[] initialArr = s.toCharArray();
309+
//新字符数组
310+
char[] newArr = new char[initialArr.length+1];//下面循环添加"单词 ",最终末尾的空格不会返回
311+
int newArrPos = 0;
312+
//i来进行整体对源字符数组从后往前遍历
313+
int i = initialArr.length-1;
314+
while(i>=0){
315+
while(i>=0 && initialArr[i] == ' '){i--;} //跳过空格
316+
//此时i位置是边界或!=空格,先记录当前索引,之后的while用来确定单词的首字母的位置
317+
int right = i;
318+
while(i>=0 && initialArr[i] != ' '){i--;}
319+
//指定区间单词取出(由于i为首字母的前一位,所以这里+1,),取出的每组末尾都带有一个空格
320+
for (int j = i+1; j <= right; j++) {
321+
newArr[newArrPos++] = initialArr[j];
322+
if(j == right){
323+
newArr[newArrPos++] = ' ';//空格
324+
}
325+
}
326+
}
327+
//若是原始字符串没有单词,直接返回空字符串;若是有单词,返回0-末尾空格索引前范围的字符数组(转成String返回)
328+
if(newArrPos == 0){
329+
return "";
330+
}else{
331+
return new String(newArr,0,newArrPos-1);
332+
}
333+
}
334+
}
335+
```
336+
337+
```java
338+
//解法三:双反转+移位,在原始数组上进行反转。空间复杂度O(1)
339+
class Solution {
340+
/**
341+
* 思路:
342+
* ①反转字符串 "the sky is blue " => " eulb si yks eht"
343+
* ②遍历 " eulb si yks eht",每次先对某个单词进行反转再移位
344+
* 这里以第一个单词进行为演示:" eulb si yks eht" ==反转=> " blue si yks eht" ==移位=> "blue si yks eht"
345+
*/
346+
public String reverseWords(String s) {
347+
//步骤1:字符串整体反转(此时其中的单词也都反转了)
348+
char[] initialArr = s.toCharArray();
349+
reverse(initialArr, 0, s.length() - 1);
350+
int k = 0;
351+
for (int i = 0; i < initialArr.length; i++) {
352+
if (initialArr[i] == ' ') {
353+
continue;
354+
}
355+
int tempCur = i;
356+
while (i < initialArr.length && initialArr[i] != ' ') {
357+
i++;
358+
}
359+
for (int j = tempCur; j < i; j++) {
360+
if (j == tempCur) { //步骤二:二次反转
361+
reverse(initialArr, tempCur, i - 1);//对指定范围字符串进行反转,不反转从后往前遍历一个个填充有问题
362+
}
363+
//步骤三:移动操作
364+
initialArr[k++] = initialArr[j];
365+
if (j == i - 1) { //遍历结束
366+
//避免越界情况,例如=> "asdasd df f",不加判断最后就会数组越界
367+
if (k < initialArr.length) {
368+
initialArr[k++] = ' ';
369+
}
370+
}
371+
}
372+
}
373+
if (k == 0) {
374+
return "";
375+
} else {
376+
//参数三:以防出现如"asdasd df f"=>"f df asdasd"正好凑满不需要省略空格情况
377+
return new String(initialArr, 0, (k == initialArr.length) && (initialArr[k - 1] != ' ') ? k : k - 1);
378+
}
379+
}
380+
381+
public void reverse(char[] chars, int begin, int end) {
382+
for (int i = begin, j = end; i < j; i++, j--) {
383+
chars[i] ^= chars[j];
384+
chars[j] ^= chars[i];
385+
chars[i] ^= chars[j];
386+
}
387+
}
388+
}
389+
```
390+
303391
python:
304392

305393
```Python

problems/0188.买卖股票的最佳时机IV.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,42 @@ class Solution:
276276
```
277277
Go:
278278

279+
```go
280+
func maxProfit(k int, prices []int) int {
281+
if len(prices)==0{
282+
return 0
283+
}
284+
dp:=make([][]int,len(prices))
285+
for i:=0;i<len(prices);i++{
286+
dp[i]=make([]int,2*k+1)
287+
}
288+
for i:=1;i<len(dp[0]);i++{
289+
if i%2!=0{
290+
dp[0][i]=-prices[0]
291+
}
292+
}
293+
for i:=1;i<len(prices);i++{
294+
dp[i][0]=dp[i-1][0]
295+
for j:=1;j<len(dp[0]);j++{
296+
if j%2!=0{
297+
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]-prices[i])
298+
}else {
299+
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+prices[i])
300+
}
301+
}
302+
}
303+
return dp[len(prices)-1][2*k]
304+
}
305+
func max(a,b int)int{
306+
if a>b{
307+
return a
308+
}
309+
return b
310+
}
311+
```
312+
313+
314+
279315

280316
Javascript:
281317

problems/0202.快乐数.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ var isHappy = function(n) {
215215
}
216216
return n === 1;
217217
};
218+
219+
// 方法四:使用Set(),求和用reduce
220+
var isHappy = function(n) {
221+
let set = new Set();
222+
let totalCount;
223+
while(totalCount !== 1) {
224+
let arr = (''+(totalCount || n)).split('');
225+
totalCount = arr.reduce((total, num) => {
226+
return total + num * num
227+
}, 0)
228+
if (set.has(totalCount)) {
229+
return false;
230+
}
231+
set.add(totalCount);
232+
}
233+
return true;
234+
};
218235
```
219236

220237
Swift:

0 commit comments

Comments
 (0)