- 博客(35)
- 收藏
- 关注
原创 老晨Leetcode 1027. Longest Arithmetic Sequence (Java)
Given an array A of integers, return the length of the longest arithmetic subsequence in A.Recall that a subsequence of A is a list A[i_1], A[i_2], …, A[i_k] with 0 <= i_1 < i_2 < … < i_...
2019-10-02 12:15:39
271
原创 老晨Leetcode 673 最长升序子序列的个数 · Number of Longest Increasing Subsequence
给定一个无序的整数序列,找到最长的升序子序列的个数。Example 1:Input: [1,3,5,4,7]Output: 2Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].这道题很明显是用动态规划做,当你处理到nums[i]这个数的时候,只需要保留以这个数结尾...
2019-09-22 23:30:38
205
原创 老晨Leetcode 659 分割数组为连续子序列 · Split Array into Consecutive Subsequences
给定一个整数数组 nums. 现在需要将 nums 拆分成若干个 (至少1个) 子序列, 并且每个子序列至少包含 3 个连续的整数.返回是否能做到这样的拆分.Example 2:Input: [1,2,3,3,4,4,5,5]Output: TrueExplanation:You can split them into two consecutive subsequences :...
2019-09-19 23:57:54
428
原创 老晨Leetcode 273. Integer to English Words 整数转单词
把一个非负整数转成英文单词的形式。给定的输入一定比2^31-1小。实现不是特别难,但是细节多,网上大神的解法值得背诵关键点一有很多字符串常量的情况用String[] 很清楚,比switch或者map要好看很多而且多注意第一个为0的Index是不是需要是“”,方便后边拿取数据private final String[] LESS_THAN_20 = {"", “One”, “Two”,...
2019-09-17 01:06:41
131
原创 老晨Leetcode 1055. Shortest Way to Form String (Java)
From any string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions).Given two strings source and target, return the minimum number of subsequences...
2019-09-16 11:24:33
2669
原创 老晨Leetcode 792. 满足要求的子串个数Number of Matching Subsequences(Java)
给定一个字符串S 和一个单词字典 words,问, words中一共有多少个单词words[i]是字符串S的子序列?注意, 子序列不同于子串, 子序列不要求连续.这题的一种容易懂的解法是这样的把字符串S里的所有字符统计一下,记录它们出现的位置然后判断一个个word是不是它的子序列的时候,一个个字符去扫描假设这个单词是abc, 原串是adsferertbca一开始找到了a的最左边的...
2019-09-16 10:54:51
751
原创 老晨Leetcode 376. Wiggle Subsequence 摆动序列
如果连续数字之间的差严格地在正和负之间交替,则这样的数字序列称为摆动序列。 第一个差值(如果存在)可以是正的也可以是负的。 少于两个元素的序列通常是摆动序列。例如,[1,7,4,9,2,5]是一个摆动序列,因为连续数字的差(6,-3,5,-7,3)交替为正和负。 相反,[1,4,7,2,5]和[1,7,4,5,5]不是摆动序列,第一个是因为它的前两个连续数字的差是正的,而第二个是因为它的最后一...
2019-09-16 10:28:02
119
原创 晨哥Leetcode 873 最长斐波那契序列的长度 · Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, …, X_n is fibonacci-like if:n >= 3X_i + X_{i+1} = X_{i+2} for all i + 2 <= nGiven a strictly increasing array A of positive integers forming a sequence, find the length ...
2019-09-15 06:09:42
112
原创 晨哥Leetcode 522.最长非公共子序列之2 · Longest Uncommon Subsequence II
给定一组字符串,你需要找到这组字符串中最长的非公共子序列。 最长的非公共子序列被定义为这些字符串之一的最长子序列,并且此子序列不应该是其他字符串的子序列。子序列是可以通过删除一些字符而不改变其余元素的顺序从一个序列导出的序列。可以说,任何字符串都是自身的子序列,空字符串是任何字符串的子序列。输入将是字符串列表,输出需要是最长的非公共子序列的长度。 如果最长的非公共子序列不存在,则返回-1。...
2019-09-14 23:36:37
410
原创 晨哥Leetcode 315. Count of Smaller Numbers After Self--binary search一次bug free写好秘诀
给定一个整数数组nums,返回一个新的counts数组。counts[i]表示:nums[i]右侧比它小的数的个数。比较好的算是是用Binary Indexed Tree, 不过我觉得掌握普通的binary search已经勉强够用了,不追求高难度的话就把下面的普通解法扎实写好就行binary search一次bug free写好秘诀:把一些常见case代进去试试:+当前list为空的情...
2019-09-14 04:21:32
125
原创 晨哥Leetcode 269. Alien Dictionary
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lex...
2019-09-13 11:23:14
138
原创 晨哥Leetcode 207. Course Schedule 拓扑排序套路
There are a total of n courses you have to take, labeled from 0 to n-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...
2019-09-13 09:03:14
152
原创 晨哥Leetcode 497. Random Point in Non-overlapping Rectangles
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the space covered by the rectangles.Note:An integer poin...
2019-09-11 07:29:20
223
原创 晨哥Leetcode 384. Shuffle an Array
Shuffle a set of numbers without duplicates.Example:// Init an array with set 1, 2, and 3.int[] nums = {1,2,3};Solution solution = new Solution(nums);// Shuffle the array [1,2,3] and return its ...
2019-09-11 06:34:01
89
原创 晨哥Leetcode Reservoir sampling题目:Random Pick Index,Linked List Random Node
Reservoir sampling 蓄水池抽样适用于我们不知道符合条件的数据集有多大的情况有一些面试题是这一类的:可否在一未知大小的集合中,随机取出一元素?。或者是Google面试题: I have a linked list of numbers of length N. N is very large and I don’t know in advance the exact value...
2019-09-10 10:53:55
122
原创 晨哥Leetcode 528. Random Pick with Weight
Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight.Note:1 <= w.length <= 1...
2019-09-10 10:03:31
141
原创 晨哥Leetcode 334. Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there exists i, j, ksuch that arr[i] < arr[j]...
2019-09-10 02:29:40
83
原创 晨哥Leetcode 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W.If there is no such window in S that covers all characters in T, return the empty string “”. If...
2019-09-08 23:43:27
1248
原创 晨哥Leetcode 363. Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.Example:Input: matrix = [[1,0,1],[0,-2,3]], k = 2Output: 2...
2019-09-08 02:01:26
180
原创 晨哥Leetcode 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).Example:Input: S = “ADOBECODEBANC”, T = “ABC”Output: “BANC”Note:If the...
2019-09-06 10:38:46
81
原创 晨哥Leetcode 1074. Number of Submatrices That Sum to Target
Given a matrix, and a target, return the number of non-empty submatrices that sum to target.A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y &l...
2019-09-06 09:43:39
1091
原创 晨哥Leetcode 1040. Moving Stones Until Consecutive II
https://leetcode.com/problems/moving-stones-until-consecutive-ii/Example 1:Input: [7,4,9]Output: [1,2]Explanation:We can move 4 -> 8 for one move to finish the game.Or, we can move 9 -> 5...
2019-09-03 07:03:14
592
原创 晨哥Leetcode: sliding window滑动窗口套路
for循环用end指针一直向后,当窗口不再满足条件时,内部while循环解决start指针以下模板应背诵:1004. Max Consecutive Ones III int res = 0; int start = 0, count = 0; for(int end = 0; end< a.length; end++) {//end指针去...
2019-09-03 06:34:22
265
原创 晨哥Leetcode 239. Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window...
2019-09-03 01:02:44
104
原创 晨哥Leetcode 424. Longest Repeating Character Replacement
Given a string s that consists of only uppercase English letters, you can perform at most k operations on that string.In one operation, you can choose any character of the string and change it to a...
2019-09-02 04:57:23
87
原创 晨哥Leetcode 992. Subarrays with K Different Integers
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.(For example, [1,2,3,1,2] h...
2019-09-02 01:44:59
155
原创 晨哥Leetcode 1031. Maximum Sum of Two Non-Overlapping Subarrays
Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray co...
2019-09-01 23:39:40
293
原创 晨哥Leetcode 689. Maximum Sum of 3 Non-Overlapping Subarrays
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum.Each subarray will be of size k, and we want to maximize the sum of all 3*k entries.Return the resu...
2019-09-01 10:39:49
185
原创 晨哥Leetcode 644. Maximum Average Subarray II
Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. And you need to output the maximum average value.E...
2019-09-01 09:15:27
262
原创 晨哥Leetcode 581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.You need to find t...
2019-09-01 08:34:25
77
原创 晨哥Leetcode 325. Maximum Size Subarray Sum Equals k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn’t one, return 0 instead.Note:The sum of the entire nums array is guaranteed to fit within...
2019-09-01 08:11:45
192
原创 晨哥Leetcode 523. Continuous Subarray Sum
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to ...
2019-09-01 02:18:16
123
原创 晨哥Leetcode 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.Example:Input: s = 7, nu...
2019-09-01 01:43:03
76
原创 晨哥Leetcode 974. Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.Example 1:Input: A = [4,5,0,-2,-3,1], K = 5Output: 7Explanation: There are 7 s...
2019-09-01 01:07:33
148
原创 晨哥Leetcode 560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.Example 1:Input:nums = [1,1,1], k = 2Output: 2Note:The length of the a...
2019-09-01 00:51:07
116
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人