一次总结两道题,两道题目都比较基础

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

分析: 这道题目最重要的知道什么叫prefix前缀, 否则一不小心就做成了最长子序列。前缀就是两个序列的前多少位

都要是一样的,不能跳着对齐,这样就比较简单了,也可以求很多个序列的公共前缀。

 class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
string result,comm;
if(strs.empty()) return result;
result= strs[];
for(int i=;i<strs.size();i++)
{
comm = "";
for(int j=;j<result.size();j++)
{
if(j>=strs[i].size()) break;
if(result[j]==strs[i][j]) comm.insert(comm.end(),result[j]);
else break;
}
result = comm;
} return result;
}
};

题目二:Search for a range

Description: Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

分析: 要找一个排序数组的某个元素的范围,要求复杂度是O(log n),则肯定是基于二分查找。因为有重复元素,则将二分查找

的边界条件变一下就可以了。

 class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> pos (,-) ;
if(n<=) return pos;
//left bound
int start=,stop = n-,mid;
while(start<=stop)
{
mid = (start+stop)/;
if(A[mid]==target && (mid== || A[mid-]<target))
{
pos[] = mid;
break;
}
else if(A[mid]<target)
{
start = mid+;
}
else{
stop = mid-;
}
}
if(pos[]==-) return pos;
//right bound
start=,stop = n-;
while(start<=stop)
{
mid = (start+stop)/;
if(A[mid]==target && (mid==n- || A[mid+]>target) )
{
pos[] = mid;
break;
}
else if(A[mid]>target)
{
stop = mid-;
}
else{
start = mid+;
}
}
return pos;
}
};

Leetcode::Longest Common Prefix && Search for a Range的更多相关文章

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

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  2. Leetcode Longest Common Prefix

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

  3. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  4. [LeetCode] Longest Common Prefix 字符串公有前序

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

  5. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  6. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  7. leetcode Longest Common Prefix python

    class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...

  8. leetcode:Longest Common Prefix【Python版】

    1.当strs为空,直接输出“” 2.当strs中含有“”,直接输出“” 3.strs[0]的最长长度由最短公共长度l决定(code line:15) class Solution: # @retur ...

  9. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

随机推荐

  1. C#-面向对象的多态思想 ---ShinePans

    总结: 多态是面向对象的核心.---------能够理解为一个方法,多种实现, 在这里能够用虚方法,抽象类,接口能够实现多态 1.首先利用接口来实现多态: 接口相当于"功能,"接口 ...

  2. ExtJs--15--Ext.is*各种类型推断的方法,简单看源代码就能够明确了

    /** * Returns true if the passed value is empty, false otherwise. The value is deemed to be empty if ...

  3. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  4. ie6、ie7真的不支持inline-block吗?

    本来今天想出JavaScript继承的博文的,由于也才刚学习不久,以及工作比较忙,所以暂推两天写JavaScript的继承,喜欢的童鞋们关注我的博客哟! okay,言归正传.之前在接触前端的时候,处理 ...

  5. Nodejs使用coffeescript编写的用户注册/登陆代码(MySQL)

    记录一下,以备后用 Settings = require '../../settings.js' exports.register = (req, res) -> nick_name = req ...

  6. Java多线程之进程和线程

    在并发编程中有两个基本的概率就是进程和线程.在Java编程中并发编程更多的是关注线程.但是进程也是很重要的. 一个计算机一般会有很多活跃的进程和线程.有一点是没有疑问的在单核系统中,任何时候实际上都是 ...

  7. 2.3 LINQ查询表达式中 使用select子句 指定目标数据

    本篇讲解LINQ查询的三种形式: 查询对象 自定义查询对象某个属性 查询匿名类型结果 [1.查询结果返回集合元素] 在LINQ查询中,select子句和from子句都是必备子句.LINQ查询表达式必须 ...

  8. lua及luci学习

    由于项目需要对Luci进行修改,所以这里开始地luci进行较深入的研究. 探索其中的运行路径. Openwrt默认的HTTP服务器为uhttpd,该WEB服务器是由Luci的开发者自行开发的,非常小巧 ...

  9. leetcode第29题--Substring with Concatenation of All Words

    problem: You are given a string, S, and a list of words, L, that are all of the same length. Find al ...

  10. 确保Zend Studio最佳性能的10点建议

    作为一个PHP开发人员,你需要知道使用Zend Studio时,什么应该做,什么要避免.就像Roy Ganor说的那样“你必须掌握的你IDE”.从IDE角度来看,建立PHP项目时,了解Zend Stu ...