
LeetCode
mudamu88
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
215. Kth Largest Element in an Array数组中的第K个最大元素
数组中的第K个最大元素 使用PriorityQueueimport java.util.PriorityQueue;class Solution { public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> queue = new PriorityQueue<>...原创 2018-08-28 21:50:53 · 215 阅读 · 0 评论 -
669. Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the re...原创 2018-04-13 15:49:33 · 168 阅读 · 0 评论 -
637 Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Exp...原创 2018-04-13 15:43:52 · 174 阅读 · 0 评论 -
排序算法模板(java)
1 快速排序public class Solution { public void sort(int[] nums){ if (nums == null || nums.length == 0) return; quickSort(nums, 0, nums.legnth-1) } private void quickSort(in...原创 2018-04-23 09:39:40 · 542 阅读 · 0 评论 -
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false. Note: You may assume ...原创 2018-04-22 23:48:32 · 151 阅读 · 0 评论 -
162. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that...原创 2018-04-21 23:17:08 · 187 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array(java)
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.Coul原创 2017-08-23 23:15:21 · 431 阅读 · 0 评论 -
119. Pascal's Triangle II待续。。。
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?这道题跟Pascal's原创 2017-08-05 23:16:39 · 211 阅读 · 0 评论 -
118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]每一行cur都是前一行pre生成出来的,原创 2017-08-05 22:23:47 · 229 阅读 · 0 评论 -
485. Max Consecutive Ones
给定数组,求最大的连续1的个数。思路:遍历数组,用一个计数器tmp来统计1的个数,如果当前数字为0,那么tmp重置为0,如果是1,tmp自增1,然后每次更新结果max。原创 2017-08-05 21:11:15 · 205 阅读 · 0 评论 -
617. Merge Two Binary Trees
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...原创 2018-04-13 15:55:19 · 162 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.For example: Given binary tree [3,9,2...原创 2018-04-13 17:46:36 · 169 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题意:找出二叉树的最小深度。/** * Definition for a ...原创 2018-04-13 19:26:19 · 126 阅读 · 0 评论 -
LeetCode23. 合并K个排序链表
合并K个排序链表 public static ListNode mergeKListNode(List<ListNode> list){ PriorityQueue<ListNode> queue = new PriorityQueue<>(Comparator.comparing(node -> node.val)); ...原创 2018-08-28 21:34:31 · 371 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order 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 ...原创 2018-04-21 22:24:57 · 604 阅读 · 0 评论 -
69. Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer Since the return type is an integer, the decimal digits are truncated and only ...原创 2018-04-21 21:18:34 · 177 阅读 · 0 评论 -
74. Search a 2D Matrix
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 from left to right. The first integer of each r...原创 2018-04-21 17:04:31 · 189 阅读 · 0 评论 -
240. Search a 2D Matrix II
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 ea...原创 2018-04-21 16:25:47 · 136 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order 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). Find the minimum element. You may assume no duplicat...原创 2018-04-21 15:41:30 · 169 阅读 · 0 评论 -
189. Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note: Try to come up as many solutions as you can,...原创 2018-04-21 13:39:15 · 154 阅读 · 0 评论 -
278. First Bad Version
第一个错误版本 思路:二分法 You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is devel...原创 2018-04-21 11:53:32 · 150 阅读 · 0 评论 -
226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1**反转二叉树 方法一**:引入一个队列进行层序遍历(leetcode时间超过20%的提交)/** * Definition ...原创 2018-04-13 20:43:51 · 140 阅读 · 0 评论 -
414. Third Maximum Number
找出数组中第三大的数,如果不存在的话那么就返回最大的数。思路:用三个变量first, second, third来分别保存第一大、第二大和第三大的数,然后遍历数组,如果遍历到的数字大于当前first,那么三个变量各自错位赋值,如果当前数字大于second且小于first,那么就更新second和third,如果当前数字大于third且小于second,那就只更新third。原创 2017-08-05 17:19:47 · 262 阅读 · 0 评论 -
219. Contains Duplicate II
给定一个整数数组nums与一个整数k,当且仅当存在两个不同的下标i和j满足nums[i] = nums[j]并且|i-j|<=k时返回true,否则返回false。 思路:遍历数组中每个元素,将(muns[i], i)存入一个map中。如果键nums[k]已经存在,则比较之前的索引和当前的索引的差值,如果差值不大于k,说明到了满足条件的两个值,否则使用新的索引作为值 。原创 2017-08-05 16:12:34 · 238 阅读 · 0 评论 -
217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2017-08-05 10:36:49 · 201 阅读 · 0 评论 -
66. Plus One(java)
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digi原创 2017-08-02 20:14:17 · 448 阅读 · 0 评论 -
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.public class Solution { public ListNode detectCycle(ListNod原创 2017-07-25 15:16:17 · 297 阅读 · 0 评论 -
141. Linked List Cycle(java)
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?给定一个链表,判断它是否有环。思路:使用快慢引用的思路。两个指针fast、slow都指向head,从head开始遍历,fast每次走两步,slow每次前走一步。如果链表有环,那么进入环...原创 2017-07-25 11:48:00 · 354 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List(java)
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.给定链表,删除所有重复,使得每个元素只出现一次原创 2017-07-25 00:54:36 · 323 阅读 · 0 评论 -
203. Remove Linked List Elements(Java)
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删除单链表中所有数据域为给定val的节点:pub原创 2017-07-25 00:21:27 · 291 阅读 · 0 评论 -
53. Maximum Subarray(java)
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] ha原创 2017-08-01 22:56:25 · 668 阅读 · 0 评论 -
232. Implement Queue using Stacks(java)
用栈实现队列在进队列的时候,把栈s1逆序压入另一个辅助栈s2,再逆序入s1;出队列的时候直接出栈s1。原创 2017-08-01 20:05:53 · 536 阅读 · 0 评论 -
225. Implement Stack using Queues(java)
用两个队列q1,q2实现一个栈。push时把新元素添加到q1的队尾。pop时把q1中除最后一个元素外逐个添加到q2中,然后pop掉q1中的最后一个元素,然后交换q1和q2,以保证添加元素时始终向q1中添加。原创 2017-08-01 21:22:08 · 304 阅读 · 0 评论 -
496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums原创 2017-08-01 16:25:12 · 266 阅读 · 0 评论 -
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 in place with constant memory.原创 2017-08-02 21:34:19 · 291 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
26. Remove Duplicates from Sorted ArrayGiven a sorted array, remove the duplicates in place such that each element appear only once and return the new length.给定一个排过序的数组,删除数组中的重复元素,使得每个元素只出现一次,返回删除后的新长度。原创 2017-08-03 00:03:30 · 196 阅读 · 0 评论 -
88. Merge Sorted Array(java)
给定两个已排序的数组nums1和nums2,合并数组nums2到nums1并排序。nums1有足够的空间(长度大于等于m+n)来容纳nums2的元素。nums1和nums2初始元素的数量分别是m和n。原创 2017-08-03 17:05:54 · 574 阅读 · 1 评论 -
167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two number原创 2017-08-05 01:23:46 · 232 阅读 · 0 评论 -
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 you原创 2017-08-04 23:33:39 · 263 阅读 · 0 评论 -
268. Missing Number(java)
给定数组包含n个独立的数字(从1到n),找到数组中缺失的那个元素。方法一:遍历数组所有元素做累加得到一个和,让完整的0到n的和(高斯公式)减去这个和,就得到了缺失的元素。原创 2017-08-04 20:31:54 · 298 阅读 · 0 评论