NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
## [14\. 最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/) >Easy #### 思路 要求最长的公共前缀,我们可以想成是字符串都排好,然后我们从左到右扫描一遍。观察都是否都相同,又不相同或者超出字串的长度,就结束。如下图 ``` result f ↓ f l o w e r f l o w f l i g h t result f l ↓ f l o w e r f l o w f l i g h t result f l ↓ f l o w e r f l o w f l i g h t ``` 尝试写一下代码,AC! #### 代码 python3 ``` class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs) == 0: return '' result = '' cur = 0 while True: temp = '' for s in strs: if cur >= len(s): return result if temp == '': temp = s[cur] elif temp != '' and temp != s[cur]: return result result += temp temp = '' cur += 1 return result ```