力扣---2020.5.2

3. 無重複字符的最長子串

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length()==0) return 0;
        HashMap<Character,Integer> map = new HashMap<>();
        int left = 0;
        int max = 0;//記錄最長子串長度
        for(int i = 0;i<s.length();i++){
            if(map.containsKey(s.charAt(i))){
                left = Math.max(left,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;
    }
}

面試題14- II. 剪繩子 II

class Solution {
    public int cuttingRope(int n) {
        if(n==2){
            return 1;
        }
        if(n==3){
            return 2;
        }
        long res = 1;
        while(n>4){
            res *= 3;
            res = res % 1000000007;
            n = n - 3;
        }
        return (int)(res*n % 1000000007);
    }
}

你知道的越多,你不知道的越多。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章