摘要: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? Solution: 1. Rotate one-fourth of the image clockwise. 2. 123 -> 147 -> 741 (preferable) 456 258 852 7... 阅读全文
posted @ 2014-04-06 05:46 beehard 阅读(193) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? Solution: 1. Iterative way (stack). Time: O(n), Space: O(n). 2. Recursive sol... 阅读全文
posted @ 2014-04-06 05:25 beehard 阅读(115) 评论(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. Solution: dp. 1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) ... 阅读全文
posted @ 2014-04-06 04:41 beehard 阅读(100) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Solution: 1. Recursion. Pay attention to cases when the non-leaf node has only one child. 2. Iteration + Queue. 1 /** 2 * Defin... 阅读全文
posted @ 2014-04-06 04:36 beehard 阅读(115) 评论(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: [ . 阅读全文
posted @ 2014-04-06 04:36 beehard 阅读(159) 评论(0) 推荐(0) 编辑