《LeetBook》leetcode题解(14):Longest Common Prefix[E]
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
014.Longest Common Prefix[E]
问题
Write a function to find the longest common prefix string amongst an array of strings.
Subscribe to see which companies asked this question
思路
这个没啥思路的,怎么都要两重循环,因为是最长公共子串,随便找一个(一般是第一个作为基准),然后拿拿的首部慢慢去匹配后面的字符串就行了。
public class Solution {
public String longestCommonPrefix(String[] strs) {
String s = "";
if(strs.length == 0)
return "";
for(int i = 0; i < strs[0].length();i++)
{
char c = strs[0].charAt(i);
for(int j = 1;j < strs.length;j++)
{
if(i >= strs[j].length() || strs[j].charAt(i) != c)
return s;
}
s += c;
}
return s;
}
}
《LeetBook》leetcode题解(14):Longest Common Prefix[E]的更多相关文章
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- C# 写 LeetCode easy #14 Longest Common Prefix
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. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
- 【LeetCode】14. Longest Common Prefix (2 solutions)
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)
1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...
随机推荐
- .NET基础 (10)流和序列化
流和序列化1 什么是流,.NET中有哪些常见的流2 如何使用压缩流3 Serializable特性有何作用4 .NET提供了哪几种可进行序列化操作的类型5 如何自定义序列化和反序列化的过程 流和序列化 ...
- Appium之手机屏幕亮度控制条处理
手机设置下的屏幕亮度控制条看上去是悬浮的,想手动调整亮度有两种方法:一.在控制条上左右任意拖动:二.在控制条上点击任意一点.如下图:
- C++笔记16之const的用法总结
const主要是为了程序的健壮型,减少程序出错. 最基本的用法: const int a=100; b的内容不变,b只能是100也就是声明一个int类型的常量(#define b =100) int ...
- C#动态编译引擎-CS-Script 简单使用
Technorati 标记: cs-script 介绍可以参看 http://www.cnblogs.com/shanyou/p/3413585.html 还可以参看 这个 项目介绍 性能测试 c ...
- Linux 用户和文件
Linux系统中用户的扩展研究 进程 用户和文件 Linux中的用户及用户组 linux中只有两个等级:root和非root, 一个用户至少属于一个用户组 一个用户可以属于多个用户组 用户本身的区别主 ...
- WebApi Post string 参数 为空
用webApi做开发也有很久了 一些 细节平时可能未必很留心 今天就很奇葩 post 只接受一个string 参数的数据 但接收是一直未空 很奇怪 看了一些资料后得出以下结论
- cannot connect cube with sharepoint dashboard designer
需要下载WindowsIdentityFoundation-SDK-4.0进行安装
- html开发基础
1 Doctype Doctype告诉浏览器使用什么样的html或xhtml规范来解析html文档 有和无的区别 BackCompat:标准兼容模式未开启(或叫怪异模式[Quirks mode].混杂 ...
- 使用apache-fileupload处理文件上传与上传多个文件 二(60)
一 使用apache-fileupload处理文件上传 框架:是指将用户经常处理的业务进行一个代码封装.让用户可以方便的调用. 目前文件上传的(框架)组件: Apache----fileupload ...
- MongoDB健壮集群——用副本集做分片
1. MongoDB分片+副本集 健壮的集群方案 多个配置服务器 多个mongos服务器 每个片都是副本集 正确设置w 架构图 说明: 1. 此实验环境在一台机器上通过不同port和dbp ...