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

解题思路:

老实遍历即可,注意边界条件:

JAVA实现:

	static public String longestCommonPrefix(String[] strs) {
if (strs.length == 0)
return "";
for (int i = 0; i < strs[0].length(); i++) {
for (int j = 1; j < strs.length; j++)
if (strs[j].length() == i || strs[j].charAt(i) != strs[0].charAt(i))
return strs[0].substring(0, i);
}
return strs[0];
}

C++:

 class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() == )
return "";
for (int i = ; i < strs[].length(); i++) {
for (int j = ; j < strs.size(); j++)
if (strs[j].length() == i || strs[j][i] != strs[][i])
return strs[].substr(, i);
}
return strs[];
}
};

【JAVA、C++】LeetCode 014 Longest Common Prefix的更多相关文章

  1. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. 【CodeForces 602C】H - Approximating a Constant Range(dijk)

    Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...

  2. oracle存储过程执行中输出日志文件

    create or replace procedure p_outputdebug(a varchar2,b varchar2,c varchar2)is vFileName varchar2(100 ...

  3. Spring Boot - fish

    1. @RestController combines @Controller and @ResponseBody, 这是不是意味着不用再import jakson的包(@ResponseBody时用 ...

  4. POJ1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  5. Model1模式的学生信息增删改查

    Student.java package entity; public class Student { private int stuid; private String stuname; priva ...

  6. python urllib2使用心得

    python urllib2使用心得 1.http GET请求 过程:获取返回结果,关闭连接,打印结果 f = urllib2.urlopen(req, timeout=10) the_page = ...

  7. ExtJS入门教程01,Window如此简单,你怎能不会?

    这是一系列ExtJS教程,今天的是第一篇,主要介绍ExtJS中Window的基本用法.希望大家能够支持! 来吧,创建一个漂亮的弹出窗 var win = Ext.create("Ext.Wi ...

  8. Bootstrap 新手学习笔记——布局组件

    1.字形图标: <button type="button" class="btn btn-primary btn-lg" style="font ...

  9. UML(5)——协作图

    协作图中表示了角色之间的关系,通过协作图限定协作中的对象或链.协作指的是在一定的语境中一组对象以及实现某些行为的对象间的相互作用. 协 作图是表现对象协作关系的图,表示了协作中作为各种类元角色的对象所 ...

  10. Android(Java):jni源代码

    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);      ...