zoukankan      html  css  js  c++  java
  • [面试真题] LeetCode:Longest Common Prefix

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

     1 class Solution {
     2 public:
     3     string longestCommonPrefix(vector<string> &strs) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         stringstream rtn;
     7         int index = 0;
     8         if(strs.size() == 0){
     9             return rtn.str();
    10         }
    11         if(strs.size() == 1){
    12             return strs[0];
    13         }
    14         while(true){
    15             int i = 0;
    16             char ch = strs[i][index];
    17             if(ch != '\0'){
    18                 for(; i<strs.size(); i++){
    19                     if(ch != strs[i][index]){
    20                         return rtn.str();
    21                     }
    22                 } 
    23                 rtn << ch;
    24                 index++;
    25             }else{
    26                 return rtn.str();
    27             }
    28         }
    29         return rtn.str();
    30     }
    31 };

    Run Status: Accepted!
    Program Runtime: 28 milli secs

    Progress: 117/117 test cases passed.
  • 相关阅读:
    CentOS-Docker安装RabbitMQ集群(rabbitmq:3.7.16-management)
    Xcode Shortcuts
    In App Purchase
    CoreData
    Sandbox 文件存放规则
    在 mac os 上搭建 git server
    Git
    Git and Xcode
    心算技巧
    AppleScript
  • 原文地址:https://www.cnblogs.com/infinityu/p/3076311.html
Copyright © 2011-2022 走看看