zoukankan      html  css  js  c++  java
  • [LeetCode] 14

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

    class Solution {
      public:
        string longestCommonPrefix(vector<string>& strs) {
          string res;
          int nums = strs.size();
          if (nums == 0) {
            return res;
          }
          int len = (strs[0]).size();
          for (const string& str : strs) {
          if (str.size() < len) {
            len = str.size();
          }
        }

        for (int i = 0; i < len ; ++i) {
          char c = (strs[0])[i];
          bool same = true;
          for (const string& str : strs ) {
            if (str[i] != c) {
              same = false;
              break;
            }
          }
          if (!same) {
            break;
          }
          res.push_back(c);
        }
        return res;
      }
    };

  • 相关阅读:
    CSS hack
    字符串中常用的方法
    排序算法
    拾遗
    数组类型检测
    数组常用的方法
    go 文件服务器(标准库) 添加关机,睡眠,退出功能
    go cmd 交互 初始化执行某些命令
    go 内网IP及外网IP获取
    go 快排实现
  • 原文地址:https://www.cnblogs.com/shoemaker/p/4765922.html
Copyright © 2011-2022 走看看