03 2014 档案
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8 1 /** 2 * Definition
阅读全文
摘要:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100". 1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 string ans; 5 int carry = 0; 6 int sum = 0; 7 int i = a.size() - 1, j = b.size() - 1; 8 ...
阅读全文
摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int * recor...
阅读全文
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int ans = 0; 5 ...
阅读全文
摘要:There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors.What is the minimum candies you mu
阅读全文
摘要:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gray code s
阅读全文
摘要:You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?到达 n 有两种途径:从 n - 1 走一步 or 从 n - 2 走两步.所以 f(n) = f(n - 1) + f (n - 2), 即斐波那契数列。可以迭代求解或者直接用通项公式。 1 class Solution { 2 public: 3 int clim...
阅读全文
摘要:Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list. 1 class Solution { 2 public: 3 vector plusOne(vector &digits) { 4 int d = digits.size(); 5 int sum = 0, car...
阅读全文
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place? 1 class Solution { 2 public: 3 void rotate(vector > &matrix) { 4 int n = matrix.size(); 5 for(int i = 0; i < n / 2; ++i) { 6 for(int j ...
阅读全文
摘要:Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3].The largest rectangle is shown in the shaded area, which has ar
阅读全文
摘要:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 1 class S
阅读全文
摘要:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of
阅读全文
摘要:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.A partially filled sudoku which is valid.Note:A valid Sudoku board (partially filled) is not necessarily solvable. Only the f
阅读全文
摘要:Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] 1 class Solution { 2 public: 3 vector > combine(int n, int k) { 4 vector> result; 5 vector used(n + 1, f...
阅读全文
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1]. 1 class Solution { 2 public: 3 vector > permuteUnique(vector &num) { 4 vector > result; 5 so...
阅读全文
摘要:Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1]. 1 class Solution { 2 public: 3 vector > permute(vector &num) { 4 vector > result; 5 sort(num.begin(), num.end())...
阅读全文
摘要:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not
阅读全文
摘要:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He
阅读全文
摘要:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length. 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 int i = 0; 5 ...
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie,a≤b≤c≤d)The solution set must not contain duplicate quadruplets. .
阅读全文
摘要: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 ...
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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..
阅读全文
摘要: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
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文

浙公网安备 33010602011771号