Skip to content

Commit 5fb95d8

Browse files
committed
fork from aQuaYi/LeetCode-in-Go
1 parent 02ab476 commit 5fb95d8

File tree

2,158 files changed

+103671
-0
lines changed

Some content is hidden

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

2,158 files changed

+103671
-0
lines changed

.travis.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
language: rust
2+
3+
sudo: required
4+
# e6f235e5-d272-40ee-87f0-60264eb6ce19
5+
rust:
6+
- stable
7+
- beta
8+
- nightly
9+
10+
matrix:
11+
allow_failures:
12+
- rust: nightly
13+
14+
addons:
15+
apt:
16+
packages:
17+
- libcurl4-openssl-dev
18+
- libelf-dev
19+
- libdw-dev
20+
- cmake
21+
- gcc
22+
- binutils-dev
23+
- libiberty-dev
24+
25+
after_success: |
26+
wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz &&
27+
tar xzf master.tar.gz &&
28+
cd kcov-master &&
29+
mkdir build &&
30+
cd build &&
31+
cmake .. &&
32+
make &&
33+
make install DESTDIR=../../kcov-build &&
34+
cd ../.. &&
35+
rm -rf kcov-master &&
36+
for file in target/debug/examplerust-*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; ./kcov-build/usr/local/bin/kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
37+
bash <(curl -s https://codecov.io/bash) &&
38+
echo "Uploaded code coverage"

Algorithms/0001.two-sum/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
```text
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+
```rs
21+
a + b = target
22+
```
23+
24+
也可以看成是
25+
26+
```rs
27+
a = target - b
28+
```
29+
30+
`map[整数]整数的序号`中,可以查询到a的序号。这样就不用嵌套两个for循环了。

Algorithms/0001.two-sum/two-sum.rs

Whitespace-only changes.

Algorithms/0001.two-sum/two-sum_test.rs

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
```text
10+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
11+
Output: 7 -> 0 -> 8
12+
```
13+
14+
## 解题思路
15+
16+
```text
17+
(2 -> 4 -> 3)是 342
18+
19+
(5 -> 6 -> 4)是 465
20+
21+
(7 -> 0 -> 8)是 807
22+
23+
342 + 465 = 807
24+
```
25+
26+
所以,题目的本意是,把整数换了一种表达方式后,实现其加法。
27+
28+
设计程序时候,需要处理的点有
29+
30+
1. 位上的加法,需要处理进位问题
31+
1. 如何进入下一位运算
32+
1. 按位相加结束后,也还需要处理进位问题。
33+
34+
## 总结
35+
36+
读懂题意后,按步骤实现题目要求。
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem0002
2+
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Go/kit"
5+
)
6+
7+
// ListNode defines for singly-linked list.
8+
// type ListNode struct {
9+
// Val int
10+
// Next *ListNode
11+
// }
12+
type ListNode = kit.ListNode
13+
14+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
15+
resPre := &ListNode{}
16+
cur := resPre
17+
carry := 0
18+
19+
for l1 != nil || l2 != nil || carry > 0 {
20+
sum := carry
21+
22+
if l1 != nil {
23+
sum += l1.Val
24+
l1 = l1.Next
25+
}
26+
27+
if l2 != nil {
28+
sum += l2.Val
29+
l2 = l2.Next
30+
}
31+
32+
carry = sum / 10
33+
34+
cur.Next = &ListNode{Val: sum % 10}
35+
cur = cur.Next
36+
}
37+
38+
return resPre.Next
39+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package problem0002
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type para struct {
10+
one *ListNode
11+
two *ListNode
12+
}
13+
14+
type ans struct {
15+
one *ListNode
16+
}
17+
18+
type question struct {
19+
p para
20+
a ans
21+
}
22+
23+
func makeListNode(is []int) *ListNode {
24+
if len(is) == 0 {
25+
return nil
26+
}
27+
28+
res := &ListNode{
29+
Val: is[0],
30+
}
31+
temp := res
32+
33+
for i := 1; i < len(is); i++ {
34+
temp.Next = &ListNode{Val: is[i]}
35+
temp = temp.Next
36+
}
37+
38+
return res
39+
}
40+
41+
func Test_OK(t *testing.T) {
42+
ast := assert.New(t)
43+
44+
qs := []question{
45+
question{
46+
p: para{
47+
one: makeListNode([]int{2, 4, 3}),
48+
two: makeListNode([]int{5, 6, 4}),
49+
},
50+
a: ans{
51+
one: makeListNode([]int{7, 0, 8}),
52+
},
53+
},
54+
question{
55+
p: para{
56+
one: makeListNode([]int{9, 8, 7, 6, 5}),
57+
two: makeListNode([]int{1, 1, 2, 3, 4}),
58+
},
59+
a: ans{
60+
one: makeListNode([]int{0, 0, 0, 0, 0, 1}),
61+
},
62+
},
63+
question{
64+
p: para{
65+
one: makeListNode([]int{0}),
66+
two: makeListNode([]int{5, 6, 4}),
67+
},
68+
a: ans{
69+
one: makeListNode([]int{5, 6, 4}),
70+
},
71+
},
72+
}
73+
74+
for _, q := range qs {
75+
a, p := q.a, q.p
76+
ast.Equal(a.one, addTwoNumbers(p.one, p.two), "输入:%v", p)
77+
}
78+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
2+
3+
## 题目
4+
Given a string, find the length of the longest substring without repeating characters.
5+
6+
Examples:
7+
8+
Given "abcabcbb", the answer is "abc", which the length is 3.
9+
10+
Given "bbbbb", the answer is "b", with the length of 1.
11+
12+
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
13+
14+
## 解题思路
15+
利用s[left:i+1]来表示s[:i+1]中的包含s[i]的最长子字符串。
16+
location[s[i]]是字符s[i]在s[:i+1]中倒数第二次出现的序列号。
17+
当left < location[s[i]]的时候,说明字符s[i]出现了两次。需要设置 left = location[s[i]] + 1,
18+
保证字符s[i]只出现一次。
19+
20+
## 总结
21+
利用Location保存字符上次出现的序列号,避免了查询工作。location和[Two Sum](./algorithms/0001.two-sum)中的m是一样的作用。
22+
```go
23+
// m 负责保存map[整数]整数的序列号
24+
m := make(map[int]int, len(nums))
25+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package problem0003
2+
3+
func lengthOfLongestSubstring(s string) int {
4+
// location[s[i]] == j 表示:
5+
// s中第i个字符串,上次出现在s的j位置,所以,在s[j+1:i]中没有s[i]
6+
// location[s[i]] == -1 表示: s[i] 在s中第一次出现
7+
location := [256]int{} // 只有256长是因为,假定输入的字符串只有ASCII字符
8+
for i := range location {
9+
location[i] = -1 // 先设置所有的字符都没有见过
10+
}
11+
12+
maxLen, left := 0, 0
13+
14+
for i := 0; i < len(s); i++ {
15+
// 说明s[i]已经在s[left:i+1]中重复了
16+
// 并且s[i]上次出现的位置在location[s[i]]
17+
if location[s[i]] >= left {
18+
left = location[s[i]] + 1 // 在s[left:i+1]中去除s[i]字符及其之前的部分
19+
} else if i+1-left > maxLen {
20+
// fmt.Println(s[left:i+1])
21+
maxLen = i + 1 - left
22+
}
23+
location[s[i]] = i
24+
}
25+
26+
return maxLen
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package problem0003
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type para struct {
10+
one string
11+
}
12+
13+
type ans struct {
14+
one int
15+
}
16+
17+
type question struct {
18+
p para
19+
a ans
20+
}
21+
22+
func Test_OK(t *testing.T) {
23+
ast := assert.New(t)
24+
25+
qs := []question{
26+
question{
27+
p: para{
28+
one: "abcabcbb",
29+
},
30+
a: ans{
31+
one: 3,
32+
},
33+
},
34+
question{
35+
p: para{
36+
one: "bbbbbbbb",
37+
},
38+
a: ans{
39+
one: 1,
40+
},
41+
},
42+
question{
43+
p: para{
44+
one: "pwwkew",
45+
},
46+
a: ans{
47+
one: 3,
48+
},
49+
},
50+
}
51+
52+
for _, q := range qs {
53+
a, p := q.a, q.p
54+
ast.Equal(a.one, lengthOfLongestSubstring(p.one), "输入:%v", p)
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# [4. Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)
2+
3+
## 题目
4+
There are two sorted arrays nums1 and nums2 of size m and n respectively.
5+
6+
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7+
8+
Example 1:
9+
```
10+
nums1 = [1, 3]
11+
nums2 = [2]
12+
The median is 2.0
13+
```
14+
Example 2:
15+
```
16+
nums1 = [1, 2]
17+
nums2 = [3, 4]
18+
The median is (2 + 3)/2 = 2.5
19+
```
20+
## 解题思路
21+
输入两个已经排序好的整数切片,合并两个切片,返回合并后切片的中位数。
22+
23+
所以,题目的关键是,如何高效地合并切片。
24+
25+
## 总结
26+
分步骤完成程序
27+

0 commit comments

Comments
 (0)