This commit is contained in:
fleyx 2024-04-07 09:28:21 +08:00
parent 08ddcc8929
commit 1241441ffb
179 changed files with 855 additions and 368 deletions

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

@ -1,44 +0,0 @@
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));
}
}

@ -0,0 +1,15 @@
package com.fanxb.common;
public class Q918 {
public int maxSubarraySumCircular(int[] nums) {
int total = nums[0], curMax = nums[0], totalMax = nums[0], curMin = nums[0], totalMin = nums[0];
for (int i = 1; i < nums.length; i++) {
curMax = Math.max(curMax + nums[i], nums[i]);
totalMax = Math.max(curMax, totalMax);
total += nums[i];
curMin = Math.min(curMin + nums[i], nums[i]);
totalMin = Math.min(curMin, totalMin);
}
return totalMax > 0 ? Math.max(totalMax, total - totalMin) : totalMax;
}
}

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

@ -0,0 +1,18 @@
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;
}
}

@ -1,55 +1,55 @@
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;
}
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.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;
}
}

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

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

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

@ -1,7 +1,5 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
/**

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

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

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

@ -1,10 +1,12 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import com.fanxb.common.ListNode;
public class Q21 {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
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;
ListNode res = null;
com.fanxb.common.ListNode res = null;
if (list1.val < list2.val) {
res = list1;
list1 = list1.next;

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

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.*;
import java.util.stream.Collectors;
@ -102,6 +102,40 @@ 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);

@ -1,13 +1,15 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import com.fanxb.common.ListNode;
public class Q25 {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode res = new ListNode(), index = head;
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:结束翻转的节点
ListNode beforeL = res, l = beforeL.next, r, afterR;
com.fanxb.common.ListNode beforeL = res, l = beforeL.next, r, afterR;
while (index != null) {
if (++count == k) {
//进行翻转
@ -34,22 +36,22 @@ public class Q25 {
* @param start
* @param n
*/
private void reverse(ListNode start, int n) {
private void reverse(com.fanxb.common.ListNode start, int n) {
//反转节点
ListNode prev = null;
com.fanxb.common.ListNode prev = null;
for (int i = 0; i < n; i++) {
ListNode next = start.next;
com.fanxb.common.ListNode next = start.next;
start.next = prev;
prev = start;
start = next;
}
}
public ListNode newReverseKGroup(ListNode head, int k) {
public com.fanxb.common.ListNode newReverseKGroup(com.fanxb.common.ListNode head, int k) {
if (k == 1) return head;
boolean firstDeal = true;
int count = 0;
ListNode res = head, left = null, right, lastRight = null;
com.fanxb.common.ListNode res = head, left = null, right, lastRight = null;
while (head != null) {
if (left == null) left = head;
count++;
@ -73,8 +75,8 @@ public class Q25 {
return res;
}
private void reverse(ListNode start, ListNode end) {
ListNode last = start, current = start.next, next, temp = end.next;
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;
@ -85,12 +87,12 @@ public class Q25 {
}
public ListNode new1ReverseKGroup(ListNode head, int k) {
ListNode res = new ListNode(0);
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;
ListNode pre = res, next;
com.fanxb.common.ListNode pre = res, next;
while (head != null) {
ListNode end = head;
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;
@ -104,8 +106,8 @@ public class Q25 {
return res.next;
}
public ListNode reverse1(ListNode head) {
ListNode pre = head, cur = head.next, 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;
@ -116,10 +118,10 @@ public class Q25 {
}
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);
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);
}

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

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

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

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

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p100;
public class Q33 {
@ -47,7 +47,34 @@ public class Q33 {
return nums[l] == target ? l : -1;
}
public int search1(int[] nums, int target) {
int n = nums.length;
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) return mid;
if (nums[l] > nums[r]) {
//说明l,r分别在两段中
if (target < nums[l]) {
//说明target在右边的段
if (nums[mid] >= nums[l]) l = mid + 1; //mid在左边
else if (nums[mid] > target) r = mid - 1; //mid在左边
else l = mid + 1;
} else {
//target在左边的段
if (nums[mid] < nums[l]) r = mid - 1; //mid在左边
else if (nums[mid] > target) r = mid - 1; //mid在右边
else l = mid + 1;
}
} else {
if (nums[mid] > target) r = mid - 1;
else l = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(new Q33().search(new int[]{1, 3}, 0));
System.out.println(new Q33().search1(new int[]{4, 5, 6, 7, 0, 1, 2}, 0));
}
}

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.Arrays;
@ -63,6 +63,23 @@ 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)));
}

@ -0,0 +1,14 @@
package com.fanxb.common.p100;
public class Q35 {
public int searchInsert(int[] nums, int target) {
int l = 0, r = nums.length - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] > target) r = mid + 1;
else l = mid + 1;
}
return l;
}
}

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

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

