description
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
solution
public class Solution {
public int lengthOfLongestSubstring(String s) {
int lenS = s.length();
int lenRet = lenS > 0 ? 1 : 0;
int i = 0;
int j = 1;
while (j < lenS) {
int sameIndex = s.substring(i, j).indexOf(s.charAt(j));
if (sameIndex >= 0) {// found repetition
i += sameIndex + 1;
j++;
} else {// no repetition
int lenThis = j - i + 1;
if (lenThis > lenRet) {
lenRet = lenThis;
}
j++;
}
}
return lenRet;
}
}
心得
这道题做的时候,充分体现了我的性格--眼高手低!看完题干我觉得我是会的,大致理了下思路,开始写代码实现。完成后,提交上去,超时了...为啥呢,我忘了,反正是很小的一个点。然后改完再提交,有的case错了,这样重复了两次,才ac。脑子一点都不清楚,做的时候觉得大致的思路有,但其实具体实现的时候,是有些迷糊的。就不能一步一步的思路清晰的做下来,总得出点差错,一次一次的改!
我希望你可以集中注意力,摸清自己的规律,困了就扇自己两巴掌!
浙公网安备 33010602011771号