Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a44f8d

Browse files
committed
feat: leetcode
1 parent 1cd8f3a commit 5a44f8d

File tree

1,561 files changed

+76168
-15
lines changed

Some content is hidden

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

1,561 files changed

+76168
-15
lines changed

0001.Two-Sum/1. Two Sum.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode
2+
3+
func twoSum(nums []int, target int) []int {
4+
m := make(map[int]int)
5+
for k, v := range nums {
6+
if idx, ok := m[target-v]; ok {
7+
return []int{idx, k}
8+
}
9+
m[v] = k
10+
}
11+
return nil
12+
}

0001.Two-Sum/1. Two Sum_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1 struct {
9+
para1
10+
ans1
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1 struct {
16+
nums []int
17+
target int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem1(t *testing.T) {
27+
28+
qs := []question1{
29+
{
30+
para1{[]int{3, 2, 4}, 6},
31+
ans1{[]int{1, 2}},
32+
},
33+
34+
{
35+
para1{[]int{3, 2, 4}, 5},
36+
ans1{[]int{0, 1}},
37+
},
38+
39+
{
40+
para1{[]int{0, 8, 7, 3, 3, 4, 2}, 11},
41+
ans1{[]int{1, 3}},
42+
},
43+
44+
{
45+
para1{[]int{0, 1}, 1},
46+
ans1{[]int{0, 1}},
47+
},
48+
49+
{
50+
para1{[]int{0, 3}, 5},
51+
ans1{[]int{}},
52+
},
53+
// 如需多个测试,可以复制上方元素。
54+
}
55+
56+
fmt.Printf("------------------------Leetcode Problem 1------------------------\n")
57+
58+
for _, q := range qs {
59+
_, p := q.ans1, q.para1
60+
fmt.Printf("【input】:%v 【output】:%v\n", p, twoSum(p.nums, p.target))
61+
}
62+
fmt.Printf("\n\n\n")
63+
}

0001.Two-Sum/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [1. Two Sum](https://leetcode.com/problems/two-sum/)
2+
3+
## 题目
4+
5+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
6+
7+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
8+
9+
Example:
10+
11+
```
12+
Given nums = [2, 7, 11, 15], target = 9,
13+
14+
Because nums[0] + nums[1] = 2 + 7 = 9,
15+
return [0, 1].
16+
```
17+
18+
19+
20+
## 题目大意
21+
22+
在数组中找到 2 个数之和等于给定值的数字,结果返回 2 个数字在数组中的下标。
23+
24+
## 解题思路
25+
26+
这道题最优的做法时间复杂度是 O(n)。
27+
28+
顺序扫描数组,对每一个元素,在 map 中找能组合给定值的另一半数字,如果找到了,直接返回 2 个数字的下标即可。如果找不到,就把这个数字存入 map 中,等待扫到“另一半”数字的时候,再取出来返回结果。
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// ListNode define
8+
type ListNode = structures.ListNode
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* type ListNode struct {
13+
* Val int
14+
* Next *ListNode
15+
* }
16+
*/
17+
18+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
19+
head := &ListNode{Val: 0}
20+
n1, n2, carry, current := 0, 0, 0, head
21+
for l1 != nil || l2 != nil || carry != 0 {
22+
if l1 == nil {
23+
n1 = 0
24+
} else {
25+
n1 = l1.Val
26+
l1 = l1.Next
27+
}
28+
if l2 == nil {
29+
n2 = 0
30+
} else {
31+
n2 = l2.Val
32+
l2 = l2.Next
33+
}
34+
current.Next = &ListNode{Val: (n1 + n2 + carry) % 10}
35+
current = current.Next
36+
carry = (n1 + n2 + carry) / 10
37+
}
38+
return head.Next
39+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question2 struct {
11+
para2
12+
ans2
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para2 struct {
18+
one []int
19+
another []int
20+
}
21+
22+
// ans 是答案
23+
// one 代表第一个答案
24+
type ans2 struct {
25+
one []int
26+
}
27+
28+
func Test_Problem2(t *testing.T) {
29+
30+
qs := []question2{
31+
32+
{
33+
para2{[]int{}, []int{}},
34+
ans2{[]int{}},
35+
},
36+
37+
{
38+
para2{[]int{1}, []int{1}},
39+
ans2{[]int{2}},
40+
},
41+
42+
{
43+
para2{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
44+
ans2{[]int{2, 4, 6, 8}},
45+
},
46+
47+
{
48+
para2{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
49+
ans2{[]int{2, 4, 6, 8, 0, 1}},
50+
},
51+
52+
{
53+
para2{[]int{1}, []int{9, 9, 9, 9, 9}},
54+
ans2{[]int{0, 0, 0, 0, 0, 1}},
55+
},
56+
57+
{
58+
para2{[]int{9, 9, 9, 9, 9}, []int{1}},
59+
ans2{[]int{0, 0, 0, 0, 0, 1}},
60+
},
61+
62+
{
63+
para2{[]int{2, 4, 3}, []int{5, 6, 4}},
64+
ans2{[]int{7, 0, 8}},
65+
},
66+
67+
{
68+
para2{[]int{1, 8, 3}, []int{7, 1}},
69+
ans2{[]int{8, 9, 3}},
70+
},
71+
// 如需多个测试,可以复制上方元素。
72+
}
73+
74+
fmt.Printf("------------------------Leetcode Problem 2------------------------\n")
75+
76+
for _, q := range qs {
77+
_, p := q.ans2, q.para2
78+
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(addTwoNumbers(structures.Ints2List(p.one), structures.Ints2List(p.another))))
79+
}
80+
fmt.Printf("\n\n\n")
81+
}

0002.Add-Two-Numbers/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)
2+
3+
## 题目
4+
5+
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
Example:
10+
11+
```
12+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
Output: 7 -> 0 -> 8
14+
```
15+
Explanation: 342 + 465 = 807.
16+
17+
18+
## 题目大意
19+
20+
2 个逆序的链表,要求从低位开始相加,得出结果也逆序输出,返回值是逆序结果链表的头结点。
21+
22+
## 解题思路
23+
24+
需要注意的是各种进位问题。
25+
26+
极端情况,例如
27+
```
28+
Input: (9 -> 9 -> 9 -> 9 -> 9) + (1 -> )
29+
Output: 0 -> 0 -> 0 -> 0 -> 0 -> 1
30+
```
31+
32+
为了处理方法统一,可以先建立一个虚拟头结点,这个虚拟头结点的 Next 指向真正的 head,这样 head 不需要单独处理,直接 while 循环即可。另外判断循环终止的条件不用是 p.Next != nil,这样最后一位还需要额外计算,循环终止条件应该是 p != nil。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode
2+
3+
// 解法一 位图
4+
func lengthOfLongestSubstring(s string) int {
5+
if len(s) == 0 {
6+
return 0
7+
}
8+
var bitSet [256]bool
9+
result, left, right := 0, 0, 0
10+
for left < len(s) {
11+
// 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
12+
if bitSet[s[right]] {
13+
bitSet[s[left]] = false
14+
left++
15+
} else {
16+
bitSet[s[right]] = true
17+
right++
18+
}
19+
if result < right-left {
20+
result = right - left
21+
}
22+
if left+result >= len(s) || right >= len(s) {
23+
break
24+
}
25+
}
26+
return result
27+
}
28+
29+
// 解法二 滑动窗口
30+
func lengthOfLongestSubstring_(s string) int {
31+
if len(s) == 0 {
32+
return 0
33+
}
34+
var freq [256]int
35+
result, left, right := 0, 0, -1
36+
37+
for left < len(s) {
38+
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
39+
freq[s[right+1]-'a']++
40+
right++
41+
} else {
42+
freq[s[left]-'a']--
43+
left++
44+
}
45+
result = max(result, right-left+1)
46+
}
47+
return result
48+
}
49+
50+
func max(a int, b int) int {
51+
if a > b {
52+
return a
53+
}
54+
return b
55+
}
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 question3 struct {
9+
para3
10+
ans3
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para3 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans3 struct {
22+
one int
23+
}
24+
25+
func Test_Problem3(t *testing.T) {
26+
27+
qs := []question3{
28+
29+
{
30+
para3{"abcabcbb"},
31+
ans3{3},
32+
},
33+
34+
{
35+
para3{"bbbbb"},
36+
ans3{1},
37+
},
38+
39+
{
40+
para3{"pwwkew"},
41+
ans3{3},
42+
},
43+
44+
{
45+
para3{""},
46+
ans3{0},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 3------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans3, q.para3
54+
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring_(p.s))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}

0 commit comments

Comments
 (0)