LeetCode算法入门- Longest Common Prefix -day13
生活随笔
收集整理的這篇文章主要介紹了
LeetCode算法入门- Longest Common Prefix -day13
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
LeetCode算法入門- Longest Common Prefix -day13
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string “”.
Example 1:
Input: [“flower”,“flow”,“flight”]
Output: “fl”
Example 2:
Input: [“dog”,“racecar”,“car”]
Output: “”
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z.
思路分析:
題目的意思是給定一個字符串數組,找出數組中所有字符串最長的公共前綴。
解題思路:可以將數組中的第一個元素strs[0]當做最大的公共前綴,然后來一一判斷其他數組中是否有此公共前綴,如果沒有,就將strs[0]中的最后一個元素去掉繼續判斷。。。
code如下:
總結
以上是生活随笔為你收集整理的LeetCode算法入门- Longest Common Prefix -day13的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内部类详解————匿名内部类
- 下一篇: LeetCode算法入门- Longes