上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 34 下一页
摘要: Given a matrix ofmxnelements (mrows,ncolumns), 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) { vector ans; ... 阅读全文
posted @ 2013-12-15 14:03 七年之后 阅读(163) 评论(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.思考:最大字串和。class Solution {public: int maxSubArray(int A[], int n) { int last=A[... 阅读全文
posted @ 2013-12-15 00:13 七年之后 阅读(179) 评论(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?思考:两步,先对角线交换再上下交换。class Solution {public: void rotate(vector > &matrix) { int n=matrix.size(); int i,j; for(i=0;i<n;i++) { for(... 阅读全文
posted @ 2013-12-13 20:46 七年之后 阅读(176) 评论(0) 推荐(0) 编辑
该文被密码保护。 阅读全文
posted @ 2013-12-13 18:43 七年之后 阅读(9) 评论(0) 推荐(0) 编辑
摘要: Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.Fo... 阅读全文
posted @ 2013-12-12 22:31 七年之后 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思考:不使用额外空间的一种途径就是利用已存在的空间。此题数组中的数放到A数组中对应的位置。class Solution {public: int firstMissingPositive(int A[], int n)... 阅读全文
posted @ 2013-12-12 18:47 七年之后 阅读(188) 评论(0) 推荐(0) 编辑
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak) must 阅读全文
posted @ 2013-12-12 17:18 七年之后 阅读(159) 评论(0) 推荐(0) 编辑
摘要: Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak 阅读全文
posted @ 2013-12-12 16:12 七年之后 阅读(204) 评论(0) 推荐(0) 编辑
摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode... 阅读全文
posted @ 2013-12-12 15:25 七年之后 阅读(163) 评论(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-12-12 10:56 七年之后 阅读(208) 评论(0) 推荐(0) 编辑
上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 34 下一页