摘要:
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... 阅读全文