LeetCode: Longest Common Prefix
string.erase没掌握好,悲了个剧,2次过
1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 string pre = ""; 7 if (!strs.size()) return pre; 8 for (int i = 0; i < strs[0].size(); i++) { 9 pre += strs[0][i]; 10 int j = 1; 11 for (; j < strs.size(); j++) { 12 if (pre != strs[j].substr(0, i+1)) break; 13 } 14 if (j != strs.size()) { 15 pre.erase(pre.size()-1, 1); 16 return pre; 17 } 18 } 19 return pre; 20 } 21 };
后来写了个更加精简的代码
1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 sort(strs.begin(), strs.end()); 7 string ret; 8 if (!strs.size() || !strs[0].size()) return ret; 9 int cur = 0; 10 while (cur < strs[0].size() && strs[0][cur] == strs[strs.size()-1][cur]) { 11 ret += strs[0][cur]; 12 cur++; 13 } 14 return ret; 15 } 16 };
C#
1 public class Solution { 2 public string LongestCommonPrefix(string[] strs) { 3 Array.Sort(strs); 4 string ans = ""; 5 if (strs.Length == 0 || strs[0].Length == 0) return ans; 6 int cur = 0; 7 while (cur < strs[0].Length && strs[0][cur] == strs[strs.Length-1][cur]) { 8 ans += strs[0][cur]; 9 cur++; 10 } 11 return ans; 12 } 13 }
View Code
转载于:https://www.cnblogs.com/yingzhongwen/archive/2013/04/02/2995674.html
LeetCode: Longest Common Prefix相关推荐
- LeetCode Longest Common Prefix
原题链接在这里:https://leetcode.com/problems/longest-common-prefix/ 题目: Write a function to find the longes ...
- LeetCode - Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 以第一个字 ...
- LeetCode:Longest Common Prefix
题目链接 Write a function to find the longest common prefix string amongst an array of strings. 题目的意思说的不 ...
- [leetcode] Longest Common Prefix 字符窜最长公共前缀判断
题目:略 char* longestCommonPrefix(char** strs, int strsSize) {int i = 0, j = 0;int sum = 0;char *buf = ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- LeetCode之Longest Common Prefix
1.题目 Write a function to find the longest common prefix string amongst an array of strings 2.代码实现 pa ...
- LeetCode - Easy - 14. Longest Common Prefix
Topic String Description https://leetcode.com/problems/longest-common-prefix/ Write a function to fi ...
- LeetCode算法入门- Longest Common Prefix -day13
LeetCode算法入门- Longest Common Prefix -day13 题目描述: Write a function to find the longest common prefix ...
- leetcode python3 简单题14. Longest Common Prefix
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十四题 (1)题目 英文: Write a function to find th ...
最新文章
- 基于octree的空间划分及搜索操作
- 学python好找工作嘛-Python开发学完好找工作吗?
- ie8.0 不能用document.all兼容IE7模式
- 频段表_VoLTE高低频段覆盖能力研究
- C++ memset 栈空间出现段错误
- js中WINDOW对象中的navigator成员对象
- Windows键盘如何映射为Mac使用
- 用命令从FTP服务器下载文件
- 记录一次pre环境OOM异常解决过程
- UVM中starting_phase
- 获取手机唯一标识插件_H5能获取到手机设备ID或者手机浏览器唯一识别码吗
- html怎么设置左侧区域宽度,【CSS】左侧定宽、右侧宽度自适应布局
- VS2019与fluter通过MethodChannel进行双向通信(原创)
- 红旗 linux 在哪儿 看 版本,简介红旗Linux不同版本的介绍
- UAP安装使用FindBugs
- 手机计算机软件删除了怎么恢复,电脑的程序软件不小心被删掉,该如何恢复?...
- win32bridge.server.exe函数不正确解决方案
- 2021年3月1日 星期一 冬月八九 阴
- 直播课堂系统05-后台管理系统
- 【Android UI】贝塞尔曲线 ① ( 一阶贝塞尔曲线 | 二阶贝塞尔曲线 )