摘要: Roman to IntegerGiven a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Traverse given string, if next roman value is smaller than the previous one, just add it to result.If next roman value is larger than previous one, we need to do a minus. 1 class 阅读全文
posted @ 2013-10-23 11:30 昱铭 阅读(192) 评论(0) 推荐(0)
摘要: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文
posted @ 2013-10-22 20:53 昱铭 阅读(175) 评论(0) 推荐(0)
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.============================================================================================If you would have to choose an array element to be the root of a balanced BST, which element would you pick? Th 阅读全文
posted @ 2013-10-22 17:45 昱铭 阅读(164) 评论(0) 推荐(0)
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文
posted @ 2013-10-22 17:23 昱铭 阅读(113) 评论(0) 推荐(0)
摘要: Remove ElementGiven 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 // ... 阅读全文
posted @ 2013-10-21 16:21 昱铭 阅读(126) 评论(0) 推荐(0)
摘要: Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.====================================== 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 // Note: The Solution object is instantiated only once and is reused by each test... 阅读全文
posted @ 2013-10-21 16:17 昱铭 阅读(135) 评论(0) 推荐(0)
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N... 阅读全文
posted @ 2013-10-16 16:56 昱铭 阅读(152) 评论(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 aremandnrespectively. 1 class Solution { 2 public: 3 void merge(int A[], int m, int B[], int n... 阅读全文
posted @ 2013-10-16 16:29 昱铭 阅读(158) 评论(0) 推荐(0)
摘要: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.====================================================================================One thing to mark. The expression below is wrong,1 cur_node = new NodeList( 阅读全文
posted @ 2013-10-16 16:20 昱铭 阅读(122) 评论(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-10-15 11:53 昱铭 阅读(121) 评论(0) 推荐(0)