- LeetCode1.两数之和
- LeetCode4 寻找两个正序数组的中位数
- LeetCode11 盛水最多的容器
- LeetCode15 三数之和
- LeetCode16最接近的三数之和
- LeetCode18 四数之和
- LeetCode26 删除有序数组的重复项
- LeetCode移除元素
- LeetCode31下一个排列
- LeetCode33 搜索旋转排序数组
- LeetCode34 排序数组中找元素第一个和最后一个位置
- LeetCode35 搜索插入位置
- LeetCode36 有效的数独
- LeetCode37 解数独
- LeetCode39 组合总和
- LeetCode40 组合总和2
- LeetCode41 缺失的第一个正数
- LeetCode42 接雨水
- LeetCode45 跳跃游戏
- LeetCode46 全排列
- Leetcode47 全排列
- LeetCode48 旋转图像
- LeetCode51 N皇后
- LeetCode53 最大子数组和
- LeetCode54 螺旋矩阵
- LeetCode55 跳跃游戏
- LeetCode56 合并区间
- LeetCode57 插入区间
- Leetcode59 螺旋矩阵2
- LeetCode63不同路径2
- LeetCode64最小路径和
- LeetCode66 加一
- LeetCode73 矩阵置0
- LeetCode74 搜索二维矩阵
- LeetCode75 颜色分类
- LeetCode78 子集
- LeetCode79 单词搜索
- LeetCode80 删除数组中的重复项2
- LeetCode81搜索旋转排序数组
- LeetCode84 柱状图最大矩形
- LeetCode85 最大矩形
- LeetCode88 合并两个有序数组
- LeetCode90 子集2
- LeetCode105 前序中序构造树
- LeetCode106 中序后序构造树
- LeetCode108 有序数组到二叉搜索树
- LeetCode118 杨辉三角
- LeetCode119 杨辉三角2
- LeetCode120 三角形最小路径和
- LeetCode121 买卖股票最佳时机
- LeetCode122.买卖股票最佳时机2
- LeetCode123 买卖股票最佳时机3
- LeetCode128 最长连续序列
- Leetcode130被围绕的区域
- LeetCode134 加油站
- LeetCode135 分发糖果
- LeetCode136 只出现一次的数字
- LeetCode137 只出现一次的数字2
- LeetCode149 直线上最多的点数
- LeetCode150 逆波兰表达式求值
- LeetCode152 乘积最大子数组
- LeetCode153 寻找排序数组中最小数字
- LeetCode154 寻找排序树组最小值2
- LeetCode162 寻找峰值
- LeetCode164 最大间距
- LeetCode167 两数之和2
- LeetCode169 多数元素
- LeetCode174 地下城游戏
- LeetCode188买卖股票最佳时机4
- LeetCode189 轮转数组
- LeetCode198 打家劫舍
- LeetCode200 岛屿数量
- LeetCode204 计算质数
- LeetCode209 长度最小的子数组
- LeetCode212 单词搜索2
- LeetCode213 打家劫舍2
- LeetCode215 数组中第K大的元素
- LeetCode216组合总和3
- LeetCode217 存在重复元素
- LeetCode218 天际线问题
- LeetCode219 存在重复元素2
- LeetCode220 存在重复元素3
- LeetCode221最大正方形
- LeetCode228 汇总区间
- LeetCode229 求众数2
- LeetCode238 除自身以外数组的乘积
- LeetCode239 滑动窗口最大值
- LeetCode240 搜索二维矩阵2
- LeetCode260只出现一次的数字3
- LeetCode268 丢失的数字
- LeetCode274 H指数
- LeetCode275 H指数2
- LeetCode283 移动0
- LeetCode284 顶端迭代器
- LeetCode287 寻找重复数
- LeetCode289 生命游戏
- LeetCode300 最长子序列
- LeetCode303 区域和检索-数组不可变
- LeetCode307 区间检索-数组可修改
- LeetCode309 股票最佳购买时机含冷冻时间
- LeetCode312 戳气球
- LeetCode313 超级丑数
- LeetCode315 计算右侧小于当前元素的个数
- LeetCode318 最大单词长度乘积
- LeetCode322 零钱兑换
- LeetCode324 摆动排序
- LeetCode327 区间和的个数
- LeetCode330 按要求补全数组
- LeetCode334递增的三元数列
- LeetCode335 路径交叉
- LeetCode336 回文对
- LeetCode347 前k个高频元素
- LeetCode349 两个数组的交集
- LeetCode350 两个数组交集2
- LeetCode354 俄罗斯套娃信封问题
- LeetCode363 矩形区域不超过k的最大数值和
- LeetCode368 最大整除子集
- LeetCode373 查找和最小的K对数字
- LeetCode376 摆动序列
- LeetCode377 组合总和4
- LeetCode378 有序矩阵中第K小的元素
- LeetCode380O(1)时间插入、删除、获取随机元素
- LeetCode381 O(1)时间插入、删除、获取随机元素包含重复元素
- LeetCode384 打乱数组
- LeetCode391 完美矩形
- LeetCode393 UTF-8编码验证
- LeetCode396 旋转函数
- LeetCode399 除法求值
- LeetCode403 青蛙过河
- LeetCode406 根据身高重建队列
- LeetCode407 接雨水
- LeetCode410 分割数组的最大值
- LeetCode413 等差数列划分
- LeetCode414 第三大的数
- LeetCode416 分割等和子集
- LeetCode417 太平洋大西洋水流问题
- LeetCode419 甲板上的战舰
- LeetCode421 数组中的最大异或值
- LeetCode427 建立四叉树
- LeetCode435 无重叠区间
- LeetCode436 寻找右区间
- LeetCode442 数组中重复的数据
- LeetCode446 等差数列划分2
- LeetCode447 回旋镖的数量
- LeetCode448找到所有数组中消失的数字
- LeetCode452 用最少的箭引爆气球
- LeetCode453 最少操作次数使数组元素相等
- LeetCode454 四数相加2
- LeetCode455 分发饼干
- LeetCode456 132模式
- LeetCode457 环形数组是否存在循环
- LeetCode462 最少移动次数使数组元素相等2
- LeetCode463 岛屿的周长
- LeetCode472 连接词
- LeetCode473 火柴拼正方形
- LeetCode474一和零
- LeetCode475 供暖器
- LeetCode477 汉明距离总和
- LeetCode480滑动窗口中位数
- LeetCode485最大连续1的个数
- LeetCode486 预测赢家
- LeetCode491 递增子序列
- LeetCode493 翻转对
- LeetCode494 目标和
- LeetCode495 提莫攻击
- LeetCode496 下一个更大元素
- LeetCode498对角线遍历
- LeetCode500 键盘行
LeetCode1.两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
class Solution {public int[] twoSum(int[] nums, int target) {HashMap<Integer, Integer> map = new HashMap<>();for(int i = 0; i < nums.length; i++){if(map.containsKey(target - nums[i])){return new int[]{i,map.get(target - nums[i])};}map.put(nums[i], i);}return new int[]{};}}
LeetCode4 寻找两个正序数组的中位数
顾名思义,暴力法直接合并两个数组
public double findMedianSortedArrays(int[] nums1, int[] nums2) {int[] nums;int m = nums1.length;int n = nums2.length;nums = new int[m + n];if (m == 0) {if (n % 2 == 0) {return (nums2[n / 2 - 1] + nums2[n / 2]) / 2.0;} else {return nums2[n / 2];}}if (n == 0) {if (m % 2 == 0) {return (nums1[m / 2 - 1] + nums1[m / 2]) / 2.0;} else {return nums1[m / 2];}}int count = 0;int i = 0, j = 0;while (count != (m + n)) {if (i == m) {while (j != n) {nums[count++] = nums2[j++];}break;}if (j == n) {while (i != m) {nums[count++] = nums1[i++];}break;}if (nums1[i] < nums2[j]) {nums[count++] = nums1[i++];} else {nums[count++] = nums2[j++];}}if (count % 2 == 0) {return (nums[count / 2 - 1] + nums[count / 2]) / 2.0;} else {return nums[count / 2];}}
进阶版,每次排除k/2个
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n = nums1.length;
int m = nums2.length;
int left = (n + m + 1) / 2;
int right = (n + m + 2) / 2;
//将偶数和奇数的情况合并,如果是奇数,会求两次同样的 k 。
return (getKth(nums1, 0, n - 1, nums2, 0, m - 1, left) + getKth(nums1, 0, n - 1, nums2, 0, m - 1, right)) * 0.5;
}
private int getKth(int[] nums1, int start1, int end1, int[] nums2, int start2, int end2, int k) {
int len1 = end1 - start1 + 1;
int len2 = end2 - start2 + 1;
//让 len1 的长度小于 len2,这样就能保证如果有数组空了,一定是 len1
if (len1 > len2) return getKth(nums2, start2, end2, nums1, start1, end1, k);
if (len1 == 0) return nums2[start2 + k - 1];
if (k == 1) return Math.min(nums1[start1], nums2[start2]);
int i = start1 + Math.min(len1, k / 2) - 1;
int j = start2 + Math.min(len2, k / 2) - 1;
if (nums1[i] > nums2[j]) {
return getKth(nums1, start1, end1, nums2, j + 1, end2, k - (j - start2 + 1));
}
else {
return getKth(nums1, i + 1, end1, nums2, start2, end2, k - (i - start1 + 1));
}
}
LeetCode11 盛水最多的容器
输入:[1,8,6,2,5,4,8,3,7]
输出:49
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

class Solution {
public int maxArea(int[] height) {
int i = 0, j = height.length - 1, res = 0;
while(i < j){
res = height[i] < height[j] ?
Math.max(res, (j - i) * height[i++]):
Math.max(res, (j - i) * height[j--]);
}
return res;
}
}
LeetCode15 三数之和
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
三指针,固定一个后面两个移动,注意去重
class Solution {
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList();
int len = nums.length;
if(nums == null || len < 3) return ans;
Arrays.sort(nums); // 排序
for (int i = 0; i < len ; i++) {
if(nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
int L = i+1;
int R = len-1;
while(L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum == 0){
ans.add(Arrays.asList(nums[i],nums[L],nums[R]));
while (L<R && nums[L] == nums[L+1]) L++; // 去重
while (L<R && nums[R] == nums[R-1]) R--; // 去重
L++;
R--;
}
else if (sum < 0) L++;
else if (sum > 0) R--;
}
}
return ans;
}
}
LeetCode16最接近的三数之和
与上一道题一样,三指针,不用考虑去重
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int ans = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length; i++) {
int start = i + 1, end = nums.length - 1;
while (start < end){
int sum = nums[i] + nums[start] + nums[end];
if(Math.abs(sum - target) < Math.abs(ans - target)){
ans = sum;
}
if(sum > target)
end--;
else if(sum < target)
start++;
else
return ans;
}
}
return ans;
}
}
LeetCode18 四数之和
输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
和上两道题一样,三数需要固定一个另外两个while循环,四数之和需要两重循环固定两个之后在while,而且可以通过每次循环前判断最大最小值是否满足条件来减少循环次数
class Solution {
public List<List<Integer>> fourSum(int[] nums,int target){
/*定义一个返回值*/
List<List<Integer>> result=new ArrayList<>();
/*当数组为null或元素小于4个时,直接返回*/
if(nums==null||nums.length<4){
return result;
}
/*对数组进行从小到大排序*/
Arrays.sort(nums);
/*数组长度*/
int length=nums.length;
/*定义4个指针k,i,j,h k从0开始遍历,i从k+1开始遍历,留下j和h,j指向i+1,h指向数组最大值*/
for(int k=0;k<length-3;k++){
/*当k的值与前面的值相等时忽略*/
if(k>0&&nums[k]==nums[k-1]){
continue;
}
/*获取当前最小值,如果最小值比目标值大,说明后面越来越大的值根本没戏*/
long min1=(long)nums[k]+nums[k+1]+nums[k+2]+nums[k+3];
if(min1>target){
break;
}
/*获取当前最大值,如果最大值比目标值小,说明后面越来越小的值根本没戏,忽略*/
long max1=(long)nums[k]+nums[length-1]+nums[length-2]+nums[length-3];
if(max1<target){
continue;
}
/*第二层循环i,初始值指向k+1*/
for(int i=k+1;i<length-2;i++){
/*当i的值与前面的值相等时忽略*/
if(i>k+1&&nums[i]==nums[i-1]){
continue;
}
/*定义指针j指向i+1*/
int j=i+1;
/*定义指针h指向数组末尾*/
int h=length-1;
/*获取当前最小值,如果最小值比目标值大,说明后面越来越大的值根本没戏*/
long min=(long)nums[k]+nums[i]+nums[j]+nums[j+1];
if(min>target){
break;
}
/*获取当前最大值,如果最大值比目标值小,说明后面越来越小的值根本没戏,忽略*/
long max=(long)nums[k]+nums[i]+nums[h]+nums[h-1];
if(max<target){
continue;
}
/*开始j指针和h指针的表演,计算当前和,如果等于目标值,j++并去重,h--并去重,当当前和大于目标值时h--,当当前和小于目标值时j++*/
while (j<h){
int curr=nums[k]+nums[i]+nums[j]+nums[h];
if(curr==target){
result.add(Arrays.asList(nums[k],nums[i],nums[j],nums[h]));
j++;
while(j<h&&nums[j]==nums[j-1]){
j++;
}
h--;
while(j<h&&i<h&&nums[h]==nums[h+1]){
h--;
}
}else if(curr>target){
h--;
}else {
j++;
}
}
}
}
return result;
}
}
LeetCode26 删除有序数组的重复项
输入:nums = [1,1,2]
输出:2, nums = [1,2,_]
解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null|| nums.length ==0)
return 0;
int l = 0;
for(int r = 1; r <nums.length; r++){
if(nums[l] != nums[r])
nums[++l] = nums[r];
}
return ++l;
}
}
LeetCode移除元素
输入:nums = [3,2,2,3], val = 3
输出:2, nums = [2,2]
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
直接遍历赋值就行
class Solution {
public int removeElement(int[] nums, int val) {
int ans = 0;
for(int num: nums) {
if(num != val) {
nums[ans] = num;
ans++;
}
}
return ans;
}
}
LeetCode31下一个排列
输入:nums = [1,2,3]
输出:[1,3,2]
从后往前找第一个递减的,在这个数后面找第一个比他大的,交换后,把后面的逆序
class Solution {
public void nextPermutation(int[] nums) {
int left = nums.length - 2;
while(left >= 0&& nums[left] >= nums[left + 1])
left--;
if(left >= 0){
int right = nums.length - 1;
while(nums[right] <= nums[left]){
right--;
}
swap(nums, left, right);
}
reverse(nums, left + 1, nums.length - 1);
}
private void reverse(int[] nums, int left, int right){
while (left < right)
swap(nums, left++, right--);
}
private void swap(int[] nums, int left, int right){
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
}
}
LeetCode33 搜索旋转排序数组
输入:nums = [4,5,6,7,0,1,2], target = 0
输出:4
本质还是二分,找哪部分有序即可
class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
int mid;
while (start <= end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
}
//前半部分有序,注意此处用小于等于
if (nums[start] <= nums[mid]) {
//target在前半部分
if (target >= nums[start] && target < nums[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
} else {
if (target <= nums[end] && target > nums[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
return -1;
}
}
LeetCode34 排序数组中找元素第一个和最后一个位置
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
public class Solution {
public int[] searchRange(int[] nums, int target) {
int len = nums.length;
if (len == 0) {
return new int[]{-1, -1};
}
int firstPosition = findFirstPosition(nums, target);
if (firstPosition == -1) {
return new int[]{-1, -1};
}
int lastPosition = findLastPosition(nums, target);
return new int[]{firstPosition, lastPosition};
}
private int findFirstPosition(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
// 小于一定不是解
if (nums[mid] < target) {
// 下一轮搜索区间是 [mid + 1, right]
left = mid + 1;
} else if (nums[mid] == target) {
// 下一轮搜索区间是 [left, mid]
right = mid;
} else {
// nums[mid] > target,下一轮搜索区间是 [left, mid - 1]
right = mid - 1;
}
}
if (nums[left] == target) {
return left;
}
return -1;
}
private int findLastPosition(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left + 1) / 2;
if (nums[mid] > target) {
// 下一轮搜索区间是 [left, mid - 1]
right = mid - 1;
} else if (nums[mid] == target){
// 下一轮搜索区间是 [mid, right]
left = mid;
} else {
// nums[mid] < target,下一轮搜索区间是 [mid + 1, right]
left = mid + 1;
}
}
return left;
}
}
LeetCode35 搜索插入位置
输入: nums = [1,3,5,6], target = 5
输出: 2
public class Solution {
public int searchInsert(int[] nums, int target) {
int len = nums.length;
// 特殊判断
if (nums[len - 1] < target) {
return len;
}
// 程序走到这里一定有 nums[len - 1] >= target,插入位置在区间 [0..len - 1]
int left = 0;
int right = len - 1;
// 在区间 nums[left..right] 里查找第 1 个大于等于 target 的元素的下标
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target){
// 下一轮搜索的区间是 [mid + 1..right]
left = mid + 1;
} else {
// 下一轮搜索的区间是 [left..mid]
right = mid;
}
}
return left;
}
}
LeetCode36 有效的数独
输入:board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:true

