ctci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
【解题思路】 | |
1.考虑只有ASICC码,建立一个长度256的boolean数组. | |
2.扫描字符串,得到字符的ASIC码的值,此值为index,查看boolean数组,如果true返回false,不是true就设为true. | |
【时间复杂度】 | |
O(n) | |
【空间复杂度】 | |
O(1) | |
【gist link】 Java | |
https://gist.github.com/LinyinWu/3a9634d00595de30aca5 | |
【test case】 | |
"" true | |
" " true | |
" " false | |
"abc" true | |
"aba" false | |
*/ | |
public class Solution { | |
public static boolean isUniqueChar(String str) { | |
if(str.length() > 256) | |
return false; | |
boolean[] bArray = new boolean[256]; | |
for(int i=0;i<str.length();i++) { | |
int val = str.charAt(i); | |
if(bArray[val]) | |
return false; | |
bArray[val] = true; | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
String s = " "; | |
boolean result = isUniqueChar(s); | |
System.out.println(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment