Created
June 15, 2014 16:12
-
-
Save anonymous/071cbca01a61d0fb3e9a to your computer and use it in GitHub Desktop.
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
class Solution{ | |
/* | |
* Time complexity O(n); Space complexity O(1). | |
*/ | |
public boolean isAllUniqChar(String str){ | |
if(str == null) return false; // Make sure that the string is valid. | |
boolean[] temp = new boolean[128]; // Temp stores all 128 ASCII characters' presences in the string. | |
char[] chars = str.toCharArray(); // Convert the string into Char array so that we can iterate each character. | |
for(boolean i in:temp) i = false; // Initialize the temp array. | |
for(char c in:chars){ | |
int index = c; // Get the ASCII value of a char. | |
if(!temp[index]) temp[index] = true; // Check if this char has showed up before. If not, mark its presence then proceed. | |
else return false; // If this char has showed up once, then this string has duplicate chars. | |
} | |
return true; // All chars are unique. | |
} | |
} |
额。。
cool
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I comment this?