class Solution {
public boolean isValidSudoku(char[][] board) {
Map<Integer, Set<Integer>> row = new HashMap<>(), col = new HashMap<>(), area = new HashMap<>();
for (int i = 0; i < 9; i++) {
row.put(i, new HashSet<>());
col.put(i, new HashSet<>());
area.put(i, new HashSet<>());
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char c = board[i][j];
if (c == '.') continue;
int u = c - '0';
int idx = i / 3 * 3 + j / 3;
if (row.get(i).contains(u) || col.get(j).contains(u) || area.get(idx).contains(u)) return false;
row.get(i).add(u);
col.get(j).add(u);
area.get(idx).add(u);
}
}
return true;
}
}
LeetCode37 解数独
输入:board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
输出:[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
class Solution {
public void solveSudoku(char[][] board) {
solveSudokuHelper(board);
}
private boolean solveSudokuHelper(char[][] board){
//「一个for循环遍历棋盘的行,一个for循环遍历棋盘的列,
// 一行一列确定下来之后,递归遍历这个位置放9个数字的可能性!」
for (int i = 0; i < 9; i++){ // 遍历行
for (int j = 0; j < 9; j++){ // 遍历列
if (board[i][j] != '.'){ // 跳过原始数字
continue;
}
for (char k = '1'; k <= '9'; k++){ // (i, j) 这个位置放k是否合适
if (isValidSudoku(i, j, k, board)){
board[i][j] = k;
if (solveSudokuHelper(board)){ // 如果找到合适一组立刻返回
return true;
}
board[i][j] = '.';
}
}
// 9个数都试完了,都不行,那么就返回false
return false;
// 因为如果一行一列确定下来了,这里尝试了9个数都不行,说明这个棋盘找不到解决数独问题的解!
// 那么会直接返回, 「这也就是为什么没有终止条件也不会永远填不满棋盘而无限递归下去!」
}
}
// 遍历完没有返回false,说明找到了合适棋盘位置了
return true;
}
/**
* 判断棋盘是否合法有如下三个维度:
* 同行是否重复
* 同列是否重复
* 9宫格里是否重复
*/
private boolean isValidSudoku(int row, int col, char val, char[][] board){
// 同行是否重复
for (int i = 0; i < 9; i++){
if (board[row][i] == val){
return false;
}
}
// 同列是否重复
for (int j = 0; j < 9; j++){
if (board[j][col] == val){
return false;
}
}
// 9宫格里是否重复
int startRow = (row / 3) * 3;
int startCol = (col / 3) * 3;
for (int i = startRow; i < startRow + 3; i++){
for (int j = startCol; j < startCol + 3; j++){
if (board[i][j] == val){
return false;
}
}
}
return true;
}
}
LeetCode39 组合总和
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer>list = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
helper(candidates,0,target);
return res;
}
public void helper(int[]candidates,int start,int target){
if(target==0){
res.add(new ArrayList<Integer>(list));
return;
}
for(int i=start;i<candidates.length;i++){
if(candidates[i]>target) break;
list.add(candidates[i]);
helper(candidates,i,target-candidates[i]);
list.remove(list.size()-1);
}
}
}
LeetCode40 组合总和2
相较于上题不能有重复的
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer>list = new ArrayList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
helper(candidates,0,target);
return res;
}
public void helper(int[]candidates,int start,int target){
if(target==0){
res.add(new ArrayList<Integer>(list));
return;
}
for(int i=start;i<candidates.length;i++){
if(candidates[i]>target) break;
//就多这一步
if(i>start&&candidates[i]==candidates[i-1]) continue;
list.add(candidates[i]);
helper(candidates,i+1,target-candidates[i]);
list.remove(list.size()-1);
}
}
}
LeetCode41 缺失的第一个正数
主要难点在于需要时间复杂度On,常数级别空间复杂度,所以不能用set判断,原地哈希就是将这个数放在索引为x-1的位置上
输入:nums = [1,2,0]
输出:3
class Solution {
public int firstMissingPositive(int[] nums) {
for (int i = 0; i < nums.length; i++){
if (i + 1 == nums[i])
continue;
int x = nums[i];
if (x >= 1 && x <= nums.length && x != nums[x - 1]){
swap(nums, i--, x - 1);
}
}
for (int i = 0; i < nums.length; i++){
if (i + 1 != nums[i])
return i + 1;
}
return nums.length + 1;
}
public void swap(int[] A, int i, int j){
if (i != j){
A[i] ^= A[j];
A[j] ^= A[i];
A[i] ^= A[j];
}
}
}
LeetCode42 接雨水
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。

class Solution {
public int trap(int[] height) {
if (height.length <= 2)
return 0;
int water = 0;
int left = 0, right = height.length - 1;
while (left < right && height[left] <= height[left + 1])
left++;
while (left < right && height[right] <= height[right - 1])
right--;
while(left < right){
int leftVal = height[left];
int rightVal = height[right];
if(leftVal <= rightVal){
while(left < right&& height[++left] < leftVal){
water += leftVal - height[left];
}
}else{
while(left < right&& height[--right] < rightVal)
water += rightVal - height[right];
}
}
return water;
}
}
或者单调栈
class Solution {
public int trap(int[] height) {
Stack<Integer> stack = new Stack<Integer>();
int water = 0;
//特殊情况
if(height.length <3){
return 0;
}
for(int i = 0; i < height.length; i++){
while(!stack.isEmpty() && height[i] > height[stack.peek()]){
//栈顶元素
int popnum = stack.pop();
//相同元素的情况例1,1
while(!stack.isEmpty()&&height[popnum] == height[stack.peek()]){
stack.pop();
}
//计算该层的水的单位
if(!stack.isEmpty()){
int temp = height[stack.peek()];//栈顶元素值
//高
int hig = Math.min(temp-height[popnum],height[i]-height[popnum]);
//宽
int wid = i-stack.peek()-1;
water +=hig * wid;
}
}
//这里入栈的是索引
stack.push(i);
}
return water;
}
}
LeetCode45 跳跃游戏
问的是最少跳跃次数,贪心算法
输入: nums = [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
class Solution {
public int jump(int[] nums) {
int position = nums.length - 1;
int steps = 0;
while (position > 0) {
for (int i = 0; i < position; i++) {
if (i + nums[i] >= position) {
position = i;
steps++;
break;
}
}
}
return steps;
}
}
LeetCode46 全排列
经典回溯问题
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
backtrack(list, new ArrayList<>(), nums);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums){
if (tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
return;
}
for (int i = 0; i < nums.length; i++){
if (tempList.contains(nums[i]))continue;
tempList.add(nums[i]);
backtrack(list, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}
Leetcode47 全排列
存在重复元素
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
class Solution {
List<List<Integer>> res = new ArrayList<>();
int[] visited;
public List<List<Integer>> permuteUnique(int[] nums) {
visited = new int[nums.length];
if(nums==null||nums.length==0)
return res;
Arrays.sort(nums);
helper(nums,new ArrayList<Integer>());
return res;
}
public void helper(int[] nums,List<Integer>cur){
if(cur.size() ==nums.length){
res.add(new ArrayList<Integer>(cur));
return;
}
for(int i =0;i<nums.length;i++){
if(visited[i]==1)
continue;
if(i>0&&nums[i]==nums[i-1]&&visited[i-1]==1)
continue;
visited[i]=1;
cur.add(nums[i]);
helper(nums,cur);
cur.remove(cur.size()-1);
visited[i]=0;
}
}
}
LeetCode48 旋转图像
找好对应位置,循环即可
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]
class Solution {
public static void rotate(int[][] matrix) {
int tR = 0;
int tC = 0;
int dR = matrix.length - 1;
int dC = matrix[0].length - 1;
while (tR < dR) {
rotateEdge(matrix, tR++, tC++, dR--, dC--);
}
}
public static void rotateEdge(int[][] m, int tR, int tC, int dR, int dC) {
int times = dC - tC;
int tmp = 0;
for (int i = 0; i != times; i++) {
tmp = m[tR][tC + i];
m[tR][tC + i] = m[dR - i][tC];
m[dR - i][tC] = m[dR][dC - i];
m[dR][dC - i] = m[tR + i][dC];
m[tR + i][dC] = tmp;
}
}
}
LeetCode51 N皇后
经典回溯题
class Solution {
List<List<String>> res = new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
char[][] chessboard = new char[n][n];
for (char[] c : chessboard) {
Arrays.fill(c, '.');
}
backTrack(n, 0, chessboard);
return res;
}
public void backTrack(int n, int row, char[][] chessboard) {
if (row == n) {
res.add(Array2List(chessboard));
return;
}
for (int col = 0;col < n; ++col) {
if (isValid (row, col, n, chessboard)) {
chessboard[row][col] = 'Q';
backTrack(n, row+1, chessboard);
chessboard[row][col] = '.';
}
}
}
public List Array2List(char[][] chessboard) {
List<String> list = new ArrayList<>();
for (char[] c : chessboard) {
list.add(String.copyValueOf(c));
}
return list;
}
public boolean isValid(int row, int col, int n, char[][] chessboard) {
// 检查列
for (int i=0; i<row; ++i) { // 相当于剪枝
if (chessboard[i][col] == 'Q') {
return false;
}
}
// 检查45度对角线
for (int i=row-1, j=col-1; i>=0 && j>=0; i--, j--) {
if (chessboard[i][j] == 'Q') {
return false;
}
}
// 检查135度对角线
for (int i=row-1, j=col+1; i>=0 && j<=n-1; i--, j++) {
if (chessboard[i][j] == 'Q') {
return false;
}
}
return true;
}
}
LeetCode53 最大子数组和
定义sum大于0就留着,小于0就重新定义
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
class Solution {
public int maxSubArray(int[] nums) {
int ans = nums[0];
int sum=0;
for(int i = 0;i<nums.length;i++){
if(sum>0){
sum+=nums[i];
}
else{
sum=nums[i];
}
ans = Math.max(sum,ans);
}
return ans;
}
}
LeetCode54 螺旋矩阵
旋转输出
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
if(matrix.length == 0) return new ArrayList<Integer>();
int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1;
List<Integer> res= new ArrayList<>();
while(true) {
for(int i = l; i <= r; i++) res.add(matrix[t][i]); // left to right.
if(++t > b) break;
for(int i = t; i <= b; i++) res.add(matrix[i][r]); // top to bottom.
if(l > --r) break;
for(int i = r; i >= l; i--) res.add(matrix[b][i]); // right to left.
if(t > --b) break;
for(int i = b; i >= t; i--) res.add(matrix[i][l]); // bottom to top.
if(++l > r) break;
}
return res;
}
}
LeetCode55 跳跃游戏
输入:nums = [2,3,1,1,4]
输出:true
解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
从后往前推,相当于终点不断前移
class Solution {
public boolean canJump(int[] nums) {
int last = nums.length - 1;
for (int i = nums.length - 2; i >= 0; i--){
if (i + nums[i] >= last)
last = i;
}
return last == 0;
}
}
LeetCode56 合并区间
输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
输出:[[1,6],[8,10],[15,18]]
解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, Comparator.comparingInt(v -> v[0]));
List<int[]> list = new ArrayList<>();
list.add(intervals[0]);
for (int i = 0; i < intervals.length; i++) {
int[] cur = intervals[i];
int[] peek = list.get(list.size() - 1);
if(cur[0] > peek[1]){
list.add(cur);
}else {
peek[1] = Math.max(peek[1], cur[1]);
}
}
return list.toArray(new int[list.size()][]);
}
}
LeetCode57 插入区间
相较于上题,先把无重叠的部分加到结果里,然后就剩一个区间,选个左右界即可
输入:intervals = [[1,3],[6,9]], newInterval = [2,5]
输出:[[1,5],[6,9]]
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
if (intervals.length == 0)
return new int[][]{newInterval};
List<int[]> resList = new ArrayList<>();
int left = 0;
int right = intervals.length - 1;
while (left < intervals.length && intervals[left][1] < newInterval[0])
resList.add(intervals[left++]);
while (right >= 0 && intervals[right][0] > newInterval[1])
resList.add(left, intervals[right--]);
int[] newArr = new int[2];
newArr[0] = left >= intervals.length ? newInterval[0] : Math.min(intervals[left][0], newInterval[0]);
newArr[1] = right < 0 ? newInterval[1] : Math.max(intervals[right][1], newInterval[1]);
resList.add(left, newArr);
int[][] resArr = new int[resList.size()][2];
for (int i = 0; i < resList.size(); i++){
resArr[i] = resList.get(i);
}
return resArr;
}
}
Leetcode59 螺旋矩阵2
与上一个螺旋矩阵思路相同
输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
class Solution {
public int[][] generateMatrix(int n) {
int l = 0, r = n - 1, t = 0, b = n - 1;
int[][] mat = new int[n][n];
int num = 1, tar = n * n;
while(num <= tar){
for(int i = l; i <= r; i++) mat[t][i] = num++; // left to right.
t++;
for(int i = t; i <= b; i++) mat[i][r] = num++; // top to bottom.
r--;
for(int i = r; i >= l; i--) mat[b][i] = num++; // right to left.
b--;
for(int i = b; i >= t; i--) mat[i][l] = num++; // bottom to top.
l++;
}
return mat;
}
}
LeetCode63不同路径2
经典动态规划题
输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出:2
解释:3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int dp[][] = new int[m][n];
for (int i = 0; i < m; i++){
if (obstacleGrid[i][0] == 0)
dp[i][0] = 1;
else
break;
}
for (int i = 0; i < n; i++){
if (obstacleGrid[0][i] == 0)
dp[0][i] = 1;
else
break;
}
for (int i = 1; i < m; ++i){
for (int j = 1; j < n; ++j){
if (obstacleGrid[i][j] == 0)
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m-1][n-1];
}
}
LeetCode64最小路径和
与上一道题一样,只能往下或者往右走,选小的就行
输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
输出:7
解释:因为路径 1→3→1→1→1 的总和最小。
class Solution {
public int minPathSum(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (i == 0 && j == 0) continue;
else if (i == 0) grid[i][j] = grid[i][j - 1] + grid[i][j];
else if (j == 0) grid[i][j] = grid[i - 1][j] + grid[i][j];
else grid[i][j] = Math.min(grid[i][j - 1], grid[i - 1][j]) + grid[i][j];
}
}
return grid[grid.length - 1][grid[0].length - 1];
}
}
LeetCode66 加一
就是数组加一,简单题当时腾讯实习面试都不会,我是废物
输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。
从后往前推,只加一,考虑下999的情况就行
class Solution {
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
digits[i]++;
digits[i] = digits[i] % 10;
if (digits[i] != 0) return digits;
}
digits = new int[digits.length + 1];
digits[0] = 1;
return digits;
}
}
LeetCode73 矩阵置0
输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出:[[1,0,1],[0,0,0],[1,0,1]]
需要原地置换,把第一行第一列拿出来作为标志行列
class Solution {
public void setZeroes(int[][] matrix) {
boolean row = false;
boolean col = false;
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length; j++){
if(matrix[i][j] == 0){
if(i == 0)
row = true;
if(j == 0)
col = true;
matrix[i][0] = matrix[0][j] = 0;
}
}
}
for(int i = 1; i < matrix.length; i++){
for(int j = 1; j < matrix[0].length; j++){
if(matrix[i][0] == 0|| matrix[0][j] == 0)
matrix[i][j] = 0;
}
}
if(col){
for(int i = 0; i < matrix.length; i++){
matrix[i][0] = 0;
}
}
if(row){
for(int j = 0; j < matrix[0].length; j++){
matrix[0][j] = 0;
}
}
}
}
LeetCode74 搜索二维矩阵
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:true
有序矩阵,左下或者右上搜就完了
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int i = matrix.length - 1, j = 0;
while(i >= 0 && j < matrix[0].length)
{
if(matrix[i][j] > target) i--;
else if(matrix[i][j] < target) j++;
else return true;
}
return false;
}
}
LeetCode75 颜色分类
输入:nums = [2,0,2,1,1,0]
输出:[0,0,1,1,2,2]
原地换,需要三个指针,l左面全是0,r右面全是2,index代表走的索引
class Solution {
public void sortColors(int[] nums) {
int l = 0;
int r = nums.length - 1;
int index = 0;
while(index <= r){
if(nums[index] == 0)
swap(nums, l++, index++);
else if(nums[index] == 1)
index++;
else if(nums[index] == 2)
swap(nums,index,r--);
}
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
LeetCode78 子集
回溯
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int start){
list.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++){
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
LeetCode79 单词搜索
根岛屿问题类似,回溯就行
class Solution {
public boolean exist(char[][] board, String word) {
char[] words = word.toCharArray();
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[0].length; j++){
if (dfs(board, words, i, j, 0))
return true;
}
}
return false;
}
boolean dfs(char[][] board, char[] word, int i, int j, int index){
if (i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[index])
return false;
if (index == word.length - 1)
return true;
char tmp = board[i][j];
board[i][j] = '.';
boolean res = dfs(board, word, i + 1, j, index + 1)|| dfs(board, word, i - 1, j, index + 1)|| dfs(board, word, i, j + 1, index + 1)|| dfs(board, word, i, j - 1, index + 1);
board[i][j] = tmp;
return res;
}
}
LeetCode80 删除数组中的重复项2
输入:nums = [1,1,1,2,2,3]
输出:5, nums = [1,1,2,2,3]
解释:函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
class Solution {
public int removeDuplicates(int[] nums) {
return process(nums, 2);
}
int process(int[] nums, int k) {
int u = 0;
for (int x : nums) {
if (u < k || nums[u - k] != x) nums[u++] = x;
}
return u;
}
}
LeetCode81搜索旋转排序数组
有重复元素版,需要去重
输入:nums = [2,5,6,0,0,1,2], target = 0
输出:true
class Solution {
public boolean search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return false;
}
int start = 0;
int end = nums.length - 1;
int mid;
while (start <= end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
return true;
}
if (nums[start] == nums[mid]) {
start++;
continue;
}
//前半部分有序
if (nums[start] < nums[mid]) {
//target在前半部分
if (nums[mid] > target && nums[start] <= target) {
end = mid - 1;
} else { //否则,去后半部分找
start = mid + 1;
}
} else {
//后半部分有序
//target在后半部分
if (nums[mid] < target && nums[end] >= target) {
start = mid + 1;
} else { //否则,去后半部分找
end = mid - 1;
}
}
}
//一直没找到,返回false
return false;
}
}
LeetCode84 柱状图最大矩形
单调栈
输入:heights = [2,1,5,6,2,3]
输出:10
解释:最大的矩形为图中红色区域,面积为 10

class Solution {
public int largestRectangleArea(int[] heights) {
// 这里为了代码简便,在柱体数组的头和尾加了两个高度为 0 的柱体。
int[] tmp = new int[heights.length + 2];
System.arraycopy(heights, 0, tmp, 1, heights.length);
Deque<Integer> stack = new ArrayDeque<>();
int area = 0;
for (int i = 0; i < tmp.length; i++) {
// 对栈中柱体来说,栈中的下一个柱体就是其「左边第一个小于自身的柱体」;
// 若当前柱体 i 的高度小于栈顶柱体的高度,说明 i 是栈顶柱体的「右边第一个小于栈顶柱体的柱体」。
// 因此以栈顶柱体为高的矩形的左右宽度边界就确定了,可以计算面积\U0001f336️ ~
while (!stack.isEmpty() && tmp[i] < tmp[stack.peek()]) {
int h = tmp[stack.pop()];
area = Math.max(area, (i - stack.peek() - 1) * h);
}
stack.push(i);
}
return area;
}
}
LeetCode85 最大矩形
结合上一题很简单
输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:6
解释:最大矩形如上图所示。

