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

717. 1-bit and 2-bit Characters #23

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 1 commit into master from 1-bit-and-2-bit-characters
Dec 29, 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
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ continually updating 😃.
* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go)   *`hash table`*
* [454. 4Sum II](./src/0454_4sum2/4sum2.go)   *`hash table`*
* [713. Subarray Product Less Than K](src/0713_subarray_product_less_than_k/spltk.go)   *`sliding window`*  *`array`*
* [717. 1-bit and 2-bit Characters](src/0717_1_bit_and_2_bit_characters/1bitand2bitc.go)
* [747. Largest Number At Least Twice of Others](./src/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)

### String
Expand Down
43 changes: 43 additions & 0 deletions src/0717_1_bit_and_2_bit_characters/1bitand2bitc.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
717. 1-bit and 2-bit Characters
https://leetcode.com/problems/1-bit-and-2-bit-characters/

We have two special characters.
The first character can be represented by one bit 0.
The second character can be represented by two bits (10 or 11).

Now given a string represented by several bits.
Return whether the last character must be a one-bit character or not.
The given string will always end with a zero.

Note:
1 <= len(bits) <= 1000.
bits[i] is always 0 or 1.
*/
// time: 2018年12月28日

package onebitandtwobitcharacters

// time complexity: O(n)
// space complexity: O(1)
func isOneBitCharacter(bits []int) bool {
n := len(bits)
if 1 == n {
return true
}

cur := 0
flag := false

for cur < n {
if 0 == bits[cur] {
cur++
} else {
cur += 2
}
if cur == n-1 {
flag = true
}
}
return flag
}
18 changes: 18 additions & 0 deletions src/0717_1_bit_and_2_bit_characters/1bitand2bitc_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package onebitandtwobitcharacters

import "testing"

func TestIsOneBitCharacter(t *testing.T) {
testCases := [][]int{
{1, 0, 0},
{1, 1, 1, 0},
{0},
}
expected := []bool{true, false, true}

for index, bits := range testCases {
if res := isOneBitCharacter(bits); res != expected[index] {
t.Errorf("expected %t, got %t", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@
|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|
|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||

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