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

解题思路:

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

JAVA实现:

  1. static public String longestCommonPrefix(String[] strs) {
  2. if (strs.length == 0)
  3. return "";
  4. for (int i = 0; i < strs[0].length(); i++) {
  5. for (int j = 1; j < strs.length; j++)
  6. if (strs[j].length() == i || strs[j].charAt(i) != strs[0].charAt(i))
  7. return strs[0].substring(0, i);
  8. }
  9. return strs[0];
  10. }

C++:

  1. class Solution {
  2. public:
  3. string longestCommonPrefix(vector<string>& strs) {
  4. if (strs.size() == )
  5. return "";
  6. for (int i = ; i < strs[].length(); i++) {
  7. for (int j = ; j < strs.size(); j++)
  8. if (strs[j].length() == i || strs[j][i] != strs[][i])
  9. return strs[].substr(, i);
  10. }
  11. return strs[];
  12. }
  13. };

【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. 【Gym 100685J】Just Another Disney Problem(交互/排序)

    第一次做交互题. 题意是有n个数(n<1000),你通过问1 a b,后台返回你YES代表a<b,NO代表a>b.要你在10000次询问内给出一个符合的排列.n=1000来说,100 ...

  2. SqlParameter中的size

    SqlParameter中size对于需要指定大小的数据库中的数据类型参数有影响[如nvarchar],如果对于这些类型没有指定size则会默认根据赋的值进行推导应该指定的size,而对于那些大小固定 ...

  3. UVa 12505 Searching in sqrt(n)

    传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...

  4. 一个cheat命令 == Linux命令小抄大全

    本文介绍一个Linux超级命令,有了这个命令,你就可以开开心心的使用linux上的各种命令了.当你要执行一个linux命令,在这个命令参数选项众多时,你一般怎么做?对,我们大多数人都会去求助man命令 ...

  5. Android学习笔记01-Mac下搭建Java开发环境

    一 安装JDK 下载 mac 下专用的jdk1.7, 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downlo ...

  6. kindeditor粘贴word文档内容时去除格式的方法?如何设置为默认无文本格式呢?

    打开文件夹找到kindeditor-min.js文件,搜索pasteType函数,默认值是2.设置为1即可. 设置粘贴类型,0:禁止粘贴, 1:纯文本粘贴, 2:HTML粘贴.

  7. Android:View中的performClick()触发条件

    http://blog.sina.com.cn/s/blog_70ae1d7b0102v7uk.html 先看看performClick()源码:   public boolean performCl ...

  8. 初学hibernate之缓存

    一.1.Session级别缓存属于一级缓存,持久化对象保存在Session一级缓存中(一级缓存引用持久化对象地址),只要session不关闭,一级缓存就存在,缓存中对象也不会被回收: Session会 ...

  9. linux(Debian)下安装与MySql的安装、卸载、配置及使用

    参考资料:http://www.cnblogs.com/xusir/p/3334217.html 以下是简要记录. 一.安装 安装:apt-get install mysql-server mysql ...

  10. photoshop几个基本技巧

    原文地址:http://blog.thmz.com/user1/936/archives/2008/20418.htm 去除文字的几种方法: 1.访印图章工具 2.修补工具 3.修复画笔工具 4.画笔 ...