摘要: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n, generate the nth sequence. Note 阅读全文
posted @ 2014-04-05 10:28 beehard 阅读(148) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Solution: Recursion. Pre-order. O(n) 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NU... 阅读全文
posted @ 2014-04-05 10:26 beehard 阅读(153) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Solution: Recursion. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right... 阅读全文
posted @ 2014-04-05 09:49 beehard 阅读(127) 评论(0) 推荐(0) 编辑
摘要: Given a 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 carry = 1; 5 for(int i = digits.size()-1; i >= 0 ... 阅读全文
posted @ 2014-04-05 01:25 beehard 阅读(170) 评论(0) 推荐(0) 编辑
摘要: The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print thesequence of gray code. A gray code sequence must begin with 0.For example, given n = 2, return [0,1,3,2]. Its gray co 阅读全文
posted @ 2014-04-05 01:01 beehard 阅读(136) 评论(0) 推荐(0) 编辑
摘要: Implement pow(x, n).Solution: recursion. 1 class Solution { 2 public: 3 double pow(double x, int n) { 4 if(x < 0) return n % 2 == 0 ? pow(-x, n) : -pow(-x, n); 5 if(x == 0 || x == 1) return x; 6 if(n == 0) return 1.0; 7 if(n < 0) return 1.0/pow(x, -n); 8 ... 阅读全文
posted @ 2014-04-05 01:00 beehard 阅读(106) 评论(0) 推荐(0) 编辑
摘要: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Solution: use or (clean) 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 unordered_map map; 5 map['M'] = 1000; 6 map['D'] = 500; 7 map['C'] = 10... 阅读全文
posted @ 2014-04-05 00:58 beehard 阅读(109) 评论(0) 推荐(0) 编辑
摘要: Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.Solution: Buffer the roman numbers. 1 class Solution { 2 public: 3 string rome[4][10] = {{"", "I", "II", "III", "IV", "V", "VI&qu 阅读全文
posted @ 2014-04-05 00:55 beehard 阅读(125) 评论(0) 推荐(0) 编辑
摘要: Divide two integers without using multiplication, division and mod operator. 1 class Solution { 2 public: 3 int divide(int dividend, int divisor) { 4 assert(divisor != 0); 5 int res = 0; 6 bool flag = (dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0); 7 ... 阅读全文
posted @ 2014-04-05 00:54 beehard 阅读(121) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appears three times except for one.Find that single one.Your algorithm should have a linear runtime complexity. Could you implement itwithout using extra memory?Solution: Count the number of each bit. 1 class Solution { 2 public: 3 // assume that integer... 阅读全文
posted @ 2014-04-05 00:51 beehard 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appears twice except for one.Find that single one.Your algorithm should have a linear runtime complexity.Could you implement it without using extra memory?Solution: XOR. 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int sing... 阅读全文
posted @ 2014-04-05 00:49 beehard 阅读(112) 评论(0) 推荐(0) 编辑
摘要: Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1) (No!)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have 阅读全文
posted @ 2014-04-05 00:48 beehard 阅读(143) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3].Solution:Two pointers ('last' and 'lastlast'). 1 class Solution { 2 public: 3 int 阅读全文
posted @ 2014-04-05 00:43 beehard 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 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.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A = [2,3,1,1,4]The minimum 阅读全文
posted @ 2014-04-05 00:41 beehard 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 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], return true.A = [3,2,1,0,4], return fals 阅读全文
posted @ 2014-04-05 00:39 beehard 阅读(134) 评论(0) 推荐(0) 编辑
摘要: Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B.The number of elements initialized in A and B are m and n respectively.Solution: From back to forth. 1 class Solution { 2 public: 3 void merg... 阅读全文
posted @ 2014-04-05 00:38 beehard 阅读(151) 评论(0) 推荐(0) 编辑
摘要: 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.Solution: Refactor: Update solution. Use two pointers. 1 class Solution { 2 public: 3 int removeElement(i 阅读全文
posted @ 2014-04-05 00:37 beehard 阅读(104) 评论(0) 推荐(0) 编辑
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.Solution: Although we can only use constant space, we can still exchange elements within input A!Swap ele 阅读全文
posted @ 2014-04-05 00:36 beehard 阅读(129) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear only once and 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 阅读全文
posted @ 2014-04-05 00:28 beehard 阅读(141) 评论(0) 推荐(0) 编辑
摘要: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).n vertical lines are drawn such that the two endpoints of line i is 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.N 阅读全文
posted @ 2014-04-05 00:27 beehard 阅读(149) 评论(0) 推荐(0) 编辑