摘要: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3.class Solution {public: ListNode *deleteDuplicates(Li 阅读全文
posted @ 2013-05-01 22:08 caijinlong 阅读(114) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3. 1 class Solution { 2 public: 3 ListNode *deleteDuplicates(ListNode *head) { 4 // Start typing your C/C++ solutio 阅读全文
posted @ 2013-05-01 22:07 caijinlong 阅读(168) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int removeDuplicates(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if (n <= 2) return n; int cur = 1; for (int i = 2; i < n; ++i) { if (!(A[i] == A[cur] && A[i] == A[cur - 1])) ... 阅读全文
posted @ 2013-05-01 21:41 caijinlong 阅读(112) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 int removeDuplicates(int arr[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if(n<=1) 7 return n; 8 int insertPos = 0; 9 for (int i = 1; i < n; i++)10 {11 if (arr[i] != arr[insertPos])12 arr[++in... 阅读全文
posted @ 2013-05-01 21:23 caijinlong 阅读(101) 评论(0) 推荐(0) 编辑
摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2013-05-01 21:12 caijinlong 阅读(123) 评论(0) 推荐(0) 编辑
摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文
posted @ 2013-05-01 20:31 caijinlong 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 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(NULL) {} 8 * }; 9 ... 阅读全文
posted @ 2013-05-01 16:30 caijinlong 阅读(123) 评论(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-05-01 16:16 caijinlong 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Best Time to Buy and Sell Stock ISay you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 1 int maxProfit(vector< 阅读全文
posted @ 2013-05-01 14:28 caijinlong 阅读(3047) 评论(0) 推荐(0) 编辑