leetcode鍒烽璁板綍鈥斺€斿瓧绗︿覆
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode鍒烽璁板綍鈥斺€斿瓧绗︿覆相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/toc' title='toc'>toc
lin phi longest 瀵规瘮 return indexof242.鏈夋晥鍦板瓧姣嶅紓浣嶈瘝
鐢变簬鏈鐨勫瓧绗︿覆鍙寘鍚?26 涓皬鍐欏瓧绗︼紝鍥犳鍙互浣跨敤闀垮害涓?26 鐨勬暣鍨嬫暟缁勫瀛楃涓插嚭鐜扮殑瀛楃杩涜缁熻锛屽苟瀵规瘮瀛楁瘝鍑虹幇鐨勬鏁版槸鍚︿竴鑷淬€備笉鍐嶄娇鐢?HashMap銆?/p>
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;
}
}
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; } }
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鍒烽璁板綍鈥斺€斿瓧绗︿覆的主要内容,如果未能解决你的问题,请参考以下文章