class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0) {
return 0;
}
int[] heights = new int[matrix[0].length];
int maxArea = 0;
for (int row = 0; row < matrix.length; row++) {
//遍历每一列,更新高度
for (int col = 0; col < matrix[0].length; col++) {
if (matrix[row][col] == '1') {
heights[col] += 1;
} else {
heights[col] = 0;
}
}
//调用上一题的解法,更新函数
maxArea = Math.max(maxArea, largestRectangleArea(heights));
}
return maxArea;
}
public int largestRectangleArea(int[] heights) {
// 这里为了代码简便,在柱体数组的头和尾加了两个高度为 0 的柱体。
int[] tmp = new int[heights.length + 2];
System.arraycopy(heights, 0, tmp, 1, heights.length);
Deque<Integer> stack = new ArrayDeque<>();
int area = 0;
for (int i = 0; i < tmp.length; i++) {
// 对栈中柱体来说,栈中的下一个柱体就是其「左边第一个小于自身的柱体」;
// 若当前柱体 i 的高度小于栈顶柱体的高度,说明 i 是栈顶柱体的「右边第一个小于栈顶柱体的柱体」。
// 因此以栈顶柱体为高的矩形的左右宽度边界就确定了,可以计算面积\U0001f336️ ~
while (!stack.isEmpty() && tmp[i] < tmp[stack.peek()]) {
int h = tmp[stack.pop()];
area = Math.max(area, (i - stack.peek() - 1) * h);
}
stack.push(i);
}
return area;
}
}
LeetCode88 合并两个有序数组
输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:[1,2,2,3,5,6]
解释:需要合并 [1,2,3] 和 [2,5,6] 。
合并结果是 [1,2,2,3,5,6] ,其中斜体加粗标注的为 nums1 中的元素
从后往前走
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int len1 = m - 1;
int len2 = n - 1;
int len = m + n - 1;
while(len1 >= 0 && len2 >= 0) {
// 注意--符号在后面,表示先进行计算再减1,这种缩写缩短了代码
nums1[len--] = nums1[len1] > nums2[len2] ? nums1[len1--] : nums2[len2--];
}
// 表示将nums2数组从下标0位置开始,拷贝到nums1数组中,从下标0位置开始,长度为len2+1
System.arraycopy(nums2, 0, nums1, 0, len2 + 1);
}
}
LeetCode90 子集2
有重复元素版本
输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
res.add(new ArrayList<>());
Arrays.sort(nums);
dfs(nums,0);
return res;
}
public void dfs(int[]nums,int start){
if(start>=nums.length)
return;
for(int i=start;i<nums.length;i++){
if(i>start&&nums[i]==nums[i-1]) continue;
list.add(nums[i]);
res.add(new ArrayList<Integer>(list));
dfs(nums,i+1);
list.remove(list.size()-1);
}
}
}
LeetCode105 前序中序构造树
输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
输出: [3,9,20,null,null,15,7]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
List<Integer> preorderList = new ArrayList<>();
List<Integer> inorderList = new ArrayList<>();
for (int i = 0; i < preorder.length; i++){
preorderList.add(preorder[i]);
inorderList.add(inorder[i]);
}
return helper(preorderList, inorderList);
}
private TreeNode helper(List<Integer> preorderList, List<Integer> inorderList){
if (inorderList.size() == 0)
return null;
int rootVal = preorderList.remove(0);
TreeNode root = new TreeNode(rootVal);
//找到中序中根的位置
int mid = inorderList.indexOf(rootVal);
root.left = helper(preorderList, inorderList.subList(0, mid));
root.right = helper(preorderList, inorderList.subList(mid + 1, inorderList.size()));
return root;
}
}
LeetCode106 中序后序构造树
输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
HashMap<Integer,Integer> memo = new HashMap<>();
int[] post;
public TreeNode buildTree(int[] inorder, int[] postorder) {
for(int i = 0;i < inorder.length; i++) memo.put(inorder[i], i);
post = postorder;
TreeNode root = buildTree(0, inorder.length - 1, 0, post.length - 1);
return root;
}
public TreeNode buildTree(int is, int ie, int ps, int pe) {
if(ie < is || pe < ps) return null;
int root = post[pe];
int ri = memo.get(root);
TreeNode node = new TreeNode(root);
node.left = buildTree(is, ri - 1, ps, ps + ri - is - 1);
node.right = buildTree(ri + 1, ie, ps + ri - is, pe - 1);
return node;
}
}
LeetCode108 有序数组到二叉搜索树
输入:nums = [-10,-3,0,5,9]
输出:[0,-3,9,-10,null,5]
解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums == null|| nums.length == 0)
return null;
return sortedArrayToBST(nums, 0, nums.length - 1);
}
public TreeNode sortedArrayToBST(int[] nums, int start, int end){
if(start > end)
return null;
int mid = (start + end) >> 1;
TreeNode node = new TreeNode(nums[mid]);
node.left = sortedArrayToBST(nums, start, mid - 1);
node.right = sortedArrayToBST(nums, mid + 1, end);
return node;
}
}
LeetCode118 杨辉三角
输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
模拟即可
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
for (int i = 0; i < numRows; ++i) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j <= i; ++j) {
if (j == 0 || j == i) {
row.add(1);
} else {
row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
}
}
ret.add(row);
}
return ret;
}
}
LeetCode119 杨辉三角2
找具体某一行
class Solution {
public List<Integer> getRow(int rowIndex) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
for (int i = 0; i <= rowIndex; ++i) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j <= i; ++j) {
if (j == 0 || j == i) {
row.add(1);
} else {
row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
}
}
ret.add(row);
}
return ret.get(rowIndex);
}
}
LeetCode120 三角形最小路径和
输入:triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
输出:11
解释:如下面简图所示:
2
3 4
6 5 7
4 1 8 3
自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。
动态规划即可
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int n = triangle.size();
int[][] f = new int[n][n];
f[0][0] = triangle.get(0).get(0);
for (int i = 1; i < n; ++i) {
f[i][0] = f[i - 1][0] + triangle.get(i).get(0);
for (int j = 1; j < i; ++j) {
f[i][j] = Math.min(f[i - 1][j - 1], f[i - 1][j]) + triangle.get(i).get(j);
}
f[i][i] = f[i - 1][i - 1] + triangle.get(i).get(i);
}
int minTotal = f[n - 1][0];
for (int i = 1; i < n; ++i) {
minTotal = Math.min(minTotal, f[n - 1][i]);
}
return minTotal;
}
}
LeetCode121 买卖股票最佳时机
输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
只能买卖一次就是找个最大差
class Solution {
public int maxProfit(int[] prices) {
int res = Integer.MIN_VALUE;
if(prices==null||prices.length<1)
return 0;
int min = Integer.MAX_VALUE;
for(int i = 0; i < prices.length; i++){
if(prices[i]<min){
min = prices[i];
}
res = Math.max(res,prices[i]-min);
}
return res;
}
}
LeetCode122.买卖股票最佳时机2
可以买卖多次,动态规划即可
输入: prices = [7,1,5,3,6,4]
输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
class Solution {
public int maxProfit(int[] prices) {
int length = prices.length;
int[][] dp = new int[length][2];
dp[0][1] = -prices[0];
dp[0][0] = 0;
for (int i = 1; i < length; i++){
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[length - 1][0];
}
}
LeetCode123 买卖股票最佳时机3
最多可以买卖两次,动态规划,三维dp,j代表买卖几次,k代表持有未持有
输入:prices = [3,3,5,0,0,3,1,4]
输出:6
解释:在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。
class Solution {
public int maxProfit(int[] prices) {
int[][][] dp = new int[prices.length][3][2];
dp[0][0][0] = 0;
dp[0][0][1] = -prices[0];
dp[0][1][0] = Integer.MIN_VALUE / 2;
dp[0][1][1] = Integer.MIN_VALUE / 2;
dp[0][2][0] = Integer.MIN_VALUE / 2;
dp[0][2][1] = Integer.MIN_VALUE / 2;
for(int i = 1; i < prices.length; i++){
dp[i][0][0] = 0;
dp[i][1][0] = Math.max(dp[i-1][0][1] + prices[i], dp[i-1][1][0]);
dp[i][0][1] = Math.max(dp[i-1][0][0] - prices[i], dp[i-1][0][1]);
dp[i][1][1] = Math.max(dp[i-1][1][0] - prices[i], dp[i-1][1][1]);
dp[i][2][0] = Math.max(dp[i-1][1][1] + prices[i], dp[i-1][2][0]);
}
return Math.max(dp[prices.length - 1][0][0], Math.max(dp[prices.length - 1][1][0],
dp[prices.length - 1][2][0]));
}
}
LeetCode128 最长连续序列
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
要求On不能排序,需要个集合类储存元素,并且遍历时,不断判断是否有curNum+1
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int max = 0;
for (int num : set) {
if (!set.contains(num - 1)) {
int curNum = num;
int curMax = 1;
while (set.contains(curNum + 1)) {
curMax += 1;
curNum += 1;
}
max = Math.max(max, curMax);
}
}
return max;
}
}
Leetcode130被围绕的区域
输入:board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
输出:[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
解释:被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
边界上的O对其dfs,将O变成A,后续遍历即可,A变O,其余的变X
class Solution {
public void solve(char[][] board) {
if (board == null || board.length == 0)
return;
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[0].length; j++){
if(i == 0|| i == board.length - 1|| j == 0|| j == board[0].length - 1){
if(board[i][j] == 'O')
dfs(i, j, board);
}
}
}
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[0].length; j++){
if (board[i][j] == 'A')
board[i][j] = 'O';
else
board[i][j] = 'X';
}
}
return;
}
public void dfs(int i, int j, char[][] board){
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length)
return;
if (board[i][j] != 'O')
return;
board[i][j] = 'A';
dfs(i - 1, j, board);//上
dfs(i + 1, j, board);//下
dfs(i, j - 1, board);//左
dfs(i, j + 1, board);//右
}
}
LeetCode134 加油站
输入: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
输出: 3
解释:
从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
因此,3 可为起始索引。