@ -1,6 +1,4 @@
package com.fanxb.common;
import java.util.Arrays;
package com.fanxb.common.p100;
/**
* Created with IntelliJ IDEA
@ -8,7 +6,6 @@ import java.util.Arrays;
* 地址 https://leetcode-cn.com/problems/sqrtx/
* 思路 双路规避排序查找
*
*
* @author fanxb
* Date: 2020/6/11 9:56
*/
@ -40,7 +37,28 @@ 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().findMedianSortedArrays(new int[]{2,3,4}, new int[]{1}));
System.out.println(new Q4().findMedianSortedArrays1(new int[]{1, 2}, new int[]{3, 4}));
}
}

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

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

@ -1,6 +1,4 @@
package com.fanxb.common;
import java.util.Stack;
package com.fanxb.common.p100;
/**
* @author fanxb

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

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

@ -1,9 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class Q48 {

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

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

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

@ -0,0 +1,20 @@
package com.fanxb.common.p100;
/**
* 转移方程dp[i]表示当以i为结尾时的最大子数组和
* 当dp[i-1]>0时dp[i]=dp[i-1]+nums[i]
* 否则dp[i]=nums[i]
*/
public class Q53 {
public int maxSubArray(int[] nums) {
int res = nums[0];
int last = nums[0];
for (int i = 1; i < nums.length; i++) {
int newVal = last > 0 ? last + nums[i] : nums[i];
res = Math.max(res, newVal);
last = newVal;
}
return res;
}
}

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

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

@ -1,9 +1,7 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class Q56 {
public int[][] merge(int[][] intervals) {

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

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

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

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

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

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

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

@ -0,0 +1,18 @@
package com.fanxb.common.p100;
public class Q67 {
public String addBinary(String a, String b) {
int m = a.length(), n = b.length();
if (m < n) return addBinary(b, a);
char[] acs = a.toCharArray();
int temp = 0;
for (int i = 0; i < m; i++) {
int ac = acs[m - 1 - i] - '0';
int bc = n - 1 - i >= 0 ? b.charAt(n - 1 - i) - '0' : 0;
int val = ac + bc + temp;
temp = val >= 2 ? 1 : 0;
acs[m - 1 - i] = (char) ('0' + val % 2);
}
return (temp == 1 ? "1" : "") + new String(acs);
}
}

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

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

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

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p100;
/**
* 定义f(n)表示爬n级台阶的方法

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

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

@ -1,8 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.Arrays;
public class Q73 {
public void setZeroes(int[][] matrix) {

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.Arrays;
@ -7,7 +7,6 @@ import java.util.Arrays;
* 题目地址 https://leetcode-cn.com/problems/search-a-2d-matrix/
* 解题思路两次二分即可第一次二分列找到所在行然后二分所在行找到是否存在目标值由于行列数少于100,直接搜索问题也不大
*
*
* @author fanxb
* Date: 2020/6/9 15:10
*/
@ -27,6 +26,20 @@ 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));

