Write a function to find the longest common prefix string amongst an array of strings.
额,这个暴力点就成
额,这个暴力点就成
public String longestCommonPrefix(String[] strs) { String result = ""; if(strs == null || strs.length==0){ return ""; } int s = strs[0].length(); int len = strs.length; int index= 0; boolean temp = false; for(int i = 0;i<len;i++){ if(strs[i].length()<s){ s = strs[i].length(); } } for(int i = 0 ;i<s;i++){ for(int j = 0;j<len-1 ;j++){ if(strs[j].charAt(i) != strs[j+1].charAt(i)){ temp = true; break; } } if(temp){ break; } index = i+1; } result = strs[0].substring(0, index); return result; }
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 325Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 363Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 307Given a string containing just ... -
Remove Nth Node From End of List
2015-03-05 22:31 338Given a linked list, remove the ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 329Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 310Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 291Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 317Given an array S of n integers, ... -
Roman to Integer
2015-03-03 22:20 326Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 289Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 321Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 365Implement regular expression ma ... -
Palindrome Number
2015-02-13 22:08 330Determine whether an integer is ... -
String to Integer (atoi)
2015-02-13 11:07 340Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 227Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 254The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 333Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 299You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 424[size=24px;]Longest Substring W ...
相关推荐
LeetCode Longest Common Prefix解决方案
asks for the length of the longest common prefix between suffixes beginning at i and j. A compressed LCE data structure is a data structure that stores T in a compressed form while supporting fast LCE...
算法#26--查找字符串数组中最长的公共前缀Write a function to find the longest common prefix string
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 ...Common Prefix 简单 字串 Valid Parentheses 简单 堆叠 重要 Merge Two Sorted Lists 简单 链结串列 重要 Remove Duplicates from
Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement ...
Longest Common Prefix Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid ...
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two ...Longest ...Longest ...Longest Common Prefix 019. R
leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。...Common Prefix 简单 15 3Sum 中等 16 3Sum Closest 中等 17 Letter Combinations of a Phone Number DFS 中等 18 4Sum 中等 19 Remo
leetcode ...Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of a Phone Number 018 4Sum 020 Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat
第 338 章力码 LeetCode刷题总结 1.Two Sum 2.Add Two ...Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter Combinations of a Phone Number 18.4Sum 19.Remove Nth Node From End
Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array ...
leetcode中文版 LeetCode # Title Chinese Tag Solution ...Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个有序链表 lin
Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
lru cache leetcode #算法解题报告 主要记录我每天做的题目,包括leetcode, ...Common Prefix 15.3Sum 16.3Sum Closest 19.Remove Nth Node From End of List 20.Valid Parentheses 21.Merge Two Sorted L
longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 ...Common Prefix :star: 0038 Count and Say :star: 0043 Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi
Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 毫秒内存使用:22.5 MB 26. Remove Duplicates ...
leetcode 答案programming-language-learning Javacript 复习基本DOM操作 C ...Longest Common Prefix 记得要判断strsSize = 0 && 1 的情况即可刚刚一直没判断会出现segment fault Python PHP Java
Flamg The flames of youth are raging. 解题口诀 动规递归是神器, 全无用处想哈希。 先用暴力得初解, 再据题意破玄奇。 贪心大法出身好, 正确路线永不倒。... Longest Common Prefix][27]][28] [!