Skip to content

Commit 3221a3c

Browse files
committed
暂存-剑指offer-17
1 parent 1971e05 commit 3221a3c

File tree

75 files changed

+2874
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2874
-537
lines changed

Leetcode/array/3Sum/readme.md

+39
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,45 @@ res.erase(unique(res.begin(), res.end()), res.end());
8484
{
8585
break;
8686
}
87+
## 容易错的地方
88+
89+
### 1.
90+
91+
```
92+
while(front<back&&nums[front]==tri[1]) front++;//防止中间数字有重复
93+
while(front<back&&nums[back]==tri[2]) back--;//防止最后的数字有重复
94+
```
95+
96+
上面的代码不可以写成下面的样子
97+
98+
```
99+
while(front<back&&nums[front]==nums[front+1]){front++;}
100+
while(front<back&&nums[back]==nums[back-1]){back--;}
101+
```
102+
103+
原因是`while(front<back&&nums[front]==nums[front+1]){front++;}`执行完后front指向的是中间数字的左后一个重复元素, 那么想用第二种写法, 怎么才是对的呢, 如下
104+
105+
```
106+
while(front<back&&nums[front]==nums[front+1]){front++;}
107+
front++;
108+
while(front<back&&nums[back]==nums[back-1]){back--;}
109+
back--;
110+
```
111+
112+
### 2.
113+
114+
```
115+
while(i<nums.size()-1&&nums[i]==nums[i+1]) i++;//防止第一个数字重复
116+
```
117+
118+
上面的代码不可以写成下面的样子
119+
120+
```
121+
while(i<nums.size()-1&&nums[i]==nums[i+1]){i++;}
122+
```
123+
124+
125+
87126
## 代码
88127
89128
### [c++代码](./src/cpp/3Sum.cpp)

Leetcode/array/MergeSortedArray/readme.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@ tags: Array
66

77
## 题目原文
88

