上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 14 下一页
摘要: Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to ... 阅读全文
posted @ 2014-03-17 21:47 小菜刷题史 阅读(124) 评论(0) 推荐(0) 编辑
摘要: Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in non-descending order. (ie,a≤b≤c)The solution set must not contain duplicate triplets. For example, given array S... 阅读全文
posted @ 2014-03-17 13:45 小菜刷题史 阅读(124) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are 阅读全文
posted @ 2014-03-17 12:36 小菜刷题史 阅读(131) 评论(0) 推荐(0) 编辑
摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity.数据元素无序但要求时间复杂度O(n)应联想到hash.stl中unordere 阅读全文
posted @ 2014-03-16 21:42 小菜刷题史 阅读(179) 评论(0) 推荐(0) 编辑
摘要: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).更一般的问题是查找两个数据的第K小/大元素。除了利用归并过程查找外还有更优的方法:利用二分的思想,充分利用数组已经分别有序的条件,使得每比较一个数,就可以确定该数组中一部分元素是位于第K个位置之前还是之后。因为有两个数组,两者的元素之间大小关系未知,所以应该比较数组的第[k 阅读全文
posted @ 2014-03-16 20:35 小菜刷题史 阅读(131) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array. 1 class Solution { 2 public: 3 bool search(int A[], int n, int target) { 4 int start = 0, en.. 阅读全文
posted @ 2014-03-16 19:20 小菜刷题史 阅读(237) 评论(0) 推荐(0) 编辑
摘要: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array. 1 class Solution { 2 public: 3 阅读全文
posted @ 2014-03-16 18:58 小菜刷题史 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should ret... 阅读全文
posted @ 2014-03-16 16:50 小菜刷题史 阅读(96) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for a... 阅读全文
posted @ 2014-03-16 16:29 小菜刷题史 阅读(148) 评论(0) 推荐(0) 编辑
摘要: 题目来源:http://poj.org/problem?id=1068题目大意: S = s1 s2...s2n 是由n对配对的左右括号组成的串. 可以用两种方式对S进行编码: P-序列: P = p1 p2...pn, pi 表示第 i 个右括号之前的左括号数。 W-序列:W = w1 w2...wn, wi 表示从与第 i 个右括号匹配的左括号位置处开始计到该右括号是第几个右括号。 下面是一个实例: S (((()()()))) P-sequence 4 5 6666 W-sequence 1 1 1456写一个程序将P-序列转换为W-序列。输入:第一行t(1 ... 阅读全文
posted @ 2013-12-11 16:34 小菜刷题史 阅读(237) 评论(0) 推荐(0) 编辑
上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 14 下一页