- 博客(158)
- 资源 (1)
- 收藏
- 关注
原创 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.一个一个比较的方式太trivial,所以简单的用前缀树写了写。class Node{ char val; Map childs; public Node(){ val = 0; childs = new H
2014-01-20 05:20:19 640
原创 Largest Rectangle in Histogram
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 histogram where width o
2014-01-16 15:20:41 548
原创 Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain dupli
2014-01-15 17:05:54 599
原创 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-15 15:15:30 564
转载 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non
2014-01-14 13:55:23 963 1
转载 Populating Next Right Pointers in Each Node
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extr
2014-01-13 15:07:46 585
原创 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.
2014-01-13 08:44:37 628
原创 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a
2014-01-13 08:01:07 508
原创 Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word must exi
2014-01-13 07:51:12 573
原创 Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word m
2014-01-13 06:21:07 492
转载 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3
2014-01-12 08:59:22 480
原创 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region .For example,X X X XX O O
2014-01-10 23:48:14 635
原创 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","b"],
2014-01-10 23:03:28 480
原创 Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return
2014-01-10 22:07:45 549
原创 Gas Station
There 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 unlimited gas tank and it costs cost[i] of gas to travel from station i to
2014-01-10 20:54:23 881
原创 Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on
2014-01-10 05:13:28 533
原创 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], [6,
2014-01-09 22:22:49 524
转载 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-09 22:09:43 487
原创 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list./** * Definition for singly-l
2014-01-09 22:01:27 547
原创 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"
2014-01-09 21:36:48 497
原创 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 soluti
2014-01-09 20:01:13 524
原创 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-09 19:45:46 879
原创 LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if
2014-01-09 19:16:13 597
原创 Insertion Sort List
Sort a linked list using insertion sort.先贴一下我写的代码class Solution {public ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) { return head; } Li
2014-01-09 16:54:07 862
原创 Sort List
Sort a linked list in O(n log n) time using constant space complexity./** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) {
2014-01-09 16:17:00 1095 1
原创 Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line./** * Definition for a point. * class Point { * int x; * int y; * Point() {
2014-01-09 15:08:28 661
原创 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", "+",
2014-01-08 15:05:44 519
原创 Add Two Numbers (Java)
好久没做题,边界case没有很注意,看来还是需要多做一做。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next =
2014-01-01 10:47:46 2817
原创 Longest Substring Without Repeating Characters
public class Solution { public int lengthOfLongestSubstring(String s) { int maxLen = 0; int len = 0; int start = 0; int maxStart = 0; boolean [] map = new b
2014-01-01 09:03:42 511
原创 Median of Two Sorted Arrays (JAVA)
class Solution {public double findKth(int a[], int startA, int m, int b[], int startB, int n, int k){ if (m > n) return findKth(b, startB, n, a, startA, m, k); if (m == 0) return b
2014-01-01 08:31:54 2218
原创 LeetCode: Two Sum (Java)
最近G家的HR联系的紧,也不好意思一直拖着,再加上现在在EA干的活没啥意思,能跳的话也好,所以再一次开始刷题了,只是,之前用c++,现在要用java了。。。哎,编程语言来来回回的变啊。。。废话不说了,第一个题 Two Sum. 好久没写,不够简洁,好多细节也没有注意,WA了几次才过的。class Pair implements Comparable{ public int numb
2013-12-06 13:43:03 3211
原创 LeetCode : Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Retu
2013-03-13 14:21:42 1903
原创 LeetCode: Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa"
2013-03-13 13:07:18 1193
转载 二维直方图盛水
基本思路:用两个数据结构1. visited matrix 表明已经用过的cell2. PriorityQueue放目前的边界PQ总是弹出最低的cell。得到最低的cell以后找没有访问过的邻居。如果邻居小的话,就可以装水了。把邻居放入PQ中作为新的边界(如果邻居低的话,用当前height代替邻居的高度)。这样一直到PQ为空。复杂度N*N*logN, 程序不是很复杂。class wate
2013-03-12 06:18:02 1222
原创 LeetCode : Sum Root to Leaf Numbers
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 number 123.Find the to
2013-03-11 07:17:22 569
原创 把一个长度为n的数组分成k段,让每段和的最大值最小。
const int MAX_COUNT = 100;int state[MAX_COUNT][MAX_COUNT];int a[MAX_COUNT];int MinSum(int n,int m){ int i = 0, j = 0, k = 0, temp = 0, MaxInt; for (i = 1;i <=n;++i) { state[i]
2013-03-10 08:42:18 11788 3
原创 给一个单向链表,随机选择一个node in one pass
public class ReserviorSampling { public ListNode sampling(ListNode head){ ListNode s = head; Random rand = new Random(); int count = 1; while(head != null){
2013-03-10 08:33:10 912
原创 二叉树inorder iterator的跌代器
void init(){ pushLeftDown(root )}void pushLeftDown(Node cur){ if(cur == null) return; stack.push(cur); while(cur.left!=null){ stack.push(cur.left); cur = cur.l
2013-03-10 08:29:06 728
原创 hackerrank Stock Maximize
Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next N days.Each day, you can either buy on
2013-03-08 12:52:56 1742
原创 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1,
2013-02-20 13:57:23 1567
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人