Merge branch 'master' of ssh://gitea.fleyx.com:222/fanxb/demo-project
This commit is contained in:
commit
b1b713df35
@ -25,6 +25,16 @@ public class Q1 {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更优,一次循环即可
|
||||
*
|
||||
|
26
5.leetcode/src/com/fanxb/common/Q11.java
Normal file
26
5.leetcode/src/com/fanxb/common/Q11.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
68
5.leetcode/src/com/fanxb/common/Q12.java
Normal file
68
5.leetcode/src/com/fanxb/common/Q12.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
38
5.leetcode/src/com/fanxb/common/Q125.java
Normal file
38
5.leetcode/src/com/fanxb/common/Q125.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 题目地址: https://leetcode-cn.com/problems/partition-labels/
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q125 {
|
||||
|
||||
public boolean isPalindrome(String s) {
|
||||
char[] chars = s.toLowerCase().toCharArray();
|
||||
int i=0,j=chars.length-1;
|
||||
while (i<j){
|
||||
char ci = chars[i];
|
||||
if(!isOk(ci)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
char cj = chars[j];
|
||||
if(!isOk(cj)){
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
if(ci !=cj) return false;
|
||||
i++;j--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isOk(char ci){
|
||||
return (ci>='a' && ci<='z') || (ci>='0' && ci<='9');
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] prices = {7, 1, 5, 3, 6, 4};
|
||||
}
|
||||
}
|
26
5.leetcode/src/com/fanxb/common/Q128.java
Normal file
26
5.leetcode/src/com/fanxb/common/Q128.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Q128 {
|
||||
|
||||
public int longestConsecutive(int[] nums) {
|
||||
Set<Integer> set = new HashSet<>(nums.length);
|
||||
for (int num : nums) set.add(num);
|
||||
int res = 0;
|
||||
for (int num : nums) {
|
||||
if (!set.contains(num - 1)) {
|
||||
//说明num是一个序列的起点
|
||||
int temp = num, curRes = 1;
|
||||
while (set.contains(++temp)) curRes++;
|
||||
res = Math.max(res, curRes);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
45
5.leetcode/src/com/fanxb/common/Q13.java
Normal file
45
5.leetcode/src/com/fanxb/common/Q13.java
Normal file
@ -0,0 +1,45 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
39
5.leetcode/src/com/fanxb/common/Q134.java
Normal file
39
5.leetcode/src/com/fanxb/common/Q134.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 分割回文串
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/9 10:10
|
||||
*/
|
||||
public class Q134 {
|
||||
|
||||
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||
int length = gas.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
gas[i] = gas[i] - cost[i];
|
||||
}
|
||||
for (int i = 0; i < length; i++) {
|
||||
//try from i
|
||||
int sum = 0;
|
||||
boolean ok = true;
|
||||
for (int j = 0; j < length; j++) {
|
||||
sum += gas[(i + j) % length];
|
||||
if (sum < 0) {
|
||||
ok = false;
|
||||
i += j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q134().canCompleteCircuit(new int[]{1, 2, 3, 4, 5}, new int[]{3, 4, 5, 1, 2}));
|
||||
}
|
||||
}
|
@ -38,9 +38,32 @@ public class Q135 {
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int candy1(int[] ratings) {
|
||||
//every one set one candy
|
||||
int length = ratings.length;
|
||||
int[] res = new int[length];
|
||||
Arrays.fill(res, 1);
|
||||
for (int i = 1; i < length; i++) {
|
||||
//从左到右遍历,如果下一个人分数更高那么多发一颗糖
|
||||
if (ratings[i] > ratings[i - 1]) {
|
||||
res[i] = res[i - 1] + 1;
|
||||
}
|
||||
}
|
||||
for (int i = length - 2; i >= 0; i--) {
|
||||
if (ratings[i] > ratings[i + 1] && res[i] <= res[i + 1]) {
|
||||
res[i] = res[i + 1] + 1;
|
||||
}
|
||||
}
|
||||
int sum = 0;
|
||||
for (int temp : res) {
|
||||
sum += temp;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] s = {1, 2, 87, 87, 87, 2, 1};
|
||||
int[] s = {1, 3, 4, 5, 2};
|
||||
System.out.println(Arrays.toString(s));
|
||||
System.out.println(candy(s));
|
||||
System.out.println(candy1(s));
|
||||
}
|
||||
}
|
||||
|
@ -43,4 +43,21 @@ public class Q138 {
|
||||
}
|
||||
return res.next;
|
||||
}
|
||||
|
||||
public Node newCopy(Node head) {
|
||||
Map<Node, Node> map = new HashMap<>();
|
||||
Node cur = head;
|
||||
while (cur != null) {
|
||||
map.put(cur, new Node(cur.val));
|
||||
cur = cur.next;
|
||||
}
|
||||
cur = head;
|
||||
while (cur != null) {
|
||||
Node n = map.get(cur);
|
||||
n.random = map.get(cur.random);
|
||||
n.next = map.get(cur.next);
|
||||
cur = cur.next;
|
||||
}
|
||||
return map.get(head);
|
||||
}
|
||||
}
|
||||
|
47
5.leetcode/src/com/fanxb/common/Q14.java
Normal file
47
5.leetcode/src/com/fanxb/common/Q14.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
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();
|
||||
}
|
||||
}
|
33
5.leetcode/src/com/fanxb/common/Q141.java
Normal file
33
5.leetcode/src/com/fanxb/common/Q141.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @author FleyX
|
||||
* @date 2022/3/3 22:35
|
||||
*/
|
||||
public class Q141 {
|
||||
private static class Node {
|
||||
int val;
|
||||
Node next;
|
||||
|
||||
public Node(int val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasCycle(ListNode head) {
|
||||
if (head == null || head.next == null) return false;
|
||||
ListNode fast = head, slow = head;
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
if (fast == slow) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
42
5.leetcode/src/com/fanxb/common/Q15.java
Normal file
42
5.leetcode/src/com/fanxb/common/Q15.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
64
5.leetcode/src/com/fanxb/common/Q150.java
Normal file
64
5.leetcode/src/com/fanxb/common/Q150.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class Q150 {
|
||||
public void gameOfLife(int[][] board) {
|
||||
int m = board.length, n = board[0].length;
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
int aliveCount = 0;
|
||||
if (i > 0 && j > 0 && board[i - 1][j - 1] >= 1) aliveCount++;
|
||||
if (i > 0 && board[i - 1][j] >= 1) aliveCount++;
|
||||
if (i > 0 && j < n - 1 && board[i - 1][j + 1] >= 1) aliveCount++;
|
||||
if (j > 0 && board[i][j - 1] >= 1) aliveCount++;
|
||||
if (j < m - 1 && board[i][j + 1] >= 1) aliveCount++;
|
||||
if (i < m - 1 && j > 0 && board[i + 1][j - 1] >= 1) aliveCount++;
|
||||
if (i < m - 1 && board[i + 1][j] >= 1) aliveCount++;
|
||||
if (i < m - 1 && j < n - 1 && board[i + 1][j + 1] >= 1) aliveCount++;
|
||||
if (board[i][j] == 0) {
|
||||
//死到活 -1
|
||||
if (aliveCount == 3) board[i][j] = -1;
|
||||
} else {
|
||||
//活到死 2
|
||||
if (aliveCount < 2 || aliveCount > 3) board[i][j] = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (board[i][j] == -1) board[i][j] = 1;
|
||||
else if (board[i][j] == 2) board[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int evalRPN(String[] tokens) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
for (String str : tokens) {
|
||||
switch (str) {
|
||||
case "+":
|
||||
stack.push(stack.pop() + stack.pop());
|
||||
break;
|
||||
case "-":
|
||||
stack.push(-stack.pop() + stack.pop());
|
||||
break;
|
||||
case "*":
|
||||
stack.push(stack.pop() * stack.pop());
|
||||
break;
|
||||
case "/":
|
||||
int num1 = stack.pop(), num2 = stack.pop();
|
||||
stack.push(num2 / num1);
|
||||
break;
|
||||
default:
|
||||
stack.push(Integer.valueOf(str));
|
||||
}
|
||||
}
|
||||
return stack.pop();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
28
5.leetcode/src/com/fanxb/common/Q151.java
Normal file
28
5.leetcode/src/com/fanxb/common/Q151.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class Q151 {
|
||||
public String reverseWords(String s) {
|
||||
Stack<String> stack = new Stack<>();
|
||||
StringBuilder temp = new StringBuilder();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (s.charAt(i) == ' ') {
|
||||
if (temp.length() > 0) {
|
||||
stack.push(temp.toString());
|
||||
temp = new StringBuilder();
|
||||
}
|
||||
} else {
|
||||
temp.append(s.charAt(i));
|
||||
}
|
||||
}
|
||||
if (temp.length() > 0) stack.push(temp.toString());
|
||||
StringBuilder res = new StringBuilder();
|
||||
res.append(stack.pop());
|
||||
while (!stack.isEmpty()) res.append(" ").append(stack.pop());
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
28
5.leetcode/src/com/fanxb/common/Q155.java
Normal file
28
5.leetcode/src/com/fanxb/common/Q155.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Q155 {
|
||||
private LinkedList<Integer[]> stack = new LinkedList<>();
|
||||
|
||||
public Q155() {
|
||||
|
||||
}
|
||||
|
||||
public void push(int val) {
|
||||
int min = stack.isEmpty() ? val : Math.min(stack.getFirst()[1], val);
|
||||
stack.push(new Integer[]{val, min});
|
||||
}
|
||||
|
||||
public void pop() {
|
||||
stack.removeFirst();
|
||||
}
|
||||
|
||||
public int top() {
|
||||
return stack.getFirst()[0];
|
||||
}
|
||||
|
||||
public int getMin() {
|
||||
return stack.getFirst()[1];
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
|
||||
/**
|
||||
* 两数之和 II - 输入有序数组
|
||||
@ -25,9 +26,19 @@ public class Q167 {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return new int[]{i+1, j+1};
|
||||
return new int[]{i + 1, j + 1};
|
||||
}
|
||||
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
int i = 0, j = numbers.length - 1;
|
||||
while (i < j) {
|
||||
int sum = numbers[i] + numbers[j];
|
||||
if (sum == target) return new int[]{i+1, j+1};
|
||||
else if (sum > target) j--;
|
||||
else j++;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = {2, 7, 11, 15};
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
25
5.leetcode/src/com/fanxb/common/Q202.java
Normal file
25
5.leetcode/src/com/fanxb/common/Q202.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Q202 {
|
||||
|
||||
public boolean isHappy(int n) {
|
||||
Set<Long> cache = new HashSet<>();
|
||||
long temp = n;
|
||||
while (true) {
|
||||
String[] strs = Long.toString(temp).split("");
|
||||
long res = 0;
|
||||
for (String s : strs) res += (long) Integer.parseInt(s) * Integer.parseInt(s);
|
||||
if (res == 1) return true;
|
||||
if (cache.contains(res)) return false;
|
||||
cache.add(res);
|
||||
temp = res;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Q202().isHappy(2);
|
||||
}
|
||||
}
|
22
5.leetcode/src/com/fanxb/common/Q205.java
Normal file
22
5.leetcode/src/com/fanxb/common/Q205.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q205 {
|
||||
|
||||
public boolean isIsomorphic(String s, String t) {
|
||||
int[] map = new int[128];
|
||||
Arrays.fill(map, -1);
|
||||
int length = s.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
char tc = t.charAt(i), sc = s.charAt(i);
|
||||
if (map[tc] == -1) map[tc] = sc;
|
||||
if (map[tc] != sc) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] s = {1, 2, 3, 1};
|
||||
}
|
||||
}
|
27
5.leetcode/src/com/fanxb/common/Q209.java
Normal file
27
5.leetcode/src/com/fanxb/common/Q209.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q209 {
|
||||
|
||||
public int minSubArrayLen(int target, int[] nums) {
|
||||
int res = Integer.MAX_VALUE, l = 0, r = 0;
|
||||
int sum = 0;
|
||||
while (r < nums.length) {
|
||||
//循环添加右边的元素到窗口中
|
||||
sum += nums[r];
|
||||
//如果和大于target了,就开始减掉左边的元素,并计算最小值
|
||||
while (sum >= target) {
|
||||
res = Math.min(res, r - l + 1);
|
||||
sum -= nums[l];
|
||||
l++;
|
||||
}
|
||||
r++;
|
||||
}
|
||||
return res == Integer.MAX_VALUE ? 0 : res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] s = {1, 2, 3, 1};
|
||||
}
|
||||
}
|
30
5.leetcode/src/com/fanxb/common/Q21.java
Normal file
30
5.leetcode/src/com/fanxb/common/Q21.java
Normal file
@ -0,0 +1,30 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class Q21 {
|
||||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
|
||||
if (list2 == null) return list1;
|
||||
if (list1 == null) return list2;
|
||||
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;
|
||||
}
|
||||
}
|
20
5.leetcode/src/com/fanxb/common/Q219.java
Normal file
20
5.leetcode/src/com/fanxb/common/Q219.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q219 {
|
||||
public boolean containsNearbyDuplicate(int[] nums, int k) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
int num = nums[i];
|
||||
Integer index = map.get(num);
|
||||
if (index != null && Math.abs(i - index) <= k) return true;
|
||||
map.put(num, i);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
}
|
72
5.leetcode/src/com/fanxb/common/Q224.java
Normal file
72
5.leetcode/src/com/fanxb/common/Q224.java
Normal file
@ -0,0 +1,72 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Q224 {
|
||||
public int calculate(String s) {
|
||||
Stack<String> stack = new Stack<>();
|
||||
int len = s.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char ch = s.charAt(i);
|
||||
switch (ch) {
|
||||
case ' ':
|
||||
break;
|
||||
case '(':
|
||||
case '+':
|
||||
case '-':
|
||||
stack.push(ch + "");
|
||||
break;
|
||||
case ')':
|
||||
List<String> strs = new ArrayList<>();
|
||||
String temp;
|
||||
while (!(temp = stack.pop()).equals("("))
|
||||
strs.add(temp);
|
||||
stack.push(cal(strs).toString());
|
||||
break;
|
||||
default:
|
||||
//找到数字
|
||||
StringBuilder builder = new StringBuilder().append(ch);
|
||||
for (i = i + 1; i < len; i++) {
|
||||
char ca = s.charAt(i);
|
||||
if (ca >= '0' && ca <= '9') builder.append(ca);
|
||||
else break;
|
||||
}
|
||||
i--;
|
||||
stack.push(builder.toString());
|
||||
}
|
||||
}
|
||||
List<String> strings = new ArrayList<>();
|
||||
while (!stack.isEmpty())
|
||||
strings.add(stack.pop());
|
||||
return cal(strings);
|
||||
}
|
||||
|
||||
private Integer cal(List<String> strs) {
|
||||
int len = strs.size();
|
||||
if (len == 1) return Integer.valueOf(strs.get(0));
|
||||
if (len == 2) return Integer.valueOf(strs.get(1) + strs.get(0));
|
||||
String first = strs.get(len - 1);
|
||||
int i, num1;
|
||||
if (first.equals("-")) {
|
||||
num1 = -1 * Integer.parseInt(strs.get(len - 2));
|
||||
i = len - 3;
|
||||
} else {
|
||||
num1 = Integer.parseInt(first);
|
||||
i = len - 2;
|
||||
}
|
||||
for (; i >= 0; i -= 2) {
|
||||
if (strs.get(i).equals("+")) {
|
||||
num1 += Integer.parseInt(strs.get(i - 1));
|
||||
} else {
|
||||
num1 -= Integer.parseInt(strs.get(i - 1));
|
||||
}
|
||||
}
|
||||
return num1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q224().calculate("-2+1"));
|
||||
}
|
||||
}
|
20
5.leetcode/src/com/fanxb/common/Q228.java
Normal file
20
5.leetcode/src/com/fanxb/common/Q228.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Q228 {
|
||||
public List<String> summaryRanges(int[] nums) {
|
||||
List<String> res = new LinkedList<>();
|
||||
if (nums.length == 0) return res;
|
||||
int startIndex = 0, length = nums.length;
|
||||
for (int i = 1; i < length; i++) {
|
||||
if (nums[i] - nums[startIndex] != i - startIndex) {
|
||||
res.add(startIndex == i - 1 ? String.valueOf(nums[startIndex]) : (nums[startIndex] + "->" + nums[i - 1]));
|
||||
startIndex = i;
|
||||
}
|
||||
}
|
||||
res.add(startIndex == length - 1 ? String.valueOf(nums[startIndex]) : (nums[startIndex] + "->" + nums[length - 1]));
|
||||
return res;
|
||||
}
|
||||
}
|
33
5.leetcode/src/com/fanxb/common/Q238.java
Normal file
33
5.leetcode/src/com/fanxb/common/Q238.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Q238 {
|
||||
|
||||
public int[] productExceptSelf(int[] nums) {
|
||||
int length = nums.length;
|
||||
if (length <= 1) return nums;
|
||||
int[] left = new int[length];
|
||||
left[0] = nums[0];
|
||||
for (int i = 1; i < length; i++) {
|
||||
left[i] = left[i - 1] * nums[i];
|
||||
}
|
||||
int[] right = new int[length];
|
||||
right[length - 1] = nums[length - 1];
|
||||
for (int i = length - 2; i >= 0; i--) {
|
||||
right[i] = right[i + 1] * nums[i];
|
||||
}
|
||||
for (int i = 1; i < length - 1; i++) {
|
||||
nums[i] = left[i - 1] * right[i + 1];
|
||||
}
|
||||
nums[0] = right[1];
|
||||
nums[length - 1] = left[length - 2];
|
||||
return nums;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Arrays.toString(new Q238().productExceptSelf(new int[]{1, 0})));
|
||||
}
|
||||
}
|
24
5.leetcode/src/com/fanxb/common/Q242.java
Normal file
24
5.leetcode/src/com/fanxb/common/Q242.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* @author fanxb
|
||||
* @date 2021-10-25-下午4:25
|
||||
*/
|
||||
public class Q242 {
|
||||
public boolean isAnagram(String s, String t) {
|
||||
int sn = s.length(), tn = t.length();
|
||||
if (sn != tn) return false;
|
||||
int[] count = new int[128];
|
||||
for (int i = 0; i < sn; i++) {
|
||||
count[s.charAt(i)]++;
|
||||
count[t.charAt(i)]--;
|
||||
}
|
||||
for (int j : count) if (j != 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] params = {{-1, 3}};
|
||||
}
|
||||
}
|
@ -45,12 +45,82 @@ public class Q25 {
|
||||
}
|
||||
}
|
||||
|
||||
public ListNode newReverseKGroup(ListNode head, int k) {
|
||||
if (k == 1) return head;
|
||||
boolean firstDeal = true;
|
||||
int count = 0;
|
||||
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(ListNode start, ListNode end) {
|
||||
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 ListNode new1ReverseKGroup(ListNode head, int k) {
|
||||
ListNode res = new ListNode(0);
|
||||
res.next = head;
|
||||
ListNode pre = res, next;
|
||||
while (head != null) {
|
||||
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 ListNode reverse1(ListNode head) {
|
||||
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) {
|
||||
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);
|
||||
new Q25().new1ReverseKGroup(node, 2);
|
||||
}
|
||||
}
|
||||
|
28
5.leetcode/src/com/fanxb/common/Q274.java
Normal file
28
5.leetcode/src/com/fanxb/common/Q274.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/11 9:56
|
||||
*/
|
||||
public class Q274 {
|
||||
public int hIndex(int[] citations) {
|
||||
Arrays.sort(citations);
|
||||
int length = citations.length, count = 0;
|
||||
for (int i = length - 1; i >= 0; i--) {
|
||||
if (citations[i] < count + 1) {
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q274().hIndex(new int[]{1, 3, 1}));
|
||||
}
|
||||
}
|
33
5.leetcode/src/com/fanxb/common/Q290.java
Normal file
33
5.leetcode/src/com/fanxb/common/Q290.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/11 9:56
|
||||
*/
|
||||
public class Q290 {
|
||||
public boolean wordPattern(String pattern, String s) {
|
||||
List<String> arr = Stream.of(s.split(" ")).filter(item -> !item.isEmpty()).collect(Collectors.toList());
|
||||
if (arr.size() != pattern.length()) return false;
|
||||
String[] map = new String[128];
|
||||
Map<String, Character> map1 = new HashMap<>(arr.size());
|
||||
for (int i = 0; i < pattern.length(); i++) {
|
||||
char c = pattern.charAt(i);
|
||||
String str = arr.get(i);
|
||||
if (map[c] == null) map[c] = str;
|
||||
map1.putIfAbsent(str, c);
|
||||
if (!map[c].equals(str) || !map1.get(str).equals(c)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
56
5.leetcode/src/com/fanxb/common/Q36.java
Normal file
56
5.leetcode/src/com/fanxb/common/Q36.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
26
5.leetcode/src/com/fanxb/common/Q383.java
Normal file
26
5.leetcode/src/com/fanxb/common/Q383.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 两数相加
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2021/6/1
|
||||
**/
|
||||
public class Q383 {
|
||||
public boolean canConstruct(String ransomNote, String magazine) {
|
||||
int[] charCount = new int[128];
|
||||
for (int i = 0; i < magazine.length(); i++) {
|
||||
charCount[magazine.charAt(i)]++;
|
||||
}
|
||||
for (int i = 0; i < ransomNote.length(); i++) {
|
||||
char c = ransomNote.charAt(i);
|
||||
if (charCount[c] == 0)
|
||||
return false;
|
||||
charCount[c]--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
35
5.leetcode/src/com/fanxb/common/Q392.java
Normal file
35
5.leetcode/src/com/fanxb/common/Q392.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 两数相加
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2021/6/1
|
||||
**/
|
||||
public class Q392 {
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int sLength=s.length(),tLength=t.length(),si=0,ti=0;
|
||||
while (si<sLength && ti<tLength){
|
||||
if(t.charAt(ti) == s.charAt(si)){
|
||||
si++;ti++;
|
||||
continue;
|
||||
}
|
||||
int i=ti+1;
|
||||
for(;i<tLength;i++){
|
||||
if(t.charAt(i)==s.charAt(si)){
|
||||
ti = i+1;
|
||||
si++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(i==tLength){
|
||||
si=0;
|
||||
ti++;
|
||||
}
|
||||
}
|
||||
return si>=sLength;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import com.sun.tools.jconsole.JConsoleContext;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -67,9 +67,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}));
|
||||
}
|
||||
}
|
||||
|
49
5.leetcode/src/com/fanxb/common/Q45.java
Normal file
49
5.leetcode/src/com/fanxb/common/Q45.java
Normal file
@ -0,0 +1,49 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
}
|
||||
}
|
35
5.leetcode/src/com/fanxb/common/Q48.java
Normal file
35
5.leetcode/src/com/fanxb/common/Q48.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
|
||||
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/Q49.java
Normal file
25
5.leetcode/src/com/fanxb/common/Q49.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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) {
|
||||
}
|
||||
}
|
61
5.leetcode/src/com/fanxb/common/Q54.java
Normal file
61
5.leetcode/src/com/fanxb/common/Q54.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
36
5.leetcode/src/com/fanxb/common/Q55.java
Normal file
36
5.leetcode/src/com/fanxb/common/Q55.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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 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}));
|
||||
}
|
||||
}
|
27
5.leetcode/src/com/fanxb/common/Q56.java
Normal file
27
5.leetcode/src/com/fanxb/common/Q56.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
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/Q57.java
Normal file
61
5.leetcode/src/com/fanxb/common/Q57.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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/Q58.java
Normal file
43
5.leetcode/src/com/fanxb/common/Q58.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 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"));
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q6 {
|
||||
public String convert(String s, int numRows) {
|
||||
if (numRows == 1) {
|
||||
@ -39,7 +41,40 @@ public class Q6 {
|
||||
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().convert("PAYPALISHIRING", 4));
|
||||
System.out.println(new Q6().convert1("PAYPALISHIRING", 4));
|
||||
}
|
||||
}
|
||||
|
25
5.leetcode/src/com/fanxb/common/Q71.java
Normal file
25
5.leetcode/src/com/fanxb/common/Q71.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
28
5.leetcode/src/com/fanxb/common/Q73.java
Normal file
28
5.leetcode/src/com/fanxb/common/Q73.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
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) {
|
||||
}
|
||||
}
|
@ -51,6 +51,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"));
|
||||
|
67
5.leetcode/src/com/fanxb/common/Q92.java
Normal file
67
5.leetcode/src/com/fanxb/common/Q92.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user