add
This commit is contained in:
parent
bff038b9d5
commit
13a1a09aaf
64
5.leetcode/src/com/fanxb/common/Test.java
Normal file
64
5.leetcode/src/com/fanxb/common/Test.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Test {
|
||||
|
||||
private void quickSort(int[] array, int start, int end) {
|
||||
if (start >= end) return;
|
||||
int base = array[start];
|
||||
int l = start, r = end;
|
||||
while (l < r) {
|
||||
while (array[r] >= base && r > l) r--;
|
||||
while (array[l] <= base && l < r) l++;
|
||||
if (l < r) swap(array, l, r);
|
||||
}
|
||||
swap(array, start, l);
|
||||
quickSort(array, start, l - 1);
|
||||
quickSort(array, l + 1, end);
|
||||
}
|
||||
|
||||
private void bubble(int[] array) {
|
||||
int n = array.length;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (array[j + 1] < array[j]) swap(array, j + 1, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void insert(int[] array) {
|
||||
int n = array.length;
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j = i; j > 0 && array[j] < array[j - 1]; j--) swap(array, j, j - 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void swap(int[] array, int a, int b) {
|
||||
if (a == b) return;
|
||||
int temp = array[b];
|
||||
array[b] = array[a];
|
||||
array[a] = temp;
|
||||
}
|
||||
|
||||
private void shell(int[] array, int a) {
|
||||
if (a <= 0) return;
|
||||
int n = array.length;
|
||||
for (int i = 0; i < a; i++) {
|
||||
for (int j = i + a; j < n; j += a) {
|
||||
for (int k = j; k > i && array[k] < array[k - a]; k -= a) swap(array, k, k - a);
|
||||
}
|
||||
}
|
||||
shell(array, a / 2);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Test test = new Test();
|
||||
int[] arr = new int[]{1, 53, 12, 4344, 112, -22, 333211, 3333, 444, 55511, 121};
|
||||
// test.quickSort(arr, 0, arr.length - 1);
|
||||
// test.bubble(arr);
|
||||
test.insert(arr);
|
||||
// test.shell(arr, arr.length / 2);
|
||||
System.out.println(Arrays.toString(arr));
|
||||
}
|
||||
}
|
@ -8,11 +8,11 @@ public class TreeNode {
|
||||
TreeNode() {
|
||||
}
|
||||
|
||||
TreeNode(int val) {
|
||||
public TreeNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
public TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
26
5.leetcode/src/com/fanxb/common/p100/Q50.java
Normal file
26
5.leetcode/src/com/fanxb/common/p100/Q50.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
public class Q50 {
|
||||
public double myPow(double x, int n) {
|
||||
if (n < 0) return pow(1 / x, -n);
|
||||
return pow(x, n);
|
||||
}
|
||||
|
||||
public double pow(double x, long n) {
|
||||
if (n == 0) return 1;
|
||||
if (n % 2 == 1) return pow(x, n - 1) * x;
|
||||
double temp = pow(x, n / 2);
|
||||
return temp * temp;
|
||||
}
|
||||
|
||||
public double pow1(double x, long n) {
|
||||
double res = x;
|
||||
while (n > 0) {
|
||||
if ((n & 1) == 1)
|
||||
res = res * x;
|
||||
x = x * x;
|
||||
n = n >>> 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
@ -20,6 +20,18 @@ public class Q70 {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
public int climbStairs1(int n) {
|
||||
if (n == 1) return 1;
|
||||
int a = 1, b = 2;
|
||||
for (int i = 3; i <= n; i++) {
|
||||
int c = a+b;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q70().climbStairs(5));
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p1000;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p1000;
|
||||
|
||||
public class Q918 {
|
||||
public int maxSubarraySumCircular(int[] nums) {
|
@ -1,8 +1,6 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p1000;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
@ -1,20 +1,20 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* @author fanxb
|
||||
* @date 2021年12月26日 21:54
|
||||
*/
|
||||
public class Q1078 {
|
||||
public String[] findOcurrences(String text, String first, String second) {
|
||||
LinkedList<String> res = new LinkedList<>();
|
||||
String[] strs = text.split(" ");
|
||||
for (int i = 0; i < strs.length - 2; i++) {
|
||||
if (strs[i].equals(first) && strs[i + 1].equals(second)) {
|
||||
res.add(strs[i + 2]);
|
||||
}
|
||||
}
|
||||
return res.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
package com.fanxb.common.p1100;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* @author fanxb
|
||||
* @date 2021年12月26日 21:54
|
||||
*/
|
||||
public class Q1078 {
|
||||
public String[] findOcurrences(String text, String first, String second) {
|
||||
LinkedList<String> res = new LinkedList<>();
|
||||
String[] strs = text.split(" ");
|
||||
for (int i = 0; i < strs.length - 2; i++) {
|
||||
if (strs[i].equals(first) && strs[i + 1].equals(second)) {
|
||||
res.add(strs[i + 2]);
|
||||
}
|
||||
}
|
||||
return res.toArray(new String[0]);
|
||||
}
|
||||
}
|
46
5.leetcode/src/com/fanxb/common/p200/Q139.java
Normal file
46
5.leetcode/src/com/fanxb/common/p200/Q139.java
Normal file
@ -0,0 +1,46 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q139 {
|
||||
public boolean wordBreak(String s, List<String> wordDict) {
|
||||
boolean[] cache = new boolean[s.length()];
|
||||
return bfs(s, 0, new HashSet<>(wordDict), cache);
|
||||
}
|
||||
|
||||
private boolean bfs(String s, int i, Set<String> wordSet, boolean[] cache) {
|
||||
if (i >= s.length()) return true;
|
||||
if (cache[i]) return false;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (int j = i; j < s.length(); j++) {
|
||||
stringBuilder.append(s.charAt(j));
|
||||
if (!wordSet.contains(stringBuilder.toString())) continue;
|
||||
boolean exist = bfs(s, j + 1, wordSet, cache);
|
||||
if (exist) return true;
|
||||
cache[j + 1] = true;
|
||||
}
|
||||
cache[i] = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean wordBreak1(String s, List<String> wordDict) {
|
||||
int n = s.length();
|
||||
Set<String> wordSet = new HashSet<>(wordDict);
|
||||
boolean[] dp = new boolean[n + 1];
|
||||
dp[0] = true;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (dp[j] && wordSet.contains(s.substring(j, i))) {
|
||||
dp[i] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q139().wordBreak("leetcode", Arrays.asList("leet", "code")));
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
* 寻找旋转排序数组中的最小值
|
||||
@ -31,6 +34,47 @@ public class Q149 {
|
||||
return max;
|
||||
}
|
||||
|
||||
public int maxPoints1(int[][] points) {
|
||||
int n = points.length;
|
||||
if (n < 3) return points.length;
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
int temp = 2;
|
||||
for (int k = j + 1; k < n; k++) {
|
||||
if ((points[k][0] - points[i][0]) * (points[j][1] - points[i][1]) == (points[k][1] - points[i][1]) * (points[j][0] - points[i][0]))
|
||||
temp++;
|
||||
}
|
||||
res = Math.max(res, temp);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public int maxPoints2(int[][] points) {
|
||||
int n = points.length;
|
||||
if (n < 3) return n;
|
||||
int ans = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int max = 0;
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
int x = points[j][0] - points[i][0], y = points[j][1] - points[i][1];
|
||||
int gcd = gcd(x, y);
|
||||
String key = (x / gcd) + "/" + (y / gcd);
|
||||
int count = map.getOrDefault(key, 1) + 1;
|
||||
map.put(key, count);
|
||||
max = Math.max(max, count);
|
||||
}
|
||||
ans = Math.max(ans, max);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
private int gcd(int a, int b) {
|
||||
return b == 0 ? a : gcd(b, a % b);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q149().maxPoints(new int[][]{{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}}));
|
||||
}
|
||||
|
@ -17,4 +17,16 @@ public class Q198 {
|
||||
return res;
|
||||
}
|
||||
|
||||
public int rob1(int[] nums) {
|
||||
int n = nums.length;
|
||||
if (n == 1) return nums[0];
|
||||
int a = nums[0], b = Math.max(nums[0], nums[1]);
|
||||
for (int i = 2; i < n; i++) {
|
||||
int c = Math.max(b, a + nums[i]);
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
/**
|
||||
* 两数相加
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
public class Q1576 {
|
||||
public String modifyString(String s) {
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
/**
|
||||
* 具有给定数值的最小字符串
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
public class Q1688 {
|
||||
private static int[] res = new int[201];
|
@ -1,6 +1,5 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.PriorityQueue;
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 两数相加
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p2000;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
25
5.leetcode/src/com/fanxb/common/p300/Q332.java
Normal file
25
5.leetcode/src/com/fanxb/common/p300/Q332.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common.p300;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q332 {
|
||||
public int coinChange(int[] coins, int amount) {
|
||||
int[] dp = new int[amount + 1];
|
||||
Arrays.fill(dp, -1);
|
||||
Set<Integer> set = new HashSet<>();
|
||||
for (int coin : coins) {
|
||||
set.add(coin);
|
||||
}
|
||||
dp[0] = 0;
|
||||
for (int i = 1; i <= amount; i++) {
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int j = 0; j < coins.length; j++) {
|
||||
if (amount - coins[j] > 0 && dp[amount - coins[j]] >= 0) {
|
||||
min = Math.min(dp[amount - coins[j]] + 1, min);
|
||||
}
|
||||
}
|
||||
if (min != Integer.MAX_VALUE) dp[i] = min;
|
||||
}
|
||||
return dp[amount];
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package com.fanxb.common.p400;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 定义dp[i]为以nums[i]结尾的子序列最大长度
|
||||
*
|
||||
@ -39,6 +41,35 @@ public class Q300 {
|
||||
return res;
|
||||
}
|
||||
|
||||
public int better(int[] nums) {
|
||||
int[] tails = new int[nums.length];
|
||||
int res = 0;
|
||||
for (int num : nums) {
|
||||
int l = 0, r = res;
|
||||
while (l < r) {
|
||||
int m = (l + r) / 2;
|
||||
if (tails[m] < num) l = m + 1;
|
||||
else r = m;
|
||||
}
|
||||
if (l == res) res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public int bad(int[] nums) {
|
||||
int[] dp = new int[nums.length];
|
||||
Arrays.fill(dp,1);
|
||||
int res = 0;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] < nums[i]) dp[i] = Math.max(dp[j] + 1, dp[i]);
|
||||
}
|
||||
res = Math.max(res, dp[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q300().lengthOfLIS(new int[]{0, 1, 0, 3, 2, 3}));
|
||||
System.out.println(new Q300().another(new int[]{0, 1, 0, 3, 2, 3}));
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p800;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p800;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -1,6 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
package com.fanxb.common.p800;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
@ -1,8 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
package com.fanxb.common.p900;
|
||||
|
||||
public class Q825 {
|
||||
public int numFriendRequests(int[] ages) {
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p900;
|
||||
|
||||
/**
|
||||
* 两数相加
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p900;
|
||||
|
||||
/**
|
||||
* 两数相加
|
Loading…
x
Reference in New Issue
Block a user