前言
本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!
本专栏目录结构请见LeetCode 刷题汇总
正文
幕布
41. 缺失的第一个正数
题解
3 次遍历,小于0都取大,所有取绝对值,小值取反,返回第一个大于0
class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
if (nums[i] <= 0) {
nums[i] = n + 1;
}
}
for (int i = 0; i < n; ++i) {
int num = Math.abs(nums[i]);
if (num <= n) {
nums[num - 1] = -Math.abs(nums[num - 1]);
}
}
for (int i = 0; i < n; ++i) {
if (nums[i] > 0) {
return i + 1;
}
}
return n + 1;
}
}
42. 接雨水
题解
动态规划 + 3 次遍历
class Solution {
public int trap(int[] height) {
if(height.length == 0) return 0;
int n = height.length;
int[] leftMax = new int[n], rightMax = new int[n];
leftMax[0] = height[0];
for(int i = 1; i < n; i++){
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
}
rightMax[n - 1] = height[n - 1];
for(int i = n - 2; i >= 0; i--){
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
}
int res = 0;
for(int i = 0; i < n; i++){
res += Math.min(leftMax[i], rightMax[i]) - height[i];
}
return res;
}
}
单调栈
class Solution {
public int trap(int[] height) {
if(height.length == 0) return 0;
int n = height.length, res = 0;
ArrayDeque<Integer> stack = new ArrayDeque<>(n);
for(int i = 0; i < n; i++){
while(!stack.isEmpty() && height[i] > height[stack.peek()]){
int top = stack.pop();
if(stack.isEmpty()){
break;
}
int left = stack.peek();
int width = i - left - 1;
int h = Math.min(height[i], height[left]) - height[top];
res += width * h;
}
stack.push(i);
}
return res;
}
}
双指针 leftMax > rightMax
class Solution {
public int trap(int[] height) {
if(height.length == 0) return 0;
int n = height.length, left = 0, leftMax = 0, right = n - 1, rightMax = 0, res = 0;
while(left < right){
leftMax = Math.max(leftMax, height[left]);
rightMax = Math.max(rightMax, height[right]);
if(leftMax > rightMax){
res += rightMax - height[right];
right--;
}else{
res += leftMax - height[left];
left++;
}
}
return res;
}
}
双指针 height[left] > height[right]
class Solution {
public int trap(int[] height) {
if(height.length == 0) return 0;
int n = height.length, left = 0, leftMax = 0, right = n - 1, rightMax = 0, res = 0;
while(left < right){
leftMax = Math.max(leftMax, height[left]);
rightMax = Math.max(rightMax, height[right]);
if(height[left] > height[right]){
res += rightMax - height[right];
right--;
}else{
res += leftMax - height[left];
left++;
}
}
return res;
}
}
43. 字符串相乘
题解
思路
m+n数组,第一个数中每一位数和第二个数中每一位数相乘,放到相应位置,然后进位得到结果
代码实现
class Solution {
public String multiply(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
int m = num1.length(), n = num2.length();
int[] ansArr = new int[m + n];
for (int i = m - 1; i >= 0; i--) {
int x = num1.charAt(i) - '0';
for (int j = n - 1; j >= 0; j--) {
int y = num2.charAt(j) - '0';
ansArr[i + j + 1] += x * y;
}
}
for (int i = m + n - 1; i > 0; i--) {
ansArr[i - 1] += ansArr[i] / 10;
ansArr[i] %= 10;
}
int index = ansArr[0] == 0 ? 1 : 0;
StringBuffer ans = new StringBuffer();
while (index < m + n) {
ans.append(ansArr[index]);
index++;
}
return ans.toString();
}
}
44. 通配符匹配
题解
Linear runtime and constant space solution
i,j,startJ,lastMatch
class Solution {
public boolean isMatch(String s, String p) {
if(s == null || p == null){
return false;
}
if(p.isEmpty()){
return s.isEmpty();
}
int m = s.length(), n = p.length(), i = 0, j = 0, startJ = -1, lastMatch = -1;
while(i < m){
if(j < n && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?')){
i++;
j++;
}else if(j < n && p.charAt(j) == '*'){
startJ = j;
j++;
lastMatch = i;
}else if(startJ != -1){
j = startJ + 1;
lastMatch++;
i = lastMatch;
}else{
return false;
}
}
while(j < n && p.charAt(j) == '*'){
j++;
}
return j == n;
}
}
45. 跳跃游戏 II
题解
i == end 更新最远距离
class Solution {
public int jump(int[] nums) {
int n = nums.length, max = 0, end = 0, res = 0;
for(int i = 0; i < n - 1; i++){
max = Math.max(max, i + nums[i]);
if(i == end){
end = max;
res++;
}
}
return res;
}
}