interview preparation
文章平均质量分 77
flowercha
Prepare well, practice more.
展开
-
[leet code]Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./** * Definition for binary tree *原创 2013-12-31 00:27:29 · 574 阅读 · 0 评论 -
[leet code] Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2014-01-02 06:32:30 · 871 阅读 · 0 评论 -
[leet code] Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2014-01-03 00:14:05 · 539 阅读 · 0 评论 -
[leet code] Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right no原创 2014-01-03 09:17:46 · 523 阅读 · 0 评论 -
[leet code] Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.思路总结:逐个node检查值是否与后一个n原创 2014-01-03 01:38:15 · 604 阅读 · 0 评论 -
[leet code] Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solutio原创 2014-01-04 10:32:53 · 712 阅读 · 0 评论 -
[leet code] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive so原创 2014-01-04 04:47:47 · 683 阅读 · 0 评论 -
[leet code] Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2014-01-05 00:52:39 · 696 阅读 · 0 评论 -
[leet code] Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.思路与原创 2014-01-05 02:06:47 · 567 阅读 · 0 评论 -
[leet code] Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?开始觉得并不难, 最初思路是用2 steps的出现次数做原创 2014-01-07 05:25:11 · 654 阅读 · 0 评论 -
[leet code] Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.看着roman numeral是一头雾水, google过后知道是罗马数字, 就是I, II, III, IV, V, VI, VII, VIII, IX, X... 写r原创 2014-01-07 06:38:27 · 737 阅读 · 0 评论 -
[leet code] Maximum Subarray
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] ha原创 2014-01-07 01:31:05 · 725 阅读 · 0 评论 -
[leet code] Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.最初思路是创建第三个linkedlist, 然后按顺序由小到大将两个给定的linkedlist节点值放入第三个原创 2014-01-08 01:42:27 · 630 阅读 · 0 评论 -
[leet code] Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2014-01-08 08:43:44 · 686 阅读 · 0 评论 -
[leet code] Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.专门查了一下什么是height balanced BST. 介绍中还有提供插入该树的方法, 在插入过程中需要检查时是否平衡, 如果不平衡整个树需要重新部署. 幸而该题目提供的array已经排序, 于原创 2014-01-08 08:56:33 · 513 阅读 · 0 评论 -
[leet code] Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2014-01-02 01:29:26 · 744 阅读 · 0 评论 -
[leet code] Pascal's Triangle
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]]Analysis:The only difficul原创 2014-01-09 07:01:16 · 993 阅读 · 0 评论 -
[leet code] Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in原创 2014-01-09 00:12:37 · 587 阅读 · 0 评论 -
[leet code] Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2014-01-09 06:00:21 · 2850 阅读 · 0 评论 -
[leet code] Same Tree
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./** * Def原创 2014-01-01 07:09:29 · 496 阅读 · 0 评论 -
[leet code] Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c原创 2014-01-01 09:03:51 · 589 阅读 · 0 评论 -
[leet code] Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2014-01-10 01:26:35 · 532 阅读 · 0 评论 -
[leet code] Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.=========Analysis:2 key points:1. The match between roman numeral and int:原创 2014-01-10 04:03:38 · 564 阅读 · 0 评论 -
[leet code] Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2014-01-10 04:48:21 · 647 阅读 · 0 评论 -
[leet code] Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2014-01-10 07:33:16 · 1126 阅读 · 0 评论 -
[leet code] Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2014-01-11 08:07:40 · 3524 阅读 · 0 评论 -
[leet code] Gray Code
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 the sequence of原创 2014-01-13 05:24:27 · 5106 阅读 · 0 评论 -
[leet code] Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()原创 2014-01-13 07:29:49 · 1737 阅读 · 1 评论 -
[leet code] Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2014-01-13 12:41:20 · 2454 阅读 · 0 评论 -
[leet code] Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2014-01-14 00:55:50 · 649 阅读 · 0 评论 -
[leet code] Rotate Image
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?Without followup, we can directly create a new n x原创 2014-01-14 08:11:11 · 943 阅读 · 0 评论 -
[leet code] 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exact原创 2014-01-22 01:22:39 · 824 阅读 · 0 评论 -
[leet code] Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2014-01-22 07:24:38 · 641 阅读 · 0 评论 -
[leet code] Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2014-01-22 02:06:49 · 545 阅读 · 0 评论 -
[leet code] Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.==========Analysis:Nothing special, just implement how we manually count that. Idea of which is to use the原创 2014-01-22 05:54:36 · 663 阅读 · 0 评论 -
[leet code] Subsets
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For exa原创 2014-01-23 02:10:14 · 1457 阅读 · 0 评论 -
[leet code] Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?Extension from problem Linked List Cycle.原创 2014-01-15 01:17:10 · 1465 阅读 · 0 评论 -
[leet code] Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solut原创 2014-01-15 05:24:20 · 1996 阅读 · 0 评论 -
[leet code] Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2014-01-15 06:26:47 · 3043 阅读 · 0 评论 -
[leet code] Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m原创 2014-01-15 06:58:09 · 772 阅读 · 0 评论