从昨天开始,我想每天写一道算法题,虽然比较简单,但是相信积累起来还是有借鉴意义的。所以想用博客的方式记录下来。
题目
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.
分析 给定一个字符串,求出该字符串的最长字串的长度。
要求: 字串不能包含重复字母 是字串,而不是子序列测试数据 输入:abcabcbb 输出:3 输入:aab 输出:2
参考答案 这个题的解法有很多,这里只给出我自己的代码和我认为比较好的代码。
自己的答案: 算法思想: 从左到右遍历给定的字符串,用subString临时保存字串。 每加入一个新的字母,检查subString是否包含该字母, 如果有,那么从subString中那个字母的下标+1开始,构造新的字串。 如果没有包含,那么subString末尾加上新字母,并且记录下最大值。public int lengthOfLongestSubstring(String s) { String subString = ""; String temp;//用与保存字母 int max = 0;//最大长度 for(int i =0;i<s.length();i++){ temp = s.charAt(i)+""; if(subString.contains(temp)){ //包含temp字母 如:abc temp=b //那么index = 1 且subString = cb int index = subString.indexOf(temp); subString = subString.substring(index+1) + temp; }else{ //没有包含 构造新的字符串 subString += temp; max=Math.max(subString.length(),max); } } return max; }更好的答案 采用哈希表来存储字串的字母,查看新字母是否有重复更快捷。public class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(), ans = 0; Map<Character, Integer> map = new HashMap<>(); // current index of character // try to extend the range [i, j] for (int j = 0, i = 0; j < n; j++) { if (map.containsKey(s.charAt(j))) { i = Math.max(map.get(s.charAt(j)), i); } ans = Math.max(ans, j - i + 1); map.put(s.charAt(j), j + 1); } return ans; }}时间复杂度:O(n) 空间复杂度:O(min(m,n))
巧妙的方法 利用ASCII码值public class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(), ans = 0; int[] index = new int[128]; // current index of character // try to extend the range [i, j] for (int j = 0, i = 0; j < n; j++) { i = Math.max(index[s.charAt(j)], i); ans = Math.max(ans, j - i + 1); index[s.charAt(j)] = j + 1; } return ans; }}时间复杂度:O(n) 空间复杂度:O(m)
新闻热点
疑难解答