- 博客(201)
- 收藏
- 关注
原创 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I
2013-01-01 14:39:04 3816
原创 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 node.
2012-12-31 13:09:43 1861
原创 Plus One
Given a number represented as an array of digits, plus one to the number.public class Solution { public int[] plusOne(int[] digits) { if (digits == null || digits.length == 0) return nul
2012-12-30 13:30:23 779
原创 Pascal's Triangle II
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?public c
2012-12-30 13:02:28 1457
原创 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]]public class Solution
2012-12-30 12:47:42 784
原创 Partition List
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
2012-12-30 12:11:08 1532
原创 Sqrt(x)
Implement int sqrt(int x). int sqrt(int x) { if (x < 0) return -1; int start = 0; int end = x; if (x == 0 || x == 1) return x; while (start <= end) {
2012-12-30 05:09:18 1158 1
原创 Multiply Strings
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.public class Solution { public static
2012-12-29 14:19:31 2030
原创 difference between Abstract class and interface in java
1. An abstract class is a class that is only partially implemented. It may contain none or multiple abstract methods. An abstract method is simply a function definition that must be implemented in a
2012-12-29 08:41:34 1302
原创 First Missing Positive
Question:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time
2012-12-28 06:52:58 2208
转载 BK-Trees
BK-Trees, or Burkhard-Keller Trees are a tree-based data structure engineered for quickly finding near-matches to a string, for example, as used by a spelling checker, or when doing a 'fuzzy' search
2012-12-28 02:21:40 1005
原创 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 0
2012-12-27 10:43:06 598
转载 Sliding Window Maximum
A long array A[] is given to you. There is a sliding window of size w which is moving from the very left of the array to the very right. You can only see the w numbers in the window. Each time the sli
2012-12-27 00:18:54 755
原创 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [
2012-12-26 13:56:37 15428
转载 Difference between HAVING and WHERE Clause
Answer in one line is : HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.HAVING can be used only with the SELECT statement. HAVING is typically use
2012-12-25 04:21:46 923
转载 Difference between the having clause and the group by statement
In SQL, the having clause and the group by statement work together when using aggregate functions like SUM, AVG, MAX, etc. This is best illustrated by an example. Suppose we have a table called emp_bo
2012-12-23 09:58:27 651
原创 closest node to the target in BST
Question:Give a BST, find a node whose value is closest to the target.// when calling this method, set minDiff = Integer.MAX_VALUE, prev = null;public static Node closestValue(Node root, int targ
2012-12-20 00:03:27 644
原创 Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu
2012-12-16 13:12:15 1282
原创 Minimum Path Sum
Question: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 d
2012-12-14 14:17:27 1178
原创 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.Code:/
2012-12-13 10:50:44 1046
原创 Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in
2012-12-13 07:03:28 544
原创 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.public class Solution { public void setZeroes(int[][] matrix) { int[] row = new int[matri
2012-12-12 06:20:44 1021
原创 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.
2012-12-12 05:09:28 1833 1
原创 Spiral Matrix II
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 ],
2012-12-12 04:36:14 2211
原创 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
2012-12-11 06:07:16 4050 8
原创 Unique Paths II
Question:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respect
2012-12-10 03:55:09 1794
原创 Climbing Stairs
Question: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?public class So
2012-12-10 00:44:57 3274
原创 Binary Tree Level Order Traversal II
Question: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
2012-12-10 00:13:58 3149
原创 Path Sum II
Question: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
2012-12-09 12:52:50 1511
原创 Path Sum
Question:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binar
2012-12-09 11:03:47 614
原创 Remove Duplicates from Sorted Array II
Question:Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and
2012-12-09 07:05:25 3178
原创 Remove Element
Question: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 n
2012-12-09 06:47:49 3022
原创 Merge Sorted Array
Question: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 init
2012-12-08 02:19:52 1425
原创 Best Time to Buy and Sell Stock II
Question:Say you have an array for which the i-th 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 li
2012-12-07 07:25:02 676
原创 Anagrams
Question:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.Analysis:In order to determine whether two words are anagrams or not
2012-12-07 03:16:01 1339
原创 3SUM
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet
2012-12-07 01:12:45 665
原创 Largest Rectangle in Histogram
Question:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogra
2012-12-05 03:20:01 5093
原创 Java实现最长公共子序列
最长公共子序列(LCS)定义:一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列。比如数列A = “abcdef”, B = “adefcb”. 那么两个数列的公共子序列是"adef". 最长公共子序列和最长公共子字符串是有区别的,公共子序列里的元素可以不相邻,但是公共子字符串必须是连接在一起的。比如A和B的公共子字符串
2012-12-03 11:48:21 1232
原创 给一个钱数,用最少的纸币使之等于这个钱数
问题:给一个钱数,用最少的纸币使之等于这个钱数。 比如, 纸币的面额为 1, 2, 5, 10。则如果给你15块钱,你只能返回一张10块,一张5块。分析:使用DP,创建一个数组,里面保存用0块到给定块数的最小纸币数。然后利用递归函数F(n) = Min_{j \in 纸币面额}F(n - j ) + 1。代码:public static int findMinimumB
2012-12-02 07:29:55 1800
原创 从大到小打印BST的值
问题:给定一个BST, 从大到小打印里面的值。分析:我们知道通过inorder,我们可以从小到大打印BST的值。同理,为了能够从大到小打印,我们首先应该打印“根”节点的右子树的值,然后打印“根”节点,然后打印左子树的值。代码:public static void printTreeByDecreasingOrder(Node root) { if (root != null)
2012-12-02 07:00:28 1396
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人