摘要: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of inte 阅读全文
posted @ 2013-11-05 14:00 假日笛声 阅读(566) 评论(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 another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2013-11-05 13:27 假日笛声 阅读(118) 评论(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.Summary:Almost the same toSearch in Rotated Sorted Array without duplicates, but careful about 阅读全文
posted @ 2013-11-05 13:02 假日笛声 阅读(552) 评论(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.Summary:Binary search can apply 阅读全文
posted @ 2013-11-05 12:24 假日笛声 阅读(347) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Summary: pretty easy, recursive staff. 1 class Solution { 2 public: 3 TreeNode *sortedArrayToBST(vector &num) { 4 int size = num.size(); 5 if(size == 0) 6 return NULL; 7 ... 阅读全文
posted @ 2013-11-03 06:51 假日笛声 阅读(193) 评论(0) 推荐(0) 编辑
摘要: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer's last digit is 0, what should 阅读全文
posted @ 2013-11-03 06:11 假日笛声 阅读(149) 评论(0) 推荐(0) 编辑
摘要: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Summary: When meeting C/X/I, remembers to search forward to check if there is a bigger number at the front. 1 int romanToInt(string s) { 2 if(s.size() == 0) 3 return 0; 4... 阅读全文
posted @ 2013-11-02 13:37 假日笛声 阅读(403) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文
posted @ 2013-11-02 12:38 假日笛声 阅读(206) 评论(0) 推荐(0) 编辑
摘要: A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni 阅读全文
posted @ 2013-11-02 12:16 假日笛声 阅读(197) 评论(0) 推荐(0) 编辑
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).Note:Bonus point if you are a... 阅读全文
posted @ 2013-10-31 13:37 假日笛声 阅读(186) 评论(0) 推荐(0) 编辑