下图的黑色折线图即总油量剩余值,若要满足题目的要求:跑完全程再回到起点,总油量剩余值的任意部分都需要在X轴以上,且跑到终点时:总剩余汽油量 >= 0。
为了让黑色折线图任意部分都在 X 轴以上,我们需要向上移动黑色折线图,直到所有点都在X轴或X轴以上。此时,处在X轴的点即为出发点。即黑色折线图的最低值的位置:index = 3。
找到最低点后,如果有解,那么解就是最低点的下一个点,因为总(gas-cost)是大于等于0的,所以前面损失的gas我从最低点下一个点开始都会拿回来!(此处@小马哥!“我要争一口气,不是想证明我了不起。我是要告诉人家,我失去的东西一定要拿回来!”),别管后面的趋势是先加后减还是先减后加,最终结果我是能填平前面的坑的。
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
int spare = 0;
int minSpare = Integer.MAX_VALUE;
int minIndex = 0;
for (int i = 0; i < len; i++) {
spare += gas[i] - cost[i];
if (spare < minSpare) {
minSpare = spare;
minIndex = i;
}
}
return spare < 0 ? -1 : (minIndex + 1) % len;
}
}
LeetCode135 分发糖果
贪心算法,定义left与right数组,最后在统计大的即可
输入:ratings = [1,0,2]
输出:5
解释:你可以分别给第一个、第二个、第三个孩子分发 2、1、2 颗糖果。
class Solution {
public int candy(int[] ratings) {
int[] left = new int[ratings.length];
int[] right = new int[ratings.length];
Arrays.fill(left, 1);
Arrays.fill(right, 1);
for(int i = 1; i < ratings.length; i++)
if(ratings[i] > ratings[i - 1]) left[i] = left[i - 1] + 1;
for(int i = ratings.length - 1; i > 0; i--) {
if(ratings[i - 1] > ratings[i]) right[i-1] = right[i] + 1;
}
int count = 0;
for (int i = 0; i < ratings.length; i++){
count += Math.max(left[i], right[i]);
}
return count;
}
}
LeetCode136 只出现一次的数字
输入: [2,2,1]
输出: 1
class Solution {
public int singleNumber(int[] nums) {
int single = 0;
for(int num:nums){
single^=num;
}
return single;
}
}
LeetCode137 只出现一次的数字2
输入:nums = [2,2,3,2]
输出:3
class Solution {
public int singleNumber(int[] nums) {
int ans = 0;
//考虑每一位
for (int i = 0; i < 32; i++) {
int count = 0;
//考虑每一个数
for (int j = 0; j < nums.length; j++) {
//当前位是否是 1
if ((nums[j] >>> i & 1) == 1) {
count++;
}
}
//1 的个数是否是 3 的倍数
if (count % 3 != 0) {
ans |= 1 << i;
}
}
return ans;
}
}
LeetCode149 直线上最多的点数
输入:points = [[1,1],[2,2],[3,3]]
输出:3
我们知道,两个点可以确定一条线。
因此一个朴素的做法是先枚举两条点(确定一条线),然后检查其余点是否落在该线中。
为了避免除法精度问题,当我们枚举两个点 ii 和 jj 时,不直接计算其对应直线的 斜率和 截距,而是通过判断 ii 和 jj 与第三个点 kk 形成的两条直线斜率是否相等(斜率相等的两条直线要么平行,要么重合,平行需要 44 个点来唯一确定,我们只有 33 个点,所以可以直接判定两直线重合)。
class Solution {
public int maxPoints(int[][] ps) {
int n = ps.length;
int ans = 1;
for (int i = 0; i < n; i++) {
int[] x = ps[i];
for (int j = i + 1; j < n; j++) {
int[] y = ps[j];
int cnt = 2;
for (int k = j + 1; k < n; k++) {
int[] p = ps[k];
int s1 = (y[1] - x[1]) * (p[0] - y[0]);
int s2 = (p[1] - y[1]) * (y[0] - x[0]);
if (s1 == s2) cnt++;
}
ans = Math.max(ans, cnt);
}
}
return ans;
}
}
LeetCode150 逆波兰表达式求值
输入:tokens = ["2","1","+","3","*"]
输出:9
解释:该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for(String str: tokens){
if(str.equals("+")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2 + num1);
}else if(str.equals("-")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2 - num1);
}else if(str.equals("*")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2 * num1);
}else if(str.equals("/")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2 / num1);
}else{
stack.push(Integer.parseInt(str));
}
}
return stack.pop();
}
}
LeetCode152 乘积最大子数组
输入: nums = [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6。
注意当前元素是负数时,需要最大最小值互换
class Solution {
public int maxProduct(int[] nums) {
int max = Integer.MIN_VALUE, imax = 1, imin = 1;
for(int i=0; i<nums.length; i++){
if(nums[i] < 0){
int tmp = imax;
imax = imin;
imin = tmp;
}
imax = Math.max(imax*nums[i], nums[i]);
imin = Math.min(imin*nums[i], nums[i]);
max = Math.max(max, imax);
}
return max;
}
}
LeetCode153 寻找排序数组中最小数字
输入:nums = [3,4,5,1,2]
输出:1
解释:原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。
class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
};
LeetCode154 寻找排序树组最小值2
存在重复元素,多一句去重
输入:nums = [2,2,2,0,1]
输出:0
class Solution {
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) left = mid + 1;
else if (nums[mid] < nums[right]) right = mid;
else right = right - 1;
}
return nums[left];
}
}
LeetCode162 寻找峰值
输入:nums = [1,2,3,1]
输出:2
解释:3 是峰值元素,你的函数应该返回其索引 2。
假设当前分割点 midmid 满足关系 num[mid] > nums[mid + 1]num[mid]>nums[mid+1] 的话,一个很简单的想法是 num[mid]num[mid] 可能为峰值,而 nums[mid + 1]nums[mid+1] 必然不为峰值,于是让 r = midr=mid,从左半部分继续找峰值。
public class Solution {
public int findPeakElement(int[] nums) {
int l = 0, r = nums.length - 1;
while (l < r) {
int mid = (l + r) / 2;
if (nums[mid] > nums[mid + 1])
r = mid;
else
l = mid + 1;
}
return l;
}
}
LeetCode164 最大间距
输入: nums = [3,6,9,1]
输出: 3
解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。
要求On所以需要桶排序类似的思想,比较隔壁桶最大最小值
class Solution {
public int maximumGap(int[] nums) {
int n = nums.length;
if (n < 2) {
return 0;
}
int minVal = Arrays.stream(nums).min().getAsInt();
int maxVal = Arrays.stream(nums).max().getAsInt();
int d = Math.max(1, (maxVal - minVal) / (n - 1));
int bucketSize = (maxVal - minVal) / d + 1;
int[][] bucket = new int[bucketSize][2];
for (int i = 0; i < bucketSize; ++i) {
Arrays.fill(bucket[i], -1); // 存储 (桶内最小值,桶内最大值) 对, (-1, -1) 表示该桶是空的
}
for (int i = 0; i < n; i++) {
int idx = (nums[i] - minVal) / d;
if (bucket[idx][0] == -1) {
bucket[idx][0] = bucket[idx][1] = nums[i];
} else {
bucket[idx][0] = Math.min(bucket[idx][0], nums[i]);
bucket[idx][1] = Math.max(bucket[idx][1], nums[i]);
}
}
int ret = 0;
int prev = -1;
for (int i = 0; i < bucketSize; i++) {
if (bucket[i][0] == -1) {
continue;
}
if (prev != -1) {
ret = Math.max(ret, bucket[i][0] - bucket[prev][1]);
}
prev = i;
}
return ret;
}
}
LeetCode167 两数之和2
输入:numbers = [2,7,11,15], target = 9
输出:[1,2]
解释:2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。
有序数组,且要求常量级别空间复杂度,最简单的双指针即可
class Solution {
public int[] twoSum(int[] numbers, int target) {
int low = 0, high = numbers.length - 1;
while (low < high) {
int sum = numbers[low] + numbers[high];
if (sum == target) {
return new int[]{low + 1, high + 1};
} else if (sum < target) {
++low;
} else {
--high;
}
}
return new int[]{-1, -1};
}
}
LeetCode169 多数元素
输入:[2,2,1,1,1,2,2]
输出:2
摩尔投票法,定义当前元素为众数,后面的相同+1,不同-1,等于0重新定义,最后肯定是众数
class Solution {
public int majorityElement(int[] nums) {
int vote = 0, count = 0, res = 0;
for (int i = 0; i < nums.length; i++) {
if (vote == 0) res = nums[i];
vote += nums[i] == res ? 1 : -1;
}
return res;
}
}
LeetCode174 地下城游戏
编写一个函数来计算确保骑士能够拯救到公主所需的最低初始健康点数。
例如,考虑到如下布局的地下城,如果骑士遵循最佳路径 右 -> 右 -> 下 -> 下,则骑士的初始健康点数至少为 7。
-2 (K) -3 3
-5 -10 1
10 30 -5 (P)
动态规划,从右下往左上走,dp[][]表示从坐标 (i,j)(i,j) 到终点所需的最小初始值。换句话说,当我们到达坐标 (i,j)(i,j) 时,如果此时我们的路径和不小于 dp[i][j],我们就能到达终点。
class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int n = dungeon.length, m = dungeon[0].length;
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i <= n; ++i) {
Arrays.fill(dp[i], Integer.MAX_VALUE);
}
dp[n][m - 1] = dp[n - 1][m] = 1;
for (int i = n - 1; i >= 0; --i) {
for (int j = m - 1; j >= 0; --j) {
int minn = Math.min(dp[i + 1][j], dp[i][j + 1]);
dp[i][j] = Math.max(minn - dungeon[i][j], 1);
}
}
return dp[0][0];
}
}
LeetCode188买卖股票最佳时机4
输入:k = 2, prices = [2,4,1]
输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
相当于把2和3融合成一道题
class Solution {
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length == 0) {
return 0;
}
int n = prices.length;
//当k非常大时转为无限次交易
if(k>=n/2) {
int dp0 = 0, dp1 = -prices[0];
for(int i = 1; i < n; ++i) {
int tmp = dp0;
dp0 = Math.max(dp0,dp1 + prices[i]);
dp1 = Math.max(dp1,tmp - prices[i]);
}
return dp0;
}
//定义三维数组,第i天、交易了多少次、当前的买卖状态
int[][][] dp = new int[n][k + 1][2];
//初始化第一天,这里的dp[0][k][1]可以不用管,后面也不会用到
for(int i = 0; i <= k; ++i) {
dp[0][i][0] = 0;
dp[0][i][1] = -prices[0];
}
for(int i = 1;i < n; ++i) {
for(int j = 1; j <= k;++j) {
//处理第k次买入
dp[i][j - 1][1] = Math.max(dp[i - 1][j - 1][1], dp[i - 1][j - 1][0] - prices[i]);
//处理第k次卖出
dp[i][j][0] = Math.max(dp[i - 1][j][0],dp[i - 1][j - 1][1] + prices[i]);
}
}
return dp[n - 1][k][0];
}
}
LeetCode189 轮转数组
输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]
class Solution {
public void rotate(int[] nums, int k) {
int length = nums.length;
int temp[] = new int[length];
for (int i = 0; i < length; i++)
temp[i] = nums[i];
for (int i = 0; i < length; i++)
nums[(i + k) % length] = temp[i];
}
}
LeetCode198 打家劫舍
输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
class Solution {
public int rob(int[] nums) {
if (nums.length == 0)
return 0;
int n = nums.length;
int[] dp = new int[nums.length + 1];
dp[0] = 0;
dp[1] = nums[0];
for (int i = 2; i <= nums.length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
}
return dp[nums.length];
}
}
LeetCode200 岛屿数量
输入:grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
输出:1
class Solution {
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0)
return 0;
int count = 0;
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[0].length; j++){
if (grid[i][j] == '1'){
count++;
dfs(grid, i, j);
}
}
}
return count;
}
public void dfs(char[][] grid, int i, int j){
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0')
return;
grid[i][j] = '0';
dfs(grid, i - 1, j);//上
dfs(grid, i + 1, j);//下
dfs(grid, i, j + 1);//左
dfs(grid, i, j - 1);//右
}
}
LeetCode204 计算质数
输入:n = 10
输出:4
解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
把质数的各种倍数删除后,其余的都是质数
class Solution {
public int countPrimes(int n) {
boolean[] composite = new boolean[n];
int count = 0;
for (int i = 2; i < n; i++){
if (composite[i])
continue;
count++;
for (int j = i; j < n; j += i)
composite[j] = true;
}
return count;
}
}
LeetCode209 长度最小的子数组
输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
滑动窗口,双指针
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int lo = 0, hi = 0, sum = 0, min = Integer.MAX_VALUE;
while (hi < nums.length){
sum += nums[hi++];
while (sum >= target){
min = Math.min(min, hi - lo);
sum -= nums[lo++];
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
}
LeetCode212 单词搜索2
前缀树的应用,前缀树的模板,之后就是回溯
输入:board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
输出:["eat","oath"]
//改造前缀树节点
class TrieNode {
public TrieNode[] children;
public String word; //节点直接存当前的单词
public TrieNode() {
children = new TrieNode[26];
word = null;
for (int i = 0; i < 26; i++) {
children[i] = null;
}
}
}
class Trie {
TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
char[] array = word.toCharArray();
TrieNode cur = root;
for (int i = 0; i < array.length; i++) {
// 当前孩子是否存在
if (cur.children[array[i] - 'a'] == null) {
cur.children[array[i] - 'a'] = new TrieNode();
}
cur = cur.children[array[i] - 'a'];
}
// 当前节点结束,存入当前单词
cur.word = word;
}
};
class Solution {
public List<String> findWords(char[][] board, String[] words) {
Trie trie = new Trie();
//将所有单词存入前缀树中
List<String> res = new ArrayList<>();
for (String word : words) {
trie.insert(word);
}
int rows = board.length;
if (rows == 0) {
return res;
}
int cols = board[0].length;
//从每个位置开始遍历
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
existRecursive(board, i, j, trie.root, res);
}
}
return res;
}
private void existRecursive(char[][] board, int row, int col, TrieNode node, List<String> res) {
if (row < 0 || row >= board.length || col < 0 || col >= board[0].length) {
return;
}
char cur = board[row][col];//将要遍历的字母
//当前节点遍历过或者将要遍历的字母在前缀树中不存在
if (cur == '$' || node.children[cur - 'a'] == null) {
return;
}
node = node.children[cur - 'a'];
//判断当前节点是否是一个单词的结束
if (node.word != null) {
//加入到结果中
res.add(node.word);
//将当前单词置为 null,防止重复加入
node.word = null;
}
char temp = board[row][col];
//上下左右去遍历
board[row][col] = '$';
existRecursive(board, row - 1, col, node, res);
existRecursive(board, row + 1, col, node, res);
existRecursive(board, row, col - 1, node, res);
existRecursive(board, row, col + 1, node, res);
board[row][col] = temp;
}
}
LeetCode213 打家劫舍2
输入:nums = [2,3,2]
输出:3
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
class Solution {
public int rob(int[] nums) {
if (nums.length == 1)
return nums[0];
int robFirst = robHelper(nums, 0, nums.length - 2);
int robLast = robHelper(nums, 1, nums.length - 1);
return Math.max(robFirst, robLast);
}
private int robHelper(int[] num, int start, int end){
int steal = 0, noSteal = 0;
for (int j = start; j <= end; j++){
int temp = steal;
steal = noSteal + num[j];
noSteal = Math.max(noSteal, temp);
}
return Math.max(steal, noSteal);
}
}
LeetCode215 数组中第K大的元素
输入: [3,2,1,5,6,4] 和 k = 2
输出: 5
class Solution {
public int findKthLargest(int[] nums, int k) {
int length = nums.length;
//默认小根堆
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(length);
for(int i:nums)
priorityQueue.add(i);
//出去n-k个小的,第一个就是第K大的
for(int i=0;i<length-k;i++){
priorityQueue.poll();
}
return priorityQueue.peek();
}
}
LeetCode216组合总和3
输入: k = 3, n = 7
输出: [[1,2,4]]
class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
dfs(res, new ArrayList<>(), k, 1, n);
return res;
}
private void dfs(List<List<Integer>> res, List<Integer> list, int k, int start, int n){
if (list.size() == k || n <= 0){
if (list.size() == k && n == 0){
res.add(new ArrayList<>(list));
return;
}
}
for (int i = start; i <= 9; i++){
list.add(i);
dfs(res, list, k, i + 1, n - i);
list.remove(list.size() - 1);
}
}
}
LeetCode217 存在重复元素
输入:nums = [1,2,3,1]
输出:true
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for (int x : nums) {
if (!set.add(x)) {
return true;
}
}
return false;
}
}
LeetCode218 天际线问题
就是柱形图的轮廓,对我来说有点难
输入:buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
输出:[[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
class Solution {
public List<List<Integer>> getSkyline(int[][] bs) {
List<List<Integer>> ans = new ArrayList<>();
// 预处理所有的点,为了方便排序,对于左端点,令高度为负;对于右端点令高度为正
List<int[]> ps = new ArrayList<>();
for (int[] b : bs) {
int l = b[0], r = b[1], h = b[2];
ps.add(new int[]{l, -h});
ps.add(new int[]{r, h});
}
// 先按照横坐标进行排序
// 如果横坐标相同,则按照左端点排序
// 如果相同的左/右端点,则按照高度进行排序
Collections.sort(ps, (a, b)->{
if (a[0] != b[0]) return a[0] - b[0];
return a[1] - b[1];
});
// 大根堆
PriorityQueue<Integer> q = new PriorityQueue<>((a,b)->b-a);
int prev = 0;
q.add(prev);
for (int[] p : ps) {
int point = p[0], height = p[1];
if (height < 0) {
// 如果是左端点,说明存在一条往右延伸的可记录的边,将高度存入优先队列
q.add(-height);
} else {
// 如果是右端点,说明这条边结束了,将当前高度从队列中移除
q.remove(height);
}
// 取出最高高度,如果当前不与前一矩形“上边”延展而来的那些边重合,则可以被记录
int cur = q.peek();
if (cur != prev) {
List<Integer> list = new ArrayList<>();
list.add(point);
list.add(cur);
ans.add(list);
prev = cur;
}
}
return ans;
}
}
LeetCode219 存在重复元素2
输入:nums = [1,2,3,1], k = 3
输出:true
与1的思路相同,用set,不过有abs(i - j) <= k的要求所以,set的size需要<=k,大于时,删除最前面的元素
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashSet<Integer> set = new HashSet<>();
for(int i = 0; i < nums.length; i++) {
if(set.contains(nums[i])) {
return true;
}
set.add(nums[i]);
if(set.size() > k) {
set.remove(nums[i - k]);
}
}
return false;
}
}
LeetCode220 存在重复元素3
输入:nums = [1,2,3,1], k = 3, t = 0
输出:true
与上一道题一样,维护一个set储存最近的k个元素,且判断是否有满足目标的abs(nums[i] - nums[j]) <= t ,同时又满足 abs(i - j) <= k 。
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
int n = nums.length;
TreeSet<Long> ts = new TreeSet<>();
for (int i = 0; i < n; i++) {
Long u = nums[i] * 1L;
// 从 ts 中找到小于等于 u 的最大值(小于等于 u 的最接近 u 的数)
Long l = ts.floor(u);
// 从 ts 中找到大于等于 u 的最小值(大于等于 u 的最接近 u 的数)
Long r = ts.ceiling(u);
if(l != null && u - l <= t) return true;
if(r != null && r - u <= t) return true;
// 将当前数加到 ts 中,并移除下标范围不在 [max(0, i - k), i) 的数(维持滑动窗口大小为 k)
ts.add(u);
if (i >= k) ts.remove(nums[i - k] * 1L);
}
return false;
}
}
LeetCode221最大正方形
输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:4
class Solution {
public int maximalSquare(char[][] matrix) {
int h = matrix.length;
int w = matrix[0].length;
int max = 0;
int[][] dp = new int[h+1][w+1];
for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++){
if(matrix[i-1][j-1] == '1'){
//以当前位置为右下角的最大正方形长度
dp[i][j] = Math.min(dp[i-1][j], Math.min(dp[i][j-1], dp[i-1][j-1])) + 1;
max = Math.max(max, dp[i][j]);
}
}
}
return max*max;
}
}
LeetCode228 汇总区间
输入:nums = [0,1,2,4,5,7]
输出:["0->2","4->5","7"]
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
// i 初始指向第 1 个区间的起始位置
int i = 0;
for (int j = 0; j < nums.length; j++) {
// j 向后遍历,直到不满足连续递增(即 nums[j] + 1 != nums[j + 1])
// 或者 j 达到数组边界,则当前连续递增区间 [i, j] 遍历完毕,将其写入结果列表。
if (j + 1 == nums.length || nums[j] + 1 != nums[j + 1]) {
// 将当前区间 [i, j] 写入结果列表
StringBuilder sb = new StringBuilder();
sb.append(nums[i]);
if (i != j) {
sb.append("->").append(nums[j]);
}
res.add(sb.toString());
// 将 i 指向更新为 j + 1,作为下一个区间的起始位置
i = j + 1;
}
}
return res;
}
}
LeetCode229 求众数2
输入:[3,2,3]
输出:[3]
摩尔投票法,扩充为两个数
class Solution {
public List<Integer> majorityElement(int[] nums) {
int numA = Integer.MAX_VALUE;
int numB = Integer.MAX_VALUE;
int cntA = 0, cntB = 0;
for(int num: nums){
if(num == numA)
cntA++;
else if(num == numB)
cntB++;
else if(cntA == 0){
numA = num;
cntA = 1;
}
else if(cntB == 0){
numB = num;
cntB = 1;
}
else{
cntA--;
cntB--;
}
}
cntA = 0;
cntB = 0;
for(int num: nums){
if(num == numA)
cntA++;
}
for(int num: nums){
if(num == numB)
cntB++;
}
List<Integer> res = new ArrayList<>();
if(cntA > nums.length / 3)
res.add(numA);
if(cntB > nums.length /3)
res.add(numB);
return res;
}
}
LeetCode238 除自身以外数组的乘积
输入: nums = [1,2,3,4]
输出: [24,12,8,6]
类似找峰值,分糖果一样,分左右两个数组
class Solution {
public int[] productExceptSelf(int[] nums) {
int length = nums.length;
// L 和 R 分别表示左右两侧的乘积列表
int[] L = new int[length];
int[] R = new int[length];
int[] answer = new int[length];
// L[i] 为索引 i 左侧所有元素的乘积
// 对于索引为 '0' 的元素,因为左侧没有元素,所以 L[0] = 1
L[0] = 1;
for (int i = 1; i < length; i++) {
L[i] = nums[i - 1] * L[i - 1];
}
// R[i] 为索引 i 右侧所有元素的乘积
// 对于索引为 'length-1' 的元素,因为右侧没有元素,所以 R[length-1] = 1
R[length - 1] = 1;
for (int i = length - 2; i >= 0; i--) {
R[i] = nums[i + 1] * R[i + 1];
}
// 对于索引 i,除 nums[i] 之外其余各元素的乘积就是左侧所有元素的乘积乘以右侧所有元素的乘积
for (int i = 0; i < length; i++) {
answer[i] = L[i] * R[i];
}
return answer;
}
}
LeetCode239 滑动窗口最大值
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
单调栈经典问题
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums == null || nums.length < 2) return nums;
// 双向队列 保存当前窗口最大值的数组位置 保证队列中数组位置的数值按从大到小排序
LinkedList<Integer> queue = new LinkedList();
// 结果数组
int[] result = new int[nums.length-k+1];
// 遍历nums数组
for(int i = 0;i < nums.length;i++){
// 保证从大到小 如果前面数小则需要依次弹出,直至满足要求
while(!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]){
queue.pollLast();
}
// 添加当前值对应的数组下标
queue.addLast(i);
// 判断当前队列中队首的值是否有效
if(queue.peek() <= i-k){
queue.poll();
}
// 当窗口长度为k时 保存当前窗口中最大值
if(i+1 >= k){
result[i+1-k] = nums[queue.peek()];
}
}
return result;
}
}
LeetCode240 搜索二维矩阵2
输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
输出:true
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 1)
return false;
int row = matrix.length-1, col = 0;
while (row >= 0 && col < matrix[0].length) {
if(matrix[row][col]<target)
col++;
else if(matrix[row][col]>target)
row--;
else
return true;
}
return false;
}
}
LeetCode260只出现一次的数字3
输入:nums = [1,2,1,3,2,5]
输出:[3,5]
解释:[5, 3] 也是有效的答案。
两个元素出现一次,其余两次,异或和肯定不为0,找个1的位,按他来区分成两个数
class Solution {
public int[] singleNumber(int[] nums) {
int diff = 0;
for (int n : nums) {
diff ^= n;
}
diff &= -diff;
int[] result = { 0, 0 };
for (int n : nums) {
//当前位是 0 的组, 然后组内异或
if ((diff & n) == 0) {
result[0] ^= n;
//当前位是 1 的组
} else {
result[1] ^= n;
}
}
return result;
}
}
LeetCode268 丢失的数字
输入:nums = [3,0,1]
输出:2
解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。
class Solution {
public int missingNumber(int[] nums) {
//0~n异或和,与当前数组异或和,只有缺失的数字出现一次
int n = nums.length;
int ans = 0;
for (int i = 0; i <= n; i++) ans ^= i;
for (int i : nums) ans ^= i;
return ans;
}
}
class Solution {
public int missingNumber(int[] nums) {
//0~n的总和sum减去当前数组总和就是缺失数字
int n = nums.length;
int cur = 0, sum = n * (n + 1) / 2;
for (int i : nums) cur += i;
return sum - cur;
}
}
LeetCode274 H指数
输入:citations = [3,0,6,1,5]
输出:3
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
根据 H 指数的定义,如果当前H 指数为 h并且在遍历过程中找到当前值 citations[i] > h,则说明我们找到了一篇被引用了至少 h+1次的论文,所以将现有的 h值加1。继续遍历直到 h 无法继续增大。最后返回 h作为最终答案。
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int h = 0, i = citations.length - 1;
while (i >= 0 && citations[i] > h) {
h++;
i--;
}
return h;
}
}
LeetCode275 H指数2
相较于上题,排好序但有logn时间复杂度的要求,肯定是二分
输入:citations = [0,1,3,5,6]
输出:3
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 0, 1, 3, 5, 6 次。
由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3 。
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (citations[mid] >= n - mid) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return n - left;
}
}
LeetCode283 移动0
输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]
class Solution {
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0)
return;
int index = 0;
for (int i = 0; i < nums.length; i++){
if (nums[i] != 0){
nums[index++] = nums[i];
}
}
while (index < nums.length){
nums[index++] = 0;
}
}
}
LeetCode284 顶端迭代器
输入:
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
输出:
[null, 1, 2, 2, 3, false]
解释:
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]
peekingIterator.next(); // 返回 1 ,指针移动到下一个元素 [1,2,3]
peekingIterator.peek(); // 返回 2 ,指针未发生移动 [1,2,3]
peekingIterator.next(); // 返回 2 ,指针移动到下一个元素 [1,2,3]
peekingIterator.next(); // 返回 3 ,指针移动到下一个元素 [1,2,3]
peekingIterator.hasNext(); // 返回 False
先把next的值储存起来,在判断需不需要移动指针
class PeekingIterator implements Iterator<Integer> {
Iterator<Integer> iter;
Integer next;
public PeekingIterator(Iterator<Integer> iterator) {
iter = iterator;
if (iter.hasNext()) next = iter.next();
}
public Integer peek() {
return next;
}
@Override
public Integer next() {
Integer ans = next;
next = iter.hasNext() ? iter.next() : null;
return ans;
}
@Override
public boolean hasNext() {
return next != null;
}
}
LeetCode287 寻找重复数
输入:nums = [1,3,4,2,2]
输出:2
class Solution {
public int findDuplicate(int[] nums) {
//二分,类似抽屉原理,那半多用哪半
int left = 1;
int right = nums.length - 1;
while(left < right){
int mid = (left + right)>>1;
int cnt = 0;
for(int num: nums){
if(num <= mid)
cnt++;
}
if(cnt > mid)
right = mid;
else
left = mid + 1;
}
return left;
}
}
LeetCode289 生命游戏
输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
class Solution {
public void gameOfLife(int[][] board) {
int[] neighbors = {0, 1, -1};
int rows = board.length;
int cols = board[0].length;
// 创建复制数组 copyBoard
int[][] copyBoard = new int[rows][cols];
// 从原数组复制一份到 copyBoard 中
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
copyBoard[row][col] = board[row][col];
}
}
// 遍历面板每一个格子里的细胞
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
// 对于每一个细胞统计其八个相邻位置里的活细胞数量
int liveNeighbors = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!(neighbors[i] == 0 && neighbors[j] == 0)) {
int r = (row + neighbors[i]);
int c = (col + neighbors[j]);
// 查看相邻的细胞是否是活细胞
if ((r < rows && r >= 0) && (c < cols && c >= 0) && (copyBoard[r][c] == 1)) {
liveNeighbors += 1;
}
}
}
}
// 规则 1 或规则 3
if ((copyBoard[row][col] == 1) && (liveNeighbors < 2 || liveNeighbors > 3)) {
board[row][col] = 0;
}
// 规则 4
if (copyBoard[row][col] == 0 && liveNeighbors == 3) {
board[row][col] = 1;
}
}
}
}
}
LeetCode300 最长子序列
输入:nums = [10,9,2,5,3,7,101,18]
输出:4
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
public class Solution {
public int lengthOfLIS(int[] nums) {
int len = nums.length;
if (len == 0) {
return 0;
}
int max = 1;
int[] dp = new int[len];
Arrays.fill(dp, 1);
for (int i = 0; i < len; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
}
}
max = Math.max(dp[i], max);
}
return max;
}
}
优化版
public class Solution {
public int lengthOfLIS(int[] nums) {
ArrayList<Integer> list = new ArrayList<>();
for (int num : nums){
if (list.size() == 0 || list.get(list.size() - 1) < num){
list.add(num);
}
else{
int i = Collections.binarySearch(list, num);
list.set((i < 0) ? -i - 1 : i, num);
}
}
return list.size();
}
}
LeetCode303 区域和检索-数组不可变
输入:
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
输出:
[null, 1, -1, -3]
class NumArray {
int[] sums;
public NumArray(int[] nums) {
int n = nums.length;
//前缀和即可
sums = new int[n + 1];
for (int i = 0; i < n; i++) {
sums[i + 1] = sums[i] + nums[i];
}
}
public int sumRange(int i, int j) {
return sums[j + 1] - sums[i];
}
}
LeetCode307 区间检索-数组可修改
输入:
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
输出:
[null, 9, null, 8]
解释:
NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // 返回 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1,2,5]
numArray.sumRange(0, 2); // 返回 1 + 2 + 5 = 8
//树状数组模板
class NumArray {
int[] tree;
int lowbit(int x) {
return x & -x;
}
int query(int x) {
int ans = 0;
for (int i = x; i > 0; i -= lowbit(i)) ans += tree[i];
return ans;
}
void add(int x, int u) {
for (int i = x; i <= n; i += lowbit(i)) tree[i] += u;
}
int[] nums;
int n;
public NumArray(int[] _nums) {
nums = _nums;
n = nums.length;
tree = new int[n + 1];
for (int i = 0; i < n; i++) add(i + 1, nums[i]);
}
public void update(int i, int val) {
add(i + 1, val - nums[i]);
nums[i] = val;
}
public int sumRange(int l, int r) {
return query(r + 1) - query(l);
}
}
LeetCode309 股票最佳购买时机含冷冻时间
输入: prices = [1,2,3,0,2]
输出: 3
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if (len < 2) {
return 0;
}
int[][] dp = new int[len][3];
dp[0][0] = 0;
dp[0][1] = -prices[0];
dp[0][2] = 0;
// dp[i][0]: 手上不持有股票,并且今天不是由于卖出股票而不持股,我们拥有的现金数
// dp[i][1]: 手上持有股票时,我们拥有的现金数
// dp[i][2]: 手上不持有股票,并且今天是由于卖出股票而不持股,我们拥有的现金数
for (int i = 1; i < len; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
dp[i][2] = dp[i - 1][1] + prices[i];
}
return Math.max(dp[len - 1][0], dp[len - 1][2]);
}
}
LeetCode312 戳气球
输入:nums = [3,1,5,8]
输出:167
解释:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
class Solution {
public int maxCoins(int[] nums) {
int length = nums.length;
int[] temp = new int[length + 2];
for (int i = 1; i <= length; i++){
temp[i] = nums[i - 1];
}
temp[0] = temp[length+1] = 1;
int[][] dp = new int[length + 2][length + 2];
for (int i = length - 1; i >= 0; i--) {
for (int j = i + 2; j <= length + 1; j++){
for (int k = i + 1; k < j; k++){
int sum = temp[i] * temp[k] * temp[j] + dp[i][k] + dp[k][j];
dp[i][j] = Math.max(dp[i][j], sum);
}
}
}
return dp[0][length + 1];
}
}
LeetCode313 超级丑数
输入:n = 12, primes = [2,7,13,19]
输出:32
解释:给定长度为 4 的质数数组 primes = [2,7,13,19],前 12 个超级丑数序列为:[1,2,4,7,8,13,14,16,19,26,28,32] 。
起始先将最小丑数 11 放入队列
每次从队列取出最小值 xx,然后将 xx 所对应的丑数 x * primes[i]进行入队。
对步骤 22 循环多次,第 nn 次出队的值即是答案。
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
PriorityQueue<Integer> q = new PriorityQueue<>();
q.add(1);
while (n-- > 0) {
int x = q.poll();
if (n == 0) return x;
for (int k : primes) {
if (k <= Integer.MAX_VALUE / x) q.add(k * x);
if (x % k == 0) break;
}
}
return -1; // never
}
}
LeetCode315 计算右侧小于当前元素的个数
输入:nums = [5,2,6,1]
输出:[2,1,1,0]
解释:
5 的右侧有 2 个更小的元素 (2 和 1)
2 的右侧仅有 1 个更小的元素 (1)
6 的右侧有 1 个更小的元素 (1)
1 的右侧有 0 个更小的元素
public static List<Integer> brute(int[] nums) {
Integer[] res = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
res[i] = 0;
for (int j = i + 1; j < nums.length; j++) if (nums[j] < nums[i]) res[i]++;
}
return Arrays.asList(res);
}
class Solution {
public List<Integer> countSmaller(int[] nums) {
Integer[] ret = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
ret[i] = 0;
}
List<Integer> list = new ArrayList<>();
TreeNode root = null;
for (int i = nums.length - 1; i >= 0; i--) {
root = insert(root, new TreeNode(nums[i]), ret, i);
}
return Arrays.asList(ret);
}
public TreeNode insert(TreeNode root, TreeNode node, Integer[] ret, int i) {
if (root == null) {
root = node;
return root;
}
if (root.val >= node.val) { // 注意小于等于插入到左子树,防止多加1
root.count++; // 小于根节点,则小于根节点的数量count++
root.left = insert(root.left, node, ret, i);
} else {
ret[i] += root.count + 1; // 累加小于当前节点的个数和当前节点(1)
root.right = insert(root.right, node, ret, i);
}
return root;
}
}
class TreeNode {
int val;
int count;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
left = null;
right = null;
count = 0; // 记录小于当前节点值的个数
}
}
LeetCode318 最大单词长度乘积
输入:words = ["abcw","baz","foo","bar","xtfn","abcdef"]
输出:16
解释:这两个单词为 "abcw", "xtfn"。
根据题意进行模拟即可,利用每个 words[i]只有小写字母,且只需要区分两字符是否有字母重复。
我们可以使用一个 int 来代指某个 word[i]:低 2626 来代指字母 a-z 是否出现过。
然后对每个「字符对」所对应的两个 int 值执行 & 操作(若两字符无重复字符,则结果为 0),并得出最终答案。
class Solution {
public int maxProduct(String[] words) {
int n = words.length, idx = 0;
int[] masks = new int[n];
for (String w : words) {
int t = 0;
for (int i = 0; i < w.length(); i++) {
int u = w.charAt(i) - 'a';
t |= (1 << u);
}
masks[idx++] = t;
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if ((masks[i] & masks[j]) == 0) ans = Math.max(ans, words[i].length() * words[j].length());
}
}
return ans;
}
}
LeetCode322 零钱兑换
输入:coins = [1, 2, 5], amount = 11
输出:3
解释:11 = 5 + 5 + 1
public class Solution {
public int coinChange(int[] coins, int amount) {
int max = amount + 1;
int[] dp = new int[amount + 1];
Arrays.fill(dp, max);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) {
if (coins[j] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}
LeetCode324 摆动排序
输入:nums = [1,5,1,1,6,4]
输出:[1,6,1,5,1,4]
解释:[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。
class Solution {
public void wiggleSort(int[] nums) {
int[] help = nums.clone(); //不能写成int[] help = nums,排序后两个数组都改变
Arrays.sort(help);
int N = nums.length;
//比如123456
for (int i = 1; i < nums.length; i += 2) {
nums[i] = help[--N]; //遍历完成后 x 6 x 5 x 4
}
for (int i = 0; i < nums.length; i += 2) {
nums[i] = help[--N]; //便利完成后 3 6 2 5 1 4
}
}
}
LeetCode327 区间和的个数
输入:nums = [-2,5,-1], lower = -2, upper = 2
输出:3
解释:存在三个区间:[0,0]、[2,2] 和 [0,2] ,对应的区间和分别是:-2 、-1 、2 。
class Solution {
public int countRangeSum(int[] nums, int lower, int upper) {
long s = 0;
long[] sum = new long[nums.length + 1];
for (int i = 0; i < nums.length; ++i) {
s += nums[i];
sum[i + 1] = s;
}
return countRangeSumRecursive(sum, lower, upper, 0, sum.length - 1);
}
public int countRangeSumRecursive(long[] sum, int lower, int upper, int left, int right) {
if (left == right) {
return 0;
} else {
int mid = (left + right) / 2;
int n1 = countRangeSumRecursive(sum, lower, upper, left, mid);
int n2 = countRangeSumRecursive(sum, lower, upper, mid + 1, right);
int ret = n1 + n2;
// 首先统计下标对的数量
int i = left;
int l = mid + 1;
int r = mid + 1;
while (i <= mid) {
while (l <= right && sum[l] - sum[i] < lower) {
l++;
}
while (r <= right && sum[r] - sum[i] <= upper) {
r++;
}
ret += r - l;
i++;
}
// 随后合并两个排序数组
long[] sorted = new long[right - left + 1];
int p1 = left, p2 = mid + 1;
int p = 0;
while (p1 <= mid || p2 <= right) {
if (p1 > mid) {
sorted[p++] = sum[p2++];
} else if (p2 > right) {
sorted[p++] = sum[p1++];
} else {
if (sum[p1] < sum[p2]) {
sorted[p++] = sum[p1++];
} else {
sorted[p++] = sum[p2++];
}
}
}
for (int j = 0; j < sorted.length; j++) {
sum[left + j] = sorted[j];
}
return ret;
}
}
}
LeetCode330 按要求补全数组
输入: nums = [1,3], n = 6
输出: 1
解释:
根据 nums 里现有的组合 [1], [3], [1,3],可以得出 1, 3, 4。
现在如果我们将 2 添加到 nums 中, 组合变为: [1], [2], [3], [1,3], [2,3], [1,2,3]。
其和可以表示数字 1, 2, 3, 4, 5, 6,能够覆盖 [1, 6] 区间里所有的数。
所以我们最少需要添加一个数字。
class Solution {
public int minPatches(int[] nums, int n) {
//累加的总和
long total = 0;
//需要补充的数字个数
int count = 0;
//访问的数组下标索引
int index = 0;
while (total < n) {
if (index < nums.length && nums[index] <= total + 1) {
//如果数组能组成的数字范围是[1,total],那么加上nums[index]
//就变成了[1,total]U[nums[index],total+nums[index]]
//结果就是[1,total+nums[index]]
total += nums[index++];
} else {
//添加一个新数字,并且count加1
total = total + (total + 1);
count++;
}
}
return count;
}
}
LeetCode334递增的三元数列
输入:nums = [1,2,3,4,5]
输出:true
解释:任何 i < j < k 的三元组都满足题意
class Solution {
public boolean increasingTriplet(int[] nums) {
int small = Integer.MAX_VALUE;
int mid = Integer.MAX_VALUE;
for(int num: nums){
if(num <= small)
small = num;
else if(num <= mid)
mid = num;
else
return true;
}
return false;
}
}
LeetCode335 路径交叉
输入:distance = [2,1,1,2]
输出:true

class Solution {
public boolean isSelfCrossing(int[] d) {
int n = d.length;
if (n < 4) return false;
for (int i = 3; i < n; i++) {
if (d[i] >= d[i - 2] && d[i - 1] <= d[i - 3]) return true;
if (i >= 4 && d[i - 1] == d[i - 3] && d[i] + d[i - 4] >= d[i - 2]) return true;
if (i >= 5 && d[i - 1] <= d[i - 3] && d[i - 2] > d[i - 4] && d[i] + d[i - 4] >= d[i - 2] && d[i - 1] + d[i - 5] >= d[i - 3]) return true;
}
return false;
}
}
LeetCode336 回文对
输入:words = ["abcd","dcba","lls","s","sssll"]
输出:[[0,1],[1,0],[3,2],[2,4]]
解释:可拼接成的回文串为 ["dcbaabcd","abcddcba","slls","llssssll"]
class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> ans = new ArrayList<>();
int n = words.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
if (!check(words[i]+words[j])) continue;
List<Integer> temp = new ArrayList<>();
temp.add(i); temp.add(j);
ans.add(temp);
}
}
return ans;
}
private boolean check(String s) {
int i = 0, j = s.length()-1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) return false;
i++; j--;
}
return true;
}
}
LeetCode347 前k个高频元素
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>();
for (int num : nums) {
occurrences.put(num, occurrences.getOrDefault(num, 0) + 1);
}
// int[] 的第一个元素代表数组的值,第二个元素代表了该值出现的次数
PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {
public int compare(int[] m, int[] n) {
return m[1] - n[1];
}
});
for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) {
int num = entry.getKey(), count = entry.getValue();
if (queue.size() == k) {
if (queue.peek()[1] < count) {
queue.poll();
queue.offer(new int[]{num, count});
}
} else {
queue.offer(new int[]{num, count});
}
}
int[] ret = new int[k];
for (int i = 0; i < k; ++i) {
ret[i] = queue.poll()[0];
}
return ret;
}
}
LeetCode349 两个数组的交集
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
for (int num : nums1) {
set1.add(num);
}
for (int num : nums2) {
set2.add(num);
}
return getIntersection(set1, set2);
}
public int[] getIntersection(Set<Integer> set1, Set<Integer> set2) {
if (set1.size() > set2.size()) {
return getIntersection(set2, set1);
}
Set<Integer> intersectionSet = new HashSet<Integer>();
for (int num : set1) {
if (set2.contains(num)) {
intersectionSet.add(num);
}
}
int[] intersection = new int[intersectionSet.size()];
int index = 0;
for (int num : intersectionSet) {
intersection[index++] = num;
}
return intersection;
}
}
LeetCode350 两个数组交集2
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0;
int j = 0;
List<Integer> list = new ArrayList<>();
while(i < nums1.length&& j < nums2.length){
if(nums1[i] > nums2[j]){
j++;
}else if(nums1[i] < nums2[j]){
i++;
}else{
list.add(nums1[i]);
i++;
j++;
}
}
int index = 0;
int[] nums = new int[list.size()];
for(int k = 0; k < list.size(); k++){
nums[index++] = list.get(k);
}
return nums;
}
}
LeetCode354 俄罗斯套娃信封问题
输入:envelopes = [[5,4],[6,4],[6,7],[2,3]]
输出:3
解释:最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。
class Solution {
//先按第一个元素排序之后,最长递增子序列LIS
public int maxEnvelopes(int[][] envelopes) {
if (envelopes.length == 0) {
return 0;
}
int n = envelopes.length;
Arrays.sort(envelopes, new Comparator<int[]>() {
public int compare(int[] e1, int[] e2) {
if (e1[0] != e2[0]) {
return e1[0] - e2[0];
} else {
return e2[1] - e1[1];
}
}
});
List<Integer> f = new ArrayList<Integer>();
f.add(envelopes[0][1]);
for (int i = 1; i < n; ++i) {
int num = envelopes[i][1];
if (num > f.get(f.size() - 1)) {
f.add(num);
} else {
int index = binarySearch(f, num);
f.set(index, num);
}
}
return f.size();
}
public int binarySearch(List<Integer> f, int target) {
int low = 0, high = f.size() - 1;
while (low < high) {
int mid = (high - low) / 2 + low;
if (f.get(mid) < target) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
}
LeetCode363 矩形区域不超过k的最大数值和
输入:matrix = [[1,0,1],[0,-2,3]], k = 2
输出:2
解释:蓝色边框圈出来的矩形区域 [[0, 1], [-2, 3]] 的数值和是 2,且 2 是不超过 k 的最大数字(k = 2)。

class Solution {
//二维前缀和
public int maxSumSubmatrix(int[][] mat, int k) {
int m = mat.length, n = mat[0].length;
int[][] sum = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mat[i - 1][j - 1];
}
}
int ans = Integer.MIN_VALUE;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
for (int p = i; p <= m; p++) {
for (int q = j; q <= n; q++) {
int cur = sum[p][q] - sum[i - 1][q] - sum[p][j - 1] + sum[i - 1][j - 1];
if (cur <= k) {
ans = Math.max(ans, cur);
}
}
}
}
}
return ans;
}
}
LeetCode368 最大整除子集
输入:nums = [1,2,4,8]
输出:[1,2,4,8]
class Solution {
public List<Integer> largestDivisibleSubset(int[] nums) {
int len = nums.length;
Arrays.sort(nums);
// 第 1 步:动态规划找出最大子集的个数、最大子集中的最大整数
//与最长递增子序列的思想相同
int[] dp = new int[len];
Arrays.fill(dp, 1);
int maxSize = 1;
int maxVal = dp[0];
for (int i = 1; i < len; i++) {
for (int j = 0; j < i; j++) {
// 题目中说「没有重复元素」很重要
if (nums[i] % nums[j] == 0) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
if (dp[i] > maxSize) {
maxSize = dp[i];
maxVal = nums[i];
}
}
// 第 2 步:倒推获得最大子集
List<Integer> res = new ArrayList<Integer>();
if (maxSize == 1) {
res.add(nums[0]);
return res;
}
for (int i = len - 1; i >= 0 && maxSize > 0; i--) {
if (dp[i] == maxSize && maxVal % nums[i] == 0) {
res.add(nums[i]);
maxVal = nums[i];
maxSize--;
}
}
return res;
}
}
LeetCode373 查找和最小的K对数字
输入: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
输出: [1,2],[1,4],[1,6]
解释: 返回序列中的前 3 对数:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
- 先把所有的 nums1 的索引入队,即入队的元素有 [0, 0]、[1, 0]、[2, 0]、[3, 0]、……
- 首次弹出的肯定是 [0, 0],再把 [0, 1] 入队;
- 这样就可以通过优先级队列比较 [0, 1] 和 [1, 0] 的结果,再弹出较小者;
依次进行,进行 k 轮。
class Solution { public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) { // 小顶堆 PriorityQueue<int[]> queue = new PriorityQueue<>( //相当于比较n1[0]+n2[1]与n1[1]+n2[0] (o1, o2) -> (nums1[o1[0]] + nums2[o1[1]]) - (nums1[o2[0]] + nums2[o2[1]])); List<List<Integer>> res = new LinkedList<>(); // 两个数组有一个为空,返回空 if(nums1.length==0 || nums2.length == 0){ return res; } // 将我们假想的每个数组的第一项加入小顶堆 for (int i = 0; i < Math.min(nums1.length, k); i++) { queue.add(new int[] { i, 0 }); // 加入的是坐标,小顶堆的比较器也是基于坐标比较 } // 循环K次或者堆空 while (k > 0 && !queue.isEmpty()) { // 弹出堆顶元素 int[] pair = queue.poll(); List<Integer> item = new ArrayList<>(); item.add(nums1[pair[0]]); item.add(nums2[pair[1]]); // 若我们假想的数组有下一个元素,则加入小顶堆 if (pair[1] < nums2.length - 1) { queue.add(new int[] { pair[0], pair[1] + 1 }); } res.add(item); k--; } return res; } }
LeetCode376 摆动序列
输入:nums = [1,7,4,9,2,5]
输出:6
解释:整个序列均为摆动序列,各元素之间的差值为 (6, -3, 5, -7, 3) 。
class Solution {
public int wiggleMaxLength(int[] nums) {
int down = 1, up = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1])
up = down + 1;
else if (nums[i] < nums[i - 1])
down = up + 1;
}
return nums.length == 0 ? 0 : Math.max(down, up);
}
}

