Compare commits

..

1 Commits

Author SHA1 Message Date
dependabot[bot]
df2a48455d
Bump mysql-connector-java from 8.0.16 to 8.0.28 in /spring-boot/sjdemo
Bumps [mysql-connector-java](https://github.com/mysql/mysql-connector-j) from 8.0.16 to 8.0.28.
- [Release notes](https://github.com/mysql/mysql-connector-j/releases)
- [Changelog](https://github.com/mysql/mysql-connector-j/blob/release/8.0/CHANGES)
- [Commits](https://github.com/mysql/mysql-connector-j/compare/8.0.16...8.0.28)

---
updated-dependencies:
- dependency-name: mysql:mysql-connector-java
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-06-21 01:24:11 +00:00
237 changed files with 544 additions and 6561 deletions

View File

@ -1,4 +1,7 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
import java.util.HashMap;
import java.util.Map;
/**
* 两数相加

View File

@ -1,8 +1,8 @@
package com.fanxb.common;
public class ListNode {
public int val;
public ListNode next;
int val;
ListNode next;
public ListNode() {
}

View File

@ -1,55 +1,45 @@
package com.fanxb.common.p100;
import java.util.HashMap;
import java.util.Map;
/**
* @author fanxb
* @date 2021年12月26日 22:02
*/
public class Q1 {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numIndexMap = new HashMap<>(nums.length);
for (int i = 0; i < nums.length; i++) {
numIndexMap.put(nums[i], i);
}
int cur;
Integer targetIndex;
for (int i = 0; i < nums.length; i++) {
cur = nums[i];
targetIndex = numIndexMap.get(target - cur);
if (targetIndex != null && targetIndex > i) {
return new int[]{i, targetIndex};
}
}
return null;
}
public int[] twoSum1(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) map.put(nums[i], i);
for (int i = 0; i < nums.length; i++) {
Integer targetI = map.get(target - nums[i]);
if (targetI != null && targetI != i) return new int[]{i, map.get(target - nums[i])};
}
return null;
}
/**
* 更优一次循环即可
*
* @return int[]
* @author fanxb
* @date 2021/12/26 22:08
*/
public int[] betterTwoSum(int[] nums, int target) {
Map<Integer, Integer> numIndexMap = new HashMap<>(nums.length);
for (int i = 0; i < nums.length; i++) {
if (numIndexMap.containsKey(target - nums[i])) {
return new int[]{numIndexMap.get(target - nums[i]), i};
}
numIndexMap.put(nums[i], i);
}
return null;
}
}
package com.fanxb.common;
import java.util.HashMap;
import java.util.Map;
/**
* @author fanxb
* @date 2021年12月26日 22:02
*/
public class Q1 {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numIndexMap = new HashMap<>(nums.length);
for (int i = 0; i < nums.length; i++) {
numIndexMap.put(nums[i], i);
}
int cur;
Integer targetIndex;
for (int i = 0; i < nums.length; i++) {
cur = nums[i];
targetIndex = numIndexMap.get(target - cur);
if (targetIndex != null && targetIndex > i) {
return new int[]{i, targetIndex};
}
}
return null;
}
/**
* 更优一次循环即可
*
* @return int[]
* @author fanxb
* @date 2021/12/26 22:08
*/
public int[] betterTwoSum(int[] nums, int target) {
Map<Integer, Integer> numIndexMap = new HashMap<>(nums.length);
for (int i = 0; i < nums.length; i++) {
if (numIndexMap.containsKey(target - nums[i])) {
return new int[]{numIndexMap.get(target - nums[i]), i};
}
numIndexMap.put(nums[i], i);
}
return null;
}
}

View File

@ -1,20 +1,20 @@
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]);
}
}
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]);
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
import java.util.Arrays;
import java.util.List;

View File

