Skip to content

Commit

Permalink
暂存-剑指offer-17
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard-coder committed Jun 30, 2019
1 parent 1971e05 commit 3221a3c
Show file tree
Hide file tree
Showing 75 changed files with 2,874 additions and 537 deletions.
39 changes: 39 additions & 0 deletions Leetcode/array/3Sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,45 @@ res.erase(unique(res.begin(), res.end()), res.end());
{
break;
}
## 容易错的地方
### 1.
```
while(front<back&&nums[front]==tri[1]) front++;//防止中间数字有重复
while(front<back&&nums[back]==tri[2]) back--;//防止最后的数字有重复
```
上面的代码不可以写成下面的样子
```
while(front<back&&nums[front]==nums[front+1]){front++;}
while(front<back&&nums[back]==nums[back-1]){back--;}
```
原因是`while(front<back&&nums[front]==nums[front+1]){front++;}`执行完后front指向的是中间数字的左后一个重复元素, 那么想用第二种写法, 怎么才是对的呢, 如下
```
while(front<back&&nums[front]==nums[front+1]){front++;}
front++;
while(front<back&&nums[back]==nums[back-1]){back--;}
back--;
```
### 2.
```
while(i<nums.size()-1&&nums[i]==nums[i+1]) i++;//防止第一个数字重复
```
上面的代码不可以写成下面的样子
```
while(i<nums.size()-1&&nums[i]==nums[i+1]){i++;}
```
## 代码
### [c++代码](./src/cpp/3Sum.cpp)
Expand Down
20 changes: 16 additions & 4 deletions Leetcode/array/MergeSortedArray/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ tags: Array

## 题目原文

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

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

Note:
**说明:**

- 初始化 nums1 和 nums2 的元素数量分别为 m 和 n。
- 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。

**示例:**

```
输入:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
输出: [1,2,2,3,5,6]
```

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.
## 题目大意
nums1和nums2是两个排序好的数组,把两个数组合并放入nums1并排好序。

Expand Down
39 changes: 25 additions & 14 deletions Leetcode/array/Pascal'sTriangle/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ tags: Array
---

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

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

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
在杨辉三角中,每个数是它左上方和右上方的数的和。

![img](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
**示例:**

```
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
```

## 题目大意

Expand All @@ -37,13 +42,13 @@ Return

## c++ 知识点

### vector resize和reverse区别
### vector resize和reserve区别

```
void reserve (size_type n);
```

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

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

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

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

容器调用resize()函数后,所有的空间都已经初始化了,所以可以直接访问。

而reserve()函数预分配出的空间没有被初始化,所以不可访问。



参考: [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)

## python 知识点
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 17 additions & 6 deletions Leetcode/array/Pascal'sTriangleII/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ tags: Array
---

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

Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

![img](img/readme.assets/PascalTriangleAnimated2.gif)

在杨辉三角中,每个数是它左上方和右上方的数的和。

**示例:**

```
输入: 3
输出: [1,3,3,1]
```

进阶:

你可以优化你的算法到 O(k) 空间复杂度吗?

Note:

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

给出帕斯卡三角形第k层,注意这里的K从0开始计数
Expand Down
25 changes: 21 additions & 4 deletions Leetcode/array/PlusOne/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,30 @@ tags: Array,Math
---

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

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

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

The digits are stored such that the most significant digit is at the head of the list.
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

**示例 1:**

```
输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
```

**示例 2:**

```
输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。
```

## 题目大意
用vector保存一个数字,个位数在索引最大的位置,计算这个数字加1后的结果。
Expand Down
54 changes: 44 additions & 10 deletions Leetcode/array/RemoveDuplicatesFromSortedArray/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,50 @@ tags: Array,Two Pointers
---

## 题目原文
[原文网址](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/)

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

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.
[题目连接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

**示例 1:**

```
给定数组 nums = [1,1,2],
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
你不需要考虑数组中超出新长度后面的元素。
```
**示例 2:**

```
给定 nums = [0,0,1,1,1,2,2,3,3,4],
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
你不需要考虑数组中超出新长度后面的元素。
```

**说明:**

为什么返回数值是整数,但输出的答案是数组呢?

请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

你可以想象内部操作如下:

```
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}
```


## 题目大意
删除数组中重复的元素,返回删除重复元素数组的长度
Expand Down
56 changes: 50 additions & 6 deletions Leetcode/array/RemoveDuplicatesFromSortedArrayII/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,50 @@ tags: Array,Two Pointers
---

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

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

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

Given sorted array nums = [1,1,1,2,2,3],
**示例 1:**

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.
```
给定 nums = [1,1,1,2,2,3],
函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。
你不需要考虑数组中超出新长度后面的元素。
```

**示例 2:**

```
给定 nums = [0,0,1,1,1,1,2,3,3],
函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。
你不需要考虑数组中超出新长度后面的元素。
```

**说明:**

为什么返回数值是整数,但输出的答案是数组呢?

请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

你可以想象内部操作如下:

```
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}
```

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

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

在重复两次的时候也要进行交换, 即代码中的
```
else{
temp++;
if (temp<last)
nums[++i]=nums[index];
}
```

至于原因用[0,0,1,1,1,1,2,3,3]作为测试用例便可知道

本文c++版本的代码写的比较通用, 不仅可以处理重复两次的情况, 重复任意次都可以

## python代码知识点
Expand Down
22 changes: 13 additions & 9 deletions Leetcode/array/TwoSum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@ tags: Array, Hash Table

## 题目原文

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

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

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

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

UPDATE (2016/2/13):
```
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
```

The return format had been changed to zero-based indices. Please read the above updated description carefully.
## 题目大意

n这道题目的意思是给定一个数组和一个值, 让求出这个数组中两个值的和等于这个给定值的坐
标。 输出是有要求的, 1, 坐标较小的放在前面, 较大的放在后面。 2, 这俩坐标从0开始计数。



需要注意的是, 整数数组中的元素是没有排序好的.

## 解题思路
第一步: 我们要分析题意, 其中有三个关键点:
1. 求出来的坐标值要按序排列。
Expand Down
Loading

0 comments on commit 3221a3c

Please sign in to comment.