Skip to content

Commit a2cee11

Browse files
committed
Merge branch 'master' of github.com:baici1/leetcode-master
2 parents 35154b7 + 6e7f13c commit a2cee11

12 files changed

+369
-93
lines changed

problems/0135.分发糖果.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,33 @@ public:
132132
Java:
133133
```java
134134
class Solution {
135+
/**
136+
分两个阶段
137+
1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1
138+
2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
139+
*/
135140
public int candy(int[] ratings) {
136-
int[] candy = new int[ratings.length];
137-
for (int i = 0; i < candy.length; i++) {
138-
candy[i] = 1;
139-
}
140-
141+
int[] candyVec = new int[ratings.length];
142+
candyVec[0] = 1;
141143
for (int i = 1; i < ratings.length; i++) {
142144
if (ratings[i] > ratings[i - 1]) {
143-
candy[i] = candy[i - 1] + 1;
145+
candyVec[i] = candyVec[i - 1] + 1;
146+
} else {
147+
candyVec[i] = 1;
144148
}
145149
}
146150
147151
for (int i = ratings.length - 2; i >= 0; i--) {
148152
if (ratings[i] > ratings[i + 1]) {
149-
candy[i] = Math.max(candy[i],candy[i + 1] + 1);
153+
candyVec[i] = Math.max(candyVec[i], candyVec[i + 1] + 1);
150154
}
151155
}
152156
153-
int count = 0;
154-
for (int i = 0; i < candy.length; i++) {
155-
count += candy[i];
157+
int ans = 0;
158+
for (int s : candyVec) {
159+
ans += s;
156160
}
157-
158-
return count;
161+
return ans;
159162
}
160163
}
161164
```

problems/0143.重排链表.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public:
5050
cur = cur->next;
5151
count++;
5252
}
53-
if (vec.size() % 2 == 0) { // 如果是偶数,还要多处理中间的一个
54-
cur->next = vec[i];
55-
cur = cur->next;
56-
}
5753
cur->next = nullptr; // 注意结尾
5854
}
5955
};
@@ -249,12 +245,6 @@ public class ReorderList {
249245
cur = cur.next;
250246
count++;
251247
}
252-
// 当是偶数的话,需要做额外处理
253-
if (list.size() % 2== 0){
254-
cur.next = list.get(l);
255-
cur = cur.next;
256-
}
257-
258248
// 注意结尾要结束一波
259249
cur.next = null;
260250
}
@@ -376,11 +366,6 @@ var reorderList = function(head, s = [], tmp) {
376366
cur = cur.next;
377367
count++;
378368
}
379-
// 当是偶数的话,需要做额外处理
380-
if(list.length % 2 == 0){
381-
cur.next = list[l];
382-
cur = cur.next;
383-
}
384369
// 注意结尾要结束一波
385370
cur.next = null;
386371
}

problems/0206.翻转链表.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ class Solution {
164164
}
165165
```
166166

167+
```java
168+
// 从后向前递归
169+
class Solution {
170+
ListNode reverseList(ListNode head) {
171+
// 边缘条件判断
172+
if(head == null) return null;
173+
if (head.next == null) return head;
174+
175+
// 递归调用,翻转第二个节点开始往后的链表
176+
ListNode last = reverseList(head.next);
177+
// 翻转头节点与第二个节点的指向
178+
head.next.next = head;
179+
// 此时的 head 节点为尾节点,next 需要指向 NULL
180+
head.next = null;
181+
return last;
182+
}
183+
}
184+
```
185+
167186
Python迭代法:
168187
```python
169188
#双指针

problems/0337.打家劫舍III.md

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -370,46 +370,39 @@ class Solution:
370370

371371
Go:
372372

373-
树形DP
373+
动态规划
374374

375375
```go
376-
/**
377-
* Definition for a binary tree node.
378-
* type TreeNode struct {
379-
* Val int
380-
* Left *TreeNode
381-
* Right *TreeNode
382-
* }
383-
*/
384376
func rob(root *TreeNode) int {
385-
return max(robTree(root))
377+
res := robTree(root)
378+
return max(res[0], res[1])
386379
}
387-
func robTree(root *TreeNode)(int,int){
388-
if root==nil{
389-
return 0,0
390-
}
391-
//获取左节点的偷的值与不偷的值
392-
left0,left1:=robTree(root.Left)
393-
//获取右节点的偷的值与不偷的值
394-
right0,right1:=robTree(root.Right)
395-
//
396-
val1:=root.Val
397-
val1+=left1+right1
398-
//不偷
399-
val2:=0
400-
val2+=max(left0,left1)+max(right0,right1)
401-
return val1,val2
380+
381+
func max(a, b int) int {
382+
if a > b {
383+
return a
384+
}
385+
return b
402386
}
403-
func max(a,b int)int{
404-
if a>b{
405-
return a
406-
}
407-
return b
387+
388+
func robTree(cur *TreeNode) []int {
389+
if cur == nil {
390+
return []int{0, 0}
391+
}
392+
// 后序遍历
393+
left := robTree(cur.Left)
394+
right := robTree(cur.Right)
395+
396+
// 考虑去偷当前的屋子
397+
robCur := cur.Val + left[0] + right[0]
398+
// 考虑不去偷当前的屋子
399+
notRobCur := max(left[0], left[1]) + max(right[0], right[1])
400+
401+
// 注意顺序:0:不偷,1:去偷
402+
return []int{notRobCur, robCur}
408403
}
409404
```
410405

