Skip to content

Commit 7772157

Browse files
committed
添加 problem 7、136、137、169、187、190、201、229、260
1 parent 7d0c085 commit 7772157

27 files changed

+990
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode
2+
3+
func reverse_7(x int) int {
4+
tmp := 0
5+
for x != 0 {
6+
tmp = tmp*10 + x%10
7+
x = x / 10
8+
}
9+
if tmp > 1<<31-1 || tmp < -(1<<31) {
10+
return 0
11+
}
12+
return tmp
13+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question7 struct {
9+
para7
10+
ans7
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para7 struct {
16+
one int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans7 struct {
22+
one int
23+
}
24+
25+
func Test_Problem7(t *testing.T) {
26+
27+
qs := []question7{
28+
29+
question7{
30+
para7{321},
31+
ans7{123},
32+
},
33+
34+
question7{
35+
para7{-123},
36+
ans7{-321},
37+
},
38+
39+
question7{
40+
para7{120},
41+
ans7{21},
42+
},
43+
44+
question7{
45+
para7{1534236469},
46+
ans7{0},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 7------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans7, q.para7
54+
fmt.Printf("【input】:%v 【output】:%v\n", p.one, reverse_7(p.one))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [7. Reverse Integer](https://leetcode.com/problems/reverse-integer/)
2+
3+
4+
## 题目:
5+
6+
Given a 32-bit signed integer, reverse digits of an integer.
7+
8+
**Example 1:**
9+
10+
Input: 123
11+
Output: 321
12+
13+
**Example 2:**
14+
15+
Input: -123
16+
Output: -321
17+
18+
**Example 3:**
19+
20+
Input: 120
21+
Output: 21
22+
23+
**Note:**Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
24+
25+
## 题目大意
26+
27+
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。注意:假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31,  2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
28+
29+
30+
31+
## 解题思路
32+
33+
34+
- 这一题是简单题,要求反转 10 进制数。类似的题目有第 190 题。
35+
- 这一题只需要注意一点,反转以后的数字要求在 [−2^31, 2^31 − 1]范围内,超过这个范围的数字都要输出 0 。
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package leetcode
2+
3+
func singleNumber(nums []int) int {
4+
result := 0
5+
for i := 0; i < len(nums); i++ {
6+
result ^= nums[i]
7+
}
8+
return result
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question136 struct {
9+
para136
10+
ans136
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para136 struct {
16+
s []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans136 struct {
22+
one int
23+
}
24+
25+
func Test_Problem136(t *testing.T) {
26+
27+
qs := []question136{
28+
29+
question136{
30+
para136{[]int{2, 2, 1}},
31+
ans136{1},
32+
},
33+
34+
question136{
35+
para136{[]int{4, 1, 2, 1, 2}},
36+
ans136{4},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 136------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans136, q.para136
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, singleNumber(p.s))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# [136. Single Number](https://leetcode.com/problems/single-number/)
2+
3+
## 题目:
4+
5+
Given a **non-empty** array of integers, every element appears *twice* except for one. Find that single one.
6+
7+
**Note:**
8+
9+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
10+
11+
**Example 1:**
12+
13+
Input: [2,2,1]
14+
Output: 1
15+
16+
**Example 2:**
17+
18+
Input: [4,1,2,1,2]
19+
Output: 4
20+
21+
## 题目大意
22+
23+
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。要求算法时间复杂度是线性的,并且不使用额外的辅助空间。
24+
25+
26+
## 解题思路
27+
28+
- 题目要求不能使用辅助空间,并且时间复杂度只能是线性的。
29+
- 题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现依次的数字,因为那些出现两次的数字全部在异或中抵消掉了。于是最终做法是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其他数字都出现了两次,在异或中全部抵消掉了。**利用的性质是 x^x = 0**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode
2+
3+
func singleNumberII(nums []int) int {
4+
ones, twos := 0, 0
5+
for i := 0; i < len(nums); i++ {
6+
ones = (ones ^ nums[i]) & ^twos
7+
twos = (twos ^ nums[i]) & ^ones
8+
}
9+
return ones
10+
}
11+
12+
// 以下是拓展题
13+
// 在数组中每个元素都出现 5 次,找出只出现 1 次的数。
14+
15+
// 解法一
16+
func singleNumberIIIII(nums []int) int {
17+
na, nb, nc := 0, 0, 0
18+
for i := 0; i < len(nums); i++ {
19+
nb = nb ^ (nums[i] & na)
20+
na = (na ^ nums[i]) & ^nc
21+
nc = nc ^ (nums[i] & ^na & ^nb)
22+
}
23+
return na & ^nb & ^nc
24+
}
25+
26+
// 解法二
27+
func singleNumberIIIII_(nums []int) int {
28+
twos, threes, ones := 0xffffffff, 0xffffffff, 0
29+
for i := 0; i < len(nums); i++ {
30+
threes = threes ^ (nums[i] & twos)
31+
twos = (twos ^ nums[i]) & ^ones
32+
ones = ones ^ (nums[i] & ^twos & ^threes)
33+
}
34+
return ones
35+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question137 struct {
9+
para137
10+
ans137
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para137 struct {
16+
s []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans137 struct {
22+
one int
23+
}
24+
25+
func Test_Problem137(t *testing.T) {
26+
27+
qs := []question137{
28+
29+
question137{
30+
para137{[]int{2, 2, 3, 2}},
31+
ans137{3},
32+
},
33+
34+
question137{
35+
para137{[]int{0, 1, 0, 1, 0, 1, 99}},
36+
ans137{99},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 137------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans137, q.para137
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, singleNumberII(p.s))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [137. Single Number II](https://leetcode.com/problems/single-number-ii/)
2+
3+
4+
## 题目:
5+
6+
Given a **non-empty** array of integers, every element appears *three* times except for one, which appears exactly once. Find that single one.
7+
8+
**Note:**
9+
10+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
11+
12+
**Example 1:**
13+
14+
Input: [2,2,3,2]
15+
Output: 3
16+
17+
**Example 2:**
18+
19+
Input: [0,1,0,1,0,1,99]
20+
Output: 99
21+
22+
23+
## 题目大意
24+
25+
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。要求算法时间复杂度是线性的,并且不使用额外的辅助空间。
26+
27+
28+
29+
30+
## 解题思路
31+
32+
- 这一题是第 136 题的加强版。这类题也可以扩展,在数组中每个元素都出现 5 次,找出只出现 1 次的数。
33+
- 本题中要求找出只出现 1 次的数,出现 3 次的数都要被消除。第 136 题是消除出现 2 次的数。这一题也会相当相同的解法,出现 3 次的数也要被消除。定义状态,00、10、01,这 3 个状态。当一个数出现 3 次,那么它每个位置上的 1 出现的次数肯定是 3 的倍数,所以当 1 出现 3 次以后,就归零清除。如何能做到这点呢?仿造`三进制(00,10,01)` 就可以做到。
34+
- 变量 ones 中记录遍历中每个位上出现 1 的个数。将它与 A[i] 进行异或,目的是:
35+
- 每位上两者都是 1 的,表示历史统计结果 ones 出现1次、A[i]中又出现1次,则是出现 2 次,需要进位到 twos 变量中。
36+
- 每位上两者分别为 0、1 的,加入到 ones 统计结果中。
37+
- 最后还要 & ^twos ,是为了能做到三进制,出现 3 次就清零。例如 ones = x,那么 twos = 0,当 twos = x,那么 ones = 0;
38+
- 变量 twos 中记录遍历中每个位上出现 1 ,2次 的个数。与 A[i] 进行异或的目的和上述描述相同,不再赘述。
39+
40+
> 在 golang 中,&^ 表示 AND NOT 的意思。这里的 ^ 作为一元操作符,表示按位取反 (^0001 0100 = 1110 1011),X &^ Y 的意思是将 X 中与 Y 相异的位保留,相同的位清零。
41+
42+
> 在 golang 中没有 Java 中的 ~ 位操作运算符,Java 中的 ~ 运算符代表按位取反。这个操作就想当于 golang 中的 ^ 运算符当做一元运算符使用的效果。
43+
44+
这一题还可以继续扩展,在数组中每个元素都出现 5 次,找出只出现 1 次的数。那该怎么做呢?思路还是一样的,模拟一个五进制,5 次就会消除。代码如下:
45+
46+
// 解法一
47+
func singleNumberIII(nums []int) int {
48+
na, nb, nc := 0, 0, 0
49+
for i := 0; i < len(nums); i++ {
50+
nb = nb ^ (nums[i] & na)
51+
na = (na ^ nums[i]) & ^nc
52+
nc = nc ^ (nums[i] & ^na & ^nb)
53+
}
54+
return na & ^nb & ^nc
55+
}
56+
57+
// 解法二
58+
func singleNumberIIII(nums []int) int {
59+
twos, threes, ones := 0xffffffff, 0xffffffff, 0
60+
for i := 0; i < len(nums); i++ {
61+
threes = threes ^ (nums[i] & twos)
62+
twos = (twos ^ nums[i]) & ^ones
63+
ones = ones ^ (nums[i] & ^twos & ^threes)
64+
}
65+
return ones
66+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
// 解法一 时间复杂度 O(n) 空间复杂度 O(1)
4+
func majorityElement(nums []int) int {
5+
res, count := nums[0], 0
6+
for i := 0; i < len(nums); i++ {
7+
if count == 0 {
8+
res, count = nums[i], 1
9+
} else {
10+
if nums[i] == res {
11+
count++
12+
} else {
13+
count--
14+
}
15+
}
16+
}
17+
return res
18+
}
19+
20+
// 解法二 时间复杂度 O(n) 空间复杂度 O(n)
21+
func majorityElement_(nums []int) int {
22+
m := make(map[int]int)
23+
for _, v := range nums {
24+
m[v]++
25+
if m[v] > len(nums)/2 {
26+
return v
27+
}
28+
}
29+
return 0
30+
}

0 commit comments

Comments
 (0)