摘要: You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Code:class Solution {public: void rotate(vector > &matrix) { int n=matrix.size(); for(int i=0;i<n/2;i++){ for(int j=i;j<n-i-1;j++){ ... 阅读全文
posted @ 2013-11-14 14:54 WinsCoder 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"Code:class Solution {public: void generate(int l, int r, st 阅读全文
posted @ 2013-11-14 12:53 WinsCoder 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Given a number represented as an array of digits, plus one to the number.Code:class Solution {public: vector plusOne(vector &digits) { int n=digits.size(); bool carry=true; for(int i=n-1;i>-1;i--){ if(digits[i]==9&&carry==1){ digits[i]=0; ... 阅读全文
posted @ 2013-11-14 08:38 WinsCoder 阅读(132) 评论(0) 推荐(0) 编辑
摘要: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.Code:class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { if(p&&q){ if(p->val!=q->... 阅读全文
posted @ 2013-11-14 08:05 WinsCoder 阅读(168) 评论(0) 推荐(0) 编辑
摘要: Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.Code:class Solution {public: int maxSubArray(int A[], int n) { int maxSub=0; ... 阅读全文
posted @ 2013-11-14 06:10 WinsCoder 阅读(141) 评论(0) 推荐(0) 编辑
摘要: Sort a linked list using insertion sort.Code:class Solution {public: ListNode *insertionSortList(ListNode *head) { if(head==NULL) return head; ListNode *start=new ListNode(0); start->next=head; ListNode *pre=head; ListNode *cur=head->next; ListNode *nex; ... 阅读全文
posted @ 2013-11-14 06:06 WinsCoder 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not 阅读全文
posted @ 2013-11-13 17:45 WinsCoder 阅读(167) 评论(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.Code:class Solution {public: int removeElement(int A[], int n, int elem) { int j=0; for(int ... 阅读全文
posted @ 2013-11-08 18:15 WinsCoder 阅读(115) 评论(0) 推荐(0) 编辑
摘要: We know the elements can be printed post-order easily using recursion,as follow:void postOrderTraversal(BinaryTree *p) { if (!p) return; postOrderTraversal(p->left); postOrderTraversal(p->right); cout data;}This is the most difficult of all types of iterative tree traversals. You should attemp 阅读全文
posted @ 2013-11-08 17:52 WinsCoder 阅读(485) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?Code:class Solution {public: void findNode(vector &nodes,TreeNode *node){ if(n... 阅读全文
posted @ 2013-11-08 17:41 WinsCoder 阅读(116) 评论(0) 推荐(0) 编辑