Compare commits
53 Commits
dependabot
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
7148ad31fb | ||
|
e775d389c0 | ||
|
90b120b4ba | ||
|
13a1a09aaf | ||
|
bff038b9d5 | ||
|
1241441ffb | ||
|
08ddcc8929 | ||
|
dd90fb290f | ||
|
610048f4ea | ||
|
121cc9b5ba | ||
|
5770a9f011 | ||
|
0a779cdd32 | ||
|
bb6fcf1c53 | ||
|
5c1401ba7a | ||
|
bfcc5da9f6 | ||
|
b1b713df35 | ||
|
38daf3ac26 | ||
|
4984a2b597 | ||
|
85737c7e8d | ||
|
1403b5a7eb | ||
|
ff9af65feb | ||
|
f72bb3069a | ||
|
3c231d2345 | ||
|
007a62df51 | ||
|
08407ea39c | ||
|
0a6b066776 | ||
|
f6d2c9c195 | ||
|
81df394d2c | ||
|
e89cc79716 | ||
|
6657ad1707 | ||
|
9612be20a5 | ||
|
cbb59d2f9c | ||
|
c8c466a8e0 | ||
|
255999758c | ||
|
7d263d1735 | ||
|
ed167e9058 | ||
|
8ff7654633 | ||
|
dbcc8ee582 | ||
|
909475b196 | ||
|
5545564d98 | ||
|
a1d07aae6a | ||
|
f3adc25ddb | ||
|
2a0657fa79 | ||
|
42bbdd309b | ||
|
73748b9dd7 | ||
|
11c5fe7e3f | ||
|
1f7d52d2bb | ||
|
92e87cbf03 | ||
|
e6daa3aafa | ||
|
7c8b2ca1d0 | ||
|
861597228a | ||
|
28072a53ce | ||
|
3966dcc8c3 |
@ -1,8 +1,8 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
public int val;
|
||||
public ListNode next;
|
||||
|
||||
public ListNode() {
|
||||
}
|
||||
|
@ -1,47 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
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}}));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
66
5.leetcode/src/com/fanxb/common/Test.java
Normal file
66
5.leetcode/src/com/fanxb/common/Test.java
Normal file
@ -0,0 +1,66 @@
|
||||
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();
|
||||
}
|
@ -1,18 +1,18 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
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;
|
||||
|
18
5.leetcode/src/com/fanxb/common/p100/ListNode.java
Normal file
18
5.leetcode/src/com/fanxb/common/p100/ListNode.java
Normal file
@ -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,45 +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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更优,一次循环即可
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
26
5.leetcode/src/com/fanxb/common/p100/Q11.java
Normal file
26
5.leetcode/src/com/fanxb/common/p100/Q11.java
Normal file
@ -0,0 +1,26 @@
|
||||
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();
|
||||
}
|
||||
}
|
65
5.leetcode/src/com/fanxb/common/p100/Q12.java
Normal file
65
5.leetcode/src/com/fanxb/common/p100/Q12.java
Normal file
@ -0,0 +1,65 @@
|
||||
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));
|
||||
}
|
||||
}
|
45
5.leetcode/src/com/fanxb/common/p100/Q13.java
Normal file
45
5.leetcode/src/com/fanxb/common/p100/Q13.java
Normal file
@ -0,0 +1,45 @@
|
||||
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"));
|
||||
}
|
||||
}
|
11
5.leetcode/src/com/fanxb/common/p100/Q136.java
Normal file
11
5.leetcode/src/com/fanxb/common/p100/Q136.java
Normal file
@ -0,0 +1,11 @@
|
||||
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;
|
||||
}
|
||||
}
|
19
5.leetcode/src/com/fanxb/common/p100/Q137.java
Normal file
19
5.leetcode/src/com/fanxb/common/p100/Q137.java
Normal file
@ -0,0 +1,19 @@
|
||||
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;
|
||||
}
|
||||
}
|
45
5.leetcode/src/com/fanxb/common/p100/Q14.java
Normal file
45
5.leetcode/src/com/fanxb/common/p100/Q14.java
Normal file
@ -0,0 +1,45 @@
|
||||
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();
|
||||
}
|
||||
}
|
41
5.leetcode/src/com/fanxb/common/p100/Q15.java
Normal file
41
5.leetcode/src/com/fanxb/common/p100/Q15.java
Normal file
@ -0,0 +1,41 @@
|
||||
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();
|
||||
}
|
||||
}
|
30
5.leetcode/src/com/fanxb/common/p100/Q17.java
Normal file
30
5.leetcode/src/com/fanxb/common/p100/Q17.java
Normal file
@ -0,0 +1,30 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
||||
* 两数相加
|
||||
@ -62,17 +62,39 @@ 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));
|
||||
}
|
||||
}
|
32
5.leetcode/src/com/fanxb/common/p100/Q21.java
Normal file
32
5.leetcode/src/com/fanxb/common/p100/Q21.java
Normal file
@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
}
|
51
5.leetcode/src/com/fanxb/common/p100/Q22.java
Normal file
51
5.leetcode/src/com/fanxb/common/p100/Q22.java
Normal file
@ -0,0 +1,51 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -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);
|
128
5.leetcode/src/com/fanxb/common/p100/Q25.java
Normal file
128
5.leetcode/src/com/fanxb/common/p100/Q25.java
Normal file
@ -0,0 +1,128 @@
|
||||
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);
|
||||
}
|
||||
}
|
45
5.leetcode/src/com/fanxb/common/p100/Q26.java
Normal file
45
5.leetcode/src/com/fanxb/common/p100/Q26.java
Normal file
@ -0,0 +1,45 @@
|
||||
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));
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@ -24,7 +24,20 @@ 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) {
|
||||
System.out.println(new Q27().removeElement(new int[]{0, 1, 2, 2, 3, 0, 4, 2}, 2));
|
||||
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));
|
||||
}
|
||||
}
|
@ -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.*;
|
||||
|
||||
@ -39,7 +39,32 @@ 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().lengthOfLongestSubstring("bbbbbbbbbbbbbbbbb"));
|
||||
System.out.println(new Q3().lengthOfLongestSubstring1("abcabcbb"));
|
||||
}
|
||||
}
|
80
5.leetcode/src/com/fanxb/common/p100/Q33.java
Normal file
80
5.leetcode/src/com/fanxb/common/p100/Q33.java
Normal file
@ -0,0 +1,80 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
public class Q33 {
|
||||
|
||||
|
||||
public int search(int[] nums, int target) {
|
||||
int n = nums.length;
|
||||
if (n == 1) return nums[0] == target ? 0 : -1;
|
||||
int l = 0, r = n - 1;
|
||||
while (l < r) {
|
||||
boolean s = nums[l] < nums[r];
|
||||
int m = (l + r) / 2;
|
||||
if (nums[m] == target) {
|
||||
return m;
|
||||
} else {
|
||||
if (nums[m] >= nums[0]) {
|
||||
//说明m落在左边的升序
|
||||
if (nums[m] < target) {
|
||||
if (l == m) l++;
|
||||
else l = m;
|
||||
} else {
|
||||
if (!s && nums[l] > target) {
|
||||
//说明target在右边的升序里
|
||||
if (l == m) l++;
|
||||
else l = m;
|
||||
} else {
|
||||
r = m;
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
//m落在右边的升序
|
||||
if (nums[m] > target) {
|
||||
r = m;
|
||||
} else {
|
||||
if (s || nums[l] > target) {
|
||||
//说明target在右边的升序里
|
||||
if (l == m) l++;
|
||||
else l = m;
|
||||
} else {
|
||||
r = m;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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().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)));
|
||||
}
|
14
5.leetcode/src/com/fanxb/common/p100/Q35.java
Normal file
14
5.leetcode/src/com/fanxb/common/p100/Q35.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
56
5.leetcode/src/com/fanxb/common/p100/Q36.java
Normal file
56
5.leetcode/src/com/fanxb/common/p100/Q36.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Q36 {
|
||||
public boolean isValidSudoku(char[][] board) {
|
||||
boolean[] check = new boolean[10];
|
||||
char c;
|
||||
for (int i = 0; i < 9; i++) {
|
||||
//检查行
|
||||
for (int j = 0; j < 9; j++) {
|
||||
c = board[i][j];
|
||||
if (c != '.') {
|
||||
int num = c - '0';
|
||||
if (check[num]) return false;
|
||||
check[num] = true;
|
||||
}
|
||||
}
|
||||
Arrays.fill(check, false);
|
||||
//检查列
|
||||
for (int j = 0; j < 9; j++) {
|
||||
c = board[j][i];
|
||||
if (c != '.') {
|
||||
int num = c - '0';
|
||||
if (check[num]) return false;
|
||||
check[num] = true;
|
||||
}
|
||||
}
|
||||
Arrays.fill(check, false);
|
||||
//检查小9宫格
|
||||
int starti = 3 * (i / 3), startj = (i % 3) * 3;
|
||||
for (int x = 0; x < 3; x++) {
|
||||
for (int y = 0; y < 3; y++) {
|
||||
c = board[starti + x][startj + y];
|
||||
if (c != '.') {
|
||||
int num = c - '0';
|
||||
if (check[num]) return false;
|
||||
check[num] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Arrays.fill(check, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
char[][] chars =
|
||||
new char[][]{{'5', '3', '.', '.', '7', '.', '.', '.', '.'}, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
|
||||
System.out.println(new Q36().isValidSudoku(chars));
|
||||
}
|
||||
}
|
68
5.leetcode/src/com/fanxb/common/p100/Q39.java
Normal file
68
5.leetcode/src/com/fanxb/common/p100/Q39.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/10 15:10
|
||||
*/
|
||||
public class Q39 {
|
||||
public List<List<Integer>> combinationSum(int[] candidates, int target) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
Arrays.sort(candidates);
|
||||
dfs(candidates, target, 0, new Stack<>(), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void dfs(int[] candidates, int target, int index, Stack<Integer> temp, List<List<Integer>> res) {
|
||||
if (target == 0) {
|
||||
//说明找到一个结果序列
|
||||
res.add(new ArrayList<>(temp));
|
||||
return;
|
||||
}
|
||||
for (int i = index; i < candidates.length; i++) {
|
||||
if (candidates[i] > target) {
|
||||
//前面已经排序过,所以在这里可以进行剪枝操作,如果candidates[index]都小于target了,那就不需要比较后面的了,肯定不满足要求
|
||||
return;
|
||||
}
|
||||
temp.push(candidates[i]);
|
||||
dfs(candidates, target - candidates[i], i, temp, res);
|
||||
temp.pop();
|
||||
}
|
||||
}
|
||||
|
||||
private class NewSolution {
|
||||
private List<List<Integer>> res;
|
||||
private List<Integer> temp;
|
||||
private int sum;
|
||||
|
||||
public List<List<Integer>> combinationSum(int[] candidates, int target) {
|
||||
res = new ArrayList<>();
|
||||
temp = new ArrayList<>();
|
||||
sum = 0;
|
||||
dfs(candidates, target, 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void dfs(int[] candidates, int target, int cur) {
|
||||
if (sum > target) return;
|
||||
if (sum == target) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
return;
|
||||
}
|
||||
for (int i = cur; i < candidates.length; i++) {
|
||||
temp.add(candidates[i]);
|
||||
sum += candidates[i];
|
||||
dfs(candidates, target, i);
|
||||
sum -= candidates[i];
|
||||
temp.remove(temp.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Q39().combinationSum(new int[]{2, 3, 5}, 8).forEach(System.out::println);
|
||||
}
|
||||
}
|
@ -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}));
|
||||
}
|
||||
}
|
40
5.leetcode/src/com/fanxb/common/p100/Q40.java
Normal file
40
5.leetcode/src/com/fanxb/common/p100/Q40.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q40 {
|
||||
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
Arrays.sort(candidates);
|
||||
dfs(candidates, target, 0, new Stack<>(), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void dfs(int[] candidates, int target, int index, Stack<Integer> temp, List<List<Integer>> res) {
|
||||
if (target == 0) {
|
||||
//说明找到一个结果序列
|
||||
res.add(new ArrayList<>(temp));
|
||||
return;
|
||||
}
|
||||
for (int i = index; i < candidates.length; ) {
|
||||
if (candidates[i] > target) {
|
||||
//前面已经排序过,所以在这里可以进行剪枝操作,如果candidates[index]都小于target了,那就不需要比较后面的了,肯定不满足要求
|
||||
return;
|
||||
}
|
||||
temp.push(candidates[i]);
|
||||
dfs(candidates, target - candidates[i], i + 1, temp, res);
|
||||
temp.pop();
|
||||
//手动控制i的增长,对于同一个数字不能重复处理
|
||||
int nextI = i + 1;
|
||||
while (nextI < candidates.length && candidates[nextI] == candidates[i]) {
|
||||
nextI++;
|
||||
}
|
||||
i = nextI;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Q40().combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8).forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
||||
@ -67,9 +65,31 @@ 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}));
|
||||
}
|
||||
}
|
47
5.leetcode/src/com/fanxb/common/p100/Q45.java
Normal file
47
5.leetcode/src/com/fanxb/common/p100/Q45.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* @author fanxb
|
||||
* @date 2021-11-03-下午3:12
|
||||
*/
|
||||
public class Q45 {
|
||||
|
||||
public int jump(int[] nums) {
|
||||
int maxIndex = 0;//下一跳能到达的最远边界
|
||||
int end = 0; //一次跳跃的边界
|
||||
int step = 0;//使用的部署
|
||||
//只需走到倒数第二个节点即可
|
||||
for (int i = 0; i < nums.length-1; i++) {
|
||||
//look for next step's max value
|
||||
maxIndex = Math.max(maxIndex, i+nums[i]);
|
||||
//走到一步的边界了,已经找到下一跳的起点
|
||||
if(i==end){
|
||||
step++;
|
||||
end = maxIndex;
|
||||
}
|
||||
}
|
||||
return step;
|
||||
}
|
||||
|
||||
public int solve(int[] nums,int i,int[] map){
|
||||
if(i>nums.length-1) return 0;
|
||||
if(map[i]>0) return map[i];
|
||||
int minStep = 100000,step;
|
||||
int v = nums[i];
|
||||
for(int j=0;j<v;j++){
|
||||
step = solve(nums,i+j+1,map);
|
||||
minStep = Math.min(minStep,step+1);
|
||||
}
|
||||
map[i] = minStep;
|
||||
return minStep;
|
||||
}
|
||||
|
||||
public int jump1(int[] nums){
|
||||
int[] map = new int[nums.length];
|
||||
return solve(nums,0,map);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
81
5.leetcode/src/com/fanxb/common/p100/Q46.java
Normal file
81
5.leetcode/src/com/fanxb/common/p100/Q46.java
Normal file
@ -0,0 +1,81 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q46 {
|
||||
|
||||
public List<List<Integer>> permute(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
dfs(nums, new Stack<>(), new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param used 记录已选择数字的位置
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, Stack<Integer> temp, boolean[] used, List<List<Integer>> res) {
|
||||
if (temp.size() == nums.length) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
}
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (!used[i]) {
|
||||
temp.push(nums[i]);
|
||||
used[i] = true;
|
||||
dfs(nums, temp, used, res);
|
||||
used[i] = false;
|
||||
temp.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class NewSolution {
|
||||
private List<List<Integer>> res;
|
||||
private List<Integer> temp;
|
||||
private boolean[] cache;
|
||||
|
||||
public List<List<Integer>> permute(int[] nums) {
|
||||
res = new LinkedList<>();
|
||||
temp = new ArrayList<>(nums.length);
|
||||
cache = new boolean[nums.length];
|
||||
dfs(nums, nums.length);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void dfs(int[] nums, int length) {
|
||||
if (temp.size() == nums.length) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (!cache[i]) {
|
||||
temp.add(nums[i]);
|
||||
cache[i] = true;
|
||||
dfs(nums, length);
|
||||
cache[i] = false;
|
||||
temp.remove(temp.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q46().permute(new int[]{1, 1, 2}));
|
||||
}
|
||||
}
|
58
5.leetcode/src/com/fanxb/common/p100/Q47.java
Normal file
58
5.leetcode/src/com/fanxb/common/p100/Q47.java
Normal file
@ -0,0 +1,58 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q47 {
|
||||
|
||||
public List<List<Integer>> permuteUnique(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
Arrays.sort(nums);
|
||||
dfs(nums, new Stack<>(), new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param used 记录已选择数字的位置
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, Stack<Integer> temp, boolean[] used, List<List<Integer>> res) {
|
||||
if (temp.size() == nums.length) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
}
|
||||
for (int i = 0; i < nums.length; ) {
|
||||
if (!used[i]) {
|
||||
temp.push(nums[i]);
|
||||
used[i] = true;
|
||||
dfs(nums, temp, used, res);
|
||||
used[i] = false;
|
||||
temp.pop();
|
||||
int tempI = i;
|
||||
while (i < nums.length && nums[tempI] == nums[i]) {
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q47().permuteUnique(new int[]{1, 1, 2}));
|
||||
}
|
||||
}
|
32
5.leetcode/src/com/fanxb/common/p100/Q48.java
Normal file
32
5.leetcode/src/com/fanxb/common/p100/Q48.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class Q48 {
|
||||
public void rotate(int[][] matrix) {
|
||||
int length = matrix.length;
|
||||
for (int i = 0; i < length / 2; i++) {
|
||||
int length1 = length - i * 2;
|
||||
for (int j = 0; j < length1-1; j++) {
|
||||
int temp = matrix[i + j][i + length1 - 1];
|
||||
matrix[i + j][i + length1 - 1] = matrix[i][i + j];
|
||||
int temp1 = matrix[i + length1 - 1][i + length1 - 1 - j];
|
||||
matrix[i + length1 - 1][i + length1 - 1 - j] = temp;
|
||||
temp = matrix[i+length1-1-j][i];
|
||||
matrix[i+length1-1-j][i] = temp1;
|
||||
matrix[i][i + j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] arr = new int[][]{{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}};
|
||||
Q48 ins = new Q48();
|
||||
ins.rotate(arr);
|
||||
for (int[] temp : arr) {
|
||||
System.out.println(Arrays.toString(temp));
|
||||
}
|
||||
}
|
||||
}
|
25
5.leetcode/src/com/fanxb/common/p100/Q49.java
Normal file
25
5.leetcode/src/com/fanxb/common/p100/Q49.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q49 {
|
||||
|
||||
public List<List<String>> groupAnagrams(String[] strs) {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
for (String str : strs) {
|
||||
char[] chars = str.toCharArray();
|
||||
Arrays.sort(chars);
|
||||
map.computeIfAbsent(new String(chars), k -> new LinkedList<>()).add(str);
|
||||
}
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* 定义dp[i][j]表示从i到j的字符串是否为回文串
|
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;
|
||||
}
|
||||
}
|
40
5.leetcode/src/com/fanxb/common/p100/Q52.java
Normal file
40
5.leetcode/src/com/fanxb/common/p100/Q52.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Q52 {
|
||||
private List<Integer[]> state;
|
||||
//记录列上的皇后
|
||||
private boolean[] col;
|
||||
private int res;
|
||||
|
||||
|
||||
public int totalNQueens(int n) {
|
||||
state = new ArrayList<>(n);
|
||||
col = new boolean[n];
|
||||
res = 0;
|
||||
dfs(n, 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void dfs(int n, int cur) {
|
||||
if (state.size() == n) {
|
||||
res++;
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
//当前行或者当前列已经放了,那么跳过
|
||||
if (col[i]) continue;
|
||||
//判断斜线上有没有存在的皇后,判断方法:主对角线行减列的差值相等,次对角线的行加列值相等
|
||||
int val1 = cur - i, val2 = cur + i;
|
||||
if (state.stream().anyMatch(item -> item[0] - item[1] == val1 || item[0] + item[1] == val2)) continue;
|
||||
state.add(new Integer[]{cur, i});
|
||||
col[i] = true;
|
||||
//往下一行放
|
||||
dfs(n, cur + 1);
|
||||
state.remove(state.size() - 1);
|
||||
col[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
20
5.leetcode/src/com/fanxb/common/p100/Q53.java
Normal file
20
5.leetcode/src/com/fanxb/common/p100/Q53.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
61
5.leetcode/src/com/fanxb/common/p100/Q54.java
Normal file
61
5.leetcode/src/com/fanxb/common/p100/Q54.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q54 {
|
||||
public List<Integer> spiralOrder(int[][] matrix) {
|
||||
List<Integer> list = new LinkedList<>();
|
||||
list.add(matrix[0][0]);
|
||||
int i = 0, j = 0, count = 1, m = matrix.length, n = matrix[0].length;
|
||||
int type = 0;
|
||||
matrix[0][0] = -200;
|
||||
while (count < m * n) {
|
||||
switch (type) {
|
||||
//向右走
|
||||
case 0:
|
||||
if (j + 1 >= n || matrix[i][j + 1] == -200) {
|
||||
type = 1;
|
||||
continue;
|
||||
} else j++;
|
||||
break;
|
||||
//向下
|
||||
case 1:
|
||||
if (i + 1 >= m || matrix[i + 1][j] == -200) {
|
||||
type = 2;
|
||||
continue;
|
||||
} else i++;
|
||||
break;
|
||||
//向左
|
||||
case 2:
|
||||
if (j - 1 == -1 || matrix[i][j - 1] == -200) {
|
||||
type = 3;
|
||||
continue;
|
||||
} else j--;
|
||||
break;
|
||||
//向上
|
||||
case 3:
|
||||
if (i - 1 == -1 || matrix[i - 1][j] == -200) {
|
||||
type = 0;
|
||||
continue;
|
||||
} else i--;
|
||||
break;
|
||||
}
|
||||
list.add(matrix[i][j]);
|
||||
matrix[i][j] = -200;
|
||||
count++;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.printf(new Q54().spiralOrder(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}).toString());
|
||||
}
|
||||
}
|
31
5.leetcode/src/com/fanxb/common/p100/Q55.java
Normal file
31
5.leetcode/src/com/fanxb/common/p100/Q55.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q55 {
|
||||
|
||||
public boolean canJump(int[] nums) {
|
||||
int n = nums.length;
|
||||
if (n == 1) {
|
||||
return true;
|
||||
}
|
||||
int val = nums[0];
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (val < i) {
|
||||
//说明走不到这一步,不用继续了
|
||||
return false;
|
||||
}
|
||||
val = Math.max(val, i + nums[i]);
|
||||
}
|
||||
return val >= n - 1;
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q55().canJump(new int[]{1, 0, 1, 0}));
|
||||
}
|
||||
}
|
25
5.leetcode/src/com/fanxb/common/p100/Q56.java
Normal file
25
5.leetcode/src/com/fanxb/common/p100/Q56.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Q56 {
|
||||
public int[][] merge(int[][] intervals) {
|
||||
Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
|
||||
int length = intervals.length, count = 0;
|
||||
int[][] res = new int[intervals.length][2];
|
||||
int[] startArr = intervals[0];
|
||||
for (int i = 1; i < length; i++) {
|
||||
int[] temp = intervals[i];
|
||||
if (temp[0]<=startArr[1]) {
|
||||
//可以开始合并
|
||||
startArr[1] = Math.max(startArr[1], temp[1]);
|
||||
} else {
|
||||
res[count++] = startArr;
|
||||
startArr = temp;
|
||||
}
|
||||
}
|
||||
res[count++] = startArr;
|
||||
return Arrays.copyOf(res, count);
|
||||
}
|
||||
}
|
61
5.leetcode/src/com/fanxb/common/p100/Q57.java
Normal file
61
5.leetcode/src/com/fanxb/common/p100/Q57.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q57 {
|
||||
public int[][] insert(int[][] intervals, int[] newInterval) {
|
||||
int length = intervals.length, i = 0, count = 0;
|
||||
int[][] res = new int[length + 1][2];
|
||||
if (intervals.length == 0) return new int[][]{newInterval};
|
||||
if (newInterval[1] < intervals[0][0]) {
|
||||
//说明放到最前面
|
||||
res[count++] = newInterval;
|
||||
for (int[] num : intervals) res[count++] = num;
|
||||
return res;
|
||||
}
|
||||
if (newInterval[0] > intervals[length - 1][1]) {
|
||||
//说明放到最后
|
||||
for (int[] num : intervals) res[count++] = num;
|
||||
res[count++] = newInterval;
|
||||
return res;
|
||||
}
|
||||
for (; i < length; i++) {
|
||||
//开始找开始插入的位置
|
||||
if (intervals[i][1] >= newInterval[0]) {
|
||||
if (newInterval[1] < intervals[i][0]) {
|
||||
//说明无交叉,新的放到i前面
|
||||
res[count++] = newInterval;
|
||||
for (; i < length; i++) res[count++] = intervals[i];
|
||||
return res;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
res[count++] = intervals[i];
|
||||
}
|
||||
}
|
||||
//有交叉,从i开始进行合并
|
||||
int[] start = new int[]{Math.min(intervals[i][0], newInterval[0]), Math.max(intervals[i][1], newInterval[1])};
|
||||
i++;
|
||||
for (; i < length; i++) {
|
||||
int[] temp = intervals[i];
|
||||
if (temp[0] <= start[1]) {
|
||||
//说明存在重叠,进行合并
|
||||
start[1] = Math.max(temp[1], start[1]);
|
||||
} else {
|
||||
//说明不存在重叠了,
|
||||
res[count++] = start;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == length) {
|
||||
//说明循环完了
|
||||
res[count++] = start;
|
||||
} else {
|
||||
//说明还有一段没合并完
|
||||
for (; i < length; i++) {
|
||||
res[count++] = intervals[i];
|
||||
}
|
||||
}
|
||||
return Arrays.copyOf(res, count);
|
||||
}
|
||||
}
|
43
5.leetcode/src/com/fanxb/common/p100/Q58.java
Normal file
43
5.leetcode/src/com/fanxb/common/p100/Q58.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q58 {
|
||||
|
||||
public int lengthOfLastWord(String s) {
|
||||
int count = 0, length = s.length();
|
||||
boolean lastCharBlank = false;
|
||||
for (int i = 0; i < length; i++) {
|
||||
char c = s.charAt(i);
|
||||
if (c == ' ') {
|
||||
lastCharBlank = true;
|
||||
} else {
|
||||
if (lastCharBlank) count = 0;
|
||||
count++;
|
||||
lastCharBlank = false;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public int lengthOfLastWord1(String s) {
|
||||
int length = s.length(), i;
|
||||
// find last word's first char index
|
||||
for (i = length - 1; i >= 0; i--) {
|
||||
if (s.charAt(i) != ' ') break;
|
||||
}
|
||||
// cal last word count
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
if (s.charAt(j) == ' ') return i - j;
|
||||
}
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q58().lengthOfLastWord(" asdfasdfasdf"));
|
||||
}
|
||||
}
|
80
5.leetcode/src/com/fanxb/common/p100/Q6.java
Normal file
80
5.leetcode/src/com/fanxb/common/p100/Q6.java
Normal file
@ -0,0 +1,80 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
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 String convert1(String s, int numRows) {
|
||||
int length = s.length();
|
||||
if (length <= 1 || numRows == 1) return s;
|
||||
int nSize = numRows * 2 - 2;
|
||||
int lineNum = (numRows - 1) * (length / nSize + 1);
|
||||
char[][] chars = new char[numRows][lineNum];
|
||||
for (int i = 0; i < numRows; i++) Arrays.fill(chars[i], ' ');
|
||||
int count = 0, m = 0, n = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
chars[m][n] = s.charAt(i);
|
||||
count++;
|
||||
if (count == nSize) {
|
||||
count = 0;
|
||||
m--;
|
||||
n++;
|
||||
} else if (count < numRows) {
|
||||
m++;
|
||||
} else {
|
||||
m--;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < numRows; i++) {
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
if (chars[i][j] != ' ') {
|
||||
builder.append(chars[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q6().convert1("PAYPALISHIRING", 4));
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* 旋转链表
|
29
5.leetcode/src/com/fanxb/common/p100/Q62.java
Normal file
29
5.leetcode/src/com/fanxb/common/p100/Q62.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/11 15:28
|
||||
*/
|
||||
public class Q62 {
|
||||
public int uniquePaths(int m, int n) {
|
||||
int[][] f = new int[m][n];
|
||||
Arrays.fill(f[0], 1);
|
||||
for (int i = 0; i < m; i++) {
|
||||
f[i][0] = 1;
|
||||
}
|
||||
for (int i = 1; i < m; i++) {
|
||||
for (int j = 1; j < n; j++) {
|
||||
f[i][j] = f[i - 1][j] + f[i][j - 1];
|
||||
}
|
||||
}
|
||||
return f[m - 1][n - 1];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q62().uniquePaths(3, 7));
|
||||
}
|
||||
}
|
38
5.leetcode/src/com/fanxb/common/p100/Q63.java
Normal file
38
5.leetcode/src/com/fanxb/common/p100/Q63.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/11 15:28
|
||||
*/
|
||||
public class Q63 {
|
||||
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
|
||||
int m = obstacleGrid.length, n = obstacleGrid[0].length;
|
||||
int[][] f = new int[m][n];
|
||||
for (int i = 0; i < m; i++) {
|
||||
if (obstacleGrid[i][0] == 1) {
|
||||
break;
|
||||
}
|
||||
f[i][0] = 1;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (obstacleGrid[0][i] == 1) {
|
||||
break;
|
||||
}
|
||||
f[0][i] = 1;
|
||||
}
|
||||
for (int i = 1; i < m; i++) {
|
||||
for (int j = 1; j < n; j++) {
|
||||
if (obstacleGrid[i][j] == 0) {
|
||||
f[i][j] = f[i - 1][j] + f[i][j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return f[m - 1][n - 1];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q63().uniquePathsWithObstacles(new int[][]{{0, 1}, {0, 0}}));
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
22
5.leetcode/src/com/fanxb/common/p100/Q66.java
Normal file
22
5.leetcode/src/com/fanxb/common/p100/Q66.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q66 {
|
||||
public int[] plusOne(int[] digits) {
|
||||
if (digits[0] == 0) return new int[]{1};
|
||||
int n = digits.length;
|
||||
digits[n - 1]++;
|
||||
boolean hasNext = false;
|
||||
for (int i = digits.length - 1; i >= 0; i--) {
|
||||
digits[i] += hasNext ? 1 : 0;
|
||||
hasNext = digits[i] >= 10;
|
||||
if (hasNext) digits[i] = digits[i] - 10;
|
||||
else return digits;
|
||||
}
|
||||
int[] res = new int[digits.length + 1];
|
||||
res[0] = 1;
|
||||
System.arraycopy(digits, 0, res, 1, res.length - 1);
|
||||
return res;
|
||||
}
|
||||
}
|
18
5.leetcode/src/com/fanxb/common/p100/Q67.java
Normal file
18
5.leetcode/src/com/fanxb/common/p100/Q67.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
36
5.leetcode/src/com/fanxb/common/p100/Q68.java
Normal file
36
5.leetcode/src/com/fanxb/common/p100/Q68.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/11 9:56
|
||||
*/
|
||||
public class Q68 {
|
||||
public int searchInsert(int[] nums, int target) {
|
||||
int l = 0, r = nums.length - 1;
|
||||
while (l < r) {
|
||||
int m = (l + r) / 2;
|
||||
if (nums[m] > target) {
|
||||
if (r == m) {
|
||||
l++;
|
||||
} else {
|
||||
r = m;
|
||||
}
|
||||
} else if (nums[m] == target) {
|
||||
return m;
|
||||
} else {
|
||||
if (l == m) {
|
||||
l++;
|
||||
} else {
|
||||
l = m;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nums[l] < target ? l + 1 : l;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q68().searchInsert(new int[]{1, 3, 5, 6}, 5));
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
@ -12,8 +12,6 @@ package com.fanxb.common;
|
||||
* 4. 否则判断temp1>x,如果满足r=mid,重复2
|
||||
* 5. 否则l=mid,重复2
|
||||
*
|
||||
*
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/11 9:56
|
||||
*/
|
||||
@ -39,7 +37,50 @@ 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().mySqrt(2147395599));
|
||||
System.out.println(new Q69().mySqrt1(2147395599));
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
public class Q7 {
|
||||
public int reverse(int x) {
|
38
5.leetcode/src/com/fanxb/common/p100/Q70.java
Normal file
38
5.leetcode/src/com/fanxb/common/p100/Q70.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* 定义f(n)表示爬n级台阶的方法
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/11 15:55
|
||||
*/
|
||||
public class Q70 {
|
||||
public int climbStairs(int n) {
|
||||
if (n <= 2) {
|
||||
return n;
|
||||
}
|
||||
int last1 = 1, last2 = 2, res = 2;
|
||||
for (int i = 3; i <= n; i++) {
|
||||
res = last1 + last2;
|
||||
last1 = last2;
|
||||
last2 = res;
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
25
5.leetcode/src/com/fanxb/common/p100/Q71.java
Normal file
25
5.leetcode/src/com/fanxb/common/p100/Q71.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Q71 {
|
||||
public String simplifyPath(String path) {
|
||||
Stack<String> stack = new Stack<>();
|
||||
List<String> strs = Stream.of(path.split("/+")).filter(item -> !item.isEmpty()).collect(Collectors.toList());
|
||||
for (String str : strs) {
|
||||
switch (str) {
|
||||
case ".":
|
||||
break;
|
||||
case "..":
|
||||
if (!stack.isEmpty()) stack.pop();
|
||||
break;
|
||||
default:
|
||||
stack.push(str);
|
||||
}
|
||||
}
|
||||
return stack.isEmpty() ? "/" : stack.stream().map(item -> "/" + item).collect(Collectors.joining());
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* 编辑距离,可以对任意一个单词增加一个字母;删除一个字母;替换一个字母;
|
26
5.leetcode/src/com/fanxb/common/p100/Q73.java
Normal file
26
5.leetcode/src/com/fanxb/common/p100/Q73.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
|
||||
public class Q73 {
|
||||
|
||||
public void setZeroes(int[][] matrix) {
|
||||
int m = matrix.length, n = matrix[0].length;
|
||||
//行是否有0
|
||||
int[] cache1 = new int[m];
|
||||
//列是否有0
|
||||
int[] cache2 = new int[n];
|
||||
for (int i = 0; i < m; i++)
|
||||
for (int j = 0; j < n; j++)
|
||||
if (matrix[i][j] == 0) {
|
||||
cache1[i] = 1;
|
||||
cache2[j] = 1;
|
||||
}
|
||||
for (int i = 0; i < m; i++)
|
||||
for (int j = 0; j < n; j++)
|
||||
if (cache1[i] == 1 || cache2[j] == 1)
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
||||
* 最小覆盖子串
|
||||
@ -51,6 +48,36 @@ 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"));
|
31
5.leetcode/src/com/fanxb/common/p100/Q77.java
Normal file
31
5.leetcode/src/com/fanxb/common/p100/Q77.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Q77 {
|
||||
public List<List<Integer>> combine(int n, int k) {
|
||||
List<List<Integer>> ans = new LinkedList<>();
|
||||
List<Integer> temp = new ArrayList<>(k);
|
||||
dfs(ans, temp, n, k, 1);
|
||||
return ans;
|
||||
}
|
||||
|
||||
private void dfs(List<List<Integer>> ans, List<Integer> temp, int n, int k, int cur) {
|
||||
if (temp.size() == k) {
|
||||
ans.add(new ArrayList<>(temp));
|
||||
return;
|
||||
}
|
||||
for (int i = cur; i <= n; i++) {
|
||||
temp.add(i);
|
||||
dfs(ans, temp, n, k, i + 1);
|
||||
temp.remove(temp.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Q77().combine(4, 2);
|
||||
}
|
||||
|
||||
}
|
42
5.leetcode/src/com/fanxb/common/p100/Q78.java
Normal file
42
5.leetcode/src/com/fanxb/common/p100/Q78.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q78 {
|
||||
|
||||
public List<List<Integer>> subsets(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
dfs(nums, 0, new Stack<>(), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, int index, Stack<Integer> temp, List<List<Integer>> res) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
for (int i = index; i < nums.length; i++) {
|
||||
temp.push(nums[i]);
|
||||
dfs(nums, i + 1, temp, res);
|
||||
temp.pop();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q78().subsets(new int[]{1}));
|
||||
}
|
||||
}
|
29
5.leetcode/src/com/fanxb/common/p100/Q79.java
Normal file
29
5.leetcode/src/com/fanxb/common/p100/Q79.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
public class Q79 {
|
||||
private int[][] step = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
|
||||
|
||||
public boolean exist(char[][] board, String word) {
|
||||
char[] chars = word.toCharArray();
|
||||
int m = board.length, n = board[0].length;
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (dfs(board, chars, 0, i, j, m, n)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean dfs(char[][] board, char[] word, int k, int x, int y, int m, int n) {
|
||||
if (word[k] != board[x][y]) return false;
|
||||
if (k == word.length - 1) return true;
|
||||
board[x][y] = ' ';
|
||||
for (int[] item : step) {
|
||||
int x1 = x + item[0], y1 = y + item[1];
|
||||
if (x1 < 0 || x1 >= m || y1 < 0 || y1 >= n || board[x1][y1] == ' ') continue;
|
||||
if (dfs(board, word, k + 1, x1, y1, m, n)) return true;
|
||||
}
|
||||
board[x][y] = word[k];
|
||||
return false;
|
||||
}
|
||||
}
|
53
5.leetcode/src/com/fanxb/common/p100/Q80.java
Normal file
53
5.leetcode/src/com/fanxb/common/p100/Q80.java
Normal file
@ -0,0 +1,53 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q80 {
|
||||
|
||||
public int removeDuplicates(int[] nums) {
|
||||
if (nums.length <= 2) {
|
||||
return nums.length;
|
||||
}
|
||||
//当前元素重复出现的次数
|
||||
int count = 0;
|
||||
//保留的元素下标
|
||||
int index = 0;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (nums[i] == nums[i - 1]) {
|
||||
count++;
|
||||
if (count < 2) {
|
||||
nums[++index] = nums[i];
|
||||
}
|
||||
} else {
|
||||
count = 0;
|
||||
nums[++index] = nums[i];
|
||||
}
|
||||
}
|
||||
return index + 1;
|
||||
}
|
||||
|
||||
public int better(int[] nums) {
|
||||
if (nums.length <= 2) {
|
||||
return nums.length;
|
||||
}
|
||||
int slow = 2, fast = 2;
|
||||
int n = nums.length;
|
||||
while (fast < n) {
|
||||
if (nums[slow - 2] != nums[fast]) {
|
||||
nums[slow++] = nums[fast];
|
||||
}
|
||||
fast++;
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3};
|
||||
System.out.println(new Q80().better(nums));
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
47
5.leetcode/src/com/fanxb/common/p100/Q82.java
Normal file
47
5.leetcode/src/com/fanxb/common/p100/Q82.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import com.fanxb.common.ListNode;
|
||||
|
||||
public class Q82 {
|
||||
public com.fanxb.common.ListNode deleteDuplicates(com.fanxb.common.ListNode head) {
|
||||
if (head == null || head.next == null) return head;
|
||||
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) {
|
||||
if (startBefore == null)
|
||||
startBefore = last;
|
||||
last = head;
|
||||
} else if (startBefore != null) {
|
||||
last = startBefore;
|
||||
startBefore.next = head.next;
|
||||
startBefore = null;
|
||||
} else {
|
||||
last = head;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
if (startBefore != null) startBefore.next = null;
|
||||
return res.next;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (cur.val == cur.next.val) {
|
||||
int x = cur.next.val;
|
||||
while (cur != null && cur.val == x) {
|
||||
last.next = cur.next;
|
||||
cur = cur.next;
|
||||
}
|
||||
} else {
|
||||
last = cur;
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
return res.next;
|
||||
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
25
5.leetcode/src/com/fanxb/common/p100/Q86.java
Normal file
25
5.leetcode/src/com/fanxb/common/p100/Q86.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import com.fanxb.common.ListNode;
|
||||
|
||||
public class Q86 {
|
||||
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;
|
||||
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) {
|
||||
bigCur.next = cur;
|
||||
bigCur = cur;
|
||||
pre.next = cur.next;
|
||||
} else {
|
||||
pre = cur;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
bigCur.next = null;
|
||||
pre.next = bigList.next;
|
||||
return res.next;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 合并两个有序数组
|
||||
@ -31,9 +30,23 @@ 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[12];
|
||||
int[] nums1 = new int[]{1,2,3};
|
||||
int[] nums2 = new int[]{};
|
||||
new Q88().merge(nums1,3,nums2,0);
|
||||
System.out.println(Arrays.toString(nums1));
|
||||
}
|
||||
}
|
37
5.leetcode/src/com/fanxb/common/p100/Q9.java
Normal file
37
5.leetcode/src/com/fanxb/common/p100/Q9.java
Normal file
@ -0,0 +1,37 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/10 10:49
|
||||
*/
|
||||
public class Q9 {
|
||||
|
||||
public boolean isPalindrome(int x) {
|
||||
if (x < 0) return false;
|
||||
String s = String.valueOf(x);
|
||||
int i = 0, j = s.length() - 1;
|
||||
while (i < j) {
|
||||
if (s.charAt(i++) != s.charAt(j--)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isPalindrome1(int x) {
|
||||
if (x < 0) return false;
|
||||
int temp = x, rev = 0;
|
||||
while (temp > 0) {
|
||||
rev = rev * 10 + temp % 10;
|
||||
temp = temp / 10;
|
||||
}
|
||||
return x == rev;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
48
5.leetcode/src/com/fanxb/common/p100/Q90.java
Normal file
48
5.leetcode/src/com/fanxb/common/p100/Q90.java
Normal file
@ -0,0 +1,48 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q90 {
|
||||
|
||||
public List<List<Integer>> subsetsWithDup(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
Arrays.sort(nums);
|
||||
dfs(nums, 0, new Stack<>(), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* dfs搜索
|
||||
*
|
||||
* @param nums 源数组
|
||||
* @param temp 记录已选择的数字
|
||||
* @param res 保存结果
|
||||
* @author fanxb
|
||||
* date 2022/3/11 14:20
|
||||
*/
|
||||
public void dfs(int[] nums, int index, Stack<Integer> temp, List<List<Integer>> res) {
|
||||
res.add(new ArrayList<>(temp));
|
||||
for (int i = index; i < nums.length; ) {
|
||||
temp.push(nums[i]);
|
||||
dfs(nums, i + 1, temp, res);
|
||||
temp.pop();
|
||||
int tempI = i;
|
||||
while (i < nums.length && nums[i] == nums[tempI]) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q90().subsetsWithDup(new int[]{1, 2, 2}));
|
||||
}
|
||||
}
|
62
5.leetcode/src/com/fanxb/common/p100/Q92.java
Normal file
62
5.leetcode/src/com/fanxb/common/p100/Q92.java
Normal file
@ -0,0 +1,62 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q92 {
|
||||
|
||||
public class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
ListNode() {
|
||||
}
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
ListNode(int val, ListNode next) {
|
||||
this.val = val;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
public ListNode reverseBetween(ListNode head, int left, int right) {
|
||||
if (left == right) {
|
||||
return head;
|
||||
}
|
||||
//node1 left位置前一个节点,node2 right位置后一个指针
|
||||
ListNode node1 = null, leftNode = null, node2 = null, rightNode = null;
|
||||
ListNode last = null, temp = head, next = null;
|
||||
int count = 0;
|
||||
while (temp != null) {
|
||||
count++;
|
||||
next = temp.next;
|
||||
if (left == count) {
|
||||
node1 = last;
|
||||
leftNode = temp;
|
||||
}
|
||||
if (right == count) {
|
||||
node2 = next;
|
||||
rightNode = temp;
|
||||
}
|
||||
if (count > left && count <= right) {
|
||||
temp.next = last;
|
||||
}
|
||||
|
||||
last = temp;
|
||||
temp = next;
|
||||
}
|
||||
leftNode.next = node2;
|
||||
if (node1 != null) {
|
||||
node1.next = rightNode;
|
||||
} else {
|
||||
head = rightNode;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
27
5.leetcode/src/com/fanxb/common/p100/Q94.java
Normal file
27
5.leetcode/src/com/fanxb/common/p100/Q94.java
Normal file
@ -0,0 +1,27 @@
|
||||
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(com.fanxb.common.TreeNode root) {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
if (root == null) return res;
|
||||
Stack<com.fanxb.common.TreeNode> stack = new Stack<>();
|
||||
stack.push(root);
|
||||
com.fanxb.common.TreeNode cur = root.left;
|
||||
while (!stack.isEmpty() || cur != null) {
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
TreeNode node = stack.pop();
|
||||
res.add(node.val);
|
||||
cur = node.right;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
22
5.leetcode/src/com/fanxb/common/p100/Q98.java
Normal file
22
5.leetcode/src/com/fanxb/common/p100/Q98.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import com.fanxb.common.TreeNode;
|
||||
|
||||
public class Q98 {
|
||||
private Integer lastVal = null;
|
||||
|
||||
public boolean isValidBST(com.fanxb.common.TreeNode root) {
|
||||
return dfs(root);
|
||||
}
|
||||
|
||||
private boolean dfs(TreeNode root) {
|
||||
if (root == null) return true;
|
||||
boolean left = dfs(root.left);
|
||||
if (lastVal != null && lastVal >= root.val) {
|
||||
return false;
|
||||
}
|
||||
lastVal = root.val;
|
||||
boolean right = dfs(root.right);
|
||||
return left && right;
|
||||
}
|
||||
}
|
23
5.leetcode/src/com/fanxb/common/p1000/Q909.java
Normal file
23
5.leetcode/src/com/fanxb/common/p1000/Q909.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.fanxb.common.p1000;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Q909 {
|
||||
//记录走过的格子
|
||||
private int[][] cache;
|
||||
private int res = Integer.MAX_VALUE;
|
||||
|
||||
public int snakesAndLadders(int[][] board) {
|
||||
int n = board.length;
|
||||
Queue<Integer[]> queue = new LinkedList<>();
|
||||
queue.add(new Integer[]{n - 1, 0});
|
||||
// int count
|
||||
while (!queue.isEmpty()) {
|
||||
Integer[] node = queue.poll();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
15
5.leetcode/src/com/fanxb/common/p1000/Q918.java
Normal file
15
5.leetcode/src/com/fanxb/common/p1000/Q918.java
Normal file
@ -0,0 +1,15 @@
|
||||
package com.fanxb.common.p1000;
|
||||
|
||||
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,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]);
|
||||
}
|
||||
}
|
33
5.leetcode/src/com/fanxb/common/p200/Q100.java
Normal file
33
5.leetcode/src/com/fanxb/common/p200/Q100.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
public class Q100 {
|
||||
public static class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode() {
|
||||
}
|
||||
|
||||
TreeNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSameTree(TreeNode p, TreeNode q) {
|
||||
if (p == null && q == null) return true;
|
||||
if (p != null && q != null) {
|
||||
if (p.val != q.val) return false;
|
||||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
34
5.leetcode/src/com/fanxb/common/p200/Q101.java
Normal file
34
5.leetcode/src/com/fanxb/common/p200/Q101.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
public class Q101 {
|
||||
public static class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode() {
|
||||
}
|
||||
|
||||
TreeNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSymmetric(TreeNode root) {
|
||||
return isSame(root.left, root.right);
|
||||
}
|
||||
|
||||
public boolean isSame(TreeNode left, TreeNode right) {
|
||||
if (left == null && right == null) return true;
|
||||
if (left != null && right != null) {
|
||||
if (left.val == right.val) return isSame(left.left, right.right) && isSame(left.right, right.left);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
29
5.leetcode/src/com/fanxb/common/p200/Q102.java
Normal file
29
5.leetcode/src/com/fanxb/common/p200/Q102.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import com.fanxb.common.TreeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Q102 {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if (root == null) return res;
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
List<Integer> temp = new ArrayList<>(size);
|
||||
while (size-- > 0) {
|
||||
TreeNode node = queue.poll();
|
||||
temp.add(node.val);
|
||||
if (node.left != null) queue.add(node.left);
|
||||
if (node.right != null) queue.add(node.right);
|
||||
}
|
||||
res.add(temp);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
29
5.leetcode/src/com/fanxb/common/p200/Q103.java
Normal file
29
5.leetcode/src/com/fanxb/common/p200/Q103.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import com.fanxb.common.TreeNode;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q103 {
|
||||
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if (root == null) return res;
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
boolean leftToRight = true;
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
List<Integer> temp = new ArrayList<>(size);
|
||||
while (size-- > 0) {
|
||||
TreeNode node = queue.poll();
|
||||
temp.add(node.val);
|
||||
if (node.left != null) queue.add(node.left);
|
||||
if (node.right != null) queue.add(node.right);
|
||||
}
|
||||
if (!leftToRight) Collections.reverse(temp);
|
||||
res.add(temp);
|
||||
leftToRight = !leftToRight;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
34
5.leetcode/src/com/fanxb/common/p200/Q104.java
Normal file
34
5.leetcode/src/com/fanxb/common/p200/Q104.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
public class Q104 {
|
||||
public static class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode() {
|
||||
}
|
||||
|
||||
TreeNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
|
||||
public int maxDepth(TreeNode root) {
|
||||
if (root == null) return 0;
|
||||
return Math.max(maxDepth(root.left, 2), maxDepth(root.right, 2));
|
||||
}
|
||||
|
||||
public int maxDepth(TreeNode root, int depth) {
|
||||
if (root == null) {
|
||||
return depth - 1;
|
||||
}
|
||||
return Math.max(maxDepth(root.left, depth + 1), maxDepth(root.right, depth + 1));
|
||||
}
|
||||
}
|
25
5.leetcode/src/com/fanxb/common/p200/Q105.java
Normal file
25
5.leetcode/src/com/fanxb/common/p200/Q105.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import com.fanxb.common.TreeNode;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Q105 {
|
||||
public TreeNode buildTree(int[] preorder, int[] inorder) {
|
||||
Map<Integer, Integer> map = new HashMap<>(inorder.length);
|
||||
for (int i = 0; i < inorder.length; i++) map.put(inorder[i], i);
|
||||
return buildTree(preorder, map, 0, preorder.length - 1, 0, inorder.length - 1);
|
||||
}
|
||||
|
||||
public TreeNode buildTree(int[] preorder, Map<Integer, Integer> map, int pL, int pR, int iL, int iR) {
|
||||
if (pL > pR) return null;
|
||||
TreeNode root = new TreeNode(preorder[pL]);
|
||||
int rs = map.get(root.val);
|
||||
//前序左边子树结束位置
|
||||
int newPr = pL + rs - iL;
|
||||
root.left = buildTree(preorder, map, pL + 1, newPr, iL, rs - 1);
|
||||
root.right = buildTree(preorder, map, newPr + 1, pR, rs + 1, iR);
|
||||
return root;
|
||||
}
|
||||
}
|
28
5.leetcode/src/com/fanxb/common/p200/Q106.java
Normal file
28
5.leetcode/src/com/fanxb/common/p200/Q106.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import com.fanxb.common.TreeNode;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Q106 {
|
||||
public TreeNode buildTree(int[] inorder, int[] postorder) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < inorder.length; i++) {
|
||||
map.put(inorder[i], i);
|
||||
}
|
||||
return buildTree(postorder, map, 0, postorder.length - 1, 0, inorder.length - 1);
|
||||
}
|
||||
|
||||
private TreeNode buildTree(int[] pre, Map<Integer, Integer> map, int pL, int pR, int iL, int iR) {
|
||||
if (pL > pR || iL > iR) return null;
|
||||
TreeNode root = new TreeNode(pre[pR]);
|
||||
int index = map.get(pre[pR]);
|
||||
System.out.println(index);
|
||||
int newPr = pL + index - iL - 1;
|
||||
root.left = buildTree(pre, map, pL, newPr, iL, index - 1);
|
||||
root.right = buildTree(pre, map, newPr + 1, pR - 1, index + 1, iR);
|
||||
return root;
|
||||
}
|
||||
|
||||
}
|
21
5.leetcode/src/com/fanxb/common/p200/Q108.java
Normal file
21
5.leetcode/src/com/fanxb/common/p200/Q108.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
37
5.leetcode/src/com/fanxb/common/p200/Q112.java
Normal file
37
5.leetcode/src/com/fanxb/common/p200/Q112.java
Normal file
@ -0,0 +1,37 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import com.fanxb.common.TreeNode;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Q112 {
|
||||
private static class Node {
|
||||
public int val;
|
||||
public Node left;
|
||||
public Node right;
|
||||
|
||||
public Node() {
|
||||
}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPathSum(TreeNode root, int targetSum) {
|
||||
if (root == null) return false;
|
||||
return hasPathSum(root, targetSum, 0);
|
||||
}
|
||||
|
||||
public boolean hasPathSum(TreeNode root, int targetSum, int curSum) {
|
||||
if (root == null) return false;
|
||||
if (root.left == null && root.right == null) {
|
||||
return targetSum == curSum + root.val;
|
||||
}
|
||||
int sum = root.val + curSum;
|
||||
if (sum > targetSum) return false;
|
||||
return hasPathSum(root.left, targetSum, sum) || hasPathSum(root.right, targetSum, sum);
|
||||
}
|
||||
|
||||
|
||||
}
|
54
5.leetcode/src/com/fanxb/common/p200/Q114.java
Normal file
54
5.leetcode/src/com/fanxb/common/p200/Q114.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import com.fanxb.common.TreeNode;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Q114 {
|
||||
private static class Node {
|
||||
public int val;
|
||||
public Node left;
|
||||
public Node right;
|
||||
|
||||
public Node() {
|
||||
}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
}
|
||||
|
||||
public void flatten(TreeNode root) {
|
||||
if (root == null) return;
|
||||
LinkedList<TreeNode> nodes = new LinkedList<>();
|
||||
read(root, nodes);
|
||||
for (int i = 0; i < nodes.size() - 1; i++) {
|
||||
nodes.get(i).left = null;
|
||||
nodes.get(i).right = nodes.get(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void read(TreeNode root, LinkedList<TreeNode> nodes) {
|
||||
if (root == null) return;
|
||||
else nodes.add(root);
|
||||
read(root.left, nodes);
|
||||
read(root.right, nodes);
|
||||
}
|
||||
|
||||
public void flatten1(TreeNode root) {
|
||||
if (root == null) return;
|
||||
if (root.left != null) {
|
||||
TreeNode right = root.right;
|
||||
root.right = root.left;
|
||||
root.left = null;
|
||||
TreeNode endRight = root.right;
|
||||
while (endRight.right != null) {
|
||||
endRight = endRight.right;
|
||||
}
|
||||
endRight.right = right;
|
||||
}
|
||||
flatten1(root.right);
|
||||
}
|
||||
|
||||
|
||||
}
|
66
5.leetcode/src/com/fanxb/common/p200/Q117.java
Normal file
66
5.leetcode/src/com/fanxb/common/p200/Q117.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Q117 {
|
||||
private static class Node {
|
||||
public int val;
|
||||
public Node left;
|
||||
public Node right;
|
||||
public Node next;
|
||||
|
||||
public Node() {
|
||||
}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
|
||||
public Node(int _val, Node _left, Node _right, Node _next) {
|
||||
val = _val;
|
||||
left = _left;
|
||||
right = _right;
|
||||
next = _next;
|
||||
}
|
||||
}
|
||||
|
||||
public Node connect(Node root) {
|
||||
if (root == null) return null;
|
||||
LinkedList<Node> list = new LinkedList<>();
|
||||
list.addFirst(root);
|
||||
while (!list.isEmpty()) {
|
||||
int size = list.size();
|
||||
while (size-- > 0) {
|
||||
Node node = list.removeLast();
|
||||
if (size != 0) node.next = list.peekLast();
|
||||
if (node.left != null) list.addFirst(node.left);
|
||||
if (node.right != null) list.addFirst(node.right);
|
||||
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
public Node connect1(Node root) {
|
||||
if (root == null) return null;
|
||||
Node cur = root;
|
||||
while (cur != null) {
|
||||
Node temp = cur, start = new Node(), end = start;
|
||||
while (temp != null) {
|
||||
if (temp.left != null) {
|
||||
end.next = temp.left;
|
||||
end = end.next;
|
||||
}
|
||||
if (temp.right != null) {
|
||||
end.next = temp.right;
|
||||
end = end.next;
|
||||
}
|
||||
temp = temp.next;
|
||||
}
|
||||
cur = start.next;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.fanxb.common;
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user