add
This commit is contained in:
parent
fd8fe53377
commit
f5940d3f1b
31
5.leetcode/src/com/fanxb/common/Q240.java
Normal file
31
5.leetcode/src/com/fanxb/common/Q240.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-10-25-下午4:25
|
||||||
|
*/
|
||||||
|
public class Q240 {
|
||||||
|
|
||||||
|
public boolean searchMatrix(int[][] matrix, int target) {
|
||||||
|
int m = matrix.length, n = matrix[0].length;
|
||||||
|
if (matrix[m - 1][n - 1] < target || matrix[0][0] > target) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int i = 0, j = n - 1;
|
||||||
|
while (i < m && j >= 0) {
|
||||||
|
if (matrix[i][j] == target) {
|
||||||
|
return true;
|
||||||
|
} else if (matrix[i][j] < target) {
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[][] params = {{-1, 3}};
|
||||||
|
System.out.println(new Q240().searchMatrix(params, 1));
|
||||||
|
}
|
||||||
|
}
|
29
5.leetcode/src/com/fanxb/common/Q367.java
Normal file
29
5.leetcode/src/com/fanxb/common/Q367.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-11-04-下午4:26
|
||||||
|
*/
|
||||||
|
public class Q367 {
|
||||||
|
private static Set<Integer> valSet = new HashSet<>(10000);
|
||||||
|
|
||||||
|
static {
|
||||||
|
Long val;
|
||||||
|
for (int i = 1; ; i++) {
|
||||||
|
val = i * (long) i;
|
||||||
|
if (val > Integer.MAX_VALUE) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
valSet.add(val.intValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPerfectSquare(int num) {
|
||||||
|
return valSet.contains(num);
|
||||||
|
}
|
||||||
|
}
|
38
5.leetcode/src/com/fanxb/common/Q412.java
Normal file
38
5.leetcode/src/com/fanxb/common/Q412.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-10-13-上午10:36
|
||||||
|
*/
|
||||||
|
public class Q412 {
|
||||||
|
private static final List<String> RES = new ArrayList<>(10000);
|
||||||
|
|
||||||
|
public List<String> fizzBuzz(int n) {
|
||||||
|
if (n <= RES.size()) {
|
||||||
|
return RES.subList(0, n);
|
||||||
|
}
|
||||||
|
for (int i = RES.size() + 1; i <= n; i++) {
|
||||||
|
boolean b1 = i % 3 == 0;
|
||||||
|
boolean b2 = i % 5 == 0;
|
||||||
|
if (b1 && b2) {
|
||||||
|
RES.add("FizzBuzz");
|
||||||
|
} else if (b1) {
|
||||||
|
RES.add("Fizz");
|
||||||
|
} else if (b2) {
|
||||||
|
RES.add("Buzz");
|
||||||
|
} else {
|
||||||
|
RES.add(String.valueOf(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return RES;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(new Q412().fizzBuzz(2));
|
||||||
|
System.out.println(new Q412().fizzBuzz(3));
|
||||||
|
System.out.println(new Q412().fizzBuzz(5));
|
||||||
|
System.out.println(new Q412().fizzBuzz(15));
|
||||||
|
}
|
||||||
|
}
|
75
5.leetcode/src/com/fanxb/common/Q42.java
Normal file
75
5.leetcode/src/com/fanxb/common/Q42.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-11-03-下午3:12
|
||||||
|
*/
|
||||||
|
public class Q42 {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态规划解法
|
||||||
|
*
|
||||||
|
* @param height height
|
||||||
|
* @return int
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021/11/3 下午3:51
|
||||||
|
*/
|
||||||
|
public int trap(int[] height) {
|
||||||
|
int n = height.length;
|
||||||
|
//leftMax[i]表示第i个位左边的最高高度,rightMax同理
|
||||||
|
int[] leftMax = new int[n];
|
||||||
|
leftMax[0] = height[0];
|
||||||
|
int[] rightMax = new int[n];
|
||||||
|
rightMax[n - 1] = height[n - 1];
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
|
||||||
|
rightMax[n - 1 - i] = Math.max(rightMax[n - i], height[n - i - 1]);
|
||||||
|
}
|
||||||
|
int res = 0;
|
||||||
|
for (int i = 1; i < n - 1; i++) {
|
||||||
|
res += Math.min(leftMax[i], rightMax[i]) - height[i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单调栈解法
|
||||||
|
*
|
||||||
|
* @param height height
|
||||||
|
* @return int int
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021/11/3 下午4:37
|
||||||
|
*/
|
||||||
|
public int trap1(int[] height) {
|
||||||
|
int res = 0;
|
||||||
|
if (height.length <= 2) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
Stack<Integer> stack = new Stack<>();
|
||||||
|
stack.push(0);
|
||||||
|
for (int i = 1; i < height.length; i++) {
|
||||||
|
while (!stack.isEmpty() && height[i] > height[stack.peek()]) {
|
||||||
|
//当前元素大于栈顶元素
|
||||||
|
//栈里的元素大于等于2,说明可以开始接雨水了
|
||||||
|
int bottom = stack.pop();
|
||||||
|
if (stack.isEmpty()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int l = stack.peek();
|
||||||
|
res += (Math.min(height[i], height[l]) - height[bottom]) * (i - l - 1);
|
||||||
|
}
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(new Q42().trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
|
||||||
|
System.out.println(new Q42().trap1(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user