摘要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return [1,2,3,6,9,8,7,4,5].class Solution {public: vector spiralOrder(vector > &matrix) { // No... 阅读全文
posted @ 2013-10-05 23:11 懒猫欣 阅读(164) 评论(0) 推荐(0)
摘要: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]n,n-1,n-1,n-2,n-2......,2,2,1,1class Solution {public: vector > generateMatrix(int n) { // ... 阅读全文
posted @ 2013-10-05 22:55 懒猫欣 阅读(140) 评论(0) 推荐(0)
摘要: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as:[ ... 阅读全文
posted @ 2013-10-05 22:37 懒猫欣 阅读(212) 评论(0) 推荐(0)
摘要: Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.class Solution {public: void Plus(string& a,string& b,string& temp){ int small=min(a.length(),b.length()); bool flag=false; ... 阅读全文
posted @ 2013-10-05 21:58 懒猫欣 阅读(161) 评论(0) 推荐(0)
摘要: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.class Solution {public: RandomListNode *copyRandomList(RandomListNode *head) { // Note: The Solution object is instantiated only... 阅读全文
posted @ 2013-10-05 19:23 懒猫欣 阅读(328) 评论(0) 推荐(0)
摘要: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?class Solution {public: vector getRow(int rowIndex) { // Note: The Solution object is instantiated only once and is r... 阅读全文
posted @ 2013-10-05 18:46 懒猫欣 阅读(119) 评论(0) 推荐(0)
摘要: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]class Solution {public: vector > generate(int numRows) { // Note: The Solution object is instantiated only once and is reused by each... 阅读全文
posted @ 2013-10-05 18:35 懒猫欣 阅读(111) 评论(0) 推荐(0)
摘要: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, .. 阅读全文
posted @ 2013-10-05 18:27 懒猫欣 阅读(173) 评论(0) 推荐(0)
摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].第一个版本,加了一个set去除重复class Solution {public: void sub(vector > & ret,vector& num,int index){ if(index= 阅读全文
posted @ 2013-10-05 18:14 懒猫欣 阅读(167) 评论(0) 推荐(0)
摘要: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].class Solution {public: void sub(vector > & ret,vector& num,int index){ if(index==num.size()){ ret.push_b... 阅读全文
posted @ 2013-10-05 17:41 懒猫欣 阅读(161) 评论(0) 推荐(0)
摘要: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2-&g 阅读全文
posted @ 2013-10-05 17:21 懒猫欣 阅读(161) 评论(0) 推荐(0)
摘要: You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:S: "barfoothefoobarman"L: ["foo", & 阅读全文
posted @ 2013-10-05 01:52 懒猫欣 阅读(165) 评论(0) 推荐(0)