LeetCode377 组合总和4
输入:nums = [1,2,3], target = 4
输出:7
解释:
所有可能的组合为:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
请注意,顺序不同的序列被视作不同的组合。
public class Solution {
/**
* 这里状态定义就是题目要求的,并不难,状态转移方程要动点脑子,也不难:
* 状态转移方程:dp[i]= dp[i - nums[0]] + dp[i - nums[1]] + dp[i - nums[2]] + ... (当 [] 里面的数 >= 0)
* 特别注意:dp[0] = 1,表示,如果那个硬币的面值刚刚好等于需要凑出的价值,这个就成为 1 种组合方案
* 再举一个具体的例子:nums=[1, 3, 4], target=7;
* dp[7] = dp[6] + dp[4] + dp[3]
* 即:7 的组合数可以由三部分组成,1 和 dp[6],3 和 dp[4], 4 和dp[3];
*
* @param nums
* @param target
* @return
*/
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target + 1];
// 这个值被其它状态参考,设置为 1 是合理的
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int num : nums) {
if (num <= i) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}
LeetCode378 有序矩阵中第K小的元素
输入:matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
输出:13
解释:矩阵中的元素为 [1,5,9,10,11,12,13,13,15],第 8 小元素是 13
//暴力法直接排序
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int rows = matrix.length, columns = matrix[0].length;
int[] sorted = new int[rows * columns];
int index = 0;
for (int[] row : matrix) {
for (int num : row) {
sorted[index++] = num;
}
}
Arrays.sort(sorted);
return sorted[k - 1];
}
}
//二分
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int low = matrix[0][0], high = matrix[matrix.length - 1][matrix[0].length - 1];
while (low < high){
int mid = low + (high - low) / 2;
int count = 0, j = matrix[0].length - 1;
for (int i = 0; i < matrix.length; i++){
while (j >= 0 && matrix[i][j] > mid)
j--;
count += (j + 1);
}
if (count < k)
low = mid + 1;
else
high = mid;
}
return low;
}
}

