
LeetCode
LeetCode
yexianyi
Software Architect @ Siemens
Email: yexianyi@hotmail.com
Linkedin: http://cn.linkedin.com/in/yexianyi/
Github: https://github.com/yexianyi/
展开
-
LeetCode: 974. Subarray Sums Divisible by K
MediumGiven an arrayAof integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible byK.Example 1:Input: A = [4,5,0,-2,-3,1], K = 5Output: 7Explanation: There are 7 subarrays with a sum divisible by K = 5:[4, 5, ...转载 2020-11-20 16:26:40 · 127 阅读 · 0 评论 -
LeetCode: 560. Subarray Sum Equals K
Given an array of integersnumsand an integerk, returnthe total number of continuous subarrays whose sum equals tok.Example 1:Input: nums = [1,1,1], k = 2Output: 2Example 2:Input: nums = [1,2,3], k = 3Output: 2Constraints:1 <= nums...原创 2020-11-18 16:34:43 · 261 阅读 · 0 评论 -
LeetCode: 267. Palindrome Permutation II
MediumGiven a strings, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.Example 1:Input: "aabb"Output: ["abba", "baab"]Example 2:Input: "abc"Output: []解法:.原创 2020-11-15 11:13:31 · 143 阅读 · 0 评论 -
LeetCode:47. Permutations II
Given a collection of numbers,nums,that might contain duplicates, returnall possible unique permutationsin any order.Example 1:Input: nums = [1,1,2]Output:[[1,1,2], [1,2,1], [2,1,1]]Example 2:Input: nums = [1,2,3]Output: [[1,2,3],[1,3,2...原创 2020-11-14 21:14:28 · 150 阅读 · 0 评论 -
LeetCode: 46. Permutations
MediumGiven a collection ofdistinctintegers, return all possible permutations.Example:Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]解法:(回溯)本题要求生成全排列,那么看看我们能找到什么规律。以[1,2,3]为例,全排列是[1,2,3][1,3,2][2..原创 2020-11-13 21:15:28 · 106 阅读 · 0 评论 -
LeetCode:31. Next Permutation
31.Next PermutationMediumImplementnext permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in a..原创 2020-11-10 12:44:23 · 111 阅读 · 0 评论 -
LeetCode: 核心算法(快速选择)
场景:解决Top K问题例如:输入: [3,2,1,5,6,4] 和 k = 2输出: 5解决方法:此类问题一般都是通过一次快排或者大顶堆的方式解决,但是都有问题:快排:对全数组进行了排序,如果要寻找的是第K个大的元素,那么其实我们并不关心从小到大排序后数组的第K位之后的数字;但是快排依然对这些数字进行了排序大顶堆:虽然大顶堆的方法解决了不用考虑第K位之后的数字的问题,但是它依然需要关心第K位之前的数字,所以依然浪费了很多时间问题分析仔细想想Top K问题,其实.原创 2020-10-21 10:47:32 · 354 阅读 · 0 评论 -
LeetCode: 常用API及算法
1. 小顶堆 (从小到大)Queue<Integer> heap = new PriorityQueue<>() ;2. 大顶堆 (由大到小) Queue<Integer> heap = new PriorityQueue<>((o1, o2) -> o2 - o1) ;原创 2020-10-21 10:46:52 · 381 阅读 · 0 评论 -
LeetCode:215. Kth Largest Element in an Array
MediumFind thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:Input: [3,2,1,5,6,4] and k = 2Output: 5Example 2:Input: [3,2,3,1,2,4,5,5,6] and k = 4.原创 2020-10-13 11:33:15 · 109 阅读 · 0 评论 -
LeetCode:159. Longest Substring with At Most Two Distinct Characters
Given a string s , find the length of the longest substring t that contains at most 2 distinct characters. Example 1: Input: "eceba" Output: 3 Explanation: t is "ece" which its length is 3. Example 2: Input: "ccaabbb...原创 2020-10-12 14:30:20 · 190 阅读 · 0 评论 -
Leetcode: 3. Longest Substring Without Repeating Characters
Given a strings, find the length of thelongest substringwithout repeating characters.Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answ...原创 2020-10-12 10:28:56 · 99 阅读 · 0 评论 -
Leetcode: 16. 3Sum Closest
16.3Sum ClosestMediumGiven an arraynumsofnintegers and an integertarget, find three integers innumssuch that the sum is closest totarget. Return the sum of the three integers. You may assume that each input would have exactly one solution.Ex...原创 2020-09-24 14:41:57 · 120 阅读 · 0 评论 -
Leetcode: 15. 3Sum
Given an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Notice that the solution set must not contain duplicate triplets.Example 1:Input: nums =...原创 2020-09-24 10:33:36 · 94 阅读 · 0 评论 -
Leetcode: 1. Two Sum
1.Two SumEasyGiven an array of integers, returnindicesof the two numbers such that they add up to a specific target.You may assume that each input would haveexactlyone solution, and you may not use thesameelement twice.Example:Given nums ...原创 2020-08-06 16:17:11 · 151 阅读 · 0 评论 -
Leetcode: 209. Minimum Size Subarray Sum
Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, return 0 instead.Example:Input: s = 7, ...原创 2020-01-22 17:14:17 · 132 阅读 · 0 评论 -
Leetcode: 207. Course Schedule
There are a total ofncourses you have to take, labeled from0ton-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair...原创 2020-01-22 14:21:01 · 146 阅读 · 0 评论 -
Leetcode:179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.Example 1:Input: [10,2]Output: "210"Example 2:Input: [3,30,34,5,9]Output: "9534330"Note:The r...原创 2020-01-20 16:12:57 · 186 阅读 · 0 评论 -
Leetcode 152:Maximum Product Subarray
Given an integer arraynums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation:[2,3] h...原创 2020-01-20 13:57:35 · 222 阅读 · 0 评论 -
Leetcode 150: Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Note:Division between two integers ...原创 2020-01-19 13:04:26 · 201 阅读 · 0 评论 -
Leetcode 148: Sort List
Sort a linked list inO(nlogn) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0Output: -1-...原创 2020-01-10 15:10:44 · 119 阅读 · 0 评论 -
LeetCode 146:LRU Cache
Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput.get(key)- Get the value (will always be positive) of the key if th...原创 2020-01-09 17:15:31 · 155 阅读 · 0 评论 -
Leetcode 143: Reorder List
143.Reorder ListGiven a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You maynotmodify the values in the list's nodes, only nodes itself may be changed.Example ...原创 2019-12-23 17:34:46 · 203 阅读 · 0 评论 -
Leetcode: 139 Word Break
139.Word BreakGiven anon-emptystringsand a dictionarywordDictcontaining a list ofnon-emptywords, determine ifscan be segmented into a space-separated sequence of one or more dictionary wo...原创 2019-12-16 18:28:21 · 150 阅读 · 0 评论 -
Leetcode 127: Word Ladder
127.Word LadderGiven two words (beginWordandendWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWordtoendWord, such that:Only one letter can ...原创 2019-12-16 16:20:57 · 139 阅读 · 0 评论 -
Leetcode 91. Decode Ways
A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given anon-emptystring containing only digits, determine t...原创 2019-12-09 14:53:21 · 126 阅读 · 0 评论 -
LeetCode:5. Longest Palindromic Substring
Given a strings, find the longest palindromic substring ins. You may assume that the maximum length ofsis 1000.Example 1:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Exa...原创 2019-09-06 19:29:28 · 267 阅读 · 0 评论 -
LeetCode:3. Longest Substring Without Repeating Characters
3.Longest Substring Without Repeating CharactersMediumGiven a string, find the length of thelongest substringwithout repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation...原创 2019-09-06 17:27:03 · 137 阅读 · 0 评论