最长公共前缀
思路
- 找出字符串中最小字符串长度,因为公共最长前缀不可能超过最小字符串长度
循环每个字符截取相同的前缀
public String longestCommonPrefix2(String[] strs) {// "abcc", "abc", "abeee"if (strs == null || strs.length < 1) {return "";}// 找出字符串最小长度int minLen = strs[0].length();for (int i = 1; i < strs.length; i++) {minLen = Math.min(minLen, strs[i].length());}// 任取一个字符串为公共前缀String ans = strs[0];for (int i = 0; i < strs.length; i++) {// 循环到字符不相等时的下标j,下标j<最小字符串长度,下标j<ans长度int j;for (j = 0; j < minLen && j < ans.length(); j++) {if (ans.charAt(j) != strs[i].charAt(j)) {break;}}// 截取相同字符ans = ans.substring(0, j);}return ans;}
最长回文串
思路
回文串一定是对称的,所以我们可以每次循环选择一个中心,进行左右扩展,判断左右字符是否相等即可
- 由于存在奇数的字符串和偶数的字符串,所以我们需要从一个字符开始扩展,或者从两个字符之间开始扩展,所以总共有 n + (n-1)个中心 ```java /**
- aa
- aba
最长回文串 */ public String longestPalindrome(String s) { if (s == null || s.length() < 1) {
return "";
} int start = 0, end = 0; for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);//奇数最长回文串int len2 = expandAroundCenter(s, i, i + 1);//偶数最长回文串int len = Math.max(len1, len2);if (len > end - start) {start = i - (len - 1) / 2;end = i + len / 2;}
}
return s.substring(start, end + 1); }
/**
- 中心左右探索最长回文串
- String str = “babadcad”;
- i = 0 len1 = 1
- i = 0 len2 = 0
- i = 0 len = 1, start = 0,end = 0
- i = 1 len1 = 3
- i = 1 len2 = 0
- i = 1 len = 3, start = 0,end = 2
- i = 2 len1 = 1
- i = 2 len2 = 0
- i = 2 len = 1, start = 0, end = 2
*/
public int expandAroundCenter(String s, int L, int R) {
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
} return R - L - 1; } ```L--;R++;
反转字符串
思路
- splite 空格分隔字符串
Stack 根据栈的特性把非“”字符串加入栈,后出栈中间空格拼接
/*** 反转字符串** @param s* @return*/public String reverseWords(String s) {// str = " a good boy "String[] split = s.split(" ");Stack<String> stack = new Stack<>();for (String s1 : split) {if (!"".equals(s1)) {stack.push(s1);}}StringBuilder sb = new StringBuilder();while (!stack.isEmpty()) {sb.append(stack.pop());if (!stack.isEmpty()) {sb.append(" ");}}return sb.toString();}
无重复字符的最长子串
思路
时间滑块,map保存key为字符val为字符的下一个下一个下标作为下一个起始位置
map中再次出现了重复字符,重新计算起始位置 start = Math.max(map.get(c),start)
每次计算最长无重复字符串长度,直到遍历完所有字符串 ans=Math.max(end-start+1,ans)
/*** 无重复字符的最长子串** @param s* @return*/public static int lengthOfLongestSubstring(String s) {int ans = 0;Map<Character, Integer> map = new HashMap<>();for (int end = 0, start = 0; end < s.length(); end++) {char alpha = s.charAt(end);if (map.containsKey(alpha)) {start = Math.max(map.get(alpha), start);}ans = Math.max(ans, end - start + 1);map.put(alpha, end + 1);}return ans;}
最长有效括号 只有”(“、”)”组成的字符串
利用栈
时间复杂度为 O(n),空间复杂度为 O(n)
/*** 利用栈* 始终保存栈底为 最后一个没有被匹配的右括号的下标* 时间复杂度为 O(n),空间复杂度为 O(n)*/public int longestValidParentheses(String s) {// ())((())int max = 0;Stack<Integer> stack = new Stack<>();stack.push(-1);for (int i = 0; i < s.length(); i++) {if (s.charAt(i) == '(') {stack.push(i);} else {stack.pop();if (stack.empty()) {stack.push(i);} else {max = Math.max(max, i - stack.peek());}}}return max;}
利用变量计数
时间复杂度为 O(n),空间复杂度为 O(1)
/*** 利用变量计数* 时间复杂度为 O(n),空间复杂度为 O(1)*/public int longestValidParentheses2(String s) {int max = 0, left = 0, right = 0;for (int i = 0; i < s.length(); i++) {if (s.charAt(i) == '(') {left++;} else {right++;}if (left == right) {max = Math.max(max, right * 2);}if (right > left) {left = right = 0;}}for (int i = s.length() - 1; i <= 0; i--) {if (s.charAt(i) == '(') {left++;} else {right++;}if (left == right) {max = Math.max(max, left * 2);}if (left > right) {left = right = 0;}}return max;}