每次对于「猜测」的答案 midmid,计算矩阵中有多少数不大于 mid :
如果数量不少于 k,那么说明最终答案 x 不大于 mid;
如果数量少于 k,那么说明最终答案 x 大于 mid。.
LeetCode380O(1)时间插入、删除、获取随机元素
输入
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
输出
[null, true, false, true, 2, true, false, 2]
解释
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // 向集合中插入 1 。返回 true 表示 1 被成功地插入。
randomizedSet.remove(2); // 返回 false ,表示集合中不存在 2 。
randomizedSet.insert(2); // 向集合中插入 2 。返回 true 。集合现在包含 [1,2] 。
randomizedSet.getRandom(); // getRandom 应随机返回 1 或 2 。
randomizedSet.remove(1); // 从集合中移除 1 ,返回 true 。集合现在包含 [2] 。
randomizedSet.insert(2); // 2 已在集合中,所以返回 false 。
randomizedSet.getRandom(); // 由于 2 是集合中唯一的数字,getRandom 总是返回 2 。
class RandomizedSet {
//map不能获取随机元素,所以增加一个list根据索引获取元素,map的value相当于存索引
Map<Integer, Integer> dict;
List<Integer> list;
Random rand = new Random();
/** Initialize your data structure here. */
public RandomizedSet() {
dict = new HashMap();
list = new ArrayList();
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if (dict.containsKey(val)) return false;
dict.put(val, list.size());
list.add(list.size(), val);
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
if (! dict.containsKey(val)) return false;
// move the last element to the place idx of the element to delete
int lastElement = list.get(list.size() - 1);
int idx = dict.get(val);
list.set(idx, lastElement);
dict.put(lastElement, idx);
// delete the last element
list.remove(list.size() - 1);
dict.remove(val);
return true;
}
/** Get a random element from the set. */
public int getRandom() {
return list.get(rand.nextInt(list.size()));
}
}
LeetCode381 O(1)时间插入、删除、获取随机元素包含重复元素
输入
["RandomizedCollection", "insert", "insert", "insert", "getRandom", "remove", "getRandom"]
[[], [1], [1], [2], [], [1], []]
输出
[null, true, false, true, 2, true, 1]
解释
RandomizedCollection collection = new RandomizedCollection();// 初始化一个空的集合。
collection.insert(1);// 向集合中插入 1 。返回 true 表示集合不包含 1 。
collection.insert(1);// 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。
collection.insert(2);// 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。
collection.getRandom();// getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
collection.remove(1);// 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。
collection.getRandom();// getRandom 应有相同概率返回 1 和 2 。
class RandomizedCollection {
Map<Integer, Set<Integer>> idx;
List<Integer> nums;
/** Initialize your data structure here. */
public RandomizedCollection() {
//相较于上题,用一个set储存元素的所有索引
idx = new HashMap<Integer, Set<Integer>>();
nums = new ArrayList<Integer>();
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
nums.add(val);
Set<Integer> set = idx.getOrDefault(val, new HashSet<Integer>());
set.add(nums.size() - 1);
idx.put(val, set);
//只有第一次添加返回true
return set.size() == 1;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
if (!idx.containsKey(val)) {
return false;
}
Iterator<Integer> it = idx.get(val).iterator();
int i = it.next();
int lastNum = nums.get(nums.size() - 1);
nums.set(i, lastNum);
//移除set里面的第一个索引
idx.get(val).remove(i);
idx.get(lastNum).remove(nums.size() - 1);
if (i < nums.size() - 1) {
idx.get(lastNum).add(i);
}
if (idx.get(val).size() == 0) {
idx.remove(val);
}
nums.remove(nums.size() - 1);
return true;
}
/** Get a random element from the collection. */
public int getRandom() {
return nums.get((int) (Math.random() * nums.size()));
}
}
LeetCode384 打乱数组
输入
["Solution", "shuffle", "reset", "shuffle"]
[[[1, 2, 3]], [], [], []]
输出
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
解释
Solution solution = new Solution([1, 2, 3]);
solution.shuffle(); // 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。例如,返回 [3, 1, 2]
solution.reset(); // 重设数组到它的初始状态 [1, 2, 3] 。返回 [1, 2, 3]
solution.shuffle(); // 随机返回数组 [1, 2, 3] 打乱后的结果。例如,返回 [1, 3, 2]
class Solution {
private int[] nums;
private Random random;
public Solution(int[] nums) {
this.nums = nums;
random = new Random();
}
public int[] reset() {
return nums;
}
public int[] shuffle() {
if (nums == null)
return null;
int[] a = nums.clone();
for (int j = 1; j < a.length; j++){
int i = random.nextInt(j + 1);
swap(a, i, j);
}
return a;
}
private void swap(int[] a, int i, int j){
if (i != j){
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int[] param_1 = obj.reset();
* int[] param_2 = obj.shuffle();
*/
LeetCode391 完美矩形
输入:rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
输出:true
解释:5 个矩形一起可以精确地覆盖一个矩形区域。
class Solution {
public boolean isRectangleCover(int[][] rectangles) {
//找规律型题解
// 计算每个小矩形面积是否等于大矩形面积
// 看每个顶点出现的次数,如果最后出现一次的顶点不是四个,则说明不符合完美矩形
int area = 0;
Set<Integer> set = new HashSet<>();
// 记录大矩形的左下角和右上角
int a1 = Integer.MAX_VALUE, b1 = Integer.MAX_VALUE;
int a2 = Integer.MIN_VALUE, b2 = Integer.MIN_VALUE;
for (int[] rec : rectangles) {
// 小矩形的坐标
int x1 = rec[0];
int y1 = rec[1];
int x2 = rec[2];
int y2 = rec[3];
// 计算左下角
if (x1 < a1 || y1 < b1) {
a1 = x1;
b1 = y1;
}
// 计算右上角
if (x2 > a2 || y2 > b2) {
a2 = x2;
b2 = y2;
}
// 计算面积
area += (x2 - x1) * (y2 - y1);
// 记录每个顶点出现的次数
record(set, x1, y1);
record(set, x1, y2);
record(set, x2, y1);
record(set, x2, y2);
}
// 通过左下角和右上角坐标可以算出总面积
int totalArea = (a2 - a1) * (b2 - b1);
// 如果两个面积不相等,直接返回false
if (area != totalArea) return false;
// 四个为1的顶点正好是大矩形的四个顶点
return set.size() == 4 && set.contains(key(a1, b1)) && set.contains(key(a1, b2)) && set.contains(key(a2, b1)) && set.contains(key(a2, b2));
}
private void record(Set<Integer> set, int x, int y) {
// 记录顶点出现的次数,如果一个顶点出现偶数次,则移除
int key = key(x, y);
if (set.contains(key)) {
set.remove(key);
} else {
set.add(key);
}
}
private int key(int x, int y) {
// 二维坐标转一维,方便比较
// 100000007是随便取的一个大质数
// 这里即使溢出了也没什么问题
return x * 100000007 + y;
}
}
LeetCode393 UTF-8编码验证
Char. number range | UTF-8 octet sequence
(hexadecimal) | (binary)
--------------------+---------------------------------------------
0000 0000-0000 007F | 0xxxxxxx
0000 0080-0000 07FF | 110xxxxx 10xxxxxx
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
输入:data = [197,130,1]
输出:true
解释:数据表示字节序列:11000101 10000010 00000001。
这是有效的 utf-8 编码,为一个 2 字节字符,跟着一个 1 字节字符。
class Solution {
public boolean validUtf8(int[] data) {
int n = data.length;
for (int i = 0; i < n; ) {
int t = data[i], j = 7;
//先统计 data[i]从第 77 位开始往后有多少位连续的1,代表这是一个几字节的字符,记为cnt
while (j >= 0 && (((t >> j) & 1) == 1)) j--;
int cnt = 7 - j;
//如果 cntcnt 为 11 或者大于 44 均违反编码规则(与字符长度为 11 时的编码规则 和 字符长度只能是 11 到 44 冲突)
if (cnt == 1 || cnt > 4) return false;
//如果位置 i后面不足 cnt - 1 也返回
if (i + cnt - 1 >= n) return false;
for (int k = i + 1; k < i + cnt; k++) {
if ((((data[k] >> 7) & 1) == 1) && (((data[k] >> 6) & 1) == 0)) continue;
return false;
}
if (cnt == 0) i++;
else i += cnt;
}
return true;
}
}
LeetCode396 旋转函数
输入: nums = [4,3,2,6]
输出: 26
解释:
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
假设上一次的和是这样计算的:0A[i]+1A[i+1]+2A[i+2]+……+(n-1)A[i-1],记为式一
那么下一次旋转后,就变为:0A[i-1]+1A[i]+2A[i+1]+……+(n-1)A[i-2] 调整下顺序,就是:1A[i]+2A[i+1]+……+(n-1)A[i-2]+0A[i-1],记为式二
对比式一和式二,可以看出: 1、A[i]、A[i]、A[i+1]……A[i-2],这些项系数都是式二比式一大1 2、A[i-1]这项式二系数比式一小n-1 即两式差值为A[i]+A[i]+A[i+1]……A[i-2]-(n-1)*A[i-1]
而将整个数组累加之后,就是A[i]+A[i]+A[i+1]……A[i-2]+A[i-1]
对比两式差值,显然只要再减去n*A[i-1]即可
综上,只要前一次计算的和加上整个数组的和再减去前一次的末尾元素乘以n即可
class Solution {
//数学题
public int maxRotateFunction(int[] nums) {
int n = nums.length;
int cur = 0, sum = 0;
// 一次遍历,求出数组总和,与没有翻转情况下函数的初始值
for(int i = 0; i < n; i++) {
cur += nums[i] * i;
sum += nums[i];
}
int res = cur;
// 遍历每一次的翻转情况,自然先是从末端开始翻转
for(int i = n - 1; i > 0; i --) {
// 增加总和
cur += sum;
// 减去n份的末尾值
cur -= nums[i] * n;
// 求最大值
res = Math.max(cur, res);
}
return res;
}
}
LeetCode399 除法求值
输入:equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
输出:[6.00000,0.50000,-1.00000,1.00000,-1.00000]
解释:
条件:a / b = 2.0, b / c = 3.0
问题:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
结果:[6.0, 0.5, -1.0, 1.0, -1.0 ]
class Solution {
//并查集问题
/**
* key : 当前节点
* value : 其父节点
*/
private Map<String, String> parents = new HashMap<>();
/**
* key : 当前节点
* value : 父节点/当前节点
*/
private Map<String, Double> values = new HashMap<>();
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
for (int i = 0; i < equations.size(); i++) {
union(equations.get(i).get(0), equations.get(i).get(1), values[i]);
}
double[] result = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
String e = queries.get(i).get(0);
String q = queries.get(i).get(1);
if (!(parents.containsKey(e) && parents.containsKey(q))) {
result[i] = -1;
continue;
}
if (e.equals(q)) {
result[i] = 1;
continue;
}
String r1 = root(e);
String r2 = root(q);
if (!r1.equals(r2)) {
// 如果两者不相等,说明两个节点是不连通的
result[i] = -1;
continue;
}
result[i] = pm(q)/pm(e);
}
return result;
}
private void union(String parent, String child, double value) {
add(parent);
add(child);
String r1 = root(parent);
String r2 = root(child);
if (!r1.equals(r2)) {
parents.put(r2, r1);
values.put(r2, value * (pm(parent)/pm(child)));
}
}
private void add(String x) {
if (!parents.containsKey(x)) {
parents.put(x, x);
values.put(x, 1.0);
}
}
/**
* 找到x的根节点
*/
private String root(String x) {
while (!parents.get(x).equals(x)) {
x = parents.get(x);
}
return x;
}
/**
* 循环的pm函数
*/
private double pm(String x) {
double v = 1;
while (!parents.get(x).equals(x)) {
v*= values.get(x);
x = parents.get(x);
}
return v;
}
// /**
// * 递归的pm函数
// * @param x
// * @return
// */
// private double pm(String x){
// return parents.get(x).equals(x)?1:values.get(x)*pm(parents.get(x));
// }
}
LeetCode403 青蛙过河
输入:stones = [0,1,3,5,6,8,12,17]
输出:true
解释:青蛙可以成功过河,按照如下方案跳跃:跳 1 个单位到第 2 块石子, 然后跳 2 个单位到第 3 块石子, 接着 跳 2 个单位到第 4 块石子, 然后跳 3 个单位到第 6 块石子, 跳 4 个单位到第 7 块石子, 最后,跳 5 个单位到第 8 个石子(即最后一块石子)。
首先明确,下一步可以由三种状态转换而来
上一个石头到当前位置的距离为 k
上一个石头到当前位置的距离为 k+1
上一个石头到当前位置的距离为 k-1
在这个时候就可以回头去考虑在上一个石头的状态要怎么来,然后快进到动态规划
创建一个动规数组dp[i][k],其中:
行表示对应石子的编号
列表示上一跳的距离
初始化:dp[0][0] = true,在第一个石头上的时候必然为true
遍历每个石子,然后回头再去遍历已经遍历过的石子
根据上一次所在的石子位置,判断在[±1]的范围里面是否能够满足从上一个位置跳到当前位置
动规公式:dp[i][k] = dp[j][k - 1] || dp[j][k] || dp[j][k + 1]
优化:相邻石子的距离大于其编号的时候,必然跳不过去(存在一个距离递增的关系)
最好的状态就是第一次跳1单位距离,第二次跳2单位距离…第n次跳n单位距离
其中的距离是永远无法超过其对应石子下标的
重点解释递归公式:dp[i][k] = dp[j][k - 1] || dp[j][k] || dp[j][k + 1]
优先要理解其中对应的意思
i:当前石子的下标
j:上一个石子的下标,范围必然是小于1的
k:step的长度,只能对其进行±1操作
在当前石子前面的每一个石子都有机会调到当前石子来,所以全部遍历
判断是否存在
在上上个石子,只跳k就能到上个石子
在上上个石子,只跳k + 1就能到上个石子
在上上个石子,只跳k - 1就能到上个石子
只要其中存在一个,那么就说明能用上一个石子跳到当前石子
即其中||的作用
class Solution {
public boolean canCross(int[] stones) {
int n = stones.length;
//动规数组,其中行表示对应石子的编号,列表示上一跳的距离
boolean[][] dp = new boolean[n][n];
// 初始状态一定是为true
dp[0][0] = true;
for (int i = 1; i < n; ++i) {
for (int j = i - 1; j >= 0; --j) {
int k = stones[i] - stones[j];
// 相邻石子的距离大于其编号的时候,必然跳不过去(存在一个距离递增的关系)
if (k > j + 1) {
break;
}
// j对应的上一次所在的石子位置,判断在[k±1]的范围里面是否能够满足从j跳到i
dp[i][k] = dp[j][k - 1] || dp[j][k] || dp[j][k + 1];
// 到了数组最末尾的时候,进行一个判断,判断是否能跳到最后一个石子
if (i == n - 1 && dp[i][k]) {
return true;
}
}
}
return false;
}
}
LeetCode406 根据身高重建队列
输入:people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
输出:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
解释:
编号为 0 的人身高为 5 ,没有身高更高或者相同的人排在他前面。
编号为 1 的人身高为 7 ,没有身高更高或者相同的人排在他前面。
编号为 2 的人身高为 5 ,有 2 个身高更高或者相同的人排在他前面,即编号为 0 和 1 的人。
编号为 3 的人身高为 6 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。
编号为 4 的人身高为 4 ,有 4 个身高更高或者相同的人排在他前面,即编号为 0、1、2、3 的人。
编号为 5 的人身高为 7 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。
因此 [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] 是重新构造后的队列。
class Solution {
/**
* 解题思路:先排序再插入
* 1.排序规则:按照先H高度降序,K个数升序排序
* 2.遍历排序后的数组,根据K插入到K的位置上
*
* 核心思想:高个子先站好位,矮个子插入到K位置上,前面肯定有K个高个子,矮个子再插到前面也满足K的要求
*
* @param people
* @return
*/
public int[][] reconstructQueue(int[][] people) {
// [7,0], [7,1], [6,1], [5,0], [5,2], [4,4]
// 再一个一个插入。
// [7,0]
// [7,0], [7,1]
// [7,0], [6,1], [7,1]
// [5,0], [7,0], [6,1], [7,1]
// [5,0], [7,0], [5,2], [6,1], [7,1]
// [5,0], [7,0], [5,2], [6,1], [4,4], [7,1]
Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]);
LinkedList<int[]> list = new LinkedList<>();
for (int[] i : people) {
list.add(i[1], i);
}
return list.toArray(new int[list.size()][2]);
}
}
LeetCode407 接雨水
输入: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
输出: 4
解释: 下雨后,雨水将会被上图蓝色的方块中。总的接雨水量为1+2+1=4。

