LeetCode 14. Longest Common Prefix
来源:互联网 发布:php excel多级下拉版 编辑:程序博客网 时间:2024/05/24 04:20
14. Longest Common Prefix
Description
Write a function to find the longest common prefix string amongst an array of strings.
Analysis
题目的意思是求所给字符串组中公有的最大子前缀。
我的做法是首先假定建立一个变量index。
index来确定最大前缀在第一个字符串中的最后一个字符的下标。
然后将第一个字符串与剩下所有的字符串分别进行比较,得到公有最大前缀。
Code
class Solution {public: string longestCommonPrefix(vector<string>& strs) { int len = strs.size(); //if(strs == NULL) return""; if(len == 0) return""; int index = strs[0].size(); for(int i = 1 ;i < len;++i){ int j; for(j = 0 ; j < min(strs[0].size(),strs[i].size());++j){ if(strs[0][j] != strs[i][j]) break; } if(index > j) index = j; } return strs[0].substr(0,index); }};
阅读全文
0 0
- [leetcode] 14. Longest Common Prefix[leetcode] 14. Longest Common Prefix
- [LeetCode]14.Longest Common Prefix
- LeetCode 14. Longest Common Prefix
- LeetCode --- 14. Longest Common Prefix
- LeetCode 14.Longest Common Prefix
- [Leetcode] 14. Longest Common Prefix
- 【leetcode】14. longest common prefix
- [leetcode] 14.Longest Common Prefix
- [leetcode] 14. Longest Common Prefix
- LeetCode - 14. Longest Common Prefix
- Leetcode-14. Longest Common Prefix
- Leetcode 14. Longest Common Prefix
- 14. Longest Common Prefix LeetCode
- leetcode 14. Longest Common Prefix
- leetcode 14. Longest Common Prefix
- leetcode 14. Longest Common Prefix
- LeetCode--14. Longest Common Prefix
- Leetcode 14. Longest Common Prefix
- R语言:画q-q图
- uDig+Geoserver 发布shp
- 总结iframe高度自适应,自适应子页面高度
- Myeclipse背景颜色配置
- 华为防火墙配置NAT
- LeetCode 14. Longest Common Prefix
- 栈与堆
- ArrayList,LinkedList,Stack,Queue,PriorityQueue 基本概念
- Elasticsearch Date Histogram 时区问题
- 1042. 字符统计(20) PAT
- JAVA读取TXT文件 写入TXT文件
- 【剑指offer-解题系列(17)】树的子结构
- 3-Python-字典、元组、函数(上)
- Python或者R语言出现中文字体乱码的情况