摘要: Q:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.A:首先建立字符到数字的map.其次,分析罗马数字组成的规则,从字符串后面往前看,如果 if(dict[s[i+1]]>dict[s[i]]) sum-=dict[s[i]]; else sum+=dict[s[i]]; int romanToInt(string s) {... 阅读全文
posted @ 2013-09-18 23:00 summer_zhou 阅读(170) 评论(0) 推荐(0) 编辑
摘要: Q: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 ofO(logn).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].A: 二分查找 阅读全文
posted @ 2013-09-18 22:36 summer_zhou 阅读(262) 评论(0) 推荐(0) 编辑
摘要: Q:Divide two integers without using multiplication, division and mod operator.不用乘法,除法,取余实现两个int的相除。A: 二分的应用。如果按divident = divident-divisor,直至divident0&&divisor0)) bNegative = true; long long a = dividend,b=divisor; if(dividend=b) //注意是a>=b { sum = b; cnt = 1; while(sum+sum=b) { sum = b; c 阅读全文
posted @ 2013-09-18 16:56 summer_zhou 阅读(242) 评论(0) 推荐(0) 编辑
摘要: Q:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse. 阅读全文
posted @ 2013-09-18 12:55 summer_zhou 阅读(218) 评论(0) 推荐(0) 编辑
摘要: Q:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5],[6,7], 阅读全文
posted @ 2013-09-18 10:50 summer_zhou 阅读(150) 评论(0) 推荐(0) 编辑