【leetcode刷题笔记】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer。
代码如下:
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0)
return ""; StringBuffer answer = new StringBuffer();
for(int i = 0;i < strs[0].length();i++){
char now = strs[0].charAt(i);
for(int j = 1;j < strs.length;j++){
if(i>strs[j].length()-1 || strs[j].charAt(i) != now)
return answer.toString();
}
answer.append(now);
}
return answer.toString();
}
}
【leetcode刷题笔记】Longest Common Prefix的更多相关文章
- leetCode练题——14. Longest Common Prefix
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- leetcode【14题】Longest Common Prefix
题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...
- 【LeetCode每天一题】Longest Common Prefix(最长前缀)
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【LeetCode】LeetCode——第14题:Longest Common Prefix
14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode记录之14——Longest Common Prefix
本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...
随机推荐
- Mysql 创建权限较小的用户(只对特定数据库有操作权限)
项目开发过程中,因为root的权限太大,可能对其他数据库造成修改.故创建一权限较小的用户,使其只能对特定的数据库操作,以保证数据安全. 主要语句如下: grant all on bos19.* to ...
- Android下 布局加边框 指定背景色 半透明
背景设置为自定义的shape文件: <!-- <?xml version="1.0" encoding="utf-8"?><shape ...
- angular中使用daterangepicker完全能用版
angular版本:angular5 先看效果图: 最新版是这样的: 附上插件的网址: http://www.daterangepicker.com/ 1 安装: daterangepicker依赖于 ...
- 在SDL中显示GBK点阵汉字
大家注意到没有,RA2的中文版本使用的是GBK点阵字库,这样做有一个好处:不管玩家是用的简体还是繁体都能识别显示的文字. GBK的意思大概是“国家标准汉字扩展字符集”吧,记不清了.但它的确是个好东东, ...
- 使用eclipse开发hbase程序
一:在eclipse创建一个普通的java项目 二:新建一个文件夹,把hbase需要的jar放进去,我这里把hbase/lib/*.jar 下所有的jar都放进去了,最后发现就用到了下面三个jar ...
- 【LeetCode-面试算法经典-Java实现】【118-Pascal's Triangle(帕斯卡三角形)】
[118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...
- redhat安装中文man手册
1.下载中文man手册 http://download.chinaunix.net/download.php?id=13232&ResourceID=6537 2.上传至服务器并解压 tar ...
- php 获取文件加的名称
/*** * 根本平台类型获取文件名 */ function showGetFileName($type,$url){ #判断平台类型 if($type=='android'||$type=='ios ...
- [转]基于fiddler的APP抓包及服务端模拟
在HTTP接口的测试过程中,一般我们会按照如下的步骤进行: 1)测试环境的准备 2)HTTP消息体的构造 3)HTTP消息的发送及断言 如果我们可以拿到项目组的接口文档,并且HTTP后台服务是可以工作 ...
- Netty实战
一.Netty异步和事件驱动1.Java网络编程回顾socket.accept 阻塞socket.setsockopt /非阻塞2.NIO异步非阻塞a).nio 非阻塞的关键时使用选择器(java.n ...