Default Changelist

FindAbsMinItem.java
LeetCode33.java
LeetCode48.java
LeetCode69.java
LeetCode72.java
LeetCode75.java
LeetCode309.java
LeetCode739.java
LeetCode912.java
P241001.java
This commit is contained in:
whaifree 2024-10-03 15:02:06 +08:00
parent a1524dde27
commit 00672efdc8
10 changed files with 495 additions and 0 deletions

View File

@ -0,0 +1,61 @@
package cn.whaifree.interview.HKWS;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/10/1 11:25
* @注释
*/
public class P241001 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int left;
left = Integer.parseInt(in.nextLine().trim());
int right;
right = Integer.parseInt(in.nextLine().trim());
int[] res = new P241001().getPrimes(left, right);
if(res==null||res.length==0){
System.out.print("-1");
}
for(int res_i=0; res_i < res.length; res_i++) {
System.out.print(String.valueOf(res[res_i])+" ");
}
}
/* Write Code Here */
public int[] getPrimes(int left, int right) {
List<Integer> res = new ArrayList<>();
while (left <= right) {
if (isPrime(left)) {
res.add(left);
}
left++;
}
int[] re = new int[res.size()];
for (int i = 0; i < re.length; i++) {
re[i] = res.get(i);
}
return re;
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if ( n % i == 0){
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,49 @@
package cn.whaifree.redo.redo_all_240924;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/30 16:37
* @注释
*/
public class FindAbsMinItem {
public static void main(String[] args) {
int[] nums = {-4, -2, 1, 10};
FindAbsMinItem findAbsMinItem = new FindAbsMinItem();
System.out.println(findAbsMinItem.findAbsMin(nums, 0));
}
public int findAbsMin(int[] nums,int target) {
int i = find(nums, target);
if (i < 0) {
// 没找到
i = -i;
if (Math.abs(nums[i - 1]) < Math.abs(nums[i])) {
return Math.abs(nums[i - 1]);
}else {
return Math.abs(nums[i]);
}
}
if (i == 0) {
// 0在最前面那么绝对值是递增的
return nums[i];
}
return nums[i];
}
public int find(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) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
}else {
left = mid + 1;
}
}
return -left;
}
}

View File

@ -0,0 +1,56 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/10/3 14:48
* @注释
*/
public class LeetCode309 {
@Test
public void test() {
Solution solution = new Solution();
int[] prices = {2, 1, 3, 0, 2};
System.out.println(solution.maxProfit(prices));
}
class Solution {
/**
* dp[i][0] 为第i天手头没有股票的最大利润
* - 前一天就没有 dp[i-1][0]
* - 刚刚卖出 dp[i-1][1]+value[i]
* dp[i][1] 表示第i天手头有股票的最大利润
* - 前一天就有 dp[i-1][1]
* - 刚刚买入 dp[i-2][0]-value[i]
*
* dp[0][0] = 0;
* dp[0][1] = -prices[0]
* dp[1][0] = 0;
* dp[1][1] = -math.min(prices[1],prices[0])
*
* @param prices
* @return
*/
public int maxProfit(int[] prices) {
if (prices.length <= 1) {
return 0;
}
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
dp[1][0] = Math.max(0, dp[0][1] + prices[1]);
dp[1][1] = -Math.min(prices[1], prices[0]);
for (int i = 2; i < prices.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 - 2][0] - prices[i]);
}
return dp[prices.length - 1][0];
}
}
}

View File

@ -0,0 +1,57 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/30 10:37
* @注释
*/
public class LeetCode33 {
@Test
public void test() {
int[] nums = new int[]{4,5,6,7,0,1,2};
int target = 0;
System.out.println(new Solution().search(nums, target));
}
class Solution {
/**
*
* | | | |
* 3 | 4 1 | 2
* A B C D
*
* @param nums
* @param target
* @return
*/
public int search(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) {
return mid;
}
if (nums[left] <= nums[mid]) { // A或者B
if (target < nums[mid] && target >= nums[left]) {
right = mid - 1; // A
} else {
left = mid + 1; // B
}
} else { // C或者D
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return nums[left] == target ? left : -1;
}
}
}

View File

