每日一道算法题——1

每日一道算法题——1

从昨天开始,我想每天写一道算法题,虽然比较简单,但是相信积累起来还是有借鉴意义的。所以想用博客的方式记录下来。

  1. 题目

    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.

  2. 分析
    给定一个字符串,求出该字符串的最长字串的长度。

    • 要求:
      字串不能包含重复字母
      是字串,而不是子序列
  3. 测试数据
    输入:abcabcbb 输出:3
    输入:aab 输出:2

  4. 参考答案
    这个题的解法有很多,这里只给出我自己的代码和我认为比较好的代码。

    • 自己的答案:
      算法思想:
      从左到右遍历给定的字符串,用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)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值