Skip to content

Commit 34e059f

Browse files
committed
We should keep practising.
2 parents c38a4d0 + 1f370b9 commit 34e059f

File tree

44 files changed

+1813
-116
lines changed

Some content is hidden

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

44 files changed

+1813
-116
lines changed

001.Two Sum/Jimmy Xiang/001.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func twoSum(arr []int, value int) (result [2]int) {
8+
9+
res := [2]int{-1, -1}
10+
11+
for i, v := range arr {
12+
13+
for j, o := range arr {
14+
15+
if i < j && (value-v) == o {
16+
17+
res[0] = i
18+
res[1] = j
19+
20+
return res
21+
}
22+
}
23+
}
24+
25+
return res
26+
27+
}
28+
29+
func main() {
30+
31+
arr := []int{222, 333, 11, 2, 3, 0, -2, 1, 2, 3}
32+
33+
sum := 0
34+
35+
result := twoSum(arr, sum)
36+
37+
if result[0] != -1 {
38+
39+
fmt.Printf("arr[%d]:%d + arr[%d]:%d = %d \n", result[0], arr[result[0]], result[1], arr[result[1]], sum)
40+
} else {
41+
fmt.Println("cann't find the numbers")
42+
}
43+
44+
}

001.Two Sum/Jimmy Xiang/TwoSum.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env python
22

3-
class Solution(object):
4-
def twoSum(self, nums, target):
3+
class Solution(object):
4+
def twoSum(self, nums, target):
55
d = {}
66
for i in range(len(nums)):
7-
if d.get(target - nums[i], None) == None: #not found
7+
if d.get(target - nums[i], None) == None: #not found
88
d[nums[i]] = i
99
else:
1010
return [d[target - nums[i] ] + 1 , i + 1] #rtype list
1111

1212

1313

14-
nums = [6,1,2,7]
14+
nums = [6,1,2,7]
1515
target = 9
1616

1717
s = Solution()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"container/list"
5+
"fmt"
6+
)
7+
8+
func addTwoNumbers(a *list.List, b *list.List) (c *list.List) {
9+
10+
d := list.New()
11+
12+
for e1, e2 := a.Front(), b.Front(); e1 != nil && e2 != nil; e1, e2 = e1.Next(), e2.Next() {
13+
14+
v1 := e1.Value.(int) //e1.Value 为 interface{}
15+
v2 := e2.Value.(int)
16+
17+
value := (v1 + v2) % 10
18+
19+
d.PushBack(value)
20+
21+
if e1.Next() == nil {
22+
23+
for e2.Next() != nil {
24+
25+
d.PushBack(e2.Next().Value.(int))
26+
e2 = e2.Next()
27+
}
28+
}
29+
30+
if e2.Next() == nil {
31+
32+
for e1.Next() != nil {
33+
34+
d.PushBack(e1.Next().Value.(int))
35+
e1 = e1.Next()
36+
}
37+
38+
}
39+
40+
}
41+
42+
return d
43+
}
44+
45+
func main() {
46+
47+
a := list.New()
48+
b := list.New()
49+
c := list.New()
50+
51+
a.PushBack(1)
52+
a.PushBack(2)
53+
a.PushBack(9)
54+
a.PushBack(4)
55+
56+
b.PushBack(3)
57+
b.PushBack(7)
58+
59+
c = addTwoNumbers(a, b)
60+
61+
for e := c.Front(); e != nil; e = e.Next() {
62+
63+
fmt.Println(e.Value)
64+
}
65+
}

002.Add Two Numbers/Jimmy Xiang/Add Two Numbers.cpp

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func lengthOfLongestSubstring(str string) int {
8+
9+
arr := [256]int{}
10+
11+
for _, v := range arr {
12+
13+
arr[byte(v)] = -1
14+
}
15+
16+
start := -1
17+
max := 0
18+
19+
for i, v := range str {
20+
21+
if arr[byte(v)] > start {
22+
start = arr[byte(v)]
23+
}
24+
25+
if i-start > max {
26+
27+
max = i - start
28+
}
29+
30+
arr[byte(v)] = i
31+
}
32+
33+
return max
34+
35+
}
36+
37+
func main() {
38+
39+
str := "ab"
40+
v := lengthOfLongestSubstring(str)
41+
42+
fmt.Printf("%s\n%d\n", str, v)
43+
44+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
9+
}

004.Median of Two Sorted Arrays/Median of Two Sorted Arrays.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ There are two sorted arrays nums1 and nums2 of size m and n respectively.
44

55
Find the median of the two sorted arrays.
66

7-
The overall run time complexity should be O(log (m+n)).
7+
The overall run time complexity should be O(log (m+n)).
8+

009.Palindrome Number/shadowcoder/PalindromeNum.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bool isy(int x) {
1+
bool isPalindrome(int x) {
22
int y = 0;
33
int origin = x;
44
if (x < 0) return 0;

022.Generate Parentheses/shadowcoder/parentheses.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ char** generateParenthesis(int n, int* returnSize)
2828
int left = n, right = n;
2929
char line[1024] = {0};
3030
int lp = 0;
31-
char **result = (char **)calloc(sizeof(char *), 81920);
31+
char **result = (char **)calloc(sizeof(char *), 8192);
3232
int i;
33-
for (i = 0; i < 81920; i++)
33+
for (i = 0; i < 8192; i++)
3434
result[i] = (char *)calloc(sizeof(char), n + 1);
3535
*returnSize = 0;
3636
fun(left, right, line, lp, result, returnSize);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func removeDuplicate(arr []int) int {
8+
9+
if len(arr) == 0 {
10+
return 0
11+
}
12+
13+
num := 0
14+
15+
for _, value := range arr {
16+
if value != arr[num] {
17+
num += 1
18+
arr[num] = value
19+
}
20+
21+
}
22+
23+
return num + 1
24+
}
25+
26+
func main() {
27+
28+
arr := []int{}
29+
num := removeDuplicate(arr)
30+
31+
fmt.Println(arr[:num], num)
32+
33+
}

0 commit comments

Comments
 (0)