4.json 6.1 KB
Newer Older
每日一练社区's avatar
test  
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
{
   "question_id": 5,
   "question_title": "最长回文子串",
   "difficulty": "中等",
   "question_content": "<p>给你一个字符串 <code>s</code>,找到 <code>s</code> 中最长的回文子串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = \"babad\"<strong><br />输出:</strong>\"bab\"<strong><br />解释:</strong>\"aba\" 同样是符合题意的答案。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = \"cbbd\"<strong><br />输出:</strong>\"bb\"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = \"a\"<strong><br />输出:</strong>\"a\"</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = \"ac\"<strong><br />输出:</strong>\"a\"</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>1 <= s.length <= 1000</code></li>\t<li><code>s</code> 仅由数字和英文字母(大写和/或小写)组成</li></ul>",
   "topic_link": "https://bbs.csdn.net/topics/600470834",
   "cpp": "class Solution\n{\npublic:\n\tstring longestPalindrome(string s)\n\t{\n\t\tint ti = 0, maxlen = 0, i, t;\n\t\tfor (i = 0; s[i]; i++)\n\t\t{\n\t\t\tt = 1;\n\t\t\twhile (t <= i && s[i + t])\n\t\t\t{\n\t\t\t\tif (s[i + t] == s[i - t])\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t + 1 > maxlen)\n\t\t\t{\n\t\t\t\tti = i - t;\n\t\t\t\tmaxlen = 2 * t + 1;\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; s[i]; i++)\n\t\t{\n\t\t\tt = 1;\n\t\t\twhile (t <= i + 1 && s[i + t])\n\t\t\t{\n\t\t\t\tif (s[i - t + 1] == s[i + t])\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t > maxlen)\n\t\t\t{\n\t\t\t\tti = i - t + 1;\n\t\t\t\tmaxlen = 2 * t;\n\t\t\t}\n\t\t}\n\t\ts[ti + maxlen] = 0;\n\t\treturn s.c_str() + ti;\n\t}\n};",
   "java": "class Solution {\n\tpublic String longestPalindrome(String s) {\n\t\tint ti = 0, maxlen = 0, i, t;\n\t\tfor (i = 0; i < s.length(); i++) {\n\t\t\tt = 1;\n\t\t\twhile (t <= i && i + t < s.length()) {\n\t\t\t\tif (s.charAt(i + t) == s.charAt(i - t))\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t + 1 > maxlen) {\n\t\t\t\tti = i - t;\n\t\t\t\tmaxlen = 2 * t + 1;\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; i < s.length(); i++) {\n\t\t\tt = 1;\n\t\t\twhile (t <= i + 1 && i + t < s.length()) {\n\t\t\t\tif (s.charAt(i - t + 1) == s.charAt(i + t))\n\t\t\t\t\tt++;\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tt--;\n\t\t\tif (2 * t > maxlen) {\n\t\t\t\tti = i - t + 1;\n\t\t\t\tmaxlen = 2 * t;\n\t\t\t}\n\t\t}\n\t\treturn s.substring(ti, ti + maxlen);\n\t}\n}",
   "js": "/**\n * @param {string} s\n * @return {string}\n */\nvar longestPalindrome  = function(s) {\n\tlet ti = 0;\n\tlet maxlen = 0;\n\tlet i, t;\n\n    // odd length probe\n    for (i = 0; s[i]; i++) {\n      t = 1;\n      while (t <= i && s[i + t]) {\n        if (s[i + t] == s[i - t])\n          t++;\n        else\n          break;\n      }\n      t--;\n      if (2 * t + 1 > maxlen) {\n        ti = i - t;\n        maxlen = 2 * t + 1;\n      }\n    }\n\n    // even length probe\n    for (i = 0; s[i]; i++) {\n      t = 1;\n      while (t <= i + 1 && s[i + t]) {\n        if (s[i - t + 1] == s[i + t])\n          t++;\n        else\n          break;\n      }\n      t--;\n\n      if (2 * t > maxlen) {\n        ti = i - t + 1;\n        maxlen = 2 * t;\n      }\n    }\n\n    return s.substring(ti, ti + maxlen);\n}\n\nfunction main(){\n\tconst cases = [\n\t\t[\"cbbd\",\"bb\"],\n\t\t// ['a','a'],\n\t\t// ['bab','bab'],\n\t\t// ['baab','baab'],\n\t\t// ['babad','bab'],\n\t\t// ['bb','bb'],\n\t\t// ['cbbd','bb'],\n\t\t// ['cxxabbmxxd','xx'],\n\t\t// ['aaabaaaa','aaabaaa'],\n\t\t// ['zudfweormatjycujjirzjpyrmaxurectxrtqedmmgergwdvjmjtstdhcihacqnothgttgqfywcpgnuvwglvfiuxteopoyizgehkwuvvkqxbnufkcbodlhdmbqyghkojrgokpwdhtdrwmvdegwycecrgjvuexlguayzcammupgeskrvpthrmwqaqsdcgycdupykppiyhwzwcplivjnnvwhqkkxildtyjltklcokcrgqnnwzzeuqioyahqpuskkpbxhvzvqyhlegmoviogzwuiqahiouhnecjwysmtarjjdjqdrkljawzasriouuiqkcwwqsxifbndjmyprdozhwaoibpqrthpcjphgsfbeqrqqoqiqqdicvybzxhklehzzapbvcyleljawowluqgxxwlrymzojshlwkmzwpixgfjljkmwdtjeabgyrpbqyyykmoaqdambpkyyvukalbrzoyoufjqeftniddsfqnilxlplselqatdgjziphvrbokofvuerpsvqmzakbyzxtxvyanvjpfyvyiivqusfrsufjanmfibgrkwtiuoykiavpbqeyfsuteuxxjiyxvlvgmehycdvxdorpepmsinvmyzeqeiikajopqedyopirmhymozernxzaueljjrhcsofwyddkpnvcvzixdjknikyhzmstvbducjcoyoeoaqruuewclzqqqxzpgykrkygxnmlsrjudoaejxkipkgmcoqtxhelvsizgdwdyjwuumazxfstoaxeqqxoqezakdqjwpkrbldpcbbxexquqrznavcrprnydufsidakvrpuzgfisdxreldbqfizngtrilnbqboxwmwienlkmmiuifrvytukcqcpeqdwwucymgvyrektsnfijdcdoawbcwkkjkqwzffnuqituihjaklvthulmcjrhqcyzvekzqlxgddjoir','gykrkyg']\n\t];\n\n\tfor(const c of cases){\n\t\tconsole.log('input:',c[0],', expected result:',c[1]);\n\t\tconst v  = longestPalindrome(c[0], 0);\n\t\tconsole.log('output:',v);\n\t\tconsole.assert(v===c[1]);\n\t}\t\n}\n\nmain();\n\n",
   "python": "class Solution:\n\tdef longestPalindrome(self, s: str) -> str:\n\t\tti = 0\n\t\tmaxlen = 0\n\t\ti = 0\n\t\twhile i < len(s):\n\t\t\tt = 1\n\t\t\twhile t <= i and i + t < len(s):\n\t\t\t\tif s[i + t] == s[i - t]:\n\t\t\t\t\tt += 1\n\t\t\t\telse:\n\t\t\t\t\tbreak\n\t\t\tt -= 1\n\t\t\tif 2 * t + 1 > maxlen:\n\t\t\t\tti = i - t\n\t\t\t\tmaxlen = 2 * t + 1\n\t\t\ti += 1\n\t\ti = 0\n\t\twhile i < len(s):\n\t\t\tt = 1\n\t\t\twhile t <= i + 1 and i + t < len(s):\n\t\t\t\tif s[i - t + 1] == s[i + t]:\n\t\t\t\t\tt += 1\n\t\t\t\telse:\n\t\t\t\t\tbreak\n\t\t\tt -= 1\n\t\t\tif 2 * t > maxlen:\n\t\t\t\tti = i - t + 1\n\t\t\t\tmaxlen = 2 * t\n\t\t\ti += 1\n\t\treturn s[ti:ti+maxlen]\n# %%\ns = Solution()\nprint(s.longestPalindrome('babad'))\nprint(s.longestPalindrome('cbbd'))",
   "status": 1,
   "keywords": "字符串,动态规划",
   "license": {
      "cpp": "csdn.net",
      "python": "csdn.net",
      "java": "csdn.net"
   },
   "notebook": {
      "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/4/4_cpp.ipynb?type=file",
      "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/4/4_python.ipynb?type=file",
      "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/4/4_java.ipynb?type=file"
   },
   "notebook_enable": 1
}