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. Oracle中的伪列

    分页查询中,需要用到伪列rownum,代码如下: select * from (select rownum rn, name from cost where rownum <= 6) where ...

  2. BZOJ-3669 魔法森林 Link-Cut-Tree

    意识到背模版的重要性了,记住了原理和操作,然后手打模版残了..颓我时间...... 3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 M ...

  3. Bzoj2705 Longge的问题

    Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Description Longge的数学 ...

  4. 在excel批量更改单元格类型后的批量刷新显示

    把E的东西变成完整显示 解决办法: 选中所需要更改的整列数据------>菜单栏的数据选项------>分列

  5. groovy-运算符

    算术和条件运算符 Groovy支”!”操作符,例如: 1 def expression = false 2 assert !expression 基于集合的运算符: Spread Operator ( ...

  6. python学习易错点1

    1.>>> d = {'x': 'A', 'y': 'B', 'z': 'C' } >>> for k, v in d.iteritems(): ... print ...

  7. ASP.NET MVC 3 loginUrl自动变成Account/Login,并且发生404错误的解决方法

    http://www.cnblogs.com/think8848/archive/2011/07/08/2100814.html ASP.NET MVC 3 loginUrl自动变成Account/L ...

  8. vm虚拟机自定义安装centOS找不到网卡

    问题:自定义简化安装后执行ifconfig无法找到eth0网卡 1.查看eth0网络配置: [root@minion1 ~]# cat /etc/sysconfig/network-scripts/i ...

  9. SQL2005之SA提权总结

    首先,看看xp_cmdshell存在不,不存在的话先恢复下. Exec sp_configure 'show advanced options',1;RECONFIGURE;EXEC sp_confi ...

  10. bootstrap 新手学习笔记 代码整理

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...