@ -0,0 +1,51 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/30 11:10
* @注释
*/
public class LeetCode48 {
@Test
public void test() {
Solution solution = new Solution();
int[][] matrix = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
solution.rotate(matrix);
// solution.rotate(matrix, 0, 0, 2);
for (int[] ints : matrix) {
System.out.println(Arrays.toString(ints));
}
}
class Solution {
public void rotate(int[][] matrix) {
for (int i = 0; i < matrix.length / 2; i++) {
for (int j = i; j < matrix.length - 1 - i; j++) {
int x = i;
int y = j;
int n = matrix.length - 1;
int tmp = matrix[x][y];
matrix[x][y] = matrix[n - y][x];
matrix[n - y][x] = matrix[n - x][n - y];
matrix[n - x][n - y] = matrix[y][n - x];
matrix[y][n - x] = tmp;
}
}
}
public void rotate(int[][] matrix, int x, int y, int n) {
int tmp = matrix[x][y];
matrix[x][y] = matrix[n - y][x];
matrix[n - y][x] = matrix[n - x][n - y];
matrix[n - x][n - y] = matrix[y][n - x];
matrix[y][n - x] = tmp;
}
}
}

View File

@ -0,0 +1,39 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/10/1 13:01
* @注释
*/
public class LeetCode69 {
@Test
public void test() {
Solution solution = new Solution();
int x = 8;
int res = solution.mySqrt(x);
System.out.println(res);
}
class Solution {
public int mySqrt(int x) {
int left = 0;
int right = x;
while (left <= right) {
int mid = left + (right - left) / 2;
long pow = (long) mid * mid;
if (pow == x) {
return mid;
} else if (pow > x) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right; // 最后left必然在right右边left = right+1
}
}
}

View File

@ -0,0 +1,62 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/10/1 13:10
* @注释
*/
public class LeetCode72 {
@Test
public void test() {
Solution solution = new Solution();
System.out.println(solution.minDistance("horse", "ros"));
}
class Solution {
/**
* '' h o r s e
* '' 0 1 2 3 4 5
* r 1 1 2 2 3 4
* o 2 2 1 2 3 4
* s 3 3 2 2 2 3
*
* 相同i-1 j-1
*
* 不同 min
* 替换 i-1 j-1 +1
* 增加 i j-1 +1
* 删除 i-1 j +1
*
* @param word1
* @param word2
* @return
*/
public int minDistance(String word1, String word2) {
if (word2.length() > word1.length()) {
return minDistance(word2, word1);
}
int[][] dp = new int[word2.length() + 1][word1.length() + 1];
for (int i = 0; i <= word1.length(); i++) {
dp[0][i] = i;
}
for (int i = 0; i <= word2.length(); i++) {
dp[i][0] = i;
}
for (int i = 1; i <= word2.length(); i++) {
for (int j = 1; j <= word1.length(); j++) {
if (word1.charAt(j - 1) == word2.charAt(i - 1)) {
dp[i][j] = dp[i - 1][j - 1];
}else {
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
}
}
}
return dp[word2.length()][word1.length()];
}
}
}

View File

@ -0,0 +1,39 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/30 10:33
* @注释
*/
public class LeetCode739 {
@Test
public void test() {
int[] dailyTemperatures = new Solution().dailyTemperatures(new int[]{73, 74, 75, 71, 69, 72, 76, 73});
for (int i : dailyTemperatures) {
System.out.println(i);
}
}
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
Deque<Integer> deque = new LinkedList<>();
int[] res = new int[temperatures.length];
for (int i = 0; i < temperatures.length; i++) {
while (!deque.isEmpty() && temperatures[deque.peek()] < temperatures[i]) {
Integer pop = deque.pop();
res[pop] = i - pop;
}
deque.push(i);
}
return res;
}
}
}

View File

@ -0,0 +1,71 @@
package cn.whaifree.redo.redo_all_240924;
import org.junit.Test;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/10/1 13:45
* @注释
*/
public class LeetCode75 {
@Test
public void test()
{
Solution solution = new Solution();
int[] nums = {2,0,2,1,1,1};
solution.sortColors(nums);
for (int num : nums) {
System.out.println(num);
}
}
class Solution {
/**
* 2 0 2 1 1 0
* 0 0 2 1 1 2
* 0 0 1 1 2 2
*
* @param nums
*/
public void sortColors(int[] nums) {
int index0 = 0;
int index2 = nums.length - 1;
for (int i = 0; i < nums.length; i++) {
while (index2 >= i && nums[i] == 2) {
swap(nums, index2--, i);
}
if (nums[i] == 0) {
swap(nums, index0++, i);
}
}
}
public void swap(int[] nums,int i,int j)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
list.add(i);
}
for (int i = 100; i < 1000; i++) {
set.add(i);
}
long l1 = System.currentTimeMillis();
Iterator<Integer> iterator = list.iterator();
// if (it)
}
}

View File

@ -0,0 +1,10 @@
package cn.whaifree.redo.redo_all_240924;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/10/3 15:01
* @注释
*/
public class LeetCode912 {
}