https://oj.leetcode.com/problems/longest-common-prefix/

在多个string的集合中,找出所有string的最长公共前缀。

从头开始 index 如果 所有string的 index位都相等,则index ++

string是一个类,如果是 空string则为 "",即可以 string str; string str = ""; str = ""; 不可以 str = NULL, NULL 是0,表示一个整数,不能赋值给类。

  1. class Solution {
  2. public:
  3. string longestCommonPrefix(vector<string> &strs) {
  4. int len = strs.size();
  5. if(len == )
  6. return "";
  7. if(len == )
  8. return strs[];
  9.  
  10. bool flag = false;
  11.  
  12. int index = ;
  13. while(flag == false)
  14. {
  15. for(int i = ; i< len; i++)
  16. {
  17. if(index== strs[i].size() ||strs[i][index] != strs[][index])
  18. {
  19. flag = true;
  20. break;
  21. }
  22. }
  23. index++;
  24. }
  25.  
  26. return strs[].substr(,index - );
  27. }
  28. };

LeetCode OJ-- Longest Common Prefix的更多相关文章

  1. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  2. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  3. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  4. [LeetCode] 14. Longest Common Prefix

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

  5. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  6. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  7. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  8. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  9. LeetCode(54)-Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...

  10. [leetcode]14. Longest Common Prefix 最长公共前缀

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

随机推荐

  1. 1022: [SHOI2008]小约翰的游戏John9(Auti_SG)

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3150  Solved: 2013[Submit] ...

  2. 产生指定时间区间序列、按指定单位变化时间 python实现

    示例1:给定起始日期和结束日期,如何得到中间的时间序列 import datetime def dateRange(beginDate, endDate): dates = [] dt = datet ...

  3. Careercup - Microsoft面试题 - 6751316000899072

    2014-05-12 07:10 题目链接 原题: Write a thread safe data structure such that there could be only one write ...

  4. Django框架学习-01Django介绍

    01-Django介绍 02-HTTP协议介绍 01-Django介绍 1.什么是Web框架? 随着Web最新发展趋势的不断升级,Web项目开发也越来越难,而且需要花费更多的开发时间.所以,Web程序 ...

  5. maven学习(十四)——Eclipse中使用Maven插件

    1.导入Maven项目 File→import

  6. 练习题 - js函数

    代码贴出来 1 function Cat() { 2 getColor = function(){ console.log(1);} 3 return this; 4 } 5 Cat.getColor ...

  7. HDU 4177 模拟时间问题

    Avoiding a disaster Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. 第五篇:python基础_5

    本篇内容 协程函数 递归 二分法 import语句 from...import语句 模块搜索路径 包的导入 软件开发规范 logging模块的使用 一. 协程函数 1.定义 协程函数就是使用了yiel ...

  9. arcgis engine10.1和arcObjects的一些问题

    1.arcengine10.1只支持vs2010 2.10.1以后没有engine runtimes,改成engine了,以前的engine可以理解为Arcobject,就是我们可以只装AO

  10. 洛谷P3832 [NOI2017]蚯蚓排队 【链表 + 字符串hash】

    题目链接 洛谷P3832 题解 字符串哈希然后丢到hash表里边查询即可 因为\(k \le 50\),1.2操作就暴力维护一下 经复杂度分析会发现直接这样暴力维护是对的 一开始自然溢出WA了,还以为 ...