[leetcode]14. Longest Common Prefix
来源:互联网 发布:软件项目可行性分析 编辑:程序博客网 时间:2024/06/03 05:58
Write a function to find the longest common prefix string amongst an array of strings.
char* longestCommonPrefix(char** strs, int strsSize) { char* str=strs[0]; int i,j; if(strsSize==0)return ""; for(i=1;i<strsSize;i++) { j=0; while(str[j] && strs[i][j] && str[j]==strs[i][j]) j++; str[j]=0; } return str; }
该代码参考网上代码
C++代码明显方便很多
class Solution {public: string longestCommonPrefix(vector<string>& strs) { if(strs.size() ==0) return ""; int length=strs.size(); sort(strs.begin(),strs.end()); string result = ""; for(int i=0; i<strs[0].length(); i++) { for(int j=0; j<strs.size(); j++) { if(strs[0][i] != strs[j][i]) return result; } result.append(1,strs[0][i]); } return result; }};
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
- python小函数(一)
- C#中Trim,TrimStart,TrimEnd用法
- java.lang.IllegalStateException: TimerTask is scheduled already
- DTCMS网站开发遇到的一些问题
- 压缩代码和资源
- [leetcode]14. Longest Common Prefix
- 笔记-分布式系统
- 缓存技术
- MyISAM InnoDB 区别?
- The number of method references in xxx.dex file cannot exceed 64K问题
- Java 多线程——— 线程间的通信(wait及notify方法)
- zabbix源码安装配置
- 小金防呀防不住_AK
- MYSQL中联表删除delete的用法