這題寫凌晨2、3點,本來要用c的,最後用JAVA,然後睡覺前送出去Time Limit Exceeded,心都要碎了,好家在把print拿掉後就過了,可喜可賀?
題號3 標題:Longest Substring Without Repeating Characters 難度:Medium
Given a string s, find the length of the longest substring without repeating characters.
我的程式碼
class Solution {
public int lengthOfLongestSubstring(String s) {
ArrayList<String> temp = new ArrayList<String>();
int i=0,j=0,count=1,result=1;
if(s.isEmpty()){
return 0;
}
temp.add(Character.toString(s.charAt(0)));
for(i=1;i<s.length();i++){
String dtemps = Character.toString(s.charAt(i));
if(temp.contains(dtemps)){
//System.out.println(temp.indexOf(Character.toString(s.charAt(i))));
int dtemp = temp.indexOf(dtemps);
if(result<count){
result=count;
}
for(j=0;j<=dtemp;j++){
temp.remove(0);
}
if(temp.size()!=0){
count=temp.size();
}else{
count = 0;
}
}
temp.add(dtemps);
count++;
// for(j=0;j<temp.size();j++){
// System.out.print(temp.get(j));
// }
// System.out.print(" count:"+count);
System.out.println();
}
if(count>result){
result = count;
}
return result;
}
}
DAY20心得
終於走完1/3了,加油加油,來去倒垃圾