最长公共前缀-每日1LeetCode
题目
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""
。
示例 1:
1 | 输入:strs = ["flower","flow","flight"] |
示例 2:
1 | 输入:strs = ["dog","racecar","car"] |
提示:
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] 仅由小写英文字母组成
解法
1 |
|
简单来说就是遍历这几个字符串同一位置的字符,任何字符串遇到'\0'
就收手,这里用到了迭代器。
(没想到这道题第一次做就能跑出3ms的时间效率)
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment