上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 16 下一页
摘要: 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) 编辑
摘要: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n, generate the nth sequence. Note 阅读全文
posted @ 2014-04-05 10:28 beehard 阅读(148) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Solution: Recursion. Pre-order. O(n) 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NU... 阅读全文
posted @ 2014-04-05 10:26 beehard 阅读(153) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Solution: Recursion. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right... 阅读全文
posted @ 2014-04-05 09:49 beehard 阅读(127) 评论(0) 推荐(0) 编辑
摘要: Given a number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 1 class Solution { 2 public: 3 vector plusOne(vector &digits) { 4 int carry = 1; 5 for(int i = digits.size()-1; i >= 0 ... 阅读全文
posted @ 2014-04-05 01:25 beehard 阅读(170) 评论(0) 推荐(0) 编辑
摘要: The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print thesequence of gray code. A gray code sequence must begin with 0.For example, given n = 2, return [0,1,3,2]. Its gray co 阅读全文
posted @ 2014-04-05 01:01 beehard 阅读(136) 评论(0) 推荐(0) 编辑
上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 16 下一页