Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dd5b9ac

Browse files
committed
commit solution 160 189 268
1 parent d2a7b0d commit dd5b9ac

File tree

12 files changed

+372
-3
lines changed

12 files changed

+372
-3
lines changed

‎_sidebar.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
2424
- [118.杨辉三角](solution/100-199/0118.pascals-triangle/)
2525
- [125.验证回文串](solution/100-199/0125.valid-palindrome/)
26+
- [160.相交链表](solution/100-199/0160.intersection-of-two-linked-lists/)
2627
- [167.两数之和 II - 输入有序数组](solution/100-199/0167.two-sum-ii-input-array-is-sorted/)
2728
- [169.多数元素](solution/100-199/0169.majority-element/)
29+
- [189.旋转数组](solution/100-199/0189.rotate-array/)
2830
- [217.存在重复元素](solution/200-299/0217.contains-duplicate/)
2931
- [219.存在重复元素 II](solution/200-299/0219.contains-duplicate-ii/)
32+
- [268.缺失数字](solution/200-299/0268.missing-number/description/)
3033
- [283.移动零](solution/200-299/0283.move-zeroes/)
3134

3235

‎index-tags.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
1616
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
1717
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
18+
| [189](https://leetcode-cn.com/problems/rotate-array) | [旋转数组](/solution/100-199/0189.rotate-array/) | `数组` | <font color=green>简单</font> ||
1819
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
1920
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
21+
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
2022
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
2123

2224

@@ -37,6 +39,7 @@
3739
| [7](https://leetcode-cn.com/problems/reverse-integer) | [整数反转](/solution/1-99/0007.reverse-integer/) | `数学` | <font color=green>简单</font> ||
3840
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
3941
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
42+
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
4043

4144
#### **哈希表**
4245

@@ -74,6 +77,7 @@
7477
| [2](https://leetcode-cn.com/problems/add-two-numbers) | [两数相加](/solution/1-99/0002.add-two-numbers/) | `链表`,`数学` | <font color=blue>中等</font> ||
7578
| [21](https://leetcode-cn.com/problems/merge-two-sorted-lists) | [合并两个有序链表](/solution/1-99/0021.merge-two-sorted-lists/) | `链表` | <font color=green>简单</font> ||
7679
| [83](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list) | [删除排序链表中的重复元素](/solution/1-99/0083.remove-duplicates-from-sorted-list/) | `链表` | <font color=green>简单</font> ||
80+
| [160](https://leetcode-cn.com/problems/intersection-of-two-linked-lists) | [相交链表](/solution/100-199/0160.intersection-of-two-linked-lists/) | `链表` | <font color=green>简单</font> ||
7781

7882
#### **二分查找**
7983

@@ -91,6 +95,7 @@
9195
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
9296
| --- | --- | --- | --- | --- |
9397
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
98+
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
9499

95100
#### ****
96101

‎index-type.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
| [141](https://leetcode-cn.com/problems/linked-list-cycle) | [环形链表](/solution/100-199/0141.linked-list-cycle/) | `链表`,`双指针` | <font color=green>简单</font> |
4545
| [155](https://leetcode-cn.com/problems/min-stack) | [最小栈](/solution/100-199/0155.min-stack/) | ``,`设计` | <font color=green>简单</font> |
4646
| [157](https://leetcode-cn.com/problems/read-n-characters-given-read4) | [用 read4 读取 n 个字符](/solution/100-199/0157.read-n-characters-given-read4/) | `字符串` | <font color=green>简单</font> |
47-
| [160](https://leetcode-cn.com/problems/intersection-of-two-linked-lists) | [相交链表](/solution/100-199/0160.intersection-of-two-linked-lists/) | `链表` | <font color=green>简单</font> |
47+
| [160](https://leetcode-cn.com/problems/intersection-of-two-linked-lists) | [相交链表](/solution/100-199/0160.intersection-of-two-linked-lists/) | `链表` | <font color=green>简单</font> ||
4848
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
4949
| [168](https://leetcode-cn.com/problems/excel-sheet-column-title) | [excel表列名称](/solution/100-199/0168.excel-sheet-column-title/) | `数学` | <font color=green>简单</font> |
5050
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
@@ -56,7 +56,7 @@
5656
| [181](https://leetcode-cn.com/problems/employees-earning-more-than-their-managers) | [超过经理收入的员工](/solution/100-199/0181.employees-earning-more-than-their-managers/) | | <font color=green>简单</font> |
5757
| [182](https://leetcode-cn.com/problems/duplicate-emails) | [查找重复的电子邮箱](/solution/100-199/0182.duplicate-emails/) | | <font color=green>简单</font> |
5858
| [183](https://leetcode-cn.com/problems/customers-who-never-order) | [从不订购的客户](/solution/100-199/0183.customers-who-never-order/) | | <font color=green>简单</font> |
59-
| [189](https://leetcode-cn.com/problems/rotate-array) | [旋转数组](/solution/100-199/0189.rotate-array/) | `数组` | <font color=green>简单</font> |
59+
| [189](https://leetcode-cn.com/problems/rotate-array) | [旋转数组](/solution/100-199/0189.rotate-array/) | `数组` | <font color=green>简单</font> ||
6060
| [190](https://leetcode-cn.com/problems/reverse-bits) | [颠倒二进制位](/solution/100-199/0190.reverse-bits/) | `位运算` | <font color=green>简单</font> |
6161
| [191](https://leetcode-cn.com/problems/number-of-1-bits) | [位1的个数](/solution/100-199/0191.number-of-1-bits/) | `位运算` | <font color=green>简单</font> |
6262
| [193](https://leetcode-cn.com/problems/valid-phone-numbers) | [有效电话号码](/solution/100-199/0193.valid-phone-numbers/) | | <font color=green>简单</font> |
@@ -87,7 +87,7 @@
8787
| [258](https://leetcode-cn.com/problems/add-digits) | [各位相加](/solution/200-299/0258.add-digits/) | `数学` | <font color=green>简单</font> |
8888
| [263](https://leetcode-cn.com/problems/ugly-number) | [丑数](/solution/200-299/0263.ugly-number/) | `数学` | <font color=green>简单</font> |
8989
| [266](https://leetcode-cn.com/problems/palindrome-permutation) | [回文排列](/solution/200-299/0266.palindrome-permutation/) | `哈希表` | <font color=green>简单</font> |
90-
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> |
90+
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
9191
| [270](https://leetcode-cn.com/problems/closest-binary-search-tree-value) | [最接近的二叉搜索树值](/solution/200-299/0270.closest-binary-search-tree-value/) | ``,`二分查找` | <font color=green>简单</font> |
9292
| [276](https://leetcode-cn.com/problems/paint-fence) | [栅栏涂色](/solution/200-299/0276.paint-fence/) | `动态规划` | <font color=green>简单</font> |
9393
| [278](https://leetcode-cn.com/problems/first-bad-version) | [第一个错误的版本](/solution/200-299/0278.first-bad-version/) | `二分查找` | <font color=green>简单</font> |
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [160.相交链表](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>编写一个程序,找到两个单链表相交的起始节点。</p>
7+
8+
<p>如下面的两个链表<strong>:</strong></p>
9+
10+
<p><a href="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_statement.png"><img style="height: 130px; width: 400px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_statement.png" alt=""></a></p>
11+
12+
<p>在节点 c1 开始相交。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png"><img style="height: 130px; width: 400px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_1.png" alt=""></a></p>
19+
20+
<pre><strong>输入:</strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
21+
<strong>输出:</strong>Reference of the node with value = 8
22+
<strong>输入解释:</strong>相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong>示例&nbsp;2:</strong></p>
28+
29+
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_2.png"><img style="height: 136px; width: 350px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_2.png" alt=""></a></p>
30+
31+
<pre><strong>输入:</strong>intersectVal&nbsp;= 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
32+
<strong>输出:</strong>Reference of the node with value = 2
33+
<strong>输入解释:</strong>相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong>示例&nbsp;3:</strong></p>
39+
40+
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png"><img style="height: 126px; width: 200px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_3.png" alt=""></a></p>
41+
42+
<pre><strong>输入:</strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
43+
<strong>输出:</strong>null
44+
<strong>输入解释:</strong>从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
45+
<strong>解释:</strong>这两个链表不相交,因此返回 null。
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>注意:</strong></p>
51+
52+
<ul>
53+
<li>如果两个链表没有交点,返回 <code>null</code>.</li>
54+
<li>在返回结果后,两个链表仍须保持原有的结构。</li>
55+
<li>可假定整个链表结构中没有循环。</li>
56+
<li>程序尽量满足 O(<em>n</em>) 时间复杂度,且仅用 O(<em>1</em>) 内存。</li>
57+
</ul>
58+
</div>
59+
60+
### 解题思路
61+
62+
```
63+
链表A和链表B设置指针A和指针B,然后开始遍历链表,如果遍历完当前链表,则将指针指向另外一个链表的头部继续遍历,直至两个指针相遇。
64+
65+
最终两个指针分别走过的路径为:
66+
67+
指针A:a+c+b
68+
69+
指针B :b+c+a
70+
71+
明显 a+c+b = b+c+a,因而如果两个链表相交,则指针A和指针B必定在相交结点相遇。
72+
```
73+
74+
![](http://lc-photo.xwlin.com/160.png)
75+
76+
### 代码实现
77+
78+
<!-- tabs:start -->
79+
80+
#### **Golang**
81+
```go
82+
type ListNode struct {
83+
Val int
84+
Next *ListNode
85+
}
86+
87+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
88+
p, q := headA, headB
89+
for p != q {
90+
if p == nil {
91+
p = headA
92+
} else {
93+
p = p.Next
94+
}
95+
if q == nil {
96+
q = headB
97+
} else {
98+
q = q.Next
99+
}
100+
}
101+
return p
102+
}
103+
```
104+
105+
106+
<!-- tabs:end -->
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=160 lang=golang
5+
*
6+
* [160] 相交链表
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for singly-linked list.
12+
* type ListNode struct {
13+
* Val int
14+
* Next *ListNode
15+
* }
16+
*/
17+
type ListNode struct {
18+
Val int
19+
Next *ListNode
20+
}
21+
22+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
23+
p, q := headA, headB
24+
for p != q {
25+
if p == nil {
26+
p = headA
27+
} else {
28+
p = p.Next
29+
}
30+
if q == nil {
31+
q = headB
32+
} else {
33+
q = q.Next
34+
}
35+
}
36+
return p
37+
}
38+
39+
// @lc code=end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetIntersectionNode(t *testing.T) {
8+
9+
listNode1 := &ListNode{
10+
Val: 4,
11+
Next: &ListNode{
12+
Val: 1,
13+
Next: &ListNode{
14+
Val: 4,
15+
Next: nil,
16+
},
17+
},
18+
}
19+
listNode2 := &ListNode{
20+
Val: 1,
21+
Next: &ListNode{
22+
Val: 8,
23+
Next: &ListNode{
24+
Val: 4,
25+
Next: nil,
26+
},
27+
},
28+
}
29+
getIntersectionNode(listNode1, listNode2)
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [189.旋转数组](https://leetcode-cn.com/problems/rotate-array/description/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个数组,将数组中的元素向右移动&nbsp;<em>k&nbsp;</em>个位置,其中&nbsp;<em>k&nbsp;</em>是非负数。</p>
7+
8+
<p><strong>示例 1:</strong></p>
9+
10+
<pre><strong>输入:</strong> <code>[1,2,3,4,5,6,7]</code> 和 <em>k</em> = 3
11+
<strong>输出:</strong> <code>[5,6,7,1,2,3,4]</code>
12+
<strong>解释:</strong>
13+
向右旋转 1 步: <code>[7,1,2,3,4,5,6]</code>
14+
向右旋转 2 步: <code>[6,7,1,2,3,4,5]
15+
</code>向右旋转 3 步: <code>[5,6,7,1,2,3,4]</code>
16+
</pre>
17+
18+
<p><strong>示例&nbsp;2:</strong></p>
19+
20+
<pre><strong>输入:</strong> <code>[-1,-100,3,99]</code> 和 <em>k</em> = 2
21+
<strong>输出:</strong> [3,99,-1,-100]
22+
<strong>解释:</strong>
23+
向右旋转 1 步: [99,-1,-100,3]
24+
向右旋转 2 步: [3,99,-1,-100]</pre>
25+
26+
<p><strong>说明:</strong></p>
27+
28+
<ul>
29+
<li>尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。</li>
30+
<li>要求使用空间复杂度为&nbsp;O(1) 的&nbsp;<strong>原地&nbsp;</strong>算法。</li>
31+
</ul>
32+
</div>
33+
34+
### 解题思路
35+
36+
1. copy只会复制值,下标重新开始计算
37+
38+
### 代码实现
39+
40+
<!-- tabs:start -->
41+
42+
#### **Golang**
43+
```go
44+
func rotate(nums []int, k int) {
45+
length := len(nums)
46+
if length != 0 {
47+
i := length - k%length
48+
copy(nums, append(nums[i:length], nums[0:i]...))
49+
}
50+
}
51+
```
52+
53+
54+
<!-- tabs:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=189 lang=golang
5+
*
6+
* [189] 旋转数组
7+
*/
8+
9+
// @lc code=start
10+
func rotate(nums []int, k int) {
11+
length := len(nums)
12+
if length != 0 {
13+
i := length - k%length
14+
copy(nums, append(nums[i:length], nums[0:i]...))
15+
}
16+
}
17+
18+
// @lc code=end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRotate(t *testing.T) {
8+
var target int
9+
var ret []int
10+
var nums []int
11+
12+
ret = []int{5, 6, 7, 1, 2, 3, 4}
13+
nums = []int{1, 2, 3, 4, 5, 6, 7}
14+
target = 3
15+
rotate(nums, target)
16+
for i, v := range nums {
17+
if v != ret[i] {
18+
t.Fatalf("case fails: %v\n", ret)
19+
}
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /