摘要: http://www.inf.ed.ac.uk/teaching/courses/iaml/ 阅读全文
posted @ 2013-05-04 20:34 caijinlong 阅读(99) 评论(0) 推荐(0) 编辑
摘要: 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<vector<int> > &matrix) 4 { 5 int n = matrix.size(); 6 for(int i = 0; i < n/2; ++i) 7 for(int j ... 阅读全文
posted @ 2013-05-02 14:36 caijinlong 阅读(114) 评论(0) 推荐(0) 编辑
摘要: Recursive solution is easier to understand. It uses the divide-and-conquer approach, which also runs intime. The formula is shown below:And the code is quite simple and straightforward.code: 1 class Solution { 2 public: 3 double pow(double x, int n) { 4 // Start typing your C/C++ solutio... 阅读全文
posted @ 2013-05-02 14:29 caijinlong 阅读(130) 评论(0) 推荐(0) 编辑
摘要: Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : va 阅读全文
posted @ 2013-05-02 14:12 caijinlong 阅读(134) 评论(0) 推荐(0) 编辑
摘要: Question:Given a number represented as an array of digits, plus one to the number.My Original solver: 1 class Solution { 2 public: 3 vector<int> plusOne(vector<int> &digits) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (digits.s... 阅读全文
posted @ 2013-05-02 13:26 caijinlong 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 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?This is a classic Dynamic Programming problem.Define:f(n)= number of ways you can climb to the nth step.To reach to thenthstep, you have o 阅读全文
posted @ 2013-05-02 12:42 caijinlong 阅读(142) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri 阅读全文
posted @ 2013-05-02 10:16 caijinlong 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 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) 编辑