接雨水I中,我们维护了左右两个最高的墙,那么在这里,就是维护周围一个圈,用堆来维护周围这一圈中的最小元素。为什么是维护最小的元素不是最大的元素呢,因为木桶原理呀。这个最小的元素从堆里弹出来,和它四个方向的元素去比较大小,看能不能往里灌水,怎么灌水呢,如果用方向就比较复杂了,我们可以用visited数组来表示哪些遍历过,哪些没遍历过。如果当前弹出来的高度比它周围的大,他就能往矮的里面灌水了,灌水后要把下一个柱子放进去的时候,放的高度要取两者较大的,也就是灌水后的高度,不是它原来矮的时候的高度了,如果不能灌水,继续走。
Given the following 3x6 height map:
[
[1,4,3,1,3,2],
[3,2,1,3,2,4],
[2,3,3,2,3,1]
]
就拿这个例子来说,我们先把第一圈都放进去,然后开始从堆中弹出,第一圈,最小值是1(遍历时候标记为访问过),1从堆里弹出来,比如弹出来1(坐标[0,3]),它下方的3没有被访问过,尝试灌水,发现不能灌水,3入堆,然后继续弹。比如说,我此时弹出来一个3(坐标[1,0]),它能向右边2(坐标[1,1])灌水,那这边就可以统计了,然后我们要插入2(坐标[1,1])这个位置,但是插入的时候,要记得你得是插入的高度得是灌水后的高度,而不是原来的高度了
class Solution {
public int trapRainWater(int[][] heights) {
if (heights == null || heights.length == 0) return 0;
int n = heights.length;
int m = heights[0].length;
// 用一个vis数组来标记这个位置有没有被访问过
boolean[][] vis = new boolean[n][m];
// 优先队列中存放三元组 [x,y,h] 坐标和高度
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[2] - o2[2]);
// 先把最外一圈放进去
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
pq.offer(new int[]{i, j, heights[i][j]});
vis[i][j] = true;
}
}
}
int res = 0;
// 方向数组,把dx和dy压缩成一维来做
int[] dirs = {-1, 0, 1, 0, -1};
while (!pq.isEmpty()) {
int[] poll = pq.poll();
// 看一下周围四个方向,没访问过的话能不能往里灌水
for (int k = 0; k < 4; k++) {
int nx = poll[0] + dirs[k];
int ny = poll[1] + dirs[k + 1];
// 如果位置合法且没访问过
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny]) {
// 如果外围这一圈中最小的比当前这个还高,那就说明能往里面灌水啊
if (poll[2] > heights[nx][ny]) {
res += poll[2] - heights[nx][ny];
}
// 如果灌水高度得是你灌水后的高度了,如果没灌水也要取高的
pq.offer(new int[]{nx, ny, Math.max(heights[nx][ny], poll[2])});
vis[nx][ny] = true;
}
}
}
return res;
}
}
LeetCode410 分割数组的最大值
输入:nums = [7,2,5,10,8], m = 2
输出:18
解释:
一共有四种方法将 nums 分割为 2 个子数组。
其中最好的方式是将其分为 [7,2,5] 和 [10,8] 。
因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。

class Solution {
public int splitArray(int[] nums, int m) {
int n = nums.length;
int[][] f = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
Arrays.fill(f[i], Integer.MAX_VALUE);
}
int[] sub = new int[n + 1];
for (int i = 0; i < n; i++) {
sub[i + 1] = sub[i] + nums[i];
}
f[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= Math.min(i, m); j++) {
for (int k = 0; k < i; k++) {
f[i][j] = Math.min(f[i][j], Math.max(f[k][j - 1], sub[i] - sub[k]));
}
}
}
return f[n][m];
}
}
LeetCode413 等差数列划分
输入:nums = [1,2,3,4]
输出:3
解释:nums 中有三个子等差数组:[1, 2, 3]、[2, 3, 4] 和 [1,2,3,4] 自身。
class Solution {
public int numberOfArithmeticSlices(int[] A) {
int curr = 0, sum = 0;
for (int i = 2; i < A.length; i++)
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]){
curr += 1;
sum += curr;
}else{
curr = 0;
}
return sum;
}
}
LeetCode414 第三大的数
输入:[3, 2, 1]
输出:1
解释:第三大的数是 1 。
class Solution {
public int thirdMax(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int x : nums) set.add(x);
List<Integer> list = new ArrayList<>(set);
Collections.sort(list);
return list.size() < 3 ? list.get(list.size() - 1) : list.get(list.size() - 3);
}
}
class Solution {
long INF = (long)-1e18;
public int thirdMax(int[] nums) {
long a = INF, b = INF, c = INF;
for (int x : nums) {
if (x > a) {
c = b; b = a; a = x;
} else if (x < a && x > b) {
c = b; b = x;
} else if (x < b && x > c) {
c = x;
}
}
return c != INF ? (int)c : (int)a;
}
}
LeetCode416 分割等和子集
输入:nums = [1,5,11,5]
输出:true
解释:数组可以分割成 [1, 5, 5] 和 [11] 。
class Solution {
public boolean canPartition(int[] nums) {
int len = nums.length;
int sum = 0;
for(int num: nums){
sum += num;
}
if(sum % 2 == 1)
return false;
int tar = sum / 2;
int[][] dp = new int[len + 1][tar + 1];
for(int i = 1; i <= len; i++){
for(int j = 1; j <= tar; j++){
if(j >= nums[i - 1]){
dp[i][j] = Math.max(dp[i - 1][j],dp[i-1][j-nums[i-1]] + nums[i-1]);
}else{
dp[i][j] = dp[i-1][j];
}
}
}
return dp[len][tar] == tar;
}
}
LeetCode417 太平洋大西洋水流问题
输入: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
输出: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]

思路是从海洋开始逆流 如果可以逆流到 就标记为1 然后检查两个海洋都可以逆流到的区域
class Solution {
public List<List<Integer>> pacificAtlantic(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return new ArrayList<>();
}
int m = matrix.length;
int n = matrix[0].length;
int[][] pacific = new int[m][n];
int[][] atlantic = new int[m][n];
//从海洋边界开始
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
dfs(matrix, pacific, i, j, matrix[i][j]);
}
if (i == m - 1 || j == n - 1) {
dfs(matrix, atlantic, i, j, matrix[i][j]);
}
}
}
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] == 1 && atlantic[i][j] == 1) {
res.add(Arrays.asList(i, j));
}
}
}
return res;
}
private void dfs(int[][] matrix, int[][] aux, int i, int j, int pre) {
//判断边界
if (i < 0 || j < 0 || i > matrix.length - 1 || j > matrix[0].length - 1
//已经流到过了
|| aux[i][j] == 1
//不能流动
|| matrix[i][j] < pre) {
return;
}
aux[i][j] = 1;
dfs(matrix, aux, i - 1, j, matrix[i][j]);
dfs(matrix, aux, i + 1, j, matrix[i][j]);
dfs(matrix, aux, i, j - 1, matrix[i][j]);
dfs(matrix, aux, i, j + 1, matrix[i][j]);
}
}
LeetCode419 甲板上的战舰
输入:board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
输出:2

class Solution {
public int countBattleships(char[][] board) {
int m = board.length, n = board[0].length;
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i > 0 && board[i - 1][j] == 'X') continue;
if (j > 0 && board[i][j - 1] == 'X') continue;
if (board[i][j] == 'X') ans++;
}
}
return ans;
}
}
LeetCode421 数组中的最大异或值
输入:nums = [3,10,5,25,2,8]
输出:28
解释:最大运算结果是 5 XOR 25 = 28.
如果不好理解的话,先可以思考这么一个问题: 给定1个数组nums,再给定1个数值x。请判断nums数组中是否存在2个数,且这2个数的异或结果值为x。 暴力法:
for(int i = 1; i < nums.length; ++i)
for(int j = 0; j < i; ++j)
if(nums[i] ^ nums[j] == x)
return true;
如何不使用暴力法解决该问题呢?假设数组中存在a和b,满足a ^ b == x,则a ^ x == b与b ^ x == a也必然满足。 反过来讲,你可以遍历数组nums,将当前遍历的数记为a,计算a ^ x,若数组中存在1个数(记为b),满足b == a ^ x,则说明数组中存在2个数a和b,满足a ^ b == x。 理解了这一点的话,就可利用HashSet,将nums数组中所有数值都存入HashSet,再如下列代码一样操作:
for(int num : hashSet)
if(hashSet.contains(num ^ x))
return true;
import java.util.HashSet;
import java.util.Set;
public class Solution {
// 先确定高位,再确定低位(有点贪心算法的意思),才能保证这道题的最大性质
// 一位接着一位去确定这个数位的大小
// 利用性质: a ^ b = c ,则 a ^ c = b,且 b ^ c = a
public int findMaximumXOR(int[] nums) {
int res = 0;
int mask = 0;
for (int i = 30; i >= 0; i--) {
// 注意点1:注意保留前缀的方法,mask 是这样得来的
// 用异或也是可以的 mask = mask ^ (1 << i);
mask = mask | (1 << i);
// System.out.println(Integer.toBinaryString(mask));
Set<Integer> set = new HashSet<>();
for (int num : nums) {
// 注意点2:这里使用 & ,保留前缀的意思(从高位到低位)
set.add(num & mask);
}
// 这里先假定第 n 位为 1 ,前 n-1 位 res 为之前迭代求得
int temp = res | (1 << i);
for (Integer prefix : set) {
if (set.contains(prefix ^ temp)) {
res = temp;
break;
}
}
}
return res;
}
}
LeetCode427 建立四叉树
输入:grid = [[0,1],[1,0]]
输出:[[0,1],[1,0],[1,1],[1,1],[1,0]]
解释:此示例的解释如下:
请注意,在下面四叉树的图示中,0 表示 false,1 表示 True 。

/*
// Definition for a QuadTree node.
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node() {
this.val = false;
this.isLeaf = false;
this.topLeft = null;
this.topRight = null;
this.bottomLeft = null;
this.bottomRight = null;
}
public Node(boolean val, boolean isLeaf) {
this.val = val;
this.isLeaf = isLeaf;
this.topLeft = null;
this.topRight = null;
this.bottomLeft = null;
this.bottomRight = null;
}
public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
this.val = val;
this.isLeaf = isLeaf;
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}
};
*/
class Solution {
public Node construct(int[][] grid) {
int m = grid.length, n = grid[0].length;
return constructTree(grid, 0, 0, m - 1, n - 1);
}
//upper x/y && down x/y, 上,左,下,右
public Node constructTree(int[][] grid, int ux, int uy, int dx, int dy) {
boolean isLeaf = true;
for (int i = ux; i <= dx; i++) {
for (int j = uy; j <= dy; j++) {
if (grid[i][j] != grid[ux][uy]) {
isLeaf = false;
break;
}
}
}
if (isLeaf) return new Node(grid[ux][uy] == 1, isLeaf);
//middle x/y
int mx = ux + dx >> 1, my = uy + dy >> 1;
Node upLeft = constructTree(grid, ux, uy, mx, my);
Node upRight = constructTree(grid, ux, my + 1, mx, dy);
Node downLeft = constructTree(grid, mx + 1, uy, dx, my);
Node downRight = constructTree(grid, mx + 1, my + 1, dx, dy);
return new Node(true, isLeaf, upLeft, upRight, downLeft, downRight);
}
}
LeetCode435 无重叠区间
输入: intervals = [[1,2],[2,3],[3,4],[1,3]]
输出: 1
解释: 移除 [1,3] 后,剩下的区间没有重叠。
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
if (intervals.length == 0)
return 0;
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
int end = intervals[0][1];
int count = 0;
for (int i = 1; i < intervals.length; i++){
if (intervals[i][0] < end){
end = Math.min(end, intervals[i][1]);
count++;
}else{
end = intervals[i][1];
}
}
return count;
}
}
LeetCode436 寻找右区间
输入:intervals = [[3,4],[2,3],[1,2]]
输出:[-1,0,1]
解释:对于 [3,4] ,没有满足条件的“右侧”区间。
对于 [2,3] ,区间[3,4]具有最小的“右”起点;
对于 [1,2] ,区间[2,3]具有最小的“右”起点。
class Solution {
public int[] findRightInterval(int[][] intervals) {
int n = intervals.length;
TreeMap<Integer, Integer> tree = new TreeMap<>();
for (int i = 0; i < n; i++)
tree.put(intervals[i][0], i);
int[] ans = new int[n];
for (int i = 0; i < n; i++)
ans[i] = tree.ceilingKey(intervals[i][1]) == null ? -1 : tree.get(tree.ceilingKey(intervals[i][1]));
return ans;
}
}
LeetCode442 数组中重复的数据
输入:nums = [4,3,2,7,8,2,3,1]
输出:[2,3]
class Solution {
//找到数字i时,将位置i-1处的数字翻转为负数。如果位置i-1 上的数字已经为负,则i是出现两次的数字。
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
int index = Math.abs(nums[i])-1;
if (nums[index] < 0)
res.add(Math.abs(index+1));
nums[index] = -nums[index];
}
return res;
}
}
LeetCode446 等差数列划分2
输入:nums = [2,4,6,8,10]
输出:7
解释:所有的等差子序列为:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int numberOfArithmeticSlices(int[] nums) {
int len = nums.length;
if (len < 3) {
return 0;
}
// dp[i]:以 nums[i] 结尾,公差为 key 的长度大于等于 2 的等差数列的个数
Map<Long, Integer>[] dp = new HashMap[len];
for (int i = 0; i < len; i++) {
dp[i] = new HashMap<>();
}
int res = 0;
// 从 1 开始就可以
for (int i = 1; i < len; i++) {
for (int j = 0; j < i; j++) {
long diff = (long) nums[i] - nums[j];
if (diff > Integer.MAX_VALUE || diff < Integer.MIN_VALUE) {
continue;
}
// dp[i][diff] += (dp[j][diff] + 1) ,Java 写起来有点麻烦,表示 nums[i] 可以接在之前「公差相等」的等差数列后面形成长度更长的等差数列
dp[i].put(diff, dp[i].getOrDefault(diff, 0) + dp[j].getOrDefault(diff, 0) + 1);
// 与之前的等差数列公差相等的时候,说明可以接上,此时计算结果
if (dp[j].containsKey(diff)) {
// 理解:对结果的贡献「恰好是」之前的某个 j 的对应状态值,这里的 j 一定会在之前的某一个 i 加上 1,看上面有注释的那一行代码
res += dp[j].get(diff);
}
}
}
return res;
}
}
LeetCode447 回旋镖的数量
输入:points = [[0,0],[1,0],[2,0]]
输出:2
解释:两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]
class Solution {
public int numberOfBoomerangs(int[][] points) {
int n = points.length;
int ans = 0;
for (int i = 0; i < n; i++) {
Map<Integer, Integer> map = new HashMap<>();
for (int j = 0; j < n; j++) {
if (i == j) continue;
int x = points[i][0] - points[j][0], y = points[i][1] - points[j][1];
int dist = x * x + y * y;
map.put(dist, map.getOrDefault(dist, 0) + 1);
}
for (int dist : map.keySet()) {
int cnt = map.get(dist);
ans += cnt * (cnt - 1);
}
}
return ans;
}
}
LeetCode448找到所有数组中消失的数字
输入:nums = [4,3,2,7,8,2,3,1]
输出:[5,6]
具体来说,遍历nums,每遇到一个数 x,就让nums[x−1] 增加 n。由于 nums 中所有数均在 [1,n]中,增加以后,这些数必然大于 n。最后我们遍历nums,若 nums[i] 未大于 n,就说明没有遇到过数 i+1。这样我们就找到了缺失的数字。
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
for (int num : nums) {
int x = (num - 1) % n;
nums[x] += n;
}
List<Integer> ret = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
if (nums[i] <= n) {
ret.add(i + 1);
}
}
return ret;
}
}
LeetCode452 用最少的箭引爆气球
输入:points = [[10,16],[2,8],[1,6],[7,12]]
输出:2
解释:气球可以用2支箭来爆破:
-在x = 6处射出箭,击破气球[2,8]和[1,6]。
-在x = 11处发射箭,击破气球[10,16]和[7,12]。
class Solution {
public int findMinArrowShots(int[][] points) {
//边界条件判断
if (points == null || points.length == 0)
return 0;
//按照每个气球的右边界排序
Arrays.sort(points, (a, b) -> a[1] > b[1] ? 1 : -1);
//获取排序后第一个气球右边界的位置,我们可以认为是箭射入的位置
int last = points[0][1];
//统计箭的数量
int count = 1;
for (int i = 1; i < points.length; i++) {
//如果箭射入的位置小于下标为i这个气球的左边位置,说明这支箭不能
//击爆下标为i的这个气球,需要再拿出一支箭,并且要更新这支箭射入的
//位置
if (last < points[i][0]) {
last = points[i][1];
count++;
}
}
return count;
}
}
LeetCode453 最少操作次数使数组元素相等
输入:nums = [1,2,3]
输出:3
解释:
只需要3次操作(注意每次操作会增加两个元素的值):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
class Solution {
public int minMoves(int[] nums) {
int n = nums.length;
long min = nums[0], sum = 0;
for (int i : nums) {
min = Math.min(min, i);
sum += i;
}
//核心公式
return (int)(sum - min * n);
}
}
LeetCode454 四数相加2
输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
输出:2
解释:
两个元组如下:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>();
//Map<Integer, Integer> map = new HashMap<>();
int res = 0;
for(int i = 0;i<A.length;i++){
for(int j= 0;j<B.length;j++){
int sumAB = A[i]+B[j];
if(map.containsKey(sumAB)) map.put(sumAB,map.get(sumAB)+1);
else map.put(sumAB,1);
}
}
for(int i = 0;i<C.length;i++){
for(int j = 0;j<D.length;j++){
int sumCD = -(C[i]+D[j]);
if(map.containsKey(sumCD)) res += map.get(sumCD);
}
}
return res;
}
}
LeetCode455 分发饼干
输入: g = [1,2,3], s = [1,1]
输出: 1
解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int count = 0;
for (int j = 0; count < g.length && j < s.length; j++){
if (g[count] <= s[j])
count++;
}
return count;
}
}
LeetCode456 132模式
输入:nums = [1,2,3,4]
输出:false
解释:序列中不存在 132 模式的子序列。
输入:nums = [3,1,4,2]
输出:true
解释:序列中有 1 个 132 模式的子序列: [1, 4, 2] 。
从后往前,定义单独栈单调递减,出栈的最大值定义为K,有出栈,证明栈里保存比K大的值,同时在循环中判断是否存在num[i]<k,存在就返回true
class Solution {
public boolean find132pattern(int[] nums) {
int n = nums.length;
Deque<Integer> d = new ArrayDeque<>();
int k = Integer.MIN_VALUE;
for (int i = n - 1; i >= 0; i--) {
if (nums[i] < k) return true;
while (!d.isEmpty() && d.peekLast() < nums[i]) {
// 事实上,k 的变化也具有单调性,直接使用 k = pollLast() 也是可以的
k = Math.max(k, d.pollLast());
}
d.addLast(nums[i]);
}
return false;
}
}
LeetCode457 环形数组是否存在循环
输入:nums = [2,-1,1,2,2]
输出:true
解释:存在循环,按下标 0 -> 2 -> 3 -> 0 。循环长度为 3 。
class Solution {
int n;
int[] nums;
public boolean circularArrayLoop(int[] _nums) {
nums = _nums;
n = nums.length;
for (int i = 0; i < n; i++) {
if (check(i)) return true;
}
return false;
}
boolean check(int start) {
int cur = start;
boolean flag = nums[start] > 0;
int k = 1;
while (true) {
if (k > n) return false;
int next = ((cur + nums[cur]) % n + n ) % n;
if (flag && nums[next] < 0) return false;
if (!flag && nums[next] > 0) return false;
if (next == start) return k > 1;
cur = next;
k++;
}
}
}
LeetCode462 最少移动次数使数组元素相等2
输入:
[1,2,3]
输出:
2
说明:
只有两个动作是必要的(记得每一步仅可使其中一个元素加1或减1):
[1,2,3] => [2,2,3] => [2,2,2]
class Solution {
public int minMoves2(int[] nums) {
Arrays.sort(nums);
int i = 0, j = nums.length - 1;
int count = 0;
while (i < j){
count += nums[j--] - nums[i++];
}
return count;
}
}
LeetCode463 岛屿的周长
输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
输出:16
解释:它的周长是上面图片中的 16 个黄色的边


