最长公共前缀

思路

  1. 找出字符串中最小字符串长度,因为公共最长前缀不可能超过最小字符串长度
  2. 循环每个字符截取相同的前缀

    1. public String longestCommonPrefix2(String[] strs) {
    2. // "abcc", "abc", "abeee"
    3. if (strs == null || strs.length < 1) {
    4. return "";
    5. }
    6. // 找出字符串最小长度
    7. int minLen = strs[0].length();
    8. for (int i = 1; i < strs.length; i++) {
    9. minLen = Math.min(minLen, strs[i].length());
    10. }
    11. // 任取一个字符串为公共前缀
    12. String ans = strs[0];
    13. for (int i = 0; i < strs.length; i++) {
    14. // 循环到字符不相等时的下标j,下标j<最小字符串长度,下标j<ans长度
    15. int j;
    16. for (j = 0; j < minLen && j < ans.length(); j++) {
    17. if (ans.charAt(j) != strs[i].charAt(j)) {
    18. break;
    19. }
    20. }
    21. // 截取相同字符
    22. ans = ans.substring(0, j);
    23. }
    24. return ans;
    25. }

    最长回文串

    思路

  3. 回文串一定是对称的,所以我们可以每次循环选择一个中心,进行左右扩展,判断左右字符是否相等即可

  4. 由于存在奇数的字符串和偶数的字符串,所以我们需要从一个字符开始扩展,或者从两个字符之间开始扩展,所以总共有 n + (n-1)个中心 ```java /**
  • aa
  • aba
  • 最长回文串 */ public String longestPalindrome(String s) { if (s == null || s.length() < 1) {

    1. return "";

    } int start = 0, end = 0; for (int i = 0; i < s.length(); i++) {

    1. int len1 = expandAroundCenter(s, i, i);//奇数最长回文串
    2. int len2 = expandAroundCenter(s, i, i + 1);//偶数最长回文串
    3. int len = Math.max(len1, len2);
    4. if (len > end - start) {
    5. start = i - (len - 1) / 2;
    6. end = i + len / 2;
    7. }

    }

    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)) {
    1. L--;
    2. R++;
    } return R - L - 1; } ```

    反转字符串

    思路

  1. splite 空格分隔字符串
  2. Stack 根据栈的特性把非“”字符串加入栈,后出栈中间空格拼接

    1. /**
    2. * 反转字符串
    3. *
    4. * @param s
    5. * @return
    6. */
    7. public String reverseWords(String s) {
    8. // str = " a good boy "
    9. String[] split = s.split(" ");
    10. Stack<String> stack = new Stack<>();
    11. for (String s1 : split) {
    12. if (!"".equals(s1)) {
    13. stack.push(s1);
    14. }
    15. }
    16. StringBuilder sb = new StringBuilder();
    17. while (!stack.isEmpty()) {
    18. sb.append(stack.pop());
    19. if (!stack.isEmpty()) {
    20. sb.append(" ");
    21. }
    22. }
    23. return sb.toString();
    24. }

    无重复字符的最长子串

    思路

时间滑块,map保存key为字符val为字符的下一个下一个下标作为下一个起始位置
map中再次出现了重复字符,重新计算起始位置 start = Math.max(map.get(c),start)
每次计算最长无重复字符串长度,直到遍历完所有字符串 ans=Math.max(end-start+1,ans)

  1. /**
  2. * 无重复字符的最长子串
  3. *
  4. * @param s
  5. * @return
  6. */
  7. public static int lengthOfLongestSubstring(String s) {
  8. int ans = 0;
  9. Map<Character, Integer> map = new HashMap<>();
  10. for (int end = 0, start = 0; end < s.length(); end++) {
  11. char alpha = s.charAt(end);
  12. if (map.containsKey(alpha)) {
  13. start = Math.max(map.get(alpha), start);
  14. }
  15. ans = Math.max(ans, end - start + 1);
  16. map.put(alpha, end + 1);
  17. }
  18. return ans;
  19. }

最长有效括号 只有”(“、”)”组成的字符串

利用栈
时间复杂度为 O(n),空间复杂度为 O(n)

  1. /**
  2. * 利用栈
  3. * 始终保存栈底为 最后一个没有被匹配的右括号的下标
  4. * 时间复杂度为 O(n),空间复杂度为 O(n)
  5. */
  6. public int longestValidParentheses(String s) {
  7. // ())((())
  8. int max = 0;
  9. Stack<Integer> stack = new Stack<>();
  10. stack.push(-1);
  11. for (int i = 0; i < s.length(); i++) {
  12. if (s.charAt(i) == '(') {
  13. stack.push(i);
  14. } else {
  15. stack.pop();
  16. if (stack.empty()) {
  17. stack.push(i);
  18. } else {
  19. max = Math.max(max, i - stack.peek());
  20. }
  21. }
  22. }
  23. return max;
  24. }

利用变量计数
时间复杂度为 O(n),空间复杂度为 O(1)

  1. /**
  2. * 利用变量计数
  3. * 时间复杂度为 O(n),空间复杂度为 O(1)
  4. */
  5. public int longestValidParentheses2(String s) {
  6. int max = 0, left = 0, right = 0;
  7. for (int i = 0; i < s.length(); i++) {
  8. if (s.charAt(i) == '(') {
  9. left++;
  10. } else {
  11. right++;
  12. }
  13. if (left == right) {
  14. max = Math.max(max, right * 2);
  15. }
  16. if (right > left) {
  17. left = right = 0;
  18. }
  19. }
  20. for (int i = s.length() - 1; i <= 0; i--) {
  21. if (s.charAt(i) == '(') {
  22. left++;
  23. } else {
  24. right++;
  25. }
  26. if (left == right) {
  27. max = Math.max(max, left * 2);
  28. }
  29. if (left > right) {
  30. left = right = 0;
  31. }
  32. }
  33. return max;
  34. }