1. 程式人生 > >1、位元組跳動-挑戰字串

1、位元組跳動-挑戰字串

1、無重複字元的最長子串

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int []hash = new int [500];
        int max = 0;
        int i = 0, j = 0;

        while (i < s.length() && j <s.length() ) {
            if(hash[s.charAt(j)] == 0) {
                hash[s.charAt(j)] = 1;
                j++;
                max = (j - i) > max ? (j - i) : max;
            } else {
                hash[s.charAt(i)] = 0;
                i++;
            }  
        }
        return max; 
    }
}

參考:https://blog.csdn.net/liuguangqiang/article/details/79931473

2、最長公共字首

 

public static String longestCommonPrefix(String[] strs) {
        int count = strs.length;
        String prefix = "";
        if(count != 0){
            prefix = strs[0];
        }
        for(int i=0; i<count; i++){
            //關鍵程式碼,不斷的從後往前擷取字串,然後與之相比,直到startsWith()返回true
            while(!strs[i].startsWith(prefix)){
                prefix = prefix.substring(0, prefix.length()-1);
            }
        }
        return prefix;
    }

參考:https://blog.csdn.net/fengpojian/article/details/81326781

 

3、字串的排列

 

class Solution {
    public boolean checkInclusion(String s1, String s2) {
        int l1 = s1.length();
        int l2 = s2.length();
        int [] count = new int [128];
        if(l1 > l2)
            return false;
        for(int i = 0; i<l1; i++){
            count[s1.charAt(i) - 'a']++;
            count[s2.charAt(i) - 'a']--;
        }
        if(allZero(count))
            return true;
        for(int i = l1; i<l2; i++){
            count[s2.charAt(i) - 'a']--;
            count[s2.charAt(i-l1) - 'a']++;
            if(allZero(count))
                return true;
        }
        return false;
    }
    public boolean allZero(int [] count){
        int l = count.length;
        for(int i = 0; i < l; i++){
            if(count[i] != 0)
                return false;
        }
        return true;
    }
}

參考連線:https://www.colabug.com/4388406.html

 

4、字串相乘

public class Solution {
    public String multiply(String num1, String num2) {
        num1 = new StringBuilder(num1).reverse().toString();
        num2 = new StringBuilder(num2).reverse().toString();
        int[] d = new int[num1.length()+num2.length()];
        for(int i=0;i<num1.length();i++){
            int a = num1.charAt(i)-'0';
            for(int j=0;j<num2.length();j++){
                int b = num2.charAt(j)-'0';
                d[i+j] +=a*b;
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<d.length;i++){
            int digit = d[i]%10;
            int carry = d[i]/10;
            sb.insert(0,digit);
            if(i<d.length-1){
                d[i+1] +=carry;
            }
        }
        while(sb.length()>0 && sb.charAt(0)=='0'){
            sb.deleteCharAt(0);
        }
        return sb.length()==0? "0" :sb.toString();
    }
}
原文:https://blog.csdn.net/Lynn_Baby/article/details/80757305 

5、翻轉字串裡的單詞

答案:https://blog.csdn.net/Regemc/article/details/79781586

6、簡化路徑

答案:https://www.cnblogs.com/grandyang/p/4347125.html

7、復原IP地址

答案:https://www.cnblogs.com/grandyang/p/4305572.html