Look through certain port
1 | lsof -i :port |
Kill the pid
1 | kill -9 PID |
Copy the files to your new MacBook
_config.yml
theme
source
.gitignore
package.json
scaffolds
1 | // install hexo |
1 | hexo clean |
1 | hexo clean |
<= p >=p
two part
11111111112,防止分布不均
average time complexity O(nlogn)
worst O(n^2)
space complexity O(1)
non stability
先整体有序,后局部有序
1 | public class Solution { |
worst O(nlogn)
space complexity O(n)
stability
先局部有序,后整体有序
1 | public class Solution { |
O(n)
1 | class Solution { |
1 | public class TwoSum { |
1 | public class TwoSum { |
a+b+c=0
a + b = -c
降维
1 | public List<List<Integer>> threeSum(int[] numbers) { |
a <= b <= c, a + b > c
1 | public int triangleCount(int S[]) { |
remove repeated number, O(n^3)
a+b+c+d = target
fix a, b -> by for loop
1 | public class Solution { |
a+b=-(c+d)
1 | public class Solution { |
DFS
给出一个整数数组 nums
和一个整数 k
。划分数组(即移动数组 nums
中的元素),使得:
k
的元素移到左边k
的元素移到右边返回数组划分的位置,即数组中第一个位置 i
,满足 nums[i]
大于等于 k
。
1 | public int partitionArray(int[] nums, int k) { |
给出一个含有正整数和负整数的数组,重新排列成一个正负数交错的数组。
1 | public class Solution { |
给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红、白、蓝的顺序进行排序。
我们使用整数 0,1 和 2 分别代表红,白,蓝。
左右排完,剩下的也排完了
1 | public class Solution { |
给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,…k的顺序进行排序。
1 | class Solution { |
给一个数组 nums 写一个函数将 0
移动到数组的最后面,非零元素保持原数组的顺序
1 | public class Solution { |
给定一个字符串,判断其是否为一个回文串。只考虑字母和数字,忽略大小写。
1 | public class Solution { |
给一个非空字符串 s
,你最多可以删除一个字符。判断是否可以把它变成回文串。
1 | class Solution { |
1 | private int binarySearch(int[] nums, int target) { |
1 | public class Solution { |
1 | class Solution { |
1 | public class Solution { |
Exponential Backoff
1 | Public int searchBigSortedArray(ArrayReader reader, int target) { |
1 | public int[] kClosestNumbers(int[] A, int[] target, int k) { |
12345654321
if have repeated number cannot use this method
1 | public class Solution { |
0 1 2 4 5 6 7 -> 4 5 6 7 0 1 2
-> 右边第一个位置
1 | public class Solution { |
follow up: [111111110111], repeated number O(n), can not be logn
Solution 1 : find minimum first(two binary search)
1 | public int search(int[] A, int target) { |
Solution 2, directly cut
1 | public int search(int[] A, int target) { |
Find peak element(any peak)
1 | public int findPeak(int[] A) { |
有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k
。给定L和k,你需要计算能够得到的小段木头的最大长度。
1 | public int woodCut(int[] L, int k) { |
Given an integer array nums
and an integer k
, return the length of the shortest non-empty subarray of nums
with a sum of at least k
. If there is no such subarray, return -1
. All the number is positive.
A subarray is a contiguous part of an array.
Example 1:
1 | Input: nums = [1], k = 1 |
Example 2:
1 | Input: nums = [1,2], k = 4 |
Example 3:
1 | Input: nums = [2,1,2], k = 3 |
Constraints:
1 <= nums.length <= 10^5
-10^5 <= nums[i] <= 10^5
1 <= k <= 10^9
Solution1 brute force
Step 1: analyze the question
[1, 2, 3, 4, 5, 6]
[start, end], sum -> time complexity: O(n^3) space complexity: O(1)
1 | class Solution { |
Step 1: analyze the question
[1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6] sum from index 0 -> i, length = nums.length + 1
[start, end], sum -> prefixSum[end + 1] - prefixSum[start]
time complexity: O(n^2) space complexity: O(n)
1 | class Solution { |
Step 1: analyze the question
[1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6] sum from index 0 -> i, length = nums.length + 1
[start, end], sum -> prefixSum[end + 1] - prefixSum[start]
fix start, use binary search to find end
time complexity: O(nlogn) space complexity: O(n)
1 | class Solution { |
Step 1: analyze the question
[1, 2, 3, 4, 5, 6]
i, j two point
i,j -> 0
j++ -> sum>=k
i++ until sum < k, minLength
time complexity: O(n) space complexity: O(1)
1 | class Solution { |
Given string num representing a non-negative integer num
, and an integer k
, return the smallest possible integer after removing k
digits from num
.
https://leetcode.com/problems/remove-k-digits/
1 | class Solution { |
Time complexity: O(n)
Space complexity: O(n)
Given a string s
, return the lexicographically smallest subsequence of s
that contains all the distinct characters of s
exactly once.
https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
1 | class Solution { |
Time complexity: O(n)
Space complexity: O(n)
Given an array of integers temperatures
represents the daily temperatures, return an array answer
such that answer[i]
is the number of days you have to wait after the ith
day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0
instead.
https://leetcode.com/problems/daily-temperatures/
1 | class Solution { |
Time complexity: O(n)
Space complexity: O(n)
You are given the head
of a linked list with n
nodes.
For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.
Return an integer array answer
where answer[i]
is the value of the next greater node of the ith
node (1-indexed). If the ith
node does not have a next greater node, set answer[i] = 0
.
https://leetcode.com/problems/next-greater-node-in-linked-list/
1 | class Solution { |
Time complexity: O(n)
Space complexity: O(n)