摘要: 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 阅读(125) 评论(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 阅读(140) 评论(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 阅读(123) 评论(0) 推荐(0) 编辑