Skip to content

Commit 6165466

Browse files
committed
Merge branch 'master' of https://github.com/halfrost/LeetCode-Go
2 parents e445874 + a45992e commit 6165466

File tree

156 files changed

+9087
-3603
lines changed

Some content is hidden

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

156 files changed

+9087
-3603
lines changed

README.md

Lines changed: 1987 additions & 1868 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func longestCommonPrefix(strs []string) string {
6+
sort.Slice(strs, func(i, j int) bool {
7+
return len(strs[i]) <= len(strs[j])
8+
})
9+
minLen := len(strs[0])
10+
if minLen == 0 {
11+
return ""
12+
}
13+
var commonPrefix []byte
14+
for i := 0; i < minLen; i++ {
15+
for j := 1; j < len(strs); j++ {
16+
if strs[j][i] != strs[0][i] {
17+
return string(commonPrefix)
18+
}
19+
}
20+
commonPrefix = append(commonPrefix, strs[0][i])
21+
}
22+
return string(commonPrefix)
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question14 struct {
9+
para14
10+
ans14
11+
}
12+
13+
// para 是参数
14+
type para14 struct {
15+
strs []string
16+
}
17+
18+
// ans 是答案
19+
type ans14 struct {
20+
ans string
21+
}
22+
23+
func Test_Problem14(t *testing.T) {
24+
25+
qs := []question14{
26+
27+
{
28+
para14{[]string{"flower", "flow", "flight"}},
29+
ans14{"fl"},
30+
},
31+
32+
{
33+
para14{[]string{"dog", "racecar", "car"}},
34+
ans14{""},
35+
},
36+
}
37+
38+
fmt.Printf("------------------------Leetcode Problem 14------------------------\n")
39+
40+
for _, q := range qs {
41+
_, p := q.ans14, q.para14
42+
fmt.Printf("【input】:%v 【output】:%v\n", p.strs, longestCommonPrefix(p.strs))
43+
}
44+
fmt.Printf("\n\n\n")
45+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# [14. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)
2+
3+
## 题目
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string "".
8+
9+
**Example 1**:
10+
11+
Input: strs = ["flower","flow","flight"]
12+
Output: "fl"
13+
14+
**Example 2**:
15+
16+
Input: strs = ["dog","racecar","car"]
17+
Output: ""
18+
Explanation: There is no common prefix among the input strings.
19+
20+
**Constraints:**
21+
22+
- 1 <= strs.length <= 200
23+
- 0 <= strs[i].length <= 200
24+
- strs[i] consists of only lower-case English letters.
25+
26+
## 题目大意
27+
28+
编写一个函数来查找字符串数组中的最长公共前缀。
29+
30+
如果不存在公共前缀,返回空字符串 ""。
31+
32+
## 解题思路
33+
34+
- 对 strs 按照字符串长度进行升序排序,求出 strs 中长度最小字符串的长度 minLen
35+
- 逐个比较长度最小字符串与其它字符串中的字符,如果不相等就返回 commonPrefix,否则就把该字符加入 commonPrefix
36+
37+
## 代码
38+
39+
```go
40+
41+
package leetcode
42+
43+
import "sort"
44+
45+
func longestCommonPrefix(strs []string) string {
46+
sort.Slice(strs, func(i, j int) bool {
47+
return len(strs[i]) <= len(strs[j])
48+
})
49+
minLen := len(strs[0])
50+
if minLen == 0 {
51+
return ""
52+
}
53+
var commonPrefix []byte
54+
for i := 0; i < minLen; i++ {
55+
for j := 1; j < len(strs); j++ {
56+
if strs[j][i] != strs[0][i] {
57+
return string(commonPrefix)
58+
}
59+
}
60+
commonPrefix = append(commonPrefix, strs[0][i])
61+
}
62+
return string(commonPrefix)
63+
}
64+
```

leetcode/0031.Next-Permutation/31. Next Permutation.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package leetcode
22

3+
// 解法一
34
func nextPermutation(nums []int) {
45
i, j := 0, 0
56
for i = len(nums) - 2; i >= 0; i-- {
@@ -29,3 +30,43 @@ func reverse(nums *[]int, i, j int) {
2930
func swap(nums *[]int, i, j int) {
3031
(*nums)[i], (*nums)[j] = (*nums)[j], (*nums)[i]
3132
}
33+
34+
// 解法二
35+
// [2,(3),6,5,4,1] -> 2,(4),6,5,(3),1 -> 2,4, 1,3,5,6
36+
func nextPermutation1(nums []int) {
37+
var n = len(nums)
38+
var pIdx = checkPermutationPossibility(nums)
39+
if pIdx == -1 {
40+
reverse(&nums, 0, n-1)
41+
return
42+
}
43+
44+
var rp = len(nums) - 1
45+
// start from right most to leftward,find the first number which is larger than PIVOT
46+
for rp > 0 {
47+
if nums[rp] > nums[pIdx] {
48+
swap(&nums, pIdx, rp)
49+
break
50+
} else {
51+
rp--
52+
}
53+
}
54+
// Finally, Reverse all elements which are right from pivot
55+
reverse(&nums, pIdx+1, n-1)
56+
}
57+
58+
// checkPermutationPossibility returns 1st occurrence Index where
59+
// value is in decreasing order(from right to left)
60+
// returns -1 if not found(it's already in its last permutation)
61+
func checkPermutationPossibility(nums []int) (idx int) {
62+
// search right to left for 1st number(from right) that is not in increasing order
63+
var rp = len(nums) - 1
64+
for rp > 0 {
65+
if nums[rp-1] < nums[rp] {
66+
idx = rp - 1
67+
return idx
68+
}
69+
rp--
70+
}
71+
return -1
72+
}

leetcode/0048.Rotate-Image/48. Rotate Image.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package leetcode
22

3+
// 解法一
34
func rotate(matrix [][]int) {
45
length := len(matrix)
56
// rotate by diagonal 对角线变换
@@ -15,3 +16,46 @@ func rotate(matrix [][]int) {
1516
}
1617
}
1718
}
19+
20+
// 解法二
21+
func rotate1(matrix [][]int) {
22+
n := len(matrix)
23+
if n == 1 {
24+
return
25+
}
26+
/* rotate clock-wise = 1. transpose matrix => 2. reverse(matrix[i])
27+
28+
1 2 3 4 1 5 9 13 13 9 5 1
29+
5 6 7 8 => 2 6 10 14 => 14 10 6 2
30+
9 10 11 12 3 7 11 15 15 11 7 3
31+
13 14 15 16 4 8 12 16 16 12 8 4
32+
33+
*/
34+
35+
for i := 0; i < n; i++ {
36+
// transpose, i=rows, j=columns
37+
// j = i+1, coz diagonal elements didn't change in a square matrix
38+
for j := i + 1; j < n; j++ {
39+
swap(matrix, i, j)
40+
}
41+
// reverse each row of the image
42+
matrix[i] = reverse(matrix[i])
43+
}
44+
}
45+
46+
// swap changes original slice's i,j position
47+
func swap(nums [][]int, i, j int) {
48+
nums[i][j], nums[j][i] = nums[j][i], nums[i][j]
49+
}
50+
51+
// reverses a row of image, matrix[i]
52+
func reverse(nums []int) []int {
53+
var lp, rp = 0, len(nums) - 1
54+
55+
for lp < rp {
56+
nums[lp], nums[rp] = nums[rp], nums[lp]
57+
lp++
58+
rp--
59+
}
60+
return nums
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func lengthOfLastWord(s string) int {
4+
last := len(s) - 1
5+
for last >= 0 && s[last] == ' ' {
6+
last--
7+
}
8+
if last < 0 {
9+
return 0
10+
}
11+
first := last
12+
for first >= 0 && s[first] != ' ' {
13+
first--
14+
}
15+
return last - first
16+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question58 struct {
9+
para58
10+
ans58
11+
}
12+
13+
// para 是参数
14+
type para58 struct {
15+
s string
16+
}
17+
18+
// ans 是答案
19+
type ans58 struct {
20+
ans int
21+
}
22+
23+
func Test_Problem58(t *testing.T) {
24+
25+
qs := []question58{
26+
27+
{
28+
para58{"Hello World"},
29+
ans58{5},
30+
},
31+
32+
{
33+
para58{" fly me to the moon "},
34+
ans58{4},
35+
},
36+
37+
{
38+
para58{"luffy is still joyboy"},
39+
ans58{6},
40+
},
41+
}
42+
43+
fmt.Printf("------------------------Leetcode Problem 58------------------------\n")
44+
45+
for _, q := range qs {
46+
_, p := q.ans58, q.para58
47+
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLastWord(p.s))
48+
}
49+
fmt.Printf("\n\n\n")
50+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [58. Length of Last Word](https://leetcode.com/problems/length-of-last-word/)
2+
3+
4+
## 题目
5+
6+
Given a string `s` consisting of some words separated by some number of spaces, return *the length of the **last** word in the string.*
7+
8+
**word** is a maximal substring consisting of non-space characters only.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: s = "Hello World"
14+
Output: 5
15+
Explanation: The last word is "World" with length 5.
16+
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: s = " fly me to the moon "
23+
Output: 4
24+
Explanation: The last word is "moon" with length 4.
25+
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: s = "luffy is still joyboy"
32+
Output: 6
33+
Explanation: The last word is "joyboy" with length 6.
34+
35+
```
36+
37+
**Constraints:**
38+
39+
- `1 <= s.length <= 104`
40+
- `s` consists of only English letters and spaces `' '`.
41+
- There will be at least one word in `s`.
42+
43+
## 题目大意
44+
45+
给你一个字符串 `s`,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。**单词** 是指仅由字母组成、不包含任何空格字符的最大子字符串。
46+
47+
## 解题思路
48+
49+
- 先从后过滤掉空格找到单词尾部,再从尾部向前遍历,找到单词头部,最后两者相减,即为单词的长度。
50+
51+
## 代码
52+
53+
```go
54+
package leetcode
55+
56+
func lengthOfLastWord(s string) int {
57+
last := len(s) - 1
58+
for last >= 0 && s[last] == ' ' {
59+
last--
60+
}
61+
if last < 0 {
62+
return 0
63+
}
64+
first := last
65+
for first >= 0 && s[first] != ' ' {
66+
first--
67+
}
68+
return last - first
69+
}
70+
```

leetcode/0136.Single-Number/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ Your algorithm should have a linear runtime complexity. Could you implement it w
2626
## 解题思路
2727

2828
- 题目要求不能使用辅助空间,并且时间复杂度只能是线性的。
29-
- 题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现依次的数字,因为那些出现两次的数字全部在异或中抵消掉了。于是最终做法是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其他数字都出现了两次,在异或中全部抵消掉了。**利用的性质是 x^x = 0**
29+
- 题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现一次的数字,因为那些出现两次的数字全部在异或中抵消掉了。于是最终做法是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其他数字都出现了两次,在异或中全部抵消掉了。**利用的性质是 x^x = 0**

0 commit comments

Comments
 (0)