【嘎】字符串-最长的回文子串

题目一(简单):

实现 strStr() 函数

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:

  输入: haystack = "hello", needle = "ll"
  输出: 2
示例 2:

  输入: haystack = "aaaaa", needle = "bba"
  输出: -1


说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

class Solution {
    public int strStr(String haystack, String needle) {
        if ("".equals(needle)) {
            return 0;
        }
        return haystack.indexOf(needle);
    }
}

题目二:

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

示例 1:

  输入: "babad"
  输出: "bab"
  注意: "aba" 也是一个有效答案。
示例 2:

  输入: "cbbd"
  输出: "bb"

 emmm 我实在是太笨了,昨天的题拖到了今天,击败了很少的用户,一直在debug,啊啊啊

分成了奇数个偶数个最长子串,然后用了递归,不明白51行为啥要return,一开始没写,结果一直不对,需要再琢磨琢磨

 1 class Solution {
 2     public String longestPalindrome(String s) {
 3                String longestStr = "";
 4         List<String> res = new ArrayList<String>();
 5 
 6         // 一开始没有想到
 7         if ("".equals(s.trim())) {
 8             return s;
 9         }
10         for (int i = 0; i < s.length(); i++) {
11             res.add(s.charAt(i) + "");
12         }
13         if (s.length() > 1) {
14             for (int i = 1; i < s.length(); i++) {
15                 String longstr = getPalindrome2(s, i-1, i, "");
16                 if (!"".equals(longstr.trim())) {
17                     res.add(longstr);
18                 }
19             }
20             if (s.length() > 2) {
21                 // 从第二个开始遍历
22                 for (int i = 1; i < s.length() - 1; i++) {
23                     String longstr = getPalindrome(s, i-1, i+1, s.charAt(i) + "");
24                     if (!"".equals(longstr.trim())) {
25                         res.add(longstr);
26                     }
27                 }
28             }
29         }
30         
31         
32         int longestlen = 0;
33         if (res.size() > 0) {
34             for (int i = 0; i < res.size(); i++) {
35                 int len = res.get(i).length();
36                 if (len > longestlen) {
37                     longestStr = res.get(i);
38                     longestlen = len;
39                 }
40             }
41         }
42         return longestStr;
43     }
44 
45     public String getPalindrome(String s, int x, int y, String longstr) {
46           if (s.charAt(x) == s.charAt(y)) {
47           longstr =  s.charAt(x) + longstr + s.charAt(y);
48        // 比如三位的ccc,y最多是2就要停止向下
49           if (x < 1 || y >= s.length() - 1) {
50           } else {
51               return getPalindrome(s, x-1, y+1, longstr);
52           }
53         }
54         return longstr;
55     }
56     public String getPalindrome2(String s, int x, int y, String longstr) {
57          if (s.charAt(x) == s.charAt(y)) {
58           longstr =  s.charAt(x) + longstr + s.charAt(y);
59           // 比如三位的ccc,y最多是2就要停止向下
60           if (x < 1 || y >= s.length() - 1) {
61           } else {
62               return getPalindrome2(s, x-1, y+1, longstr);
63           }
64         }
65         return longstr;
66     }
67 }

 然后我看了下官方的解法,有五种方式,我看不明白,先放弃了,就来看为啥要加return了。看到有人的评论 orz:

我突然想起很久之前做的那些面试题,类似于这样,看来当时理解的不透彻啊。字符串放在常量池里面,所以是值传递。

 下面这个讲的很明白耶:(Java 到底是值传递还是引用传递?)

https://www.zhihu.com/question/31203609

越努力越幸运~ 加油ヾ(◍°∇°◍)ノ゙
原文地址:https://www.cnblogs.com/utomboy/p/12410348.html