Skip to content

Instantly share code, notes, and snippets.

@LinyinWu
Last active August 29, 2015 14:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save LinyinWu/3a9634d00595de30aca5 to your computer and use it in GitHub Desktop.
ctci
/*
【解题思路】
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