Skip to content

Commit 27dccf4

Browse files
add 393
1 parent d625992 commit 27dccf4

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Your ideas/fixes/algorithms are more than welcome!
167167
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/RandomPickIndex.java) | | | Medium| Reservoir Sampling
168168
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/IntegerReplacement.java)| ? | ? | Easy| BFS
169169
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../master/src/main/java/com/fishercoder/solutions/RotateFunction.java)| O(n^2) could be optimized to O(n) | O(1) | Easy|
170+
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_393.java)| O(?)|O(?) | Medium| Bit Manipulation
170171
|392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_392.java)| O(m*n)|O(1) | Medium| Array, String
171172
|390|[Elimination Game](https://leetcode.com/problems/elimination-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/EliminationGame.java)| O(logn)|O(1) | Medium|
172173
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FindTheDifference.java)| O(n)|O(1) | Easy|

src/main/java/com/fishercoder/solutions/_393.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,21 @@
3838
*/
3939
public class _393 {
4040

41+
/**credit: https://discuss.leetcode.com/topic/58338/bit-manipulation-java-6ms/4*/
4142
public boolean validUtf8(int[] data) {
42-
//TODO: not finished yet
43-
if (data == null || data.length == 0 || data.length > 4) return false;
44-
int len = data.length;
45-
String[] last8Bits = new String[len];
46-
for (int i = 0; i < len; i++) {
47-
String bin = Integer.toBinaryString(data[i]);
48-
last8Bits[i] = bin.length() >= 8 ? bin.substring(0, 8) : String.format("%08d", Integer.parseInt(bin));//pad left with zeroes to make sure each number is 8 bits long to make coding easier in the later part
43+
int count = 0;
44+
for(int d:data){
45+
if(count == 0){
46+
if((d >> 5) == 0b110) count = 1;
47+
else if((d >> 4) == 0b1110) count = 2;
48+
else if((d >> 3) == 0b11110) count = 3;
49+
else if((d >> 7) == 1) return false;
50+
} else {
51+
if((d>>6) != 0b10) return false;
52+
else count--;
53+
}
4954
}
50-
if (len == 1) {
51-
if (Integer.valueOf(last8Bits[0].substring(0,1)) != 0) return false;
52-
return true;
53-
} else {
54-
55-
}
56-
return false;
55+
return count == 0;
5756
}
5857

5958
}

0 commit comments

Comments
 (0)