leetcode
文章平均质量分 64
愤怒的猴子
好的习惯是一天一天养成的,加油
展开
-
Insertion Sort List
题目描述:Sort a linked list using insertion sort.插入排序,题目很简单,只是换成链表实现而已。代码如下所示public ListNode insertionSortList(ListNode head) { ListNode cur=head; int sum=0; while(cur!=null){ cur=cur.n原创 2016-05-24 09:48:30 · 216 阅读 · 0 评论 -
Intersection of Two Linked Lists
题目描述:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists: A: a1 → a2 ↘原创 2016-05-24 10:17:11 · 278 阅读 · 0 评论 -
Remove Linked List Elements
题目描述:Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5题目简单,一次ac,代码如下:原创 2016-05-24 10:33:27 · 254 阅读 · 0 评论 -
Reverse Linked List
题目描述:Reverse a singly linked list.很简单,必须一次ac。public ListNode reverseList(ListNode head) { if(head==null){ return null; } ListNode last=head; while(last.next!=null){ last=原创 2016-05-24 10:42:56 · 215 阅读 · 0 评论 -
Palindrome Linked List
题目描述:Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?这个题要求空间复杂度为O(1),不能一开始就逆序,然后逐个比较,这样不符合要求。链表无非就是逆序,插入,这个题和Reorder l原创 2016-05-24 11:12:16 · 198 阅读 · 0 评论 -
Odd Even Linked List
题目描述:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to d原创 2016-05-24 11:42:09 · 256 阅读 · 0 评论 -
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:原创 2016-05-24 22:36:08 · 215 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
题目描述:Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answ原创 2016-05-24 21:37:08 · 211 阅读 · 0 评论 -
Sudoku Solver
题目描述:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudok原创 2016-05-25 16:54:44 · 260 阅读 · 0 评论 -
Substring with Concatenation of All Words
题目描述:You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) ins that is a concatenation of each word in words exactly o原创 2016-05-25 19:02:38 · 248 阅读 · 0 评论 -
Group Anagrams
题目描述: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:原创 2016-05-25 20:46:15 · 378 阅读 · 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.解题思路:遍历(m-1)*(n-1)个元素,若matrix[i][j]=0,则使matrix[0][j]=0,matrix[i][0]=0。然后遍历第0行的n-1个元素,若matrix[0][j]=0,原创 2016-05-12 17:54:14 · 234 阅读 · 0 评论 -
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].先将给定的intervals排序,然后从第一个开始,逐个比较,插入前面的interval,所以这个时候一原创 2016-05-12 09:23:22 · 290 阅读 · 0 评论 -
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.Exa原创 2016-05-12 09:13:40 · 234 阅读 · 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?题目说了空间复杂度是O(1),那么就不能用n或者n*n的数组存储,只有一个点一个点的看,这道题关键是把第i行四原创 2016-05-11 09:04:46 · 269 阅读 · 0 评论 -
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 of each ba原创 2016-05-10 21:17:56 · 246 阅读 · 0 评论 -
First Missing Positive
题目描述: 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 and uses constant s原创 2016-05-08 21:22:09 · 219 阅读 · 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,原创 2016-05-08 21:18:00 · 215 阅读 · 0 评论 -
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 ar原创 2016-05-07 20:05:35 · 229 阅读 · 0 评论 -
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 foun原创 2016-05-07 18:36:54 · 508 阅读 · 0 评论 -
Next Permutation
题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest poss原创 2016-05-06 20:57:28 · 250 阅读 · 0 评论 -
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原创 2016-05-06 16:43:38 · 273 阅读 · 0 评论 -
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)原创 2016-05-06 16:12:26 · 263 阅读 · 0 评论 -
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原创 2016-05-26 11:54:29 · 275 阅读 · 0 评论 -
Two Sum
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums =原创 2016-04-27 17:25:50 · 312 阅读 · 0 评论 -
Game of Life
题目描述: According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”Given a board with m原创 2016-05-13 19:39:03 · 224 阅读 · 0 评论 -
Find the Duplicate Number
题目描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate numb原创 2016-05-13 21:47:55 · 337 阅读 · 0 评论 -
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原创 2016-05-14 10:50:34 · 214 阅读 · 0 评论 -
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.暴力法超时,哈希表法空间复杂度超过。摩尔投票法 假设an={2,2,3,3,1,4,0}原创 2016-05-14 15:17:41 · 249 阅读 · 0 评论 -
Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].这个题很简单,i指向range的开始,j指向range的末尾,记得加入String后i的指针要移动就行原创 2016-05-14 15:46:09 · 339 阅读 · 0 评论 -
Combination Sum III
题目描述: Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers原创 2016-05-14 20:43:02 · 234 阅读 · 0 评论 -
Maximal Rectangle
这个题和Largest rectangle in histogram有异曲同工之妙, 把每一行上面的矩形看成是histogram就行了代码如下:public class Solution { public int maximalRectangle(char[][] matrix) { if(matrix==null||matrix.length==0) return 0;原创 2016-05-28 09:09:24 · 259 阅读 · 0 评论 -
Contains Duplicate II
题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.这里用H原创 2016-05-14 16:02:43 · 225 阅读 · 0 评论 -
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.深度复制,就所有的节点另外开辟空间咯。如果是一原创 2016-05-28 16:05:08 · 338 阅读 · 0 评论 -
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 hav原创 2016-05-26 17:26:48 · 278 阅读 · 0 评论 -
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.给n个点,看最多多少个点在同一条线。我真的服了自己,一开始想那么麻烦,还去求y=kx+b。最后还有问题,因为当k失去精度后,b的计算可能不相等。下面是一段非常复杂的计算,还没有原创 2016-05-28 21:01:48 · 304 阅读 · 0 评论 -
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 parenthe原创 2016-05-29 11:55:06 · 303 阅读 · 0 评论 -
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.原创 2016-05-29 15:15:05 · 365 阅读 · 0 评论 -
Happy Number
题目描述:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the原创 2016-05-29 16:11:16 · 318 阅读 · 0 评论 -
Count Primes
题目描述:Description:Count the number of prime numbers less than a non-negative number, n.找到比n小的所有质数的个数这里用wiki上的图来说,就很显然了。比如n=52,那么就先让2的倍数全纪录下来,然后3的倍数,直到7的倍数,然后其他没有记录下来的就是质数了,加起来就行。原创 2016-05-29 16:48:53 · 277 阅读 · 0 评论