Skip to content

Commit c96ad47

Browse files
authored
Merge pull request kamyu104#25 from xiaopeng163/master
try to add some golang solution
2 parents 6b8657a + 9a0e965 commit c96ad47

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

Golang/add-two-numbers.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
// ListNode represents a non-negative number.
4+
// You are given two linked lists representing two non-negative numbers.
5+
// The digits are stored in reverse order and each of their nodes contain a single digit.
6+
// Add the two numbers and return it as a linked list.
7+
//
8+
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
9+
// Output: 7 -> 0 -> 8
10+
type ListNode struct {
11+
Val int
12+
Next *ListNode
13+
}
14+
15+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
16+
results := &ListNode{}
17+
node := results
18+
node1 := l1
19+
node2 := l2
20+
21+
overten := false
22+
23+
for node1 != nil || node2 != nil {
24+
25+
tmp := 0
26+
27+
if node1 != nil {
28+
tmp = tmp + node1.Val
29+
node1 = node1.Next
30+
}
31+
32+
if node2 != nil {
33+
tmp = tmp + node2.Val
34+
node2 = node2.Next
35+
}
36+
if overten {
37+
tmp++
38+
}
39+
40+
if tmp >= 10 {
41+
overten = true
42+
tmp -= 10
43+
} else {
44+
overten = false
45+
}
46+
47+
node.Val = tmp
48+
49+
if node1 != nil || node2 != nil {
50+
node.Next = &ListNode{}
51+
node = node.Next
52+
}
53+
}
54+
55+
if overten {
56+
node.Next = &ListNode{}
57+
node = node.Next
58+
node.Val = 1
59+
}
60+
61+
return results
62+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package leetcode
2+
3+
// Given a string, find the length of the longest substring without repeating characters.
4+
//
5+
// Examples:
6+
// Given "abcabcbb", the answer is "abc", which the length is 3.
7+
// Given "bbbbb", the answer is "b", with the length of 1.
8+
// Given "pwwkew", the answer is "wke", with the length of 3.
9+
// Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
10+
//
11+
func lengthOfLongestSubstring(s string) int {
12+
hashmap := map[byte]int{}
13+
max := 0
14+
for i := range s {
15+
_, ok := hashmap[s[i]]
16+
if !ok {
17+
hashmap[s[i]] = i
18+
if len(hashmap) > max {
19+
max = len(hashmap)
20+
}
21+
} else {
22+
// remove repeated
23+
oldI := hashmap[s[i]]
24+
hashmap[s[i]] = i
25+
26+
for key, value := range hashmap {
27+
if value < oldI {
28+
delete(hashmap, key)
29+
}
30+
}
31+
}
32+
}
33+
return max
34+
}

Golang/two-sum.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
// Given an array of integers, return indices of the two numbers
6+
// such that they add up to a specific target.
7+
// You may assume that each input would have exactly one solution.
8+
//
9+
// Example:
10+
// Given nums = [2, 7, 11, 15], target = 9,
11+
// Because nums[0] + nums[1] = 2 + 7 = 9,
12+
// return [0, 1].
13+
func TwoSum(nums []int, target int) []int {
14+
15+
indexs := make([]int, 2)
16+
hash := map[int]int{}
17+
18+
for i := range nums {
19+
hash[target-nums[i]] = i
20+
}
21+
22+
for i := range nums {
23+
index, ok := hash[nums[i]]
24+
if ok {
25+
if i == index {
26+
continue
27+
}
28+
indexs[0] = index
29+
indexs[1] = i
30+
sort.Ints(indexs)
31+
break
32+
}
33+
continue
34+
}
35+
36+
return indexs
37+
}

Golang/two-sum_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode
2+
3+
import "testing"
4+
5+
func Test_two_sum(t *testing.T) {
6+
7+
if result := TwoSum([]int{1, 3, 5}, 4); result[0] != 0 && result[1] != 1 {
8+
t.Errorf("Error")
9+
}
10+
if result := TwoSum([]int{3, 1, 5}, 6); result[0] != 1 && result[1] != 2 {
11+
t.Errorf("Error")
12+
}
13+
}

0 commit comments

Comments
 (0)