9-
[原文地址](https://leetcode.com/problems/merge-sorted-array/description/)
9+
[题目链接](<https://leetcode-cn.com/problems/merge-sorted-array/>)
1010

11-
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
11+
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。
1212

13-
Note:
13+
**说明:**
14+
15+
- 初始化 nums1 和 nums2 的元素数量分别为 m 和 n。
16+
- 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。
17+
18+
**示例:**
19+
20+
```
21+
输入:
22+
nums1 = [1,2,3,0,0,0], m = 3
23+
nums2 = [2,5,6], n = 3
24+
25+
输出: [1,2,2,3,5,6]
26+
```
1427

15-
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
1628
## 题目大意
1729
nums1和nums2是两个排序好的数组,把两个数组合并放入nums1并排好序。
1830

Leetcode/array/Pascal'sTriangle/readme.md

+25-14
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ tags: Array
55
---
66

77
## 题目原文
8-
[原文网址](https://leetcode.com/problems/pascals-triangle/description/
8+
[题目链接](https://leetcode-cn.com/problems/pascals-triangle/)
99

10-
Given numRows, generate the first numRows of Pascal's triangle.
11-
For example, given numRows = 5,
12-
Return
10+
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
11+
![img](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
1312

14-
[
15-
[1],
16-
[1,1],
17-
[1,2,1],
18-
[1,3,3,1],
19-
[1,4,6,4,1]
20-
]
13+
在杨辉三角中,每个数是它左上方和右上方的数的和。
2114

22-
![img](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
15+
**示例:**
16+
17+
```
18+
输入: 5
19+
输出:
20+
[
21+
[1],
22+
[1,1],
23+
[1,2,1],
24+
[1,3,3,1],
25+
[1,4,6,4,1]
26+
]
27+
```
2328

2429
## 题目大意
2530

@@ -37,13 +42,13 @@ Return
3742

3843
## c++ 知识点
3944

40-
### vector resize和reverse区别
45+
### vector resize和reserve区别
4146

4247
```
4348
void reserve (size_type n);
4449
```
4550

46-
reserver函数用来给vector**预分配**存储区大小,即capacity的值 ,但是没有给这段内存进行初始化。reserve 的参数n是推荐预分配内存的大小,实际分配的可能等于或大于这个值,即n大于capacity的值,就会reallocate内存 capacity的值会大于或者等于n 。这样,当ector调用push_back函数使得size 超过原来的默认分配的capacity值时 避免了内存重分配开销。
51+
reserve函数用来给vector**预分配**存储区大小,即capacity的值 ,但是没有给这段内存进行初始化。reserve 的参数n是推荐预分配内存的大小,实际分配的可能等于或大于这个值,即n大于capacity的值,就会reallocate内存 capacity的值会大于或者等于n 。这样,当vector调用push_back函数使得size 超过原来的默认分配的capacity值时 避免了内存重分配开销。
4752

4853
需要注意的是:reserve 函数分配出来的内存空间,只是表示vector可以利用这部分内存,但vector不能有效地访问这些内存空间,访问的时候就会出现越界现象,导致程序崩溃。
4954

@@ -60,6 +65,12 @@ resize函数**重新分配**大小,改变容器的大小,并且创建对象
6065

6166
当n大于capacity()值的时候,会自动分配重新分配内存存储空间。
6267

68+
容器调用resize()函数后,所有的空间都已经初始化了,所以可以直接访问。
69+
70+
而reserve()函数预分配出的空间没有被初始化,所以不可访问。
71+
72+
73+
6374
参考: [C++:vector中的resize()函数 VS reserve()函数](https://www.cnblogs.com/biyeymyhjob/archive/2013/05/11/3072893.html), [vector resize和reverse区别](https://blog.csdn.net/yockie/article/details/7992057)
6475

6576
## python 知识点
Loading

Leetcode/array/Pascal'sTriangleII/readme.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ tags: Array
55
---
66

77
## 题目原文
8-
[原文网址](https://leetcode.com/problems/pascals-triangle-ii/description/)
8+
[题目链接](https://leetcode-cn.com/problems/pascals-triangle-ii/)
99

10-
Given an index k, return the kth row of the Pascal's triangle.
11-
For example, given k = 3,
12-
Return [1,3,3,1].
10+
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
11+
12+
![img](img/readme.assets/PascalTriangleAnimated2.gif)
13+
14+
在杨辉三角中,每个数是它左上方和右上方的数的和。
15+
16+
**示例:**
17+
18+
```
19+
输入: 3
20+
输出: [1,3,3,1]
21+
```
22+
23+
进阶:
24+
25+
你可以优化你的算法到 O(k) 空间复杂度吗?
1326

14-
Note:
1527

16-
Could you optimize your algorithm to use only O(k) extra space?
1728
## 题目大意
1829

1930
给出帕斯卡三角形第k层,注意这里的K从0开始计数

Leetcode/array/PlusOne/readme.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,30 @@ tags: Array,Math
55
---
66

77
## 题目原文
8-
[原文网址](https://leetcode.com/problems/plus-one/description/)
8+
[题目链接](https://leetcode-cn.com/problems/plus-one/)
99

10-
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
1110

12-
You may assume the integer do not contain any leading zero, except the number 0 itself.
11+
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
1312

14-
The digits are stored such that the most significant digit is at the head of the list.
13+
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。
14+
15+
你可以假设除了整数 0 之外,这个整数不会以零开头。
16+
17+
**示例 1:**
18+
19+
```
20+
输入: [1,2,3]
21+
输出: [1,2,4]
22+
解释: 输入数组表示数字 123。
23+
```
24+
25+
**示例 2:**
26+
27+
```
28+
输入: [4,3,2,1]
29+
输出: [4,3,2,2]
30+
解释: 输入数组表示数字 4321。
31+
```
1532

1633
## 题目大意
1734
用vector保存一个数字,个位数在索引最大的位置,计算这个数字加1后的结果。

Leetcode/array/RemoveDuplicatesFromSortedArray/readme.md

+44-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,50 @@ tags: Array,Two Pointers
55
---
66

77
## 题目原文
8-
[原文网址](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/)
9-
10-
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
11-
12-
Do not allocate extra space for another array, you must do this in place with constant memory.
13-
14-
For example,
15-
Given input array nums = [1,1,2],
16-
17-
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
8+
[题目连接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)
9+
10+
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
11+
12+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
13+
14+
**示例 1:**
15+
16+
```
17+
给定数组 nums = [1,1,2],
18+
19+
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
20+
21+
你不需要考虑数组中超出新长度后面的元素。
22+
```
23+
**示例 2:**
24+
25+
```
26+
给定 nums = [0,0,1,1,1,2,2,3,3,4],
27+
28+
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
29+
30+
你不需要考虑数组中超出新长度后面的元素。
31+
```
32+
33+
**说明:**
34+
35+
为什么返回数值是整数,但输出的答案是数组呢?
36+
37+
请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
38+
39+
你可以想象内部操作如下:
40+
41+
```
42+
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
43+
int len = removeDuplicates(nums);
44+
45+
// 在函数里修改输入数组对于调用者是可见的。
46+
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
47+
for (int i = 0; i < len; i++) {
48+
    print(nums[i]);
49+
}
50+
```
51+
1852

1953
## 题目大意
2054
删除数组中重复的元素,返回删除重复元素数组的长度

Leetcode/array/RemoveDuplicatesFromSortedArrayII/readme.md

+50-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,50 @@ tags: Array,Two Pointers
55
---
66

77
## 题目原文
8-
[原文网址](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/)
8+
[题目链接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/)
99

10-
Follow up for "Remove Duplicates":
10+
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。
1111

12-
What if duplicates are allowed at most twice?
13-
For example,
12+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
1413

15-
Given sorted array nums = [1,1,1,2,2,3],
14+
**示例 1:**
1615

17-
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
16+
```
17+
给定 nums = [1,1,1,2,2,3],
18+
19+
函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。
20+
21+
你不需要考虑数组中超出新长度后面的元素。
22+
```
23+
24+
**示例 2:**
25+
26+
```
27+
给定 nums = [0,0,1,1,1,1,2,3,3],
28+
29+
函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。
30+
31+
你不需要考虑数组中超出新长度后面的元素。
32+
```
33+
34+
**说明:**
35+
36+
为什么返回数值是整数,但输出的答案是数组呢?
37+
38+
请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
1839

40+
你可以想象内部操作如下:
41+
42+
```
43+
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
44+
int len = removeDuplicates(nums);
45+
46+
// 在函数里修改输入数组对于调用者是可见的。
47+
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
48+
for (int i = 0; i < len; i++) {
49+
    print(nums[i]);
50+
}
51+
```
1952

2053
## 题目大意
2154
删除数组中重复的元素,但允许重复两次,返回删除重复元素后数组的长度,
@@ -28,6 +61,17 @@ Your function should return length = 5, with the first five elements of nums 
2861

2962
判断输入数组的长度是否为空
3063

64+
在重复两次的时候也要进行交换, 即代码中的
65+
```
66+
else{
67+
temp++;
68+
if (temp<last)
69+
nums[++i]=nums[index];
70+
}
71+
```
72+
73+
至于原因用[0,0,1,1,1,1,2,3,3]作为测试用例便可知道
74+
3175
本文c++版本的代码写的比较通用, 不仅可以处理重复两次的情况, 重复任意次都可以
3276

3377
## python代码知识点

Leetcode/array/TwoSum/readme.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@ tags: Array, Hash Table
66

77
## 题目原文
88

9-
[原文网址](https://leetcode.com/problems/two-sum/description/)
9+
[题目链接](<https://leetcode-cn.com/problems/two-sum/>)
1010

11-
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
12-
You may assume that each input would have exactly one solution.
11+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
1312

14-
Example:
13+
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
1514

16-
Given nums = [2, 7, 11, 15], target = 9,
17-
Because nums[0] + nums[1] = 2 + 7 = 9,
18-
return [0, 1].
15+
**示例:**
1916

20-
UPDATE (2016/2/13):
17+
```
18+
给定 nums = [2, 7, 11, 15], target = 9
19+
20+
因为 nums[0] + nums[1] = 2 + 7 = 9
21+
所以返回 [0, 1]
22+
```
2123

22-
The return format had been changed to zero-based indices. Please read the above updated description carefully.
2324
## 题目大意
25+
2426
n这道题目的意思是给定一个数组和一个值, 让求出这个数组中两个值的和等于这个给定值的坐
2527
标。 输出是有要求的, 1, 坐标较小的放在前面, 较大的放在后面。 2, 这俩坐标从0开始计数。
2628

2729

2830

31+
需要注意的是, 整数数组中的元素是没有排序好的.
32+
2933
## 解题思路
3034
第一步: 我们要分析题意, 其中有三个关键点:
3135
1. 求出来的坐标值要按序排列。

0 commit comments

Comments
 (0)