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

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. 绑定枚举到dropdownlist

    pageTools.BindEnumToDropdownList(typeof(enumDealerArea), ddlBmwArea, new ListItem("--请选择--" ...

  2. HDOJ 5017 Ellipsoid

    第一次尝试模拟退火..... Ellipsoid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  3. Spring + Spring MVC + Hibernate

    Spring + Spring MVC + Hibernate项目开发集成(注解) Posted on 2015-05-09 11:58 沐浴未来的我和你 阅读(307) 评论(0) 编辑 收藏 在自 ...

  4. 使用javascript实现的一些功能

    原文:使用javascript实现的一些功能 今天学习了javascript中的事件,已经接近尾声,可以说明天跨入jquery的学习啦,学习了一周的javascript,感觉还没有掌握其中学习的微妙之 ...

  5. 解析http302重定向url

    bool urlparse(const u_char* data,u_int len) { ip_header *ih; udp_header *uh; tcp_header *th; u_short ...

  6. Oracle Data Provider for .NET now on NuGet

    Oracle Data Provider for .NET now on NuGet 时间 2015-03-02 22:30:00  Oracle Bloggers原文  http://cshay.b ...

  7. 修改vim/terminal配色

    http://blog.csdn.net/angle_birds/article/details/11694325

  8. Thrift官方安装手冊(译)

    本篇是Thrift官网安装文档的翻译,原地址点击这里.Thrift之前是不支持Windows的.可是似乎0.9版本号以后已经支持Window了.介绍了Thrift安装的环境要求以及在centos,De ...

  9. jquery抖动的按钮

    http://runjs.cn/detail/tyx8dbag //shakenum:抖动的次数,shakeDistance:抖动的距离 jQuery.fn.Shake = function (sha ...

  10. js面向对象学习总结

    1.函数作为参数进行传递 function a(str,fun){ console.log(fun(str)) }; function up(str){ return str.toUpperCase( ...