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 ...
随机推荐
- android95 缩放加载大图片
MainActivity: package com.itheima.loadimage; import android.os.Bundle; import android.app.Activity; ...
- mysql 函数在源码中的定义
大牛那海蓝蓝 MySQL提供了较为丰富的SQL语句,用以支持MySQL提供的主要功能.在数据库内部,MySQL又是怎么知道自己能够处理哪些对象.处理哪些事情的? 如果我们输入一条SQL语句,MySQL ...
- linux find命令详解--转
转自:http://blog.csdn.net/jakee304/article/details/1792830 (一)Get Start 最简单的find用法莫过于如此: $ find . 查找当前 ...
- Javascript数组,String对象,Math对象,Date对象,正则表达式
标题栏的滚动<html><head><title>山西众创金融</title></head>function init(){ //1.拿到标 ...
- 多版本uboot命令行分析
1.1.6 经典版本: 1.uboot第二阶段第一个函数void start_armboot (void),一路gd参数设置.设备初始化.控制台初始化.端口初始化,最后到main_loop ()命令行 ...
- css所有选择器的详解
----------------------------------------css 选择器---------------------------------------- 1,组合选择器: 1)e ...
- Visual C++ 打印编程技术-内存设备环境
1.内存设备环境 内存设备环境是一个没有设备与它联系的环境.一般利用与某个标准设备环境兼容的内存设备环境把一个位图复制到屏幕上去.为此可以先创建一个与某个标准设备环境兼容的内存设备环境,然后把所要显示 ...
- mediawiki数据库的下载地址及导入方法
mediawiki导入数据库 数据库下载:http://zh.wikipedia.org/wiki/Wikipedia:%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%8B%E8% ...
- swift-07-使用for-in 遍历数组
//for-in /* for 迭代变量 in集合变量 { 使用迭代变量便利所有数据 } */ //遍历数组 var arr = ["a" ,"b" ,&quo ...
- 反射 介绍System.Type类
本节先介绍system.Type类,通过这个类可以访问关于任何数据类型的信息. 1. system.Type类以前把Type看作一个类,但它实际上是一个抽象的基类.只要实例化了一个Type对象,实际上 ...