zoukankan      html  css  js  c++  java
  • 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings.
    
    Subscribe to see which companies asked this question
    

      

    }public class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs.length == 0)                //想换成字符数组来着,这样空间代价有点大的感觉,直接暴力法了。。。
    			return "";
    		String str = strs[0];
    		int mark = 0;
    		int count=str.length();
    		while (mark != strs.length && count>=0) {
    			mark = 0;
    			str=str.substring(0, count--);              //傻逼怎么能String,记住用StringBuilder
    for (int i = 0; i < strs.length; i++) { if (strs[i].startsWith(str)) mark++; } } return str; }

      

  • 相关阅读:
    基本命令
    Unicode Locale
    二进制查看编辑
    java reg
    java util
    js util
    跑到X
    [转]Linux AIO :libaio
    [转]c++ atomic操作
    [转] dpdk笔记
  • 原文地址:https://www.cnblogs.com/kydnn/p/5160478.html
Copyright © 2011-2022 走看看