@ -0,0 +1,47 @@
package com.fanxb.common;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* 题目地址 https://leetcode-cn.com/problems/partition-labels/
* 解题思路首先遍历一次字符串记录每个字母最后出现的位置
* 然后再遍历一遍记录当前字符串的开始位置start,结束位置end. 当第i个字母的结束位置end时end=第i个字母的结束位置知道i=end说明当前位置为字符串的结束位置
*
* @author fanxb
* Date: 2020/6/9 15:10
*/
public class Q122 {
public static int solution(int[] prices) {
int res = 0;
int start = -1, end = -1;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i + 1] > prices[i]) {
//说明是上坡
if (start == -1) {
start = prices[i];
}
end = prices[i + 1];
} else if (prices[i + 1] < prices[i]) {
//说明开始下坡了
if (end > start) {
res += end - start;
}
start = -1;
end = -1;
}
}
if (end > start) {
res += end - start;
}
return res;
}
public static void main(String[] args) {
int[] prices = {7, 1, 5, 3, 6, 4};
System.out.println(solution(prices));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
import java.math.BigDecimal;
import java.util.Arrays;
@ -38,32 +38,9 @@ public class Q135 {
return count;
}
public static int candy1(int[] ratings) {
//every one set one candy
int length = ratings.length;
int[] res = new int[length];
Arrays.fill(res, 1);
for (int i = 1; i < length; i++) {
//从左到右遍历如果下一个人分数更高那么多发一颗糖
if (ratings[i] > ratings[i - 1]) {
res[i] = res[i - 1] + 1;
}
}
for (int i = length - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1] && res[i] <= res[i + 1]) {
res[i] = res[i + 1] + 1;
}
}
int sum = 0;
for (int temp : res) {
sum += temp;
}
return sum;
}
public static void main(String[] args) {
int[] s = {1, 3, 4, 5, 2};
int[] s = {1, 2, 87, 87, 87, 2, 1};
System.out.println(Arrays.toString(s));
System.out.println(candy1(s));
System.out.println(candy(s));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
import java.util.Arrays;
import java.util.HashSet;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
import java.util.HashMap;
import java.util.Map;

View File

@ -0,0 +1,37 @@
package com.fanxb.common;
/**
* Created with IntelliJ IDEA
* 寻找旋转排序数组中的最小值
* 地址 https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/
* 思路 需要找到分界点
*
* @author fanxb
* Date: 2020/6/11 9:56
*/
public class Q149 {
public int maxPoints(int[][] points) {
int max = 1;
for (int i = 0; i < points.length; i++) {
int[] a1 = points[i];
for (int j = i + 1; j < points.length; j++) {
int[] a2 = points[j];
int n = 2;
for (int k = j + 1; k < points.length; k++) {
int[] a3 = points[k];
if ((a2[1] - a1[1]) * (a3[0] - a1[0]) == (a3[1] - a1[1]) * (a2[0] - a1[0])) {
n++;
}
}
if (n > max) {
max = n;
}
}
}
return max;
}
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}}));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA
@ -38,28 +38,6 @@ public class Q153 {
return min;
}
public int findMin1(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0];
int l = 0, r = n - 1;
while (l <= r) {
if (nums[l] < nums[r]) {
//说明l,r在同一段中
return nums[l];
}
int mid = (l + r) / 2;
if (nums[mid] > nums[r]) {
//说明mid在左边的段
l = mid + 1;
} else {
//说明mid在右边的段
r = mid;
}
}
return nums[l];
}
public static void main(String[] args) {
System.out.println(new Q153().findMin(new int[]{3, 4, 5, 1, 2}));
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
public class Q1576 {
public String modifyString(String s) {

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
/**
* 具有给定数值的最小字符串

View File

@ -1,7 +1,6 @@
package com.fanxb.common.p200;
package com.fanxb.common;
import java.util.Arrays;
import java.util.EnumSet;
/**
* 两数之和 II - 输入有序数组
@ -26,19 +25,9 @@ public class Q167 {
i++;
}
}
return new int[]{i + 1, j + 1};
return new int[]{i+1, j+1};
}
public int[] twoSum(int[] numbers, int target) {
int i = 0, j = numbers.length - 1;
while (i < j) {
int sum = numbers[i] + numbers[j];
if (sum == target) return new int[]{i+1, j+1};
else if (sum > target) j--;
else j++;
}
return null;
}
public static void main(String[] args) {
int[] numbers = {2, 7, 11, 15};

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
public class Q1688 {
private static int[] res = new int[201];

View File

@ -1,5 +1,6 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

View File

@ -1,7 +1,9 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 两数相加

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p2000;
package com.fanxb.common;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
public class Q19 {
public static class ListNode {

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
/**
* 具有给定数值的最小字符串

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p200;
package com.fanxb.common;
public class Q198 {
public int rob(int[] nums) {
@ -17,16 +17,4 @@ 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;
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
/**
* 两数相加
@ -62,39 +62,17 @@ public class Q2 {
return res;
}
public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
ListNode res = new ListNode(0);
ListNode temp = null;
//是否进位
boolean jw = false;
while (l1 != null || l2 != null) {
if (temp == null) temp = res;
else {
temp.next = new ListNode(0);
temp = temp.next;
}
if (l1 != null) {
temp.val += l1.val;
l1 = l1.next;
}
if (l2 != null) {
temp.val += l2.val;
l2 = l2.next;
}
if (jw) {
temp.val += 1;
jw = false;
}
if (temp.val >= 10) {
jw = true;
temp.val -= 10;
}
}
if (jw) temp.next = new ListNode(1);
return res;
}
public static void main(String[] args) {
ListNode l1 = new ListNode(2);
l1.next = new ListNode(4);
l1.next.next = new ListNode(9);
ListNode l2 = new ListNode(5);
l2.next = new ListNode(6);
l2.next.next = new ListNode(4);
l2.next.next.next = new ListNode(9);
new Q2().addTwoNumbers(l1, l2);
// new Q2().addTwoNumbers(new ListNode(0), new ListNode(0));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p300;
package com.fanxb.common;
import java.util.Arrays;

View File

@ -0,0 +1,44 @@
package com.fanxb.common;
import java.util.*;
public class Q215 {
public int findKthLargest(int[] nums, int k) {
//
List<Integer> heads = new ArrayList<>(k + 1);
heads.add(0);
for (int num : nums) {
//建堆
if (heads.size() < k + 1) {
heads.add(num);
for (int i = heads.size() - 1; i > 0; i--) {
cal(heads, i);
}
} else if (num >= heads.get(1)) {
heads.set(1, num);
cal(heads, 1);
}
}
return heads.get(1);
}
private static void cal(List<Integer> heads, int i) {
int left = 2 * i;
if (left < heads.size() && heads.get(i) > heads.get(left)) {
Collections.swap(heads, i, left);
cal(heads, left);
}
int right = 2 * i + 1;
if (right < heads.size() && heads.get(i) > heads.get(right)) {
Collections.swap(heads, i, right);
cal(heads, right);
}
}
public static void main(String[] args) {
int[] people = {3, 2, 3, 1, 2, 4, 5, 5, 6};
System.out.println(new Q215().findKthLargest(people, 4));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p300;
package com.fanxb.common;
import java.util.LinkedList;
import java.util.Queue;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.*;
import java.util.stream.Collectors;
@ -102,40 +102,6 @@ public class Q23 {
return listNodeList.get(0);
}
public ListNode so3(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
return dfs(lists, 0, lists.length - 1);
}
private ListNode dfs(ListNode[] lists, int startI, int endI) {
if (startI > endI) return null;
if (startI == endI) return lists[startI];
ListNode one, two;
if (endI - startI > 1) {
int mid = (startI + endI) / 2;
one = dfs(lists, startI, mid);
two = dfs(lists, mid + 1, endI);
} else {
one = lists[startI];
two = lists[endI];
}
ListNode res = new ListNode();
ListNode temp = res;
while (one != null && two != null) {
if (one.val <= two.val) {
temp.next = one;
one = one.next;
} else {
temp.next = two;
two = two.next;
}
temp = temp.next;
}
if (one != null) temp.next = one;
else if (two != null) temp.next = two;
return res.next;
}
public static void main(String[] args) {
ListNode a = new ListNode(1);
a.next = new ListNode(4);

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p300;
package com.fanxb.common;
import java.util.Stack;

View File

@ -1,6 +1,7 @@
package com.fanxb.common.p300;
package com.fanxb.common;
import java.util.*;
import java.util.stream.Collectors;
/**
* 定义转移方程dp[i][j]为长度为i+1的窗口在位置j的最大值

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p300;
package com.fanxb.common;
/**
* @author fanxb

View File

@ -0,0 +1,56 @@
package com.fanxb.common;
public class Q25 {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode res = new ListNode(), index = head;
res.next = head;
int count = 0;
//left:开始翻转节点的父节点
//right:结束翻转的节点
ListNode beforeL = res, l = beforeL.next, r, afterR;
while (index != null) {
if (++count == k) {
//进行翻转
r = index;
afterR = index.next;
reverse(l, count);
index = l;
//处理头尾节点关系
l.next = afterR;
beforeL.next = r;
//进行下一轮循环
beforeL = index;
l = index.next;
count = 0;
}
index = index.next;
}
return res.next;
}
/**
* 翻转start后的n个节点
*
* @param start
* @param n
*/
private void reverse(ListNode start, int n) {
//反转节点
ListNode prev = null;
for (int i = 0; i < n; i++) {
ListNode next = start.next;
start.next = prev;
prev = start;
start = next;
}
}
public static void main(String[] args) {
ListNode node = new ListNode(1);
node.next = new ListNode(2);
node.next.next = new ListNode(3);
node.next.next.next = new ListNode(4);
node.next.next.next.next = new ListNode(5);
new Q25().reverseKGroup(node, 3);
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p300;
package com.fanxb.common;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.Arrays;
@ -24,20 +24,7 @@ public class Q27 {
return count;
}
public int removeElement1(int[] nums, int val) {
int i = 0, j = 0;
while (j < nums.length) {
if (nums[j] != val) {
nums[i++] = nums[j];
}
j++;
}
return i;
}
public static void main(String[] args) {
int[] arr = new int[]{0, 1, 2, 2, 3, 0, 4, 2};
System.out.println(new Q27().removeElement1(arr, 2));
System.out.println(Arrays.toString(arr));
System.out.println(new Q27().removeElement(new int[]{0, 1, 2, 2, 3, 0, 4, 2}, 2));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p300;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA

View File

@ -1,4 +1,6 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.text.SimpleDateFormat;
/**
* Created with IntelliJ IDEA

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.*;
@ -39,32 +39,7 @@ public class Q3 {
return Math.max(characters.size(), res);
}
public int lengthOfLongestSubstring1(String s) {
int l = 0, r = 0, length = s.length();
int[] map = new int[129];
int res = 0;
while (r < length) {
char c = s.charAt(r);
boolean has = map[c] == 1;
if (has) {
res = Math.max(res, r - l);
//遇到重复的把l右移直到排除掉重复的
while (true) {
char c1 = s.charAt(l);
map[c1] = 0;
l++;
if (c1 == c) break;
}
} else if (r == length - 1) {
res = Math.max(res, r - l + 1);
}
map[c] = 1;
r++;
}
return res;
}
public static void main(String[] args) {
System.out.println(new Q3().lengthOfLongestSubstring1("abcabcbb"));
System.out.println(new Q3().lengthOfLongestSubstring("bbbbbbbbbbbbbbbbb"));
}
}

View File

@ -1,6 +1,6 @@
package com.fanxb.common.p400;
package com.fanxb.common;
import java.util.Arrays;
import java.util.Stack;
/**
* 定义dp[i]为以nums[i]结尾的子序列最大长度
@ -41,35 +41,6 @@ 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}));

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p400;
package com.fanxb.common;
/**
* @author fanxb

View File

@ -1,6 +1,4 @@
package com.fanxb.common.p400;
import com.fanxb.common.TreeNode;
package com.fanxb.common;
import java.util.Arrays;
import java.util.HashMap;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.Arrays;
@ -63,23 +63,6 @@ public class Q34 {
return new int[]{l, r};
}
public int[] searchRange1(int[] nums, int target) {
if (nums.length == 0) return new int[]{-1, -1};
int l = 0, r = nums.length - 1, index = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) index = mid;
if (nums[mid] > target) r = mid - 1;
else l = mid + 1;
}
if (index == -1) return new int[]{-1, -1};
int resL = index, resR = index;
while (resR < nums.length - 1 && nums[resR + 1] == target) resR++;
while (resL > 0 && nums[resL - 1] == target) resL--;
return new int[]{resL, resR};
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new Q34().searchRange(new int[]{1, 4}, 4)));
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p400;
package com.fanxb.common;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p400;
package com.fanxb.common;
import java.util.HashSet;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p400;
package com.fanxb.common;
/**
* 两数相加

View File

@ -1,4 +1,6 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA
@ -6,6 +8,7 @@ package com.fanxb.common.p100;
* 地址 https://leetcode-cn.com/problems/sqrtx/
* 思路 双路规避排序查找
*
*
* @author fanxb
* Date: 2020/6/11 9:56
*/
@ -37,28 +40,7 @@ public class Q4 {
return isDoubleSize ? (value + value2) / 2.0 : value2;
}
public double findMedianSortedArrays1(int[] nums1, int[] nums2) {
int m = nums1.length, n = nums2.length;
int mid1 = (m + n + 1) / 2, mid2 = (m + n + 2) / 2;
return (findK(nums1, 0, m - 1, nums2, 0, n - 1, mid1) + findK(nums1, 0, m - 1, nums2, 0, n - 1, mid2)) / 2.0;
}
// 两个数组中找第k小的数
private int findK(int[] arr1, int start1, int end1, int[] arr2, int start2, int end2, int k) {
int l1 = end1 - start1 + 1, l2 = end2 - start2 + 1;
if (l1 == 0) return arr2[start2 + k - 1];
if (l2 == 0) return arr1[start1 + k - 1];
if (k == 1) return Math.min(arr1[start1], arr2[start2]);
int index1 = start1 + Math.min(k / 2, l1) - 1;
int index2 = start2 + Math.min(k / 2, l2) - 1;
if (arr1[index1] >= arr2[index2])
return findK(arr1, start1, end1, arr2, index2 + 1, end2, k - (index2 - start2 + 1));
else
return findK(arr1, index1 + 1, end1, arr2, start2, end2, k - (index1 - start1 + 1));
}
public static void main(String[] args) {
System.out.println(new Q4().findMedianSortedArrays1(new int[]{1, 2}, new int[]{3, 4}));
System.out.println(new Q4().findMedianSortedArrays(new int[]{2,3,4}, new int[]{1}));
}
}

View File

@ -0,0 +1,44 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Q40 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new LinkedList<>();
Arrays.sort(candidates);
this.deal(res, new LinkedList<>(), 0, candidates, target);
return res;
}
private void deal(List<List<Integer>> res, List<Integer> cur, int start, int[] sources, int target) {
if (start >= sources.length || target < sources[start]) {
return;
}
for (int i = start; i < sources.length; i++) {
if (sources[i] > target) {
return;
}
if (i > start && sources[i] == sources[i - 1]) {
//第二个重复的元素不需要进行后续操作
continue;
}
cur.add(sources[i]);
if (sources[i] == target) {
res.add(new ArrayList<>(cur));
} else {
deal(res, cur, i + 1, sources, target - sources[i]);
}
//加入后删除当前元素尝试下一个
cur.remove(cur.size() - 1);
}
}
public static void main(String[] args) {
new Q40().combinationSum2(new int[]{2, 5, 2, 1, 2}, 5);
}
}

View File

@ -1,5 +1,6 @@
package com.fanxb.common.p500;
package com.fanxb.common;
import com.sun.tools.jconsole.JConsoleContext;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p500;
package com.fanxb.common;
import java.util.Arrays;
import java.util.LinkedList;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p500;
package com.fanxb.common;
import java.util.*;

View File

@ -1,5 +1,7 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
@ -65,31 +67,9 @@ public class Q42 {
return res;
}
public int trap2(int[] height) {
int length = height.length;
//i左边的最大高度
int[] left = new int[length];
left[0] = 0;
//i右边的最大高度
int[] right = new int[length];
right[length - 1] = 0;
for (int i = 1; i < length; i++) {
left[i] = Math.max(left[i - 1], height[i - 1]);
right[length - i - 1] = Math.max(right[length - i], height[length - i]);
}
int res = 0;
for (int i = 1; i < length - 1; i++) {
int temp = Math.min(left[i], right[i]) - height[i];
if (temp > 0) {
res += temp;
}
}
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}));
System.out.println(new Q42().trap2(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p500;
package com.fanxb.common;
import java.lang.reflect.AccessibleObject;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p500;
package com.fanxb.common;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p500;
package com.fanxb.common;
import java.util.Arrays;

View File

@ -0,0 +1,27 @@
package com.fanxb.common;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/9 15:10
*/
public class Q46 {
public int translateNum(int num) {
String str = String.valueOf(num);
//a=f(0),b=f(1)
int a = 1, b = 1, sum = 1;
for (int i = 1, length = str.length(); i < length; i++) {
String temp = str.substring(i - 1, i + 1);
sum = temp.compareTo("10") >= 0 && temp.compareTo("25") <= 0 ? a + b : b;
a = b;
b = sum;
}
return sum;
}
public static void main(String[] args) {
System.out.println(new Q46().translateNum(12258));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
/**
* 定义dp[i][j]表示从i到j的字符串是否为回文串

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p600;
package com.fanxb.common;
public class Q507 {

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p600;
package com.fanxb.common;
import java.util.Arrays;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p600;
package com.fanxb.common;
import java.util.Arrays;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p600;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA

View File

@ -0,0 +1,45 @@
package com.fanxb.common;
public class Q6 {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
int length = s.length();
int circle = 2 * numRows - 2;
char[][] res = new char[numRows][Double.valueOf(Math.ceil(length / (circle * 1.0))).intValue() * (numRows - 1)];
int y = 0, x = 0, count = 0;
for (int i = 0; i < length; i++) {
res[y][x] = s.charAt(i);
count++;
if (count == circle) {
//一轮循环结束重置
count = 0;
x++;
y--;
} else {
if (count < numRows) {
y++;
} else {
x++;
y--;
}
}
}
char[] strs = new char[length];
count = 0;
for (char[] temp : res) {
for (char one : temp) {
if (one != 0) {
strs[count++] = one;
}
}
}
return new String(strs);
}
public static void main(String[] args) {
System.out.println(new Q6().convert("PAYPALISHIRING", 4));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p700;
package com.fanxb.common;
/**
* 具有给定数值的最小字符串

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
/**
* 旋转链表

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p700;
package com.fanxb.common;
/**
* 平方数之和

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p700;
package com.fanxb.common;
import java.util.Arrays;
import java.util.LinkedList;

View File

@ -1,4 +1,6 @@
package com.fanxb.common.p700;
package com.fanxb.common;
import java.util.Arrays;
/**
* 验证回文字符串

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA
@ -12,6 +12,8 @@ package com.fanxb.common.p100;
* 4. 否则判断temp1>x,如果满足r=mid,重复2
* 5. 否则l=mid,重复2
*
*
*
* @author fanxb
* Date: 2020/6/11 9:56
*/
@ -37,50 +39,7 @@ public class Q69 {
}
}
public int mySqrt1(int x) {
if (x == 0) return x;
if (x <= 3) return 1;
int l = 2, r = x / 2, m;
while ((m = (int) Math.floor((l + r) / 2.0)) != r) {
if (m * (long) m > x) {
r = m;
} else if (m == l) {
r--;
} else {
l = m;
}
}
return m;
}
public int mySqrt2(int x) {
if (x == 0) return 0;
long l = 0, r = x;
while (l < r) {
long mid = (l + r) / 2;
long val = mid * mid;
if (val == x) return (int) mid;
else if (val > x) r = mid;
else if (mid == l) return (int) l;
else l = mid;
}
return (int) l;
}
public int mySqrt3(int x) {
if (x == 0) return 0;
double temp = x;
while (true) {
double temp1 = (temp + x / temp) / 2;
if (Math.abs(temp1 - temp) <= 0.00001) {
return (int) temp1;
}
temp =temp1;
}
}
public static void main(String[] args) {
// System.out.println(new Q69().mySqrt(2147395599));
System.out.println(new Q69().mySqrt1(2147395599));
System.out.println(new Q69().mySqrt(2147395599));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
public class Q7 {
public int reverse(int x) {

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
/**
* 编辑距离可以对任意一个单词增加一个字母删除一个字母替换一个字母

View File

@ -1,6 +1,7 @@
package com.fanxb.common.p800;
package com.fanxb.common;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
/**

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.Arrays;
@ -7,6 +7,7 @@ import java.util.Arrays;
* 题目地址 https://leetcode-cn.com/problems/search-a-2d-matrix/
* 解题思路两次二分即可第一次二分列找到所在行然后二分所在行找到是否存在目标值由于行列数少于100,直接搜索问题也不大
*
*
* @author fanxb
* Date: 2020/6/9 15:10
*/
@ -26,20 +27,6 @@ public class Q74 {
return Arrays.stream(matrix[m - 1]).anyMatch(item -> item == target);
}
public static boolean solution1(int[][] matrix, int target) {
int m = matrix.length, n = matrix[0].length;
int l = 0, r = m * n - 1;
while (l <= r) {
int mid = (l + r) / 2;
int[] midP = new int[]{mid / n, mid % n};
int val = matrix[midP[0]][midP[1]];
if (val == target) return true;
else if (val < target) l = mid + 1;
else r = mid - 1;
}
return false;
}
public static void main(String[] args) {
int[][] s = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 6}};
System.out.println(solution(s, 3));

View File

@ -1,4 +1,7 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.*;
import java.util.stream.Collectors;
/**
* 最小覆盖子串
@ -48,36 +51,6 @@ public class Q76 {
return start < 0 ? "" : s.substring(start, end + 1);
}
public String minWindow(String s, String t) {
int sSize = s.length(), needCount = t.length();
int[] map = new int[128];
for (int i = 0; i < needCount; i++) map[t.charAt(i)]++;
int l = 0, r = 0, minL = 0, minR = sSize;
while (r < sSize) {
char temp = s.charAt(r);
if (map[temp] > 0) needCount--;
map[temp]--;
if (needCount == 0) {
//说明包含了所有元素,开始把l向右移动直到刚好包含所有元素
while (needCount == 0) {
temp = s.charAt(l);
if (map[temp] == 0) {
//说明l这个位置刚好
if ((r - l) < (minR - minL)) {
minL = l;
minR = r;
}
needCount++;
}
map[temp]++;
l++;
}
}
r++;
}
return minR == sSize ? "" : s.substring(minL, minR + 1);
}
public static void main(String[] args) {
System.out.println(solution("a", "aa"));

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p800;
package com.fanxb.common;
import java.util.*;

View File

@ -1,4 +1,6 @@
package com.fanxb.common.p800;
package com.fanxb.common;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA

View File

@ -1,4 +1,6 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA

View File

@ -1,4 +1,8 @@
package com.fanxb.common.p900;
package com.fanxb.common;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class Q825 {
public int numFriendRequests(int[] ages) {

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p100;
package com.fanxb.common;
/**
* TODO 类描述
@ -41,7 +41,7 @@ public class Q83 {
* 递归版块
*
* @param head head
* @return com.fanxb.common.p200.Q100.Q83.ListNode
* @return com.fanxb.common.Q83.ListNode
* @author fanxb
* @date 2021/3/26
**/

View File

@ -1,5 +1,7 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;
/**

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p900;
package com.fanxb.common;
/**
* 两数相加

View File

@ -1,4 +1,4 @@
package com.fanxb.common.p900;
package com.fanxb.common;
/**
* 两数相加

View File

@ -1,6 +1,7 @@
package com.fanxb.common.p100;
package com.fanxb.common;
import java.util.Arrays;
import java.util.Collections;
/**
* 合并两个有序数组
@ -30,23 +31,9 @@ public class Q88 {
System.arraycopy(res, 0, nums1, 0, count);
}
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] nums1Copy = new int[m];
System.arraycopy(nums1, 0, nums1Copy, 0, m);
for (int i = 0, j = 0, k = 0; k < m + n; k++) {
if (j>=n || (i < m && nums1Copy[i] < nums2[j])) {
nums1[k] = nums1Copy[i++];
} else {
nums1[k] = nums2[j++];
}
}
}
public static void main(String[] args) {
int[] nums1 = new int[]{1,2,3};
int[] nums2 = new int[]{};
new Q88().merge(nums1,3,nums2,0);
int[] nums1 = new int[12];
System.out.println(Arrays.toString(nums1));
}
}

View File

@ -0,0 +1,35 @@
package com.fanxb.common;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q9 {
public boolean isPalindrome(int x) {
//小于0的数或者末尾为0的正数肯定不是回文数
if (x < 0 || (x > 0 && x % 10 == 0)) {
return false;
}
long temp = 1;
while (x / (temp * 10) > 0) {
temp = temp * 10;
}
for (int i = 1; i < temp; temp = temp / 10, i *= 10) {
System.out.println(x / i % 10 + ":" + x / temp % 10);
if (x / i % 10 != x / temp % 10) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Q9 instance = new Q9();
System.out.println(instance.isPalindrome(1212312));
System.out.println(instance.isPalindrome(1410110141));
System.out.println(instance.isPalindrome(-121));
}
}

View File

@ -1,6 +1,8 @@
package com.fanxb.common.p1000;
package com.fanxb.common;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**

View File

@ -1,66 +0,0 @@
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));
}
Object object =new Object();
}

View File

@ -1,18 +1,18 @@
package com.fanxb.common;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
public TreeNode(int val) {
TreeNode(int val) {
this.val = val;
}
public TreeNode(int val, TreeNode left, TreeNode right) {
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;

View File

@ -1,18 +0,0 @@
package com.fanxb.common.p100;
public class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}

View File

@ -1,26 +0,0 @@
package com.fanxb.common.p100;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q11 {
public int maxArea(int[] height) {
int length = height.length;
int max = 0, i = 0, j = length - 1;
while (i < j) {
int temp = (j - i) * Math.min(height[i], height[j]);
if (max < temp) max = temp;
if (height[i] > height[j]) j--;
else i++;
}
return max;
}
public static void main(String[] args) {
Q11 instance = new Q11();
}
}

View File

@ -1,65 +0,0 @@
package com.fanxb.common.p100;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q12 {
public String intToRoman(int num) {
StringBuilder stringBuilder = new StringBuilder();
while (num > 0) {
if (num >= 1000) {
stringBuilder.append("M");
num -= 1000;
} else if (num >= 900) {
stringBuilder.append("CM");
num -= 900;
} else if (num >= 500) {
stringBuilder.append("D");
num -= 500;
} else if (num >= 400) {
stringBuilder.append("CD");
num -= 400;
} else if (num >= 100) {
stringBuilder.append("C");
num -= 100;
} else if (num >= 90) {
stringBuilder.append("XC");
num -= 90;
} else if (num >= 50) {
stringBuilder.append("L");
num -= 50;
} else if (num >= 40) {
stringBuilder.append("XL");
num -= 40;
} else if (num >= 10) {
stringBuilder.append("X");
num -= 10;
} else if (num == 9) {
stringBuilder.append("IX");
num -= 9;
} else if (num >= 5) {
stringBuilder.append("V");
num -= 5;
} else if (num == 4) {
stringBuilder.append("IV");
num -= 4;
} else {
stringBuilder.append("I");
num -= 1;
}
}
return stringBuilder.toString();
}
public static void main(String[] args) {
Q12 instance = new Q12();
System.out.println(instance.intToRoman(3));
System.out.println(instance.intToRoman(9));
System.out.println(instance.intToRoman(58));
System.out.println(instance.intToRoman(1994));
}
}

View File

@ -1,45 +0,0 @@
package com.fanxb.common.p100;
import java.util.HashMap;
import java.util.Map;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q13 {
private static Map<Character, Integer> map = new HashMap<>(7);
static {
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
}
public int romanToInt(String s) {
int res = 0;
char[] chars = s.toCharArray();
int length = chars.length;
for (int i = 0; i < length; i++) {
int cur = map.get(chars[i]), next = i == length - 1 ? 0 : map.get(chars[i + 1]);
if (cur < next) {
res += next - cur;
i++;
} else {
res += cur;
}
}
return res;
}
public static void main(String[] args) {
Q13 instance = new Q13();
System.out.println(instance.romanToInt("MCMXCIVI"));
}
}

View File

@ -1,11 +0,0 @@
package com.fanxb.common.p100;
public class Q136 {
public int singleNumber(int[] nums) {
int res = nums[0];
for (int i = 1; i < nums.length; i++) {
res = res ^ nums[i];
}
return res;
}
}

View File

@ -1,19 +0,0 @@
package com.fanxb.common.p100;
public class Q137 {
public int singleNumber(int[] nums) {
int[] count = new int[32];
for (int num : nums) {
for (int i = 0; i < 32; i++) {
count[i] += num & 1;
num = num >>> 1;
}
}
int res = 0;
for (int i = 0; i < 32; i++) {
res = res | count[i] % 3;
res = res << 1;
}
return res;
}
}

View File

@ -1,45 +0,0 @@
package com.fanxb.common.p100;
import java.util.stream.Stream;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q14 {
public String longestCommonPrefix(String[] strs) {
if (strs.length == 1) return strs[0];
StringBuilder res = new StringBuilder();
int length = strs.length;
int minLength = Stream.of(strs).map(String::length).min(Integer::compare).orElse(0);
char a, b, c;
for (int i = 0; i < minLength; i++) {
for (int j = 1; j < length; j++) {
if (strs[j].charAt(i) != strs[j - 1].charAt(i)) return res.toString();
}
res.append(strs[0].charAt(i));
}
return res.toString();
}
public String longestCommonPrefix1(String[] strs) {
if (strs.length == 1) return strs[0];
String res = strs[0];
for (int i = 1; i < strs.length; i++) {
StringBuilder temp = new StringBuilder();
for (int j = 0; j < Math.min(res.length(), strs[i].length()); j++) {
if (res.charAt(j) == strs[i].charAt(j)) temp.append(res.charAt(j));
else break;
}
if (temp.length() == 0) return "";
res = temp.toString();
}
return res;
}
public static void main(String[] args) {
Q14 instance = new Q14();
}
}

View File

@ -1,41 +0,0 @@
package com.fanxb.common.p100;
import java.util.*;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q15 {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
Set<String> has = new HashSet<>();
int length = nums.length;
for (int i = 0; i < length - 2; i++) {
if (nums[i] > 0) {
return res;
}
int l = i + 1, r = length - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (sum == 0) {
String key = nums[i] + "," + nums[l] + "," + nums[r];
if (!has.contains(key)) {
res.add(new ArrayList<>(Arrays.asList(nums[i], nums[l], nums[r])));
has.add(key);
}
break;
} else if (sum > 0) r--;
else l++;
}
}
return res;
}
public static void main(String[] args) {
Q15 instance = new Q15();
}
}

View File

@ -1,30 +0,0 @@
package com.fanxb.common.p100;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Q17 {
private Set<String> res = new HashSet<>();
private StringBuilder stringBuilder = new StringBuilder();
private String[] map = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
if (digits.isEmpty()) return new ArrayList<>();
dfs(digits.length(), digits, 0);
return new ArrayList<>(res);
}
private void dfs(int size, String digits, int i) {
if (stringBuilder.length() == size) {
res.add(stringBuilder.toString());
return;
}
for (char c : map[digits.charAt(i) - '0'].toCharArray()) {
stringBuilder.append(c);
dfs(size, digits, i + 1);
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
}
}
}

View File

@ -1,32 +0,0 @@
package com.fanxb.common.p100;
import com.fanxb.common.ListNode;
public class Q21 {
public com.fanxb.common.ListNode mergeTwoLists(com.fanxb.common.ListNode list1, com.fanxb.common.ListNode list2) {
if (list2 == null) return list1;
if (list1 == null) return list2;
com.fanxb.common.ListNode res = null;
if (list1.val < list2.val) {
res = list1;
list1 = list1.next;
} else {
res = list2;
list2 = list2.next;
}
ListNode temp = res;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
temp.next = list1;
list1 = list1.next;
} else {
temp.next = list2;
list2 = list2.next;
}
temp = temp.next;
}
if (list1 != null) temp.next = list1;
else temp.next = list2;
return res;
}
}

View File

@ -1,51 +0,0 @@
package com.fanxb.common.p100;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Q22 {
Set<String> ans;
//左括号个数
int lCount;
//右括号个数
int rCount;
StringBuilder temp;
public List<String> generateParenthesis(int n) {
ans = new HashSet<>();
lCount = 0;
rCount = 0;
temp = new StringBuilder();
dfs(n, 2 * n);
return new ArrayList<>(ans);
}
private void dfs(int n, int total) {
if (temp.length() == total) {
ans.add(temp.toString());
return;
}
if (lCount < n) {
lCount++;
temp.append('(');
dfs(n, total);
lCount--;
temp.deleteCharAt(temp.length() - 1);
}
if (rCount < lCount) {
rCount++;
temp.append(')');
dfs(n, total);
rCount--;
temp.deleteCharAt(temp.length() - 1);
}
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
System.out.println(new Q22().generateParenthesis(8));
System.out.println(System.currentTimeMillis() - start);
}
}

View File

@ -1,128 +0,0 @@
package com.fanxb.common.p100;
import com.fanxb.common.ListNode;
public class Q25 {
public com.fanxb.common.ListNode reverseKGroup(com.fanxb.common.ListNode head, int k) {
com.fanxb.common.ListNode res = new com.fanxb.common.ListNode(), index = head;
res.next = head;
int count = 0;
//left:开始翻转节点的父节点
//right:结束翻转的节点
com.fanxb.common.ListNode beforeL = res, l = beforeL.next, r, afterR;
while (index != null) {
if (++count == k) {
//进行翻转
r = index;
afterR = index.next;
reverse(l, count);
index = l;
//处理头尾节点关系
l.next = afterR;
beforeL.next = r;
//进行下一轮循环
beforeL = index;
l = index.next;
count = 0;
}
index = index.next;
}
return res.next;
}
/**
* 翻转start后的n个节点
*
* @param start
* @param n
*/
private void reverse(com.fanxb.common.ListNode start, int n) {
//反转节点
com.fanxb.common.ListNode prev = null;
for (int i = 0; i < n; i++) {
com.fanxb.common.ListNode next = start.next;
start.next = prev;
prev = start;
start = next;
}
}
public com.fanxb.common.ListNode newReverseKGroup(com.fanxb.common.ListNode head, int k) {
if (k == 1) return head;
boolean firstDeal = true;
int count = 0;
com.fanxb.common.ListNode res = head, left = null, right, lastRight = null;
while (head != null) {
if (left == null) left = head;
count++;
if (count == k) {
right = head;
reverse(left, right);
if (firstDeal) {
res = right;
firstDeal = false;
} else {
lastRight.next = right;
}
lastRight = left;
head = left.next;
left = null;
count = 0;
} else {
head = head.next;
}
}
return res;
}
private void reverse(com.fanxb.common.ListNode start, com.fanxb.common.ListNode end) {
com.fanxb.common.ListNode last = start, current = start.next, next, temp = end.next;
while (current != null && current != temp) {
next = current.next;
current.next = last;
last = current;
current = next;
}
start.next = temp;
}
public com.fanxb.common.ListNode new1ReverseKGroup(com.fanxb.common.ListNode head, int k) {
com.fanxb.common.ListNode res = new com.fanxb.common.ListNode(0);
res.next = head;
com.fanxb.common.ListNode pre = res, next;
while (head != null) {
com.fanxb.common.ListNode end = head;
for (int i = 0; i < k - 1 && end != null; i++) end = end.next;
if (end == null) break;
next = end.next;
end.next = null;
pre.next = end;
reverse1(head);
head.next = next;
pre = head;
head = head.next;
}
return res.next;
}
public com.fanxb.common.ListNode reverse1(com.fanxb.common.ListNode head) {
com.fanxb.common.ListNode pre = head, cur = head.next, next;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
public static void main(String[] args) {
com.fanxb.common.ListNode node = new com.fanxb.common.ListNode(1);
node.next = new com.fanxb.common.ListNode(2);
node.next.next = new com.fanxb.common.ListNode(3);
node.next.next.next = new com.fanxb.common.ListNode(4);
node.next.next.next.next = new ListNode(5);
new Q25().new1ReverseKGroup(node, 2);
}
}

View File

@ -1,45 +0,0 @@
package com.fanxb.common.p100;
import java.util.Arrays;
/**
* TODO
*
* @author fanxb
* @date 2022/3/2 16:42
*/
public class Q26 {
public int removeDuplicates(int[] nums) {
int n = nums.length;
//不重复元素的个数
int count = 0;
//最后一个元素单独处理
//找到一个重复序列的最后一个元素
for (int i = 0; i < n - 1; i++) {
if (nums[i] != nums[i + 1]) {
nums[count++] = nums[i];
}
}
//由于是当前元素和下一个元素作比较那么最后一个元素一定是目标元素
nums[count++] = nums[n - 1];
return count;
}
public int newRemoveDuplicates(int[] nums) {
int i = 0, j = 1;
while (j < nums.length) {
if (nums[j] != nums[i]) {
nums[++i] = nums[j];
}
j++;
}
return i + 1;
}
public static void main(String[] args) {
int[] arr = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
System.out.println(new Q26().newRemoveDuplicates(arr));
System.out.println(Arrays.toString(arr));
}
}

Some files were not shown because too many files have changed in this diff Show More