String longestPalindrome(String s){ if (s == null || s.length() == 0) { return""; } int len = s.length(); boolean[][] temp = newboolean[len][len]; int maxLen = 1; int start = 0; for (int i = 0; i < len - 1; i++) { temp[i][i] = true; if (s.charAt(i) == s.charAt(i + 1)) { maxLen = 2; temp[i][i + 1] = true; start = i; } } temp[len - 1][len - 1] = true; for (int tmpLen = 3; tmpLen <= len; tmpLen++) { for (int i = 0; i < len - tmpLen + 1; i++) { int j = i + tmpLen - 1; if (temp[i + 1][j - 1] && s.charAt(i) == s.charAt(j)) { maxLen = tmpLen; start = i; temp[i][j] = true; } } } return s.substring(start, start + maxLen); }
Python
classSolution: deflongestPalindrome(self, s: str) -> str: ifnot s or len(s) == 0: return'' res = [[Falsefor i in range(len(s))] for j in range(len(s))] max_len = 1 start = 0 str_len = len(s) for i in range(str_len - 1): res[i][i] = True if s[i] == s[i + 1]: res[i][i + 1] = True start = i max_len = 2 res[str_len - 1][str_len - 1] = True for tmp_len in range(3, str_len + 1): for i in range(str_len - tmp_len + 1): j = i + tmp_len - 1 if res[i+1][j-1] and s[i] == s[j]: max_len = tmp_len; start = i res[i][j]=True return s[start: start + max_len]
运行数据如下:
Language
Time
Memory
python3
3768 ms
21.7 MB
java
35 ms
39.4 MB
貌似可以优化?不过这样相对比较好理解啊
DataBase
Write a SQL query to get the second highest salary from the Employee table.
Write a SQL query to get the nth highest salary from the Employee table.
# 第二高 SELECT IFNULL( (SELECTDISTINCT Salary FROM Employee ORDERBY Salary DESC LIMIT1,1), NULL) AS SecondHighestSalary # 第N高 CREATEFUNCTION getNthHighestSalary(N INT) RETURNSINT BEGIN set N = N - 1; RETURN ( # Write your MySQL query statement below. SELECTDISTINCT Salary FROM Employee ORDERBY Salary DESC LIMIT1OFFSET N ); END