leetcode
Java
Palindromic
Given a string s, return the longest palindromic substring in s.
給定一個String,並找出最長的回文子字串(palindromic substring),如有多個相同長度的最長子字串可只回傳一個
Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Input: s = "cbbd"
Output: "bb"
Input: s = "ac"
Output: "a"
start
和end
以往外擴展Approach 1
class Solution {
public String longestPalindrome(String s) {
//if(s.length() == 1) return s;
int len = s.length();
String result = new String();
int max_length = 0;
for (int i = 0;i < len;i++){
for (int j = i;j < len;j++){
String ss = s.substring(i, j+1);
boolean isPal = true;
for (int z = 0; z < ss.length(); z++){
if(ss.charAt(z) != ss.charAt(ss.length()-z-1)){
isPal = false;
break;
}
}
if(isPal && ss.length() > max_length){
max_length = ss.length();
result = ss;
}
}
}
return result;
}
}
我寫完此寫法後,因為執行時間過長導致Time Limit Exceeded,所以又找了其他方法,將Big-O變小
Approach 2
class Solution {
public String longestPalindrome(String s) {
int len = s.length();
String result = new String();
int max_length = 0;
for (int i = 0;i < len;i++){
//center odd
for (int start = i, end = i; start >= 0 && end < len;start--,end++){
if(s.charAt(start) != s.charAt(end))//判斷前後是否一致
break; //不一致的話就不是回文,跳出迴圈
if(end - start + 1 > max_length){ //檢查此次的回文子字串是否為最長的
max_length = end - start + 1;
result = s.substring(start, end+1);//將此子字串放到result
}
}
//center even
for (int start = i, end = i+1; start >= 0 && end < len;start--,end++){
//因為考慮到會有偶數的子字串(ex.cbbc)所以end在初始就往右一格或start往左一格都可
if(s.charAt(start) != s.charAt(end))
break;
if(end - start + 1 > max_length){
max_length = end - start + 1;
result = s.substring(start, end+1);
}
}
}
return result;
}
}
Runtime: 28 ms, faster than 57.01% of Java online submissions for Longest Palindromic Substring.
Memory Usage: 39.6 MB, less than 44.91% of Java online submissions for Longest Palindromic Substring.
Description The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows:
Aug 18, 2021Description Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). 將整數反轉,遇到超過32-bit的數就回傳0 Example Input: x = 123 Output: 321
Aug 15, 2021Description Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. 給一字符串數組, 將錯位詞(指相同字符不同排列的字符串) 分組 Example Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Aug 15, 2021LeetCode Two Sum Add Two Numbers Longest Substring Without Repeating Characters Longest Palindromic Substring
Aug 12, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up