Easy
文章平均质量分 64
violet_program
这个作者很懒,什么都没留下…
展开
-
Maximum Subarray
Kadane Algorithm O(n) Not working for all negative number arrayspublic class Solution { public int maxSubArray(int[] A) { // Start typing your Java solution below // DO NOT write main()原创 2013-06-06 05:10:34 · 538 阅读 · 0 评论 -
Valid a Palindrome
public class ValidPalindrome { /** * Given a string, determine if it is a palindrome, considering only * alphanumeric characters and ignoring cases. * * For example, "A man, a plan, a canal转载 2013-02-21 11:48:35 · 359 阅读 · 0 评论 -
Two Sum
import java.util.Hashtable;public class Solution { public int[] twoSum(int[] numbers, int target) { // Start typing your Java solution below // DO NOT write main() function int[] res = {0,原创 2013-04-01 09:07:13 · 667 阅读 · 4 评论 -
Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function if(prices.length == 0) return 0; int profit = 0;原创 2013-04-05 02:08:22 · 705 阅读 · 0 评论 -
Longest Common Prefix
public class Solution { public String longestCommonPrefix(String[] strs) { // Start typing your Java solution below // DO NOT write main() function if (strs.length == 0) return ""; for转载 2013-05-15 02:49:03 · 400 阅读 · 0 评论 -
Count and Say
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转载 2013-05-23 12:31:30 · 714 阅读 · 0 评论 -
Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did you consider the case where path = "/原创 2013-06-13 11:28:10 · 529 阅读 · 0 评论 -
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,原创 2013-06-15 03:38:52 · 477 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
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 A is now [1,1,2,2,3]原创 2013-06-18 04:44:58 · 479 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {转载 2013-03-30 02:10:28 · 568 阅读 · 0 评论 -
Leetcode: 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./** * Definit原创 2013-07-01 11:12:09 · 481 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * public class TreeNode转载 2013-07-08 12:26:20 · 747 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * public class TreeNode转载 2013-07-08 12:30:20 · 631 阅读 · 0 评论 -
Leetcode: Minimum Depth of Binary Tree
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./** * Definition for binary tree * p原创 2013-07-12 04:39:56 · 498 阅读 · 0 评论 -
Leetcode: Sum Root to Leaf Number
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number123.Find the total sum原创 2013-07-16 11:09:04 · 550 阅读 · 0 评论 -
Leetcode: Path Sum
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 binary tree and sum原创 2013-07-12 05:26:09 · 563 阅读 · 0 评论 -
3Sum Closest
Pay attention to this one. Passed after several bug fixes.public class Solution { public int threeSumClosest(int[] num, int target) { // Start typing your Java solution below // DO NOT w原创 2013-04-03 03:12:10 · 593 阅读 · 0 评论 -
Integer To Roman
public class Solution { public String intToRoman(int num) { // Start typing your Java solution below // DO NOT write main() function String[] symbol = { "M", "D", "C", "L", "X", "V", "I" };转载 2013-05-14 06:05:59 · 452 阅读 · 0 评论 -
Remove Duplicates From Sorted Array
public class Solution { public int removeDuplicates(int[] A) { // Start typing your Java solution below // DO NOT write main() function if(A.length == 0 || A.length == 1) return A.length;原创 2013-05-18 03:31:52 · 417 阅读 · 0 评论 -
Next Permutation
public class Solution { public void nextPermutation(int[] num) { // Start typing your Java solution below // DO NOT write main() function int len = num.length; if(len <= 1) return; fo转载 2013-05-22 05:19:45 · 401 阅读 · 0 评论 -
Search Insert Position
public class Solution { public int searchInsert(int[] A, int target) { // Start typing your Java solution below // DO NOT write main() function if(A.length == 0) return 0; int left = 0;原创 2013-05-23 04:39:02 · 461 阅读 · 0 评论 -
Trapping Rain Water
Given n non-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.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],转载 2013-05-24 05:18:05 · 494 阅读 · 0 评论 -
Plus One
Given a number represented as an array of digits, plus one to the number.public class Solution { public int[] plusOne(int[] digits) { // Start typing your Java solution below // DO NOT wri原创 2013-06-13 03:43:49 · 486 阅读 · 0 评论 -
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.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad ide原创 2013-06-15 00:43:53 · 499 阅读 · 0 评论 -
Leetcode Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Could you implement it without using extra memory? public int singleNumber(int[] A) {原创 2013-10-03 09:53:52 · 697 阅读 · 0 评论 -
Leetcode: Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",原创 2013-12-31 03:45:44 · 592 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2013-04-05 22:39:23 · 516 阅读 · 0 评论 -
Leetcode: 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 solution is trivi原创 2013-06-28 05:42:50 · 807 阅读 · 0 评论 -
Leetcode: 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 ofevery node never differ原创 2013-07-11 09:59:38 · 525 阅读 · 0 评论 -
Leetcode: Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return 6.Pay attenti原创 2013-07-16 05:32:42 · 659 阅读 · 1 评论 -
3Sum
public class Solution { public ArrayList> threeSum(int[] num) { // Start typing your Java solution below // DO NOT write main() function ArrayList> res = new ArrayList>(); if(num.length ==原创 2013-04-02 05:21:52 · 546 阅读 · 0 评论 -
Leetcode: 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 solution is triv原创 2013-11-11 10:45:12 · 824 阅读 · 0 评论 -
Container With Most Water
public class Solution { public int maxArea(int[] height) { // Start typing your Java solution below // DO NOT write main() function int maxArea = 0; int left = 0; int right = height.leng转载 2013-05-14 05:12:34 · 451 阅读 · 0 评论 -
Search in Rotated Sorted Array
public class Solution { public int search(int[] A, int target) { // Start typing your Java solution below // DO NOT write main() function if(A.length == 0) return -1; int begin = 0; i转载 2013-05-22 10:35:16 · 508 阅读 · 0 评论 -
Search For A Range
public class Solution { public int[] searchRange(int[] A, int target) { // Start typing your Java solution below // DO NOT write main() function int[] res = {-1, -1}; if(A.length == 0)转载 2013-05-22 11:40:16 · 445 阅读 · 0 评论 -
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?public class Solution { public void rotate(int[][转载 2013-05-31 03:35:20 · 491 阅读 · 0 评论 -
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 defi原创 2013-06-10 21:53:38 · 504 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.public class Solution { public int sqrt(int x) { // Start typing your Java solution below // DO NOT write main() function转载 2013-06-13 05:13:45 · 478 阅读 · 0 评论 -
Gas Station
出自 http://blog.csdn.net/lbyxiafei/article/details/12183461There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlim转载 2013-10-02 10:20:29 · 797 阅读 · 0 评论 -
Binary Tree Level Order Traversal
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {转载 2013-03-29 10:42:39 · 550 阅读 · 0 评论