自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(59)
  • 收藏
  • 关注

原创 LeetCode 283. Move Zeroes(java)

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 your funct

2018-01-30 15:06:55 322

原创 LeetCode 80. Remove Duplicates from Sorted Array II(java)

Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five element

2018-01-30 14:50:46 316

原创 LeetCode 26. Remove Duplicates from Sorted Array(java)

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying the i

2018-01-30 14:34:54 103

原创 LeetCode 27. Remove Element(java)

Given an array and a value, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place w

2018-01-30 13:47:29 345

原创 LeetCode 62. Unique Paths(java)

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto

2018-01-27 11:00:54 692

原创 LeetCode 312. Burst Balloons(java)

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[lef

2018-01-27 10:46:05 505

原创 LeetCode 218. The Skyline Problem(java)

A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as

2018-01-27 08:41:31 960

原创 LeetCode 204. Count Primes(java)

Description:Count the number of prime numbers less than a non-negative number, n.解法一:每次判断i是不是prime,然后用一个dp[]数组来存结果,然后由于每次都要判断prime的时间太长,所以被TLE了。时间复杂度为O(n^2),空间复杂度为O(n)。public int countPrimes(int n) {

2018-01-26 09:24:49 682

原创 LeetCode 240. Search a 2D Matrix II(java)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers in each co

2018-01-26 08:49:16 256

原创 LeetCode 406. Queue Reconstruction by Height(java)

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this p

2018-01-26 08:29:30 488

原创 LeetCode 279. Perfect Squares(java)

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return

2018-01-26 07:20:27 527

原创 LeetCode 448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you

2018-01-26 06:32:42 91

原创 LeetCode 142. Linked List Cycle II(java)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space?解法:快慢指针,每次慢指针走一

2018-01-25 12:08:31 232

原创 非比较排序之桶排序(bucket sort)

1. 桶排序如果输入的数组里没有重复,那么一个普通数组就可以,这个数组的大小为原数组中的max - min + 1。代码如下。public static int[] bucketSort(int[] nums) { if (nums.length <= 1) return nums; int min = Integer.MAX_VALUE, max = Integer

2018-01-25 08:27:45 210

原创 LeetCode 347. Top K Frequent Elements(java)

Given a non-empty array of integers, return the k most frequent elements.For example, Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number of unique ele

2018-01-25 07:09:30 866

原创 LeetCode 95. Unique Binary Search Trees II

Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3

2018-01-25 03:08:43 155

原创 LeetCode 96. Unique Binary Search Trees(java)

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 \ /

2018-01-24 07:54:50 138

原创 LeetCode 85. Maximal Rectangle(java)

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Retu

2018-01-23 06:45:56 481

原创 LeetCode 84. Largest Rectangle in Histogram(java)

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

2018-01-22 09:44:12 479

原创 LeetCode 221. Maximal Square(java)

Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return 4

2018-01-22 08:24:22 538

原创 LeetCode 161. One Edit Distance(java)

Given two strings S and T, determine if they are both one edit distance apart. 注意:当s和t完全相同时,需要返回false.最快的解法:先调整s和t的长度,使得s的长度小于t,解法中的解决方法很巧妙,通过交换参数,再次调用函数实现~。然后开始判断m和n的长度,如果相差大于1则返回false。遍历两个字符串,当出现两个字

2018-01-22 07:51:43 347

原创 LeetCode 72. Edit Distance(java)

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) In

2018-01-22 07:13:07 579

原创 LeetCode 48. Rotate Image(java)

You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix direct

2018-01-21 13:48:16 409

原创 LeetCode 152. Maximum Product Subarray(java)

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 produ

2018-01-21 09:08:26 163

原创 LeetCode 338. Counting Bits(java)

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.Example:For num = 5 you sho

2018-01-21 06:59:50 483

原创 LeetCode 22. Generate Parentheses(java)

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())", "(

2018-01-21 06:25:52 453

原创 LeetCode 617. Merge Two Binary Trees(java)

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. T

2018-01-21 06:06:11 269

原创 LeetCode 238. Product of Array Except Self(java)

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For e

2018-01-21 06:00:04 371

原创 LeetCode 337. House Robber III(java)

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,

2018-01-21 05:21:50 445

原创 递归优化小技巧:用hashmap或者数组来预存sub-result以加快速度

技巧如题,先用hashmap或者数组来保存某些已经算好的,之后可能会重复用到的子问题的答案,这样在每次递归之前先判断,如果是存在在map里的,就直接取value返回即可。可以大大提高递归的效率。例一:LeetCode 140. Word Break IIGiven a non-empty string s and a dictionary wordDict containing a lis

2018-01-21 04:09:11 580

原创 LeetCode 213. House Robber II(java)

Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all

2018-01-20 10:04:15 579

原创 LeetCode 198. House Robber(java)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses

2018-01-20 09:34:10 699

原创 LeetCode 23. Merge k Sorted Lists(java)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.解法一:用heap来做,先把k个list的第一个元素放进heap,每次poll出一个后,放进poll出的那个的后一个,直到heap为空。时间复杂度O(mklogk),空间复杂度为O(k).public Li

2018-01-20 08:43:25 1728

原创 LeetCode 226. Invert Binary Tree(java)

Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1解法一:递归解,对于每一个节点,其左孩子变成右孩子,右孩子变成左孩子,最后返回root即可。public TreeNode invertTree(TreeNode

2018-01-20 07:31:21 245

原创 LeetCode 42. Trapping Rain Water(java)

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], ret

2018-01-20 07:10:34 146

原创 LeetCode 687. Longest Univalue Path(java)

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.Note: The length of path between two nodes is rep

2018-01-19 09:17:48 416

原创 LeetCode 681. Next Closest Time(java)

Given a time represented in the format “HH:MM”, form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.You may assume the given input string

2018-01-17 09:55:20 3398

原创 尚未整理的面经题

尚未整理的面经题:fb 发个面经 1. check if the array is monotonic (non-increasing or non-descreasing) 2. decode ways with a follow up: what if the input is an incoming stream of characters instead of a fixed string.

2018-01-17 07:38:27 203

原创 LeetCode 148. Sort List(java)

Sort a linked list in O(n log n) time using constant space complexity.思路:由于linkedlist是一个不回头的只向下走的数据结构,因此,我们在考虑多种排序方式时,会发现快排需要双指针找pivot,因此不合适,而merge sort不需要回头,只需要找mid切断,再merge起来即可,并且时间复杂度较好,达到要求。因此,这里我们

2018-01-16 12:48:08 778

原创 LeetCode 70. Climbing Stairs(java)

You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive inte

2018-01-16 11:56:09 326

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除