问题:寻找最长公共前缀

思路:就是逐一检查每个string中的每一位,碰到不相等的时候,结束;每个string中这一位都相等,加入到common prefix中~

public String longestCommonPrefix(String[] strs) {

        int len = strs.length;
if(len == 0) return ""; String commonPrefix = ""; for(int j = 0; ; j++){
if(j >= strs[0].length()) break;
int i;
for(i = 1 ; i < len ; i++){
if(j >= strs[i].length() || strs[i].charAt(j) != strs[0].charAt(j)) break;
}
if(i == len) commonPrefix += strs[0].charAt(j);
else break;
} return commonPrefix;
}

[leetcode]_Longest Common Prefix的更多相关文章

  1. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  2. Leetcode Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

  3. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  4. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  5. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  6. [LeetCode] Longest Common Prefix 字符串公有前序

    Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...

  7. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  8. [LeetCode]-011-Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. []=>" ...

  9. LeetCode OJ-- Longest Common Prefix

    https://oj.leetcode.com/problems/longest-common-prefix/ 在多个string的集合中,找出所有string的最长公共前缀. 从头开始 index ...

随机推荐

  1. 查看.netframeword版本

    打开“我的电脑“,在地址栏输入 %systemroot%\Microsoft.NET\Framework

  2. IE,Chrome滚动条样式CSS

    <style type="text/css"> *{ scrollbar-face-color:#F3F3F3; /*面子*/ scrollbar-arrow-colo ...

  3. text透明无边框

    <input type="text" style="border:0px;background-color:transparent;outline:none;&qu ...

  4. gradle android

    从github下载两个开源项目: PagerSlidingTabStrip    |    Android-Universal-Image-Loader-master https://github.c ...

  5. Codeforces 450D Jzzhu and Cities [heap优化dij]

    #include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...

  6. POJ 3311 【状态压缩DP】

    题意: 给n个点,给出矩阵代表i到j单向边的距离. 要求,不介意访问每个点的次数,要求访问完每个点,使得路程总和最小. 思路: 由于不介意访问每个点的次数,所以可以先进行FLOYD求出任意两个点之间的 ...

  7. (easy)LeetCode 263.Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  8. ListView中item的最外层使用margin属性失效

    参考文章:http://stackoverflow.com/questions/16278159/why-linearlayouts-margin-is-being-ignored-if-used-a ...

  9. 解决eclipse中出现Resource is out of sync with the file system问题

    解决eclipse中出现Resource is out of sync with the file system问题 . 分类: 嵌入式开发平台和环境相关 2011-12-27 16:18 4872人 ...

  10. Android Activity 详述

    activity类处于android.app包中,继承关系: extends ContextThemeWrapper implements LayoutInflater.Factory2 Window ...