Longest common prefix | leetcode
Write a function to find the longest common prefix string amongst an array of strings.
思路:要去是寻找字符串vector里最长的公有前缀。
1。构造迭代器,遍历vector搜索最小string长度。然后从第一个字符开始进行遍历,如果遇到不一样的字符即返回,全部遍历完毕没问题就把它添加到前缀字符串里。
这样做效率比较差,有待改进。
class Solution { public: string longestCommonPrefix(vector<string>& strs) { unsigned minLen = 0xffffffff; unsigned i = ; string res = ""; vector<string>::iterator it; for (it = strs.begin();it != strs.end(); it++) { cout << (*it).size(); minLen = ((*it).size() < minLen) ? ((*it).size()) : minLen; } // cout << "minlen is " << minLen << endl; if ((!minLen) || (minLen == 0xffffffff)) return ""; // cout << "minlen is " << minLen << endl; while (i != minLen) { char temp; bool flag = false; it = strs.begin(); temp = (*it)[i]; for (it = strs.begin(); it != strs.end(); it++) { if (temp != (*it)[i]) return res; } res += temp; i ++; } return res; } };
Longest common prefix | leetcode的更多相关文章
- Longest Common Prefix [LeetCode 14]
1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...
- Longest Common Prefix leetcode java
题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
随机推荐
- [HTML5] Accessible Icon Buttons
Icon buttons are very common in web applications, yet they often have accessibility problems. Learn ...
- mysql优化之查询优化
Posted by Money Talks on 2012/02/24 | 第一篇 序章第二篇 连接优化第三篇 索引优化第四片 查询优化第五篇 到实战中去 查询优化 查询优化涉及到用户查询数据时使用到 ...
- C# 以ThreadStart方式实现多线程
3.1 使用ThreadStart委托 这 里先以一个例子体现一下多线程带来的好处,首先在Message类中建立一个方法ShowMessage(),里面显示了当前运行线程的Id,并使用 Threa ...
- linux mysql 卸载后重装
$sudo apt-get remove mysql-common清理残留数据:$sudo dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P ...
- QT软键盘
如何实现鼠标单击弹出软键盘 默认情况下,如果当前编辑框无焦点,则需要鼠标点击两次才弹出软键盘,其中第一次是让该编辑框获得焦点,第二次点击才弹出软键盘: 如果当前编辑框已经获得焦点,则点击一次就会弹出软 ...
- schedule
cocos2d-x(时间调度) 在游戏中,时常需要隔一段时间更新一些数据或者是人物位置,Cocos2D-x 中提供了这些时间调度的函数,所有CCNode 类的子类都有这样的函数. schedule(s ...
- Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流)
1.File类:对硬盘上的文件和目录进行操作的类. File类是文件和目录路径名抽象表现形式 构造函数: 1) File(String pathname) Creat ...
- tcl/tk实例详解——返回一个文件夹下所有文件的绝对路径
http://blog.csdn.net/dulixin/article/details/2133840 #所有代码如下,使用注释的方式讲解脚本#修改好文件夹和保存结果路径,可以把本文件直接拷贝进tc ...
- <!--[if lt IE]>
代码如下时 <!--[if lt IE9]> <script src="js/html5shiv.js"></script> <![end ...
- ffmpeg之移植到ARM
移植方法分为两种:第一种手工移植,第二种buildroot移植. 第一种手工移植: 优点:灵活性高 缺点:重复工作多 一.配置 ./configure --enable-memalign-hack - ...