Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac2fbd7

Browse files
authored
add binary (#4)
* add binary * add more ut case * update readme * update readme
1 parent d64b75e commit ac2fbd7

File tree

5 files changed

+103
-29
lines changed

5 files changed

+103
-29
lines changed

‎README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ continually updating😃.
1010

1111
## Array
1212
* [1. Two Sum](./src/0001_two_sum/twosum.go) *lookup table*
13-
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go) *sliding window*
1413
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go) *double index*
1514
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go) *sliding window*
1615
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)
1716
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)
1817

18+
## String
19+
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go) *sliding window;* *hash table*
20+
* [17. Letter Combinations of a Phone Number](./src/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go) *tree*
21+
* [67. Add Binary](./src/0067_add_binary/add_binary.go)
22+
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go) *sliding window*
23+
24+
## Linked List
25+
* [2. Add Two Numbers](./src/0002_add_two_numbers/add_two_numbers.go)
26+
1927
## Dynamic Programming
2028
* [62. Unique Paths](./src/0062_unique_paths/unique_paths.go)
2129
* [63. Unique Paths 2](./src/0063_unique_paths_2/unique_paths2.go)

‎src/0067_add_binary/README.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎src/0067_add_binary/add_binary.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
67. Add Binary
3+
https://leetcode.com/problems/add-binary/
4+
5+
Given two binary strings, return their sum (also a binary string).
6+
The input strings are both non-empty and contains only characters 1 or 0.
7+
8+
Example:
9+
Input: a = "11", b = "1"
10+
Output: "100"
11+
*/
12+
// time: 2018年12月19日
13+
14+
package addbinary
15+
16+
import (
17+
"strconv"
18+
)
19+
20+
// Time complexity: O( max( len(a), len(b) ) )
21+
// Space complexity: O(1)
22+
func addBinary(a string, b string) string {
23+
var (
24+
lenA = len(a)
25+
lenB = len(b)
26+
carry int
27+
res = ""
28+
)
29+
for lenA > 0 && lenB > 0 {
30+
tmp := int(a[lenA-1]-'0') + int(b[lenB-1]-'0') + carry
31+
res = strconv.Itoa(tmp%2) + res
32+
carry = tmp / 2
33+
lenA--
34+
lenB--
35+
}
36+
37+
if lenA == 0 {
38+
for lenB > 0 {
39+
tmp := int(b[lenB-1]-'0') + carry
40+
res = strconv.Itoa(tmp%2) + res
41+
carry = tmp / 2
42+
lenB--
43+
}
44+
}
45+
46+
if lenB == 0 {
47+
for lenA > 0 {
48+
tmp := int(a[lenA-1]-'0') + carry
49+
res = strconv.Itoa(tmp%2) + res
50+
carry = tmp / 2
51+
lenA--
52+
}
53+
}
54+
55+
if carry == 1 {
56+
res = strconv.Itoa(carry) + res
57+
}
58+
return res
59+
}

‎src/0067_add_binary/add_binary_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package addbinary
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAddBinary(t *testing.T) {
8+
type arg struct {
9+
a string
10+
b string
11+
}
12+
13+
testCases := []arg{
14+
arg{
15+
a: "11",
16+
b: "1",
17+
},
18+
arg{
19+
b: "11",
20+
a: "1",
21+
},
22+
}
23+
24+
expected := []string{
25+
"100", "100",
26+
}
27+
28+
for index, data := range testCases {
29+
if res := addBinary(data.a, data.b); res != expected[index] {
30+
t.Errorf("expected %s, got %s", expected[index], res)
31+
}
32+
}
33+
}

‎src/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
|ID|Title && solution|Coefficient of difficulty|remarks|
44
|:---:|:---:|:---:|:---:|
55
|0001|[Two Sum](./0001_two_sum/twosum.go)|Easy|*`array;`* *`lookup table`*|
6-
|0002|[Add Two Numbers](0002_add_two_numbers/add_two_numbers.go)|Medium||
6+
|0002|[Add Two Numbers](0002_add_two_numbers/add_two_numbers.go)|Medium|*`linked list`*|
77
|0003|[Longest Substring Without Repeating Characters](0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)|Medium|*`sliding window`*|
88
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
99
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy||
@@ -13,7 +13,7 @@
1313
|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
1414
|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
1515
|0064|[Minimum Path Sum](./0064_minimum_path_sum/minimum_path_sum.go)|Medium|*`dynamic programming;`* *` dfs`*|
16-
|0067|[add Binary](./0067_add_binary/README.md)|Easy||
16+
|0067|[add Binary](./0067_add_binary/add_binary.go)|Easy||
1717
|0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*|
1818
|0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*|
1919
|0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /