leetcode鍒烽璁板綍鈥斺€斿瓧绗︿覆

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode鍒烽璁板綍鈥斺€斿瓧绗︿覆相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/toc' title='toc'>toc   lin   phi   longest   瀵规瘮   return   indexof   

242.鏈夋晥鍦板瓧姣嶅紓浣嶈瘝

鐢变簬鏈鐨勫瓧绗︿覆鍙寘鍚?26 涓皬鍐欏瓧绗︼紝鍥犳鍙互浣跨敤闀垮害涓?26 鐨勬暣鍨嬫暟缁勫瀛楃涓插嚭鐜扮殑瀛楃杩涜缁熻锛屽苟瀵规瘮瀛楁瘝鍑虹幇鐨勬鏁版槸鍚︿竴鑷淬€備笉鍐嶄娇鐢?HashMap銆?/p>

toCharArray锛堬級鐨勭敤娉?/a>

java涓殑foreach鐨勭敤娉?/a>

class Solution {
    public boolean isAnagram(String s, String t) {
    int[] cnts = new int[26];
    for (char c : s.toCharArray()) {
        cnts[c - 鈥榓鈥榏++;
    }
    for (char c : t.toCharArray()) {
        cnts[c - 鈥榓鈥榏--;
    }
    for (int cnt : cnts) {
        if (cnt != 0) {
            return false;
        }
    }
    return true;
    }
}

杩涢樁锛岀敱浜庢暣涓猆niocde鐨勫瓧绗﹁寖鍥磋緝澶э紝鍙互浣跨敤鍝堝笇琛ㄦ槧灏勮€屼笉鏄浐瀹氬ぇ灏忕殑璁℃暟鍣ㄣ€?/p>

409.鏈€闀垮洖鏂囦覆

浣跨敤闀垮害涓?256 鐨勬暣鍨嬫暟缁勬潵缁熻姣忎釜瀛楃鍑虹幇鐨勪釜鏁帮紝姣忎釜瀛楃鏈夊伓鏁颁釜鍙互鐢ㄦ潵鏋勬垚鍥炴枃瀛楃涓层€?/p>

鍥犱负鍥炴枃瀛楃涓叉渶涓棿鐨勯偅涓瓧绗﹀彲浠ュ崟鐙嚭鐜帮紝鎵€浠ュ鏋滄湁鍗曠嫭鐨勫瓧绗﹀氨鎶婂畠鏀惧埌鏈€涓棿銆?/p>

public int longestPalindrome(String s) {
//浣跨敤ASCII琛ㄧず澶у皬鍐欒嫳鏂囧瓧姣嶅彧瑕佸埌128灏卞鐢ㄤ簡銆?
int[] cnts = new int[128]; for (char c : s.toCharArray()) { cnts[c]++; } int palindrome = 0; for (int cnt : cnts) { palindrome += (cnt / 2) * 2; } if (palindrome < s.length()) { palindrome++; // 杩欎釜鏉′欢涓?s 涓竴瀹氭湁鍗曚釜鏈娇鐢ㄧ殑瀛楃瀛樺湪锛屽彲浠ユ妸杩欎釜瀛楃鏀惧埌鍥炴枃鐨勬渶涓棿 } return palindrome; }

鏇存柊锛氬湪涓€涓洖鏂囧瓧绗︿覆涓紝鏈€澶氬彧鑳芥湁涓€涓嚭鐜颁竴娆$殑瀛楁瘝锛坅aa鍙互鐞嗚В涓烘湁涓€涓嚭鐜颁簡涓€娆$殑a锛夛紝鍙渶瑕佺粺璁℃湁澶氬皯涓瓧姣嶅彧鍑虹幇浜嗕竴娆★紝鐢ㄥ瓧绗︿覆鐨勯暱搴﹀噺鍘诲嚭鐜颁竴娆$殑瀛楁瘝鏁板啀鍔犱竴锛屽氨鏄渶闀垮瓧绗︿覆鐨勯暱搴︺€備袱绉嶆柟娉曟湰璐ㄤ笂閮芥槸灏嗗彧鍑虹幇涓€娆$殑瀛楁瘝鍘婚櫎锛岃繍琛岀粨鏋滃熀鏈笂涔熸病鏈夊樊寮傘€備絾鏄浜岀鏇存柟渚跨悊瑙c€?/p>

class Solution {
   public int longestPalindrome(String s) {
        int[] arr = new int[128];
        for(char c : s.toCharArray()) {
            arr[c]++;
        }
        int count = 0;
        for (int i : arr) {
            count += (i % 2);
        }
        return count == 0 ? s.length() : (s.length() - count + 1);
    }
}

205.鍚屾瀯瀛楃涓?/h1>

璁板綍涓€涓瓧绗︿笂娆″嚭鐜扮殑浣嶇疆锛屽鏋滀袱涓瓧绗︿覆涓殑瀛楃涓婃鍑虹幇鐨勪綅缃竴鏍凤紝閭d箞灏卞睘浜庡悓鏋勩€?/p>

class Solution {
    public boolean isIsomorphic(String s, String t) {
    int[] preIndexOfS = new int[256];
    int[] preIndexOfT = new int[256];
    for (int i = 0; i < s.length(); i++) {
        char sc = s.charAt(i), tc = t.charAt(i);
        if (preIndexOfS[sc] != preIndexOfT[tc]) {
            return false;
        }
        preIndexOfS[sc] = i + 1;
        preIndexOfT[tc] = i + 1;
    }
    return true;
    }
}

647.鍥炴枃瀛愪覆

浠庡瓧绗︿覆鐨勬煇涓€浣嶅紑濮嬶紝灏濊瘯鐫€鍘绘墿灞曞瓙瀛楃涓层€?br />
private int cnt = 0;

public int countSubstrings(String s) {
    for (int i = 0; i < s.length(); i++) {
        extendSubstrings(s, i, i);     // 濂囨暟闀垮害
        extendSubstrings(s, i, i + 1); // 鍋舵暟闀垮害
    }
    return cnt;
}

private void extendSubstrings(String s, int start, int end) {
    while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
        start--;
        end++;
        cnt++;
    }
}

9.鍥炴枃鏁?/h1>
class Solution {
    public boolean isPalindrome(int x) {
    if (x == 0) {
        return true;
    }
    if (x < 0 || x % 10 == 0) {
        return false;
    }
    int right = 0;
    while (x > right) {
        right = right * 10 + x % 10;
        x /= 10;
    }
    return x == right || x == right / 10;
    }
}

696.璁℃暟浜岃繘鍒跺瓙涓?/h1>
class Solution {
   public int countBinarySubstrings(String s) {
    int preLen = 0, curLen = 1, count = 0;
    for (int i = 1; i < s.length(); i++) {
        if (s.charAt(i) == s.charAt(i - 1)) {
            curLen++;
        } else {
            preLen = curLen;
            curLen = 1;
        }

        if (preLen >= curLen) {
            count++;
        }
    }
    return count;
    }
}

 

 

以上是关于leetcode鍒烽璁板綍鈥斺€斿瓧绗︿覆的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode鍒濈骇绠楁硶涔嬪瓧绗︿覆锛?42 鏈夋晥鐨勫瓧姣嶅紓浣嶈瘝

淇濇姢鍔熻兘璁板綍

Oracle寤鸿〃鑴氭湰璁板綍

Github浣跨敤璁板綍

JAVA鍏ラ棬濉潙璁板綍