//2017.5.2
1. Unique Charcters
description: Implement an algorithm to determine if a string has all unique characters.
public class Solution {
/**
* @param str: a string
* @return: a boolean
*/
public boolean isUnique(String str) {
// write your code here
if (str == null || str.length() == 0) {
return false;
}
if (str.length() > 256) {
return false;
}
boolean[] arr = new boolean[256];
for (int i = 0; i < str.length(); i++) {
if (arr[str.charAt(i)]) {
return false;
}
arr[str.charAt(i)] = true;
}
return true;
}
}
解析:假设使用的是ASCII编码方式,直接使用256个数组就可以非常迅速的处理。
1. Unique Charcters
description: Implement an algorithm to determine if a string has all unique characters.
public class Solution {
/**
* @param str: a string
* @return: a boolean
*/
public boolean isUnique(String str) {
// write your code here
if (str == null || str.length() == 0) {
return false;
}
if (str.length() > 256) {
return false;
}
boolean[] arr = new boolean[256];
for (int i = 0; i < str.length(); i++) {
if (arr[str.charAt(i)]) {
return false;
}
arr[str.charAt(i)] = true;
}
return true;
}
}
解析:假设使用的是ASCII编码方式,直接使用256个数组就可以非常迅速的处理。