- 博客(80)
- 收藏
- 关注
转载 word search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those hor
2015-09-30 23:49:28 286
原创 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
2015-09-30 23:26:39 248
转载 Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that i
2015-09-28 03:23:59 214
转载 Word Ladder II(BFS + DFS)
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a timeEac
2015-09-28 02:55:17 289
转载 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. /* * A line is determined by two factors,say y=ax+b * * If two points(x1,y1) (x2,
2015-09-27 23:44:03 310
原创 Valid Number
public class Solution { public boolean isNumber(String s) { s = s.trim(); boolean pointSeen = false; boolean eSeen = false; boolean numberSeen = false; boolean numberAfterE =
2015-09-27 21:25:30 306
转载 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
2015-09-27 11:38:48 221
转载 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.
2015-09-27 11:14:09 242
转载 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 X X O O X
2015-09-27 10:44:49 249
转载 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. class Solution { public: int divide(int dividend, int divisor) { if (!
2015-09-27 03:45:02 199
转载 Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Th
2015-09-27 03:23:39 218
转载 Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". public class Solution { public String reverseWords(String s) {
2015-09-27 02:38:32 190
转载 Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i an
2015-09-27 02:26:17 230
原创 Decode ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total nu
2015-09-26 23:04:16 219
转载 Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em
2015-09-26 04:40:30 213
原创 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should
2015-09-26 02:58:15 200
转载 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest
2015-09-24 11:00:25 206
转载 Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did
2015-09-24 10:05:04 254
原创 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.Th
2015-09-24 09:35:07 190
转载 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0
2015-09-23 05:04:44 177
转载 Number of Digit One(数学找规律)
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the follow
2015-09-23 03:29:31 234
转载 Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first
2015-09-23 03:12:17 182
转载 Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Wri
2015-09-23 02:19:15 191
原创 Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically
2015-09-23 01:34:01 193
转载 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. public class Solution { public String multiply(String num1, String num2) { if (num1 == null ||
2015-09-22 10:43:40 185
转载 Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely fille
2015-09-22 10:20:25 177
转载 Restore IP Addresses(DFS的巅峰)
Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order
2015-09-22 10:07:04 195
转载 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",
2015-09-22 02:54:53 198
转载 Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be
2015-09-21 22:20:42 309
转载 Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling you
2015-09-21 21:21:17 292
原创 Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. import java.util.*; public class Solution { p
2015-09-21 21:11:59 224
转载 course shedule(略)
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as
2015-09-20 05:33:01 262
转载 Permutation Sequence (略)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123""132""213""231""3
2015-09-20 00:01:32 210
转载 Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.题目是找到>=s的最小子数组长度 For example, given
2015-09-19 22:25:13 228
转载 Group Anagram
Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ] Note:
2015-09-19 21:32:57 283
原创 clone graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each
2015-09-18 22:43:12 295
原创 numbers of islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assu
2015-09-18 20:27:49 249
原创 Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. public class Solution { /** * e.g x =27 * 16 h = 2 最高位 100 * b = 2-1 = 1 ==> 1 10 | 100 = 110 * 110*110 = 6*6
2015-09-18 08:25:46 244
原创 H index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 二分: public int hIndex(int[] citations) { int len = citations.length;
2015-09-18 07:50:08 258
原创 H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index. According to the definition of h-index on Wikipedia: "A
2015-09-18 05:56:17 248
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人