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

add binary #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 4 commits into master from add-binary
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ continually updating😃.

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

## String
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go) *sliding window;* *hash table*
* [17. Letter Combinations of a Phone Number](./src/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go) *tree*
* [67. Add Binary](./src/0067_add_binary/add_binary.go)
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go) *sliding window*

## Linked List
* [2. Add Two Numbers](./src/0002_add_two_numbers/add_two_numbers.go)

## Dynamic Programming
* [62. Unique Paths](./src/0062_unique_paths/unique_paths.go)
* [63. Unique Paths 2](./src/0063_unique_paths_2/unique_paths2.go)
Expand Down
26 changes: 0 additions & 26 deletions src/0067_add_binary/README.md
View file Open in desktop

This file was deleted.

59 changes: 59 additions & 0 deletions src/0067_add_binary/add_binary.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
67. Add Binary
https://leetcode.com/problems/add-binary/

Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.

Example:
Input: a = "11", b = "1"
Output: "100"
*/
// time: 2018年12月19日

package addbinary

import (
"strconv"
)

// Time complexity: O( max( len(a), len(b) ) )
// Space complexity: O(1)
func addBinary(a string, b string) string {
var (
lenA = len(a)
lenB = len(b)
carry int
res = ""
)
for lenA > 0 && lenB > 0 {
tmp := int(a[lenA-1]-'0') + int(b[lenB-1]-'0') + carry
res = strconv.Itoa(tmp%2) + res
carry = tmp / 2
lenA--
lenB--
}

if lenA == 0 {
for lenB > 0 {
tmp := int(b[lenB-1]-'0') + carry
res = strconv.Itoa(tmp%2) + res
carry = tmp / 2
lenB--
}
}

if lenB == 0 {
for lenA > 0 {
tmp := int(a[lenA-1]-'0') + carry
res = strconv.Itoa(tmp%2) + res
carry = tmp / 2
lenA--
}
}

if carry == 1 {
res = strconv.Itoa(carry) + res
}
return res
}
33 changes: 33 additions & 0 deletions src/0067_add_binary/add_binary_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package addbinary

import (
"testing"
)

func TestAddBinary(t *testing.T) {
type arg struct {
a string
b string
}

testCases := []arg{
arg{
a: "11",
b: "1",
},
arg{
b: "11",
a: "1",
},
}

expected := []string{
"100", "100",
}

for index, data := range testCases {
if res := addBinary(data.a, data.b); res != expected[index] {
t.Errorf("expected %s, got %s", expected[index], res)
}
}
}
4 changes: 2 additions & 2 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
|ID|Title && solution|Coefficient of difficulty|remarks|
|:---:|:---:|:---:|:---:|
|0001|[Two Sum](./0001_two_sum/twosum.go)|Easy|*`array;`* *`lookup table`*|
|0002|[Add Two Numbers](0002_add_two_numbers/add_two_numbers.go)|Medium||
|0002|[Add Two Numbers](0002_add_two_numbers/add_two_numbers.go)|Medium|*`linked list`*|
|0003|[Longest Substring Without Repeating Characters](0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)|Medium|*`sliding window`*|
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy||
Expand All @@ -13,7 +13,7 @@
|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0064|[Minimum Path Sum](./0064_minimum_path_sum/minimum_path_sum.go)|Medium|*`dynamic programming;`* *` dfs`*|
|0067|[add Binary](./0067_add_binary/README.md)|Easy||
|0067|[add Binary](./0067_add_binary/add_binary.go)|Easy||
|0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*|
|0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*|
|0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*|
Expand Down

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