
LeetCode
文章平均质量分 72
hb_peng
这个作者很懒,什么都没留下…
展开
-
LeetCode_OJ【337】 House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour原创 2016-09-02 10:25:35 · 379 阅读 · 0 评论 -
LeetCode_OJ【16】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 exactly原创 2015-09-15 11:43:00 · 350 阅读 · 0 评论 -
LeetCode_OJ【18】4Sum
Given an array S of n integers, are there elements a,b, c, and d in S such that a + b +c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements原创 2015-09-15 15:15:44 · 260 阅读 · 0 评论 -
LeetCode_OJ【17】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string原创 2015-09-15 15:19:02 · 293 阅读 · 0 评论 -
LeetCode_OJ【19】Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the l原创 2015-09-16 12:46:22 · 344 阅读 · 0 评论 -
LeetCode_OJ【21】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.本体思路:比较两个链表当前节点的val值,并将较小值添加到新链上,当其中一个链表比较完了,将另一个链表接到新链的末原创 2015-09-16 20:25:36 · 343 阅读 · 0 评论 -
LeetCode_OJ【23】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.这道题目一开始就弄错了思路,虽然参照了之前 LeetCode_OJ【21】Merge Two Sorted Lists 的思路来解题,却使用了错误的使用方法。错误的使用方法:对于所有给定的头结原创 2015-09-16 20:46:39 · 416 阅读 · 0 评论 -
LeetCode_OJ【24】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. You m原创 2015-09-17 10:52:50 · 284 阅读 · 0 评论 -
LeetCode_OJ【29】Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这题运用移位轻松解决,但是要注意是否会溢出,在会不会溢出这里我卡了好久。int占4字节,表示范围为-2^31 ~ 2^31-1。程序首先要将除数与被除数都变成正数,原创 2015-09-21 10:12:25 · 260 阅读 · 0 评论 -
LeetCode_OJ【15】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 (a,b,c) m原创 2015-09-15 10:34:29 · 322 阅读 · 0 评论 -
LeetCode_OJ【262】Trips and Users
The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at theUsers table. Status is an ENUM type of (‘completed’, ‘cance原创 2015-09-14 09:49:05 · 1510 阅读 · 0 评论 -
LeetCode_OJ【5】Longest Palindromic Substring
Given a string S, find the longest palindromic substring inS. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.这道题还是比较简单的,主要运用分支限界的方法,原创 2015-09-07 14:58:37 · 282 阅读 · 0 评论 -
LeetCode_OJ【178】Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value.原创 2015-09-08 21:00:03 · 333 阅读 · 0 评论 -
LeetCode_OJ【177】Nth Highest Salary
Write a SQL query to get the nth highest salary from the Employee table.+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 |+----+--------+For exampl原创 2015-09-08 21:08:25 · 744 阅读 · 0 评论 -
LeetCode_OJ【10】Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input原创 2015-09-10 10:25:21 · 285 阅读 · 0 评论 -
LeetCode_OJ【180】Consecutive Numbers
Write a SQL query to find all numbers that appear at least three times consecutively.+----+-----+| Id | Num |+----+-----+| 1 | 1 || 2 | 1 || 3 | 1 || 4 | 2 || 5 | 1 || 6 | 2原创 2015-09-10 14:53:43 · 375 阅读 · 0 评论 -
LeetCode_OJ【12】【13】Integer to Roman && Roman to Integer
本题的关键是给罗马数字与阿拉伯数字的对应关系抽象成合适的数据结构;下面参考的两种解法都比较有代表性,值得学习。http://blog.youkuaiyun.com/ljiabin/article/details/39968583【题目】Given a roman numeral, convert it to an integer. Or, Given an integer, con转载 2015-09-11 11:08:00 · 341 阅读 · 0 评论 -
LeetCode_OJ【14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.找出所有字符串的公共前缀这道题看着挺简单的,但是做起来却磕磕碰碰,足足提交了四五次才通过,主要是匹配串为"",被匹配串为""等情况没有考虑到。public class Solution { public St原创 2015-09-12 10:26:32 · 371 阅读 · 0 评论 -
LeetCode_OJ【185】Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----原创 2015-09-13 10:27:44 · 889 阅读 · 0 评论 -
LeetCode_OJ【28】Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.这题虽然用朴素字符串匹配算法也能过,但是最好还是用KMP算法,毕竟笔试面试的时候可没有这么简单。KMP算法的精髓在于求出next数组,nex原创 2015-09-21 10:31:05 · 330 阅读 · 0 评论 -
LeetCode_OJ【33】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its原创 2015-10-19 16:23:15 · 235 阅读 · 0 评论 -
LeetCode_OJ【39】Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations inC where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numbe原创 2016-01-12 20:48:19 · 303 阅读 · 0 评论 -
LeetCode_OJ【60】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""312""原创 2016-02-20 11:06:38 · 351 阅读 · 0 评论 -
LeetCode_OJ【63】Unique Paths II
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 respectively in the grid.原创 2016-02-22 17:11:43 · 334 阅读 · 0 评论 -
LeetCode_OJ【68】Text Justification
Given an array of words and a length L, format the text such that each line has exactlyL characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is原创 2016-04-19 11:59:01 · 457 阅读 · 0 评论 -
LeetCode_OJ【69】Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.这道题目没看到二分查找的提示的时候感觉还是比较难的,虽然用自己的方法能做出来,但是性能太差了。在不管使用什么方法时都要注意不要出现比x大的int型变量,否则极易溢出。n^2 > x 这种表达式最好都改写为 n > x /2.java实现如下:pub原创 2016-04-19 16:22:33 · 339 阅读 · 0 评论 -
LeetCode_OJ【71】Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"这道题一开始没想好,直接进行处理,结果写出的代码又臭又长,而且是用了提交大法,错了N多次才成功的。以下为第一次AC时原创 2016-04-20 15:36:26 · 287 阅读 · 0 评论 -
LeetCode_OJ【73】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(mn) s原创 2016-04-22 15:39:29 · 354 阅读 · 0 评论 -
LeetCode_OJ【76】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".原创 2016-04-26 12:00:50 · 325 阅读 · 0 评论 -
LeetCode_OJ【78】Subsets
Given a set of distinct integers, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,原创 2016-04-27 16:21:17 · 545 阅读 · 1 评论 -
LeetCode_OJ【56】Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].这道题最好在57题做完了之后再做,leetcode上的这个顺序正好反过来了。一开始直接上手这个题目的思路原创 2016-02-19 19:53:22 · 302 阅读 · 0 评论 -
LeetCode_OJ【57】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Examp原创 2016-02-19 15:03:45 · 346 阅读 · 0 评论 -
LeetCode_OJ【42】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]原创 2016-01-13 09:44:38 · 363 阅读 · 0 评论 -
LeetCode_OJ【43】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.Subscribe to see which companies asked原创 2016-01-13 11:51:44 · 257 阅读 · 0 评论 -
LeetCode_OJ【44】Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover原创 2016-01-14 17:34:41 · 379 阅读 · 0 评论 -
LeetCode_OJ【48】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?Subscribe to see which companies asked this question原创 2016-01-19 18:53:53 · 358 阅读 · 0 评论 -
LeetCode_OJ【46】Permutations
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1],[3,1,2], and [3,2,1].Subscri原创 2016-01-28 17:10:00 · 463 阅读 · 0 评论 -
LeetCode_OJ【47】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].原创 2016-01-28 17:18:22 · 345 阅读 · 0 评论 -
LeetCode_OJ【53】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] has原创 2016-02-17 15:22:30 · 290 阅读 · 0 评论 -
LeetCode_OJ【54】Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You原创 2016-02-18 09:35:44 · 301 阅读 · 0 评论