Write a function to find the longest common prefix string amongst an array of strings.
2017-04-14 20:43
639 查看
import java.util.Scanner; import java.util.logging.Logger; import org.omg.PortableInterceptor.SYSTEM_EXCEPTION; public class Solution { public static String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; for (int j = 0; j < strs[0].length(); ++j){ // 纵向扫描 for (int i = 1; i < strs.length; ++i){ if (j == strs[i].length() ||strs[i].charAt(j) != strs[0].charAt(j)) return strs[0].substring(0, j); } } return strs[0]; } public static void main(String[] args) { String strs[] = new String[]{"abcdefg","abcef","abcdeabcg"}; System.out.println(longestCommonPrefix(strs)); } }
相关文章推荐
- 4、Write a function to find the longest common prefix string amongst an array of strings
- Write a function to find the longest common common string amongst an array of strings.
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- Write a C program to find the parity of an unsigned integer
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Write a program that gives count of common characters presented in an array of strings..(or array of
- Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba
- Given two strings s and t, write a function to determine if t is an anagram of s.
- You're given a string of lower-case Latin letters. Your task is to find the length of its longest su
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- Write a function to find the depth of a binary tree
- Write a function to find the depth of a binary tree
- Write a program that gives count of common characters presented in an array of strings..(or array of
- How to get the length of array of strings in C? - Yahoo! Answers
- The minimum number of elements to reach the end of an array
- Write one line C function to find whether a no is power of two
- CareerCup Write the code to find lexicographic minimum in a circular array
- pointer to ref of an array, and to the func that return a ref of an array
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- Find the k-th minimal element of an unsorted array