411-
412-
413406
JavaScript:
414407

415408
> 动态规划

problems/0416.分割等和子集.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,32 @@ class Solution:
226226
return taraget == dp[taraget]
227227
```
228228
Go:
229+
```go
230+
// 分割等和子集 动态规划
231+
// 时间复杂度O(n^2) 空间复杂度O(n)
232+
func canPartition(nums []int) bool {
233+
sum := 0
234+
for _, num := range nums {
235+
sum += num
236+
}
237+
// 如果 nums 的总和为奇数则不可能平分成两个子集
238+
if sum % 2 == 1 {
239+
return false
240+
}
241+
242+
target := sum / 2
243+
dp := make([]int, target + 1)
244+
245+
for _, num := range nums {
246+
for j := target; j >= num; j-- {
247+
if dp[j] < dp[j - num] + num {
248+
dp[j] = dp[j - num] + num
249+
}
250+
}
251+
}
252+
return dp[target] == target
253+
}
254+
```
229255

230256
```go
231257
func canPartition(nums []int) bool {

problems/0707.设计链表.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ Java:
282282
```Java
283283
//单链表
284284
class ListNode {
285-
int val;
286-
ListNode next;
287-
ListNode(){}
288-
ListNode(int val) {
289-
this.val=val;
290-
}
285+
int val;
286+
ListNode next;
287+
ListNode(){}
288+
ListNode(int val) {
289+
this.val=val;
290+
}
291291
}
292292
class MyLinkedList {
293293
//size存储链表元素的个数

problems/0763.划分字母区间.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ Java:
8888
class Solution {
8989
public List<Integer> partitionLabels(String S) {
9090
List<Integer> list = new LinkedList<>();
91-
int[] edge = new int[123];
91+
int[] edge = new int[26];
9292
char[] chars = S.toCharArray();
9393
for (int i = 0; i < chars.length; i++) {
94-
edge[chars[i] - 0] = i;
94+
edge[chars[i] - 'a'] = i;
9595
}
9696
int idx = 0;
9797
int last = -1;
9898
for (int i = 0; i < chars.length; i++) {
99-
idx = Math.max(idx,edge[chars[i] - 0]);
99+
idx = Math.max(idx,edge[chars[i] - 'a']);
100100
if (i == idx) {
101101
list.add(i - last);
102102
last = i;

problems/0977.有序数组的平方.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
## 暴力排序
2929

30-
最直观的相反,莫过于:每个数平方之后,排个序,美滋滋,代码如下:
30+
最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下:
3131

3232
```CPP
3333
class Solution {

problems/1002.查找常用字符.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,28 @@ javaScript
224224
var commonChars = function (words) {
225225
let res = []
226226
let size = 26
227-
let firstHash = new Array(size)
228-
for (let i = 0; i < size; i++) { // 初始化 hash 数组
229-
firstHash[i] = 0
230-
}
227+
let firstHash = new Array(size).fill(0) // 初始化 hash 数组
231228

232229
let a = "a".charCodeAt()
233230
let firstWord = words[0]
234231
for (let i = 0; i < firstWord.length; i++) { // 第 0 个单词的统计
235232
let idx = firstWord[i].charCodeAt()
236233
firstHash[idx - a] += 1
237234
}
238-
235+
236+
let otherHash = new Array(size).fill(0) // 初始化 hash 数组
239237
for (let i = 1; i < words.length; i++) { // 1-n 个单词统计
240-
let otherHash = new Array(size)
241-
for (let i = 0; i < size; i++) { // 初始化 hash 数组
242-
otherHash[i] = 0
243-
}
244-
245238
for (let j = 0; j < words[i].length; j++) {
246239
let idx = words[i][j].charCodeAt()
247240
otherHash[idx - a] += 1
248241
}
242+
249243
for (let i = 0; i < size; i++) {
250244
firstHash[i] = Math.min(firstHash[i], otherHash[i])
251245
}
246+
otherHash.fill(0)
252247
}
248+
253249
for (let i = 0; i < size; i++) {
254250
while (firstHash[i] > 0) {
255251
res.push(String.fromCharCode(i + a))

problems/剑指Offer58-II.左旋转字符串.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,14 @@ func reverse(b []byte, left, right int){
200200
JavaScript:
201201

202202
```javascript
203-
var reverseLeftWords = function (s, n) {
204-
const reverse = (str, left, right) => {
205-
let strArr = str.split("");
206-
for (; left < right; left++, right--) {
207-
[strArr[left], strArr[right]] = [strArr[right], strArr[left]];
208-
}
209-
return strArr.join("");
210-
}
211-
s = reverse(s, 0, n - 1);
212-
s = reverse(s, n, s.length - 1);
213-
return reverse(s, 0, s.length - 1);
203+
var reverseLeftWords = function(s, n) {
204+
const length = s.length;
205+
let i = 0;
206+
while (i < length - n) {
207+
s = s[length - 1] + s;
208+
i++;
209+
}
210+
return s.slice(0, length);
214211
};
215212
```
216213

0 commit comments

Comments
 (0)