{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "9a0992991f5b49779b019c87b3bdbcb2",
  "keywords": "字符串",
  "title": "最长公共前缀",
  "desc": [
    {
      "content": "\n<p>编写一个函数来查找字符串数组中的最长公共前缀。</p><p>如果不存在公共前缀，返回空字符串 <code>\"\"</code>。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>strs = [\"flower\",\"flow\",\"flight\"]<strong><br />输出：</strong>\"fl\"</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>strs = [\"dog\",\"racecar\",\"car\"]<strong><br />输出：</strong>\"\"<strong><br />解释：</strong>输入不存在公共前缀。</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>0 <= strs.length <= 200</code></li>\t<li><code>0 <= strs[i].length <= 200</code></li>\t<li><code>strs[i]</code> 仅由小写英文字母组成</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic String longestCommonPrefix(String[] strs) {\n\t\tif (strs.length == 0) {\n\t\t\treturn \"\";\n\t\t}\n\t\tint i = 0;\n\t\tStringBuilder lcp = new StringBuilder();\n\t\twhile (true) {\n\t\t\tboolean done = false;\n\t\t\tif (i >= strs[0].length()) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tfor (int j = 0; j < strs.length; j++) {\n\t\t\t\tif (i < strs[j].length()) {\n\t\t\t\t\tif (strs[j].charAt(i) != strs[0].charAt(i)) {\n\t\t\t\t\t\tdone = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tdone = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (done) {\n\t\t\t\tbreak;\n\t\t\t} else {\n\t\t\t\tlcp.append(strs[0].charAt(i));\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn lcp.toString();\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-df2108c0132f4f87a1db8b491de4dc79",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600469918"
}