岛屿的周长就是岛屿方格和非岛屿方格相邻的边的数量。注意,这里的非岛屿方格,既包括水域方格,也包括网格的边界。
class Solution {
public int islandPerimeter(int[][] grid) {
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
if (grid[r][c] == 1) {
// 题目限制只有一个岛屿,计算一个即可
return dfs(grid, r, c);
}
}
}
return 0;
}
int dfs(int[][] grid, int r, int c) {
if (!(0 <= r && r < grid.length && 0 <= c && c < grid[0].length)) {
return 1;
}
if (grid[r][c] == 0) {
return 1;
}
if (grid[r][c] != 1) {
return 0;
}
grid[r][c] = 2;
return dfs(grid, r - 1, c)
+ dfs(grid, r + 1, c)
+ dfs(grid, r, c - 1)
+ dfs(grid, r, c + 1);
}
}
LeetCode472 连接词
输入:words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
输出:["catsdogcats","dogcatsdog","ratcatdogcat"]
解释:"catsdogcats" 由 "cats", "dog" 和 "cats" 组成;
"dogcatsdog" 由 "dog", "cats" 和 "dog" 组成;
"ratcatdogcat" 由 "rat", "cat", "dog" 和 "cat" 组成。
class Solution {
Set<Long> set = new HashSet<>();
int P = 131, OFFSET = 128;
public List<String> findAllConcatenatedWordsInADict(String[] words) {
for (String s : words) {
long hash = 0;
for (char c : s.toCharArray()) hash = hash * P + (c - 'a') + OFFSET;
set.add(hash);
}
List<String> ans = new ArrayList<>();
for (String s : words) {
if (check(s)) ans.add(s);
}
return ans;
}
boolean check(String s) {
int n = s.length();
int[] f = new int[n + 1];
Arrays.fill(f, -1);
f[0] = 0;
for (int i = 0; i <= n; i++) {
if (f[i] == -1) continue;
long cur = 0;
for (int j = i + 1; j <= n; j++) {
cur = cur * P + (s.charAt(j - 1) - 'a') + OFFSET;
if (set.contains(cur)) f[j] = Math.max(f[j], f[i] + 1);
}
if (f[n] > 1) return true;
}
return false;
}
}
LeetCode473 火柴拼正方形
输入: matchsticks = [1,1,2,2,2]
输出: true
解释: 能拼成一个边长为2的正方形,每边两根火柴。
class Solution {
public boolean makesquare(int[] nums) {
int total = 0;
//统计所有火柴的长度
for (int num : nums) {
total += num;
}
//如果所有火柴的长度不是4的倍数,直接返回false
if (total == 0 || (total & 3) != 0)
return false;
//先排序
Arrays.sort(nums);
//回溯,从最长的火柴开始
return backtrack(nums, nums.length - 1, total >> 2, new int[4]);
}
//index表示访问到当前火柴的位置,target表示正方形的边长,size是长度为4的数组,
//分别保存正方形4个边的长度
private boolean backtrack(int[] nums, int index, int target, int[] size) {
if (index == -1) {
//如果火柴都访问完了,并且size的4个边的长度都相等,说明是正方形,直接返回true,
//否则返回false
if (size[0] == size[1] && size[1] == size[2] && size[2] == size[3])
return true;
return false;
}
//到这一步说明火柴还没访问完
for (int i = 0; i < size.length; i++) {
//如果把当前火柴放到size[i]这个边上,他的长度大于target,我们直接跳过。或者
// size[i] == size[i - 1]即上一个分支的值和当前分支的一样,上一个分支没有成功,
//说明这个分支也不会成功,直接跳过即可。
if (size[i] + nums[index] > target || (i > 0 && size[i] == size[i - 1]))
continue;
//如果当前火柴放到size[i]这个边上,长度不大于target,我们就放上面
size[i] += nums[index];
//然后在放下一个火柴,如果最终能变成正方形,直接返回true
if (backtrack(nums, index - 1, target, size))
return true;
//如果当前火柴放到size[i]这个边上,最终不能构成正方形,我们就把他从
//size[i]这个边上给移除,然后在试其他的边
size[i] -= nums[index];
}
//如果不能构成正方形,直接返回false
return false;
}
}
LeetCode474一和零
输入:strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
输出:4
解释:最多有 5 个 0 和 3 个 1 的最大子集是 {"10","0001","1","0"} ,因此答案是 4 。
其他满足题意但较小的子集包括 {"0001","1"} 和 {"10","1","0"} 。{"111001"} 不满足题意,因为它含 4 个 1 ,大于 n 的值 3 。
class Solution {
public int findMaxForm(String[] strs, int m, int n) {
//dp[i][j]表示i个0和j个1时的最大子集
int[][] dp = new int[m + 1][n + 1];
int oneNum, zeroNum;
for (String str : strs) {
oneNum = 0;
zeroNum = 0;
for (char ch : str.toCharArray()) {
if (ch == '0') {
zeroNum++;
} else {
oneNum++;
}
}
//倒序遍历
for (int i = m; i >= zeroNum; i--) {
for (int j = n; j >= oneNum; j--) {
dp[i][j] = Math.max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1);
}
}
}
return dp[m][n];
}
}
LeetCode475 供暖器
输入: houses = [1,2,3], heaters = [2]
输出: 1
解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。
class Solution {
// 双指针
public int findRadius(int[] houses, int[] heaters) {
// 1. 排序
Arrays.sort(houses);
Arrays.sort(heaters);
int res = 0;
int i = 0;
// 2. 双指针计算最大半径
// 下面的代码最难懂的地方是 Math.abs(heaters[i] - house) >= Math.abs(heaters[i + 1] - house)
// 我们举一个例子来说明这行代码的意思:
// houses:1,2,3,4
// heaters:1,4
// 对于 house 1,heater 1 比 heater 4 更接近 house1,所以不将 i 移动到 i + 1
// 对于 house 2,heater 1 比 heater 4 更接近 house2,所以不将 i 移动到 i + 1
// 对于 house 3,heater 4 比 heater 1 更接近 house3,所以将 i 移动到 i + 1
// 对于 house 4,依次类推
for (int house : houses) {
while (i < heaters.length - 1 &&
Math.abs(heaters[i] - house) >= Math.abs(heaters[i + 1] - house)) {
i++;
}
// 更新最大半径
res = Math.max(res, Math.abs(heaters[i] - house));
}
return res;
}
}
LeetCode477 汉明距离总和
输入:nums = [4,14,2]
输出:6
解释:在二进制表示中,4 表示为 0100 ,14 表示为 1110 ,2表示为 0010 。(这样表示是为了体现后四位之间关系)
所以答案为:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6
具体地,若长度为 n 的数组nums 的所有元素二进制的第 i位共有 c 个 1,n−c 个 0,则些元素在二进制的第 i位上的汉明距离之和为c⋅(n−c)
我们可以从二进制的最低位到最高位,逐位统计汉明距离。将每一位上得到的汉明距离累加即为答案。
class Solution {
public int totalHammingDistance(int[] nums) {
int ans = 0, n = nums.length;
for (int i = 0; i < 30; ++i) {
int c = 0;
for (int val : nums) {
//取第i位的值
c += (val >> i) & 1;
}
ans += c * (n - c);
}
return ans;
}
}
LeetCode480滑动窗口中位数
给出 nums = [1,3,-1,-3,5,3,6,7],以及 k = 3。
窗口位置 中位数
--------------- -----
[1 3 -1] -3 5 3 6 7 1
1 [3 -1 -3] 5 3 6 7 -1
1 3 [-1 -3 5] 3 6 7 -1
1 3 -1 [-3 5 3] 6 7 3
1 3 -1 -3 [5 3 6] 7 5
1 3 -1 -3 5 [3 6 7] 6
因此,返回该滑动窗口的中位数数组 [1,-1,-1,3,5,6]。
维护一个排过序的滑动窗口数组
使用二分查找检索删除的索引
将需要删除的值替换为需要插入的值
使用局部冒泡排序保证数组顺序
class Solution {
public double[] medianSlidingWindow(int[] nums, int k) {
double[] res = new double[nums.length - k + 1];
int[] window = new int[k];
//添加初始值
for (int i = 0; i < k; i++) {
window[i] = nums[i];
}
//初始的快排,懒得写直接调用
Arrays.sort(window);
res[0] = getMid(window);
//窗口滑动
for (int i = 0; i < nums.length - k; i++) {
//需要删除的数
int index = search(window, nums[i]);
//替换为需要插入的数
window[index] = nums[i + k];
//向后冒泡
while (index < window.length - 1 && window[index] > window[index + 1]) {
swap(window, index, index + 1);
index++;
}
//向前冒泡
while (index > 0 && window[index] < window[index - 1]) {
swap(window, index, index - 1);
index--;
}
res[i + 1] = getMid(window);
}
return res;
}
//交换
private void swap(int[] window, int i, int j) {
int temp = window[i];
window[i] = window[j];
window[j] = temp;
}
//求数组的中位数
private double getMid(int[] window) {
int len = window.length;
if (window.length % 2 == 0) {
//避免溢出
return window[len / 2] / 2.0 + window[len / 2 - 1] / 2.0;
} else {
return window[len / 2];
}
}
//最简单的二分查找
private int search(int[] window, int target) {
int start = 0;
int end = window.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (window[mid] > target) {
end = mid - 1;
} else if (window[mid] < target) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}
}
LeetCode485最大连续1的个数
输入:nums = [1,1,0,1,1,1]
输出:3
解释:开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int maxCount = 0, count = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
if (nums[i] == 1) {
count++;
} else {
maxCount = Math.max(maxCount, count);
count = 0;
}
}
maxCount = Math.max(maxCount, count);
return maxCount;
}
}
LeetCode486 预测赢家
输入:nums = [1,5,2]
输出:false
解释:一开始,玩家 1 可以从 1 和 2 中进行选择。
如果他选择 2(或者 1 ),那么玩家 2 可以从 1(或者 2 )和 5 中进行选择。如果玩家 2 选择了 5 ,那么玩家 1 则只剩下 1(或者 2 )可选。
所以,玩家 1 的最终分数为 1 + 2 = 3,而玩家 2 为 5 。
因此,玩家 1 永远不会成为赢家,返回 false 。
class Solution {
public boolean PredictTheWinner(int[] nums) {
int length = nums.length;
int[][] dp = new int[length][length];
for (int i = 0; i < length; i++) {
dp[i][i] = nums[i];
}
for (int i = length - 2; i >= 0; i--) {
for (int j = i + 1; j < length; j++) {
dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
}
}
return dp[0][length - 1] >= 0;
}
}
LeetCode491 递增子序列
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
class Solution {
// 定义全局变量保存结果
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
// idx 初始化为 -1,开始 dfs 搜索。
dfs(nums, -1, new ArrayList<>());
return res;
}
private void dfs(int[] nums, int idx, List<Integer> curList) {
// 只要当前的递增序列长度大于 1,就加入到结果 res 中,然后继续搜索递增序列的下一个值。
if (curList.size() > 1) {
res.add(new ArrayList<>(curList));
}
// 在 [idx + 1, nums.length - 1] 范围内遍历搜索递增序列的下一个值。
// 借助 set 对 [idx + 1, nums.length - 1] 范围内的数去重。
Set<Integer> set = new HashSet<>();
for (int i = idx + 1; i < nums.length; i++) {
// 1. 如果 set 中已经有与 nums[i] 相同的值了,说明加上 nums[i] 后的所有可能的递增序列之前已经被搜过一遍了,因此停止继续搜索。
if (set.contains(nums[i])) {
continue;
}
set.add(nums[i]);
// 2. 如果 nums[i] >= nums[idx] 的话,说明出现了新的递增序列,因此继续 dfs 搜索(因为 curList 在这里是复用的,因此别忘了 remove 哦)
if (idx == -1 || nums[i] >= nums[idx]) {
curList.add(nums[i]);
dfs(nums, i, curList);
curList.remove(curList.size() - 1);
}
}
}
}
LeetCode493 翻转对
输入: [2,4,3,5,1]
输出: 3
给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对。
class Solution {
private int findReversedPairs(int[] nums,int left,int right){
int res = 0,mid = left + (right-left)/2;
int i = left,j = mid+1;
for(;i <= mid;i++){
while(j <= right && (long)nums[i] > 2*(long)nums[j]) {
res += mid - i + 1;
j++;
}
}
return res;
}
private int mergeSort(int[] nums,int[] numsSorted,int left,int right){
if(left == right) return 0;
int mid = left + (right - left) / 2;
int res = mergeSort(nums,numsSorted,left,mid) +
mergeSort(nums,numsSorted,mid+1,right) +
findReversedPairs(nums,left,right);
int i = left,j = mid+1,k = left;
while(i <= mid && j <= right){
if(nums[i] <= nums[j]) numsSorted[k++] = nums[i++];
else numsSorted[k++] = nums[j++];
}
while(i <= mid) numsSorted[k++] = nums[i++];
while(j <= right) numsSorted[k++] = nums[j++];
for(int ind = left;ind <= right;ind++) nums[ind] = numsSorted[ind];
return res;
}
public int reversePairs(int[] nums) {
if(nums.length == 0) return 0;
int[] numsSorted = new int[nums.length];
return mergeSort(nums,numsSorted,0,nums.length-1);
}
}
LeetCode494 目标和
输入:nums = [1,1,1,1,1], target = 3
输出:5
解释:一共有 5 种方法让最终目标和为 3 。
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
class Solution {
public int findTargetSumWays(int[] nums, int target) {
int len = nums.length;
int sum = 0;
for(int num: nums){
sum += num;
}
if(sum < target|| (sum - target) % 2 == 1){
return 0;
}
int n = (sum - target)/2;
int[][] dp = new int[len + 1][n + 1];
dp[0][0] = 1;
for(int i = 1; i <= len; i++){
for(int j = 0; j <= n; j++){
if(j >= nums[i - 1]){
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]];
}else{
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[len][n];
}
}
LeetCode495 提莫攻击
输入:timeSeries = [1,4], duration = 2
输出:4
解释:提莫攻击对艾希的影响如下:
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
- 第 4 秒,提莫再次攻击艾希,艾希中毒状态又持续 2 秒,即第 4 秒和第 5 秒。
艾希在第 1、2、4、5 秒处于中毒状态,所以总中毒秒数是 4 。
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
if (timeSeries.length == 0 || duration == 0)
return 0;
int res = duration;
for (int i = 1; i < timeSeries.length; i++)
res += Math.min(timeSeries[i] - timeSeries[i - 1], duration);
return res;
}
}
LeetCode496 下一个更大元素
输入:nums1 = [4,1,2], nums2 = [1,3,4,2].
输出:[-1,3,-1]
解释:nums1 中每个值的下一个更大元素如下所述:
- 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。
- 1 ,用加粗斜体标识,nums2 = [1,3,4,2]。下一个更大元素是 3 。
- 2 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for(int num: nums2){
while (!stack.empty() && stack.peek() < num)
map.put(stack.pop(), num);
stack.push(num);
}
int[] res = new int[nums1.length];
for (int i = 0; i < nums1.length; i++)
res[i] = map.getOrDefault(nums1[i], -1);
return res;
}
}
LeetCode498对角线遍历
输入:mat = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,4,7,5,3,6,8,9]
class Solution {
public static int[] findDiagonalOrder(int[][] matrix) {
if (matrix.length == 0) {
return new int[0];
}
int rowLength = matrix.length;
int columnLength = matrix[0].length;
int[] answer = new int[rowLength * columnLength];
int count = rowLength + columnLength - 1;
int m = 0;
int n = 0;
int answerIndex = 0;
for (int i = 0; i < count; i++) {
if (i % 2 == 0) {
while (m >= 0 && n < columnLength) {
answer[answerIndex] = matrix[m][n];
answerIndex++;
m--;
n++;
}
if (n < columnLength) {
m++;
} else {
m = m + 2;
n--;
}
} else {
while (m < rowLength && n >= 0) {
answer[answerIndex] = matrix[m][n];
answerIndex++;
m++;
n--;
}
if (m < rowLength) {
n++;
}else{
m--;
n=n+2;
}
}
}
return answer;
}
}
LeetCode500 键盘行
class Solution {
private static final Map<Character, Integer> map = new HashMap<>();
static{
String[] strs = new String[]{"qwertyuiop", "asdfghjkl", "zxcvbnm"};
for(int i=0;i<strs.length;i++){
String cur = strs[i];
for(int j=0;j<cur.length();j++){
map.put(cur.charAt(j), i);
map.put(Character.toUpperCase(cur.charAt(j)), i);
}
}
}
public String[] findWords(String[] words) {
List<String> ans = new ArrayList<>();
for(String word: words){
boolean check = true;
Set<Integer> lines = new HashSet<>();
for(int i=0;i<word.length();i++){
lines.add(map.get(word.charAt(i)));
//判断是否出现其他行的字母
if(lines.size() > 1){
check = false;
break;
}
}
if(check)
ans.add(word);
}
String[] res = new String[ans.size()];
for(int i=0;i<ans.size();i++)
res[i] = ans.get(i);
return res;
}
}
