摘要: 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 阅读(169) 评论(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) 编辑