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 8385819

Browse files
authored
717 solved. (#23)
1 parent a231349 commit 8385819

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ continually updating 😃.
2929
* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go)   *`hash table`*
3030
* [454. 4Sum II](./src/0454_4sum2/4sum2.go)   *`hash table`*
3131
* [713. Subarray Product Less Than K](src/0713_subarray_product_less_than_k/spltk.go)   *`sliding window`*  *`array`*
32+
* [717. 1-bit and 2-bit Characters](src/0717_1_bit_and_2_bit_characters/1bitand2bitc.go)
3233
* [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)
3334

3435
### String
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
717. 1-bit and 2-bit Characters
3+
https://leetcode.com/problems/1-bit-and-2-bit-characters/
4+
5+
We have two special characters.
6+
The first character can be represented by one bit 0.
7+
The second character can be represented by two bits (10 or 11).
8+
9+
Now given a string represented by several bits.
10+
Return whether the last character must be a one-bit character or not.
11+
The given string will always end with a zero.
12+
13+
Note:
14+
1 <= len(bits) <= 1000.
15+
bits[i] is always 0 or 1.
16+
*/
17+
// time: 2018年12月28日
18+
19+
package onebitandtwobitcharacters
20+
21+
// time complexity: O(n)
22+
// space complexity: O(1)
23+
func isOneBitCharacter(bits []int) bool {
24+
n := len(bits)
25+
if 1 == n {
26+
return true
27+
}
28+
29+
cur := 0
30+
flag := false
31+
32+
for cur < n {
33+
if 0 == bits[cur] {
34+
cur++
35+
} else {
36+
cur += 2
37+
}
38+
if cur == n-1 {
39+
flag = true
40+
}
41+
}
42+
return flag
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package onebitandtwobitcharacters
2+
3+
import "testing"
4+
5+
func TestIsOneBitCharacter(t *testing.T) {
6+
testCases := [][]int{
7+
{1, 0, 0},
8+
{1, 1, 1, 0},
9+
{0},
10+
}
11+
expected := []bool{true, false, true}
12+
13+
for index, bits := range testCases {
14+
if res := isOneBitCharacter(bits); res != expected[index] {
15+
t.Errorf("expected %t, got %t", expected[index], res)
16+
}
17+
}
18+
}

‎src/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@
5757
|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|
5858
|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|
5959
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
60+
|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||
6061
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
6162
|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||

0 commit comments

Comments
(0)

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