@ -1,7 +1,4 @@
package com.fanxb.common;
import java.util.*;
import java.util.stream.Collectors;
package com.fanxb.common.p100;
/**
* 最小覆盖子串

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

@ -1,7 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p100;
public class Q79 {
private int[][] step = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

@ -1,8 +1,4 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
package com.fanxb.common.p100;
/**
* Created with IntelliJ IDEA

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

@ -1,9 +1,11 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import com.fanxb.common.ListNode;
public class Q82 {
public ListNode deleteDuplicates(ListNode head) {
public com.fanxb.common.ListNode deleteDuplicates(com.fanxb.common.ListNode head) {
if (head == null || head.next == null) return head;
ListNode res = new ListNode(), last = res, startBefore = null;
com.fanxb.common.ListNode res = new com.fanxb.common.ListNode(), last = res, startBefore = null;
res.next = head;
while (head.next != null) {
if (head.val == head.next.val) {
@ -23,8 +25,8 @@ public class Q82 {
return res.next;
}
public ListNode better(ListNode head) {
ListNode res = new ListNode();
public com.fanxb.common.ListNode better(com.fanxb.common.ListNode head) {
com.fanxb.common.ListNode res = new com.fanxb.common.ListNode();
res.next = head;
ListNode cur = res.next, last = res;
while (cur != null && cur.next != null) {

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

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

@ -1,10 +1,12 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import com.fanxb.common.ListNode;
public class Q86 {
public ListNode partition(ListNode head, int x) {
ListNode res = new ListNode(-1);
public com.fanxb.common.ListNode partition(com.fanxb.common.ListNode head, int x) {
com.fanxb.common.ListNode res = new com.fanxb.common.ListNode(-1);
res.next = head;
ListNode bigList = new ListNode(-1);
com.fanxb.common.ListNode bigList = new com.fanxb.common.ListNode(-1);
ListNode pre = res, cur = head, bigCur = bigList;
while (cur != null) {
if (cur.val >= x) {

@ -1,7 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import java.util.Arrays;
import java.util.Collections;
/**
* 合并两个有序数组

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

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

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

@ -1,16 +1,18 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import com.fanxb.common.TreeNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Q94 {
public List<Integer> inorderTraversal(TreeNode root) {
public List<Integer> inorderTraversal(com.fanxb.common.TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
Stack<com.fanxb.common.TreeNode> stack = new Stack<>();
stack.push(root);
TreeNode cur = root.left;
com.fanxb.common.TreeNode cur = root.left;
while (!stack.isEmpty() || cur != null) {
while (cur != null) {
stack.push(cur);

@ -1,9 +1,11 @@
package com.fanxb.common;
package com.fanxb.common.p100;
import com.fanxb.common.TreeNode;
public class Q98 {
private Integer lastVal = null;
public boolean isValidBST(TreeNode root) {
public boolean isValidBST(com.fanxb.common.TreeNode root) {
return dfs(root);
}

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
public class Q100 {
public static class TreeNode {

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
public class Q101 {
public static class TreeNode {

@ -1,4 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p200;
import com.fanxb.common.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;

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

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
public class Q104 {
public static class TreeNode {

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

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

@ -0,0 +1,21 @@
package com.fanxb.common.p200;
import com.fanxb.common.TreeNode;
public class Q108 {
public TreeNode sortedArrayToBST(int[] nums) {
return dfs(nums, 0, nums.length - 1);
}
private TreeNode dfs(int[] nums, int l, int r) {
if (l > r) return null;
if (l == r) return new TreeNode(nums[l]);
int mid = (l + r) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = dfs(nums, l, mid - 1);
node.right = dfs(nums, mid + 1, r);
return node;
}
}

@ -1,4 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p200;
import com.fanxb.common.TreeNode;
import java.util.LinkedList;

@ -1,4 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p200;
import com.fanxb.common.TreeNode;
import java.util.LinkedList;

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

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

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
/**
* 线性扫描只需找到最大和最小数的差即可假设第一个数为最小值从第二个数开始遍历如果大于最小值则计算利润如果小于最小值就将最小值设置当前值然后继续往后便利知道遍历完毕

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

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
/**
* 题目地址 https://leetcode-cn.com/problems/partition-labels/

@ -1,4 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p200;
import com.fanxb.common.TreeNode;
public class Q124 {
private int resValue = Integer.MIN_VALUE;

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
/**
* 题目地址 https://leetcode-cn.com/problems/partition-labels/

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

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

@ -1,4 +1,6 @@
package com.fanxb.common;
package com.fanxb.common.p200;
import com.fanxb.common.TreeNode;
public class Q129 {
private static class Node {

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
public class Q130 {
public void solve(char[][] board) {

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

@ -1,8 +1,4 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
package com.fanxb.common.p200;
/**
* 分割回文串

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

@ -1,4 +1,4 @@
package com.fanxb.common;
package com.fanxb.common.p200;
/**
* 分割回文串

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

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

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

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