public class Solution {// Note: this solution is not accepted, I will update it later.
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) return 0;
int maxLen = 0, i = 0;
// for(int i = 0; i < s.length(); i ++){
StringBuffer uniqSubSB = new StringBuffer();
while(i < s.length()){
// if(uniqSubSB.length() != 0) uniqSubSB.delete(0, uniqSubSB.length());
if(checkRepeat(uniqSubSB,s.charAt(i)) < 0){
uniqSubSB.append(s.charAt(i));
i ++;
if(i == s.length()){// In the case where this index has meet the end of string
return Math.max(maxLen, uniqSubSB.length());
}
}
else{
maxLen = Math.max(maxLen, uniqSubSB.length());
uniqSubSB.delete(0, uniqSubSB.length());
i = s.lastIndexOf(String.valueOf(s.charAt(i)),i - 1) + 1;// start next search from the last "same" // char before (i -1)
}
}
return maxLen;
}
public int checkRepeat(StringBuffer uniqSubSB, char c){// The O(n^2) time might be due to this method,
if(uniqSubSB.length() == 0) return -1;
//String current = String.valueOf(s.charAt(i));
int idx = uniqSubSB.toString().indexOf(String.valueOf(c));
return idx;
}
}
UPDATE:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) return 0;
int maxLen = 0, i = 0;
// for(int i = 0; i < s.length(); i ++){
StringBuffer uniqSubSB = new StringBuffer();
boolean[] checkAppear = new boolean[256];// Only 256 chars, so this array can store a flag indicating //whether the char has existed. Accelerate check speed.
Arrays.fill(checkAppear,false);
while(i < s.length()){
if(!checkAppear[s.charAt(i)]){// check
checkAppear[s.charAt(i)] = true;
uniqSubSB.append(s.charAt(i));
i ++;
if(i == s.length()){
return Math.max(maxLen, uniqSubSB.length());
}
}
else{
Arrays.fill(checkAppear,false);
maxLen = Math.max(maxLen, uniqSubSB.length());
uniqSubSB = new StringBuffer();
i = s.lastIndexOf(String.valueOf(s.charAt(i)),i - 1) + 1;
}
}
return maxLen;
}
}
Hello, welcome to Yizhe's Blog. I am Yizhe Liu, graduated from University of Arizona, with Master degree, major in Electrical and Computer Engineering. Actually, I am software guy. In my blog, many posts are about the Leetcode questions. I will post my ideas and implementations, which are accepted by Leetcode. If you have different opinions, please leave comment. I am happy to discuss with you. So lets start rocking them!!!
Tuesday, April 22, 2014
Monday, April 21, 2014
N Queen Puzzle 1
public class Solution {
public ArrayList<String[]> solveNQueens(int n) {
if(n <= 0) return null;
ArrayList<String[]> result = new ArrayList<String[]>();
//config is used to store reasonable configurations. The i th element in config stores the position of one //queen at i th row, i.e. if config.get(1) is 2, then it means there is one queen at row 1, column 2
ArrayList<Integer> config = new ArrayList<Integer>();
for(int i = 0; i< n; i++){
config.clear();
config.add(i);
findConfig(config,n,result);
}
return result;
}
public void findConfig(ArrayList<Integer> config, int n, ArrayList<String[]> result){
if(config.size() == n) // if the config has n elements, it means this config is reasonable
result.add(convert2StrArr(config));
config.remove(n - 1);
return;
}
for(int i = 0; i < n; i ++){
if(canBeInBoard(config,i)){
config.add(i);
findConfig(config,n,result);
}
}
config.remove(config.size() - 1);
}
public boolean canBeInBoard(ArrayList<Integer> config,int idx){// find reasonable position for next row
int len = config.size();
for(int i = 0; i < len; i++){
if(config.get(i) == idx) return false;// can not be at the same column
if(Math.abs(i - len) == Math.abs(config.get(i) - idx))
return false;// can not be at the same diagonal.
}
return true;
}
public String[] convert2StrArr(ArrayList<Integer> config){
String[] strArr = new String[config.size()];
for(int i = 0; i < config.size(); i++){
StringBuffer sb = new StringBuffer();
for(int j = 0; j < config.size(); j++){
if(j == config.get(i)) sb.append("Q");
else sb.append(".");
}
strArr[i] = sb.toString();
}
return strArr;
}
}
public ArrayList<String[]> solveNQueens(int n) {
if(n <= 0) return null;
ArrayList<String[]> result = new ArrayList<String[]>();
//config is used to store reasonable configurations. The i th element in config stores the position of one //queen at i th row, i.e. if config.get(1) is 2, then it means there is one queen at row 1, column 2
ArrayList<Integer> config = new ArrayList<Integer>();
for(int i = 0; i< n; i++){
config.clear();
config.add(i);
findConfig(config,n,result);
}
return result;
}
public void findConfig(ArrayList<Integer> config, int n, ArrayList<String[]> result){
if(config.size() == n) // if the config has n elements, it means this config is reasonable
result.add(convert2StrArr(config));
config.remove(n - 1);
return;
}
for(int i = 0; i < n; i ++){
if(canBeInBoard(config,i)){
config.add(i);
findConfig(config,n,result);
}
}
config.remove(config.size() - 1);
}
public boolean canBeInBoard(ArrayList<Integer> config,int idx){// find reasonable position for next row
int len = config.size();
for(int i = 0; i < len; i++){
if(config.get(i) == idx) return false;// can not be at the same column
if(Math.abs(i - len) == Math.abs(config.get(i) - idx))
return false;// can not be at the same diagonal.
}
return true;
}
public String[] convert2StrArr(ArrayList<Integer> config){
String[] strArr = new String[config.size()];
for(int i = 0; i < config.size(); i++){
StringBuffer sb = new StringBuffer();
for(int j = 0; j < config.size(); j++){
if(j == config.get(i)) sb.append("Q");
else sb.append(".");
}
strArr[i] = sb.toString();
}
return strArr;
}
}
Subscribe to:
Posts (Atom)