两年硕士超快的鸭,又要准备秋招啦!0508第一题~

题目

Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string “”.

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:

All given inputs are in lowercase letters a-z.

太久没用C++,一上来除了找了好久的格式也只剩下暴力求解:

1. 判断是不是空数组;
2. 判断是不是只有一个字符串;
3. 判断第一个字符串是不是长度为0;
4. 暴力循环,获取第一个字符串的第一个字符,依次与其他字符串的同个字符进行比较,如果不相等跳出第二层循环,返还当前的共同前缀。
class Solution {
public:string longestCommonPrefix(vector<string>& strs) {int N = strs.size();if(N == 0)return "";if(N == 1)return strs[0];if(strs[0].length() == 0 ){return "";}string s;int count = 0;int tmp = 0;for(int i=0; i<strs[0].length(); i++){tmp = 0;for(int k = 1; k<strs.size(); k++){if(strs[0][i] != strs[k][i]){tmp=1;break;}}if(tmp==1){return s;}else{s.push_back(strs[0][i]);// cout<<s;}}return s;}
};

写着写着好像觉得可以再改进改进,我去试试~

刚刚想到的,除了看起来简洁些,内存消耗还是没改进。

class Solution {
public:string longestCommonPrefix(vector<string>& strs) {int N = strs.size();if(N == 0)return "";if(N == 1)return strs[0]; if(strs[0].length() == 0 ){return "";}string s;int count = 0;int tmp = 0;for(int i=0; i<strs[0].length(); i++){tmp = 0;for(int k = 1; k<strs.size(); k++){if(strs[0][i] != strs[k][i]){tmp=1;return s;}}s.push_back(strs[0][i]);}return s;}
};

再去看看大神的
看了看题解,跟不上了,就这样吧

LeetCode:14. Longest Common Prefix相关推荐

  1. 【easy!】LeetCode 14. Longest Common Prefix

    LeetCode 14. Longest Common Prefix Solution1: 用的暴力遍历,时间复杂度O(n2)O(n2)O(n^2) class Solution { public:s ...

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

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

  3. LeetCode: 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 大意就是,写一个函数可以找 ...

  4. LeetCode 14. Longest Common Prefix

    题意:给出n个字符串,求其公共子串 思路:两两求子串,LCP(S1,S2,....) = LCP(S1, LCP(S2,....)) 代码 如下: func longestCommonPrefix(s ...

  5. LeetCode - Easy - 14. Longest Common Prefix

    Topic String Description https://leetcode.com/problems/longest-common-prefix/ Write a function to fi ...

  6. leetcode python3 简单题14. Longest Common Prefix

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十四题 (1)题目 英文: Write a function to find th ...

  7. LeetCode之Longest Common Prefix

    1.题目 Write a function to find the longest common prefix string amongst an array of strings 2.代码实现 pa ...

  8. Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]

    题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...

  9. 14. Longest Common Prefix

    Title 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Solve 1.横向扫描 用LCP(S1 -Sn)表示字符串S1 -Sn的最长 ...

最新文章

  1. 第二天:Vue基础语法
  2. 分割命令: split
  3. QNX设置开机启动命令来修改IP地址
  4. Web API-随机性案例步骤
  5. 如何从Java类创建JAR
  6. Windows 2008系统关闭端口
  7. DEAP2.1 使用方法(运筹学)
  8. 威纶通触摸屏制作自定义欢迎界面的几种方法介绍
  9. nanomsg笔记--通信协议与传输协议
  10. 树莓派接入USB摄像头
  11. 接口测试如何生成随机的参数值
  12. 线程安全问题?怎么解决线程安全
  13. flash制作文字笔顺_汉字标准读音与笔顺Flash版
  14. 北风:二类电商“空手套白狼”的赚钱套路
  15. 面试经验总结——测试岗
  16. 智能问答技术概览及在小爱同学的实践
  17. 5.网页中增加新的内容
  18. 笔记本联想小新Air14重装win10后触摸板失灵解决方案
  19. android监控io产生的应用,Android IO性能分析及排查
  20. 2021汽车行业内容营销白皮书

热门文章

  1. Kali linux 2016.2(Rolling)里Metasploit的OpenVAS
  2. fastReport 随记
  3. Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
  4. [leetcode]Multiply Strings @ Python
  5. js如何获得FCKeditor控件的值
  6. 2011年8月51CTO壁纸点评活动获奖名单【已结束】
  7. 根据年月来判断月里天数
  8. 光棍节,猪我生日快乐!
  9. Asp.Net页面输出到EXCEL
  10. 用C语言解“用天平找小球”题