From 255999758cc3035e31bbb26212a07fb9aec31476 Mon Sep 17 00:00:00 2001 From: fleyx Date: Tue, 19 Dec 2023 17:08:51 +0800 Subject: [PATCH 01/17] add --- 5.leetcode/src/com/fanxb/common/Q134.java | 39 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q135.java | 27 ++++++++++++++-- 5.leetcode/src/com/fanxb/common/Q238.java | 33 +++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q274.java | 28 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q42.java | 22 +++++++++++++ 5.leetcode/src/com/fanxb/common/Q55.java | 36 +++++++++++++++++++++ 6 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q134.java create mode 100644 5.leetcode/src/com/fanxb/common/Q238.java create mode 100644 5.leetcode/src/com/fanxb/common/Q274.java create mode 100644 5.leetcode/src/com/fanxb/common/Q55.java diff --git a/5.leetcode/src/com/fanxb/common/Q134.java b/5.leetcode/src/com/fanxb/common/Q134.java new file mode 100644 index 0000000..4bb2919 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q134.java @@ -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})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q135.java b/5.leetcode/src/com/fanxb/common/Q135.java index 387e2ac..3e8834d 100644 --- a/5.leetcode/src/com/fanxb/common/Q135.java +++ b/5.leetcode/src/com/fanxb/common/Q135.java @@ -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)); } } diff --git a/5.leetcode/src/com/fanxb/common/Q238.java b/5.leetcode/src/com/fanxb/common/Q238.java new file mode 100644 index 0000000..774a7d4 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q238.java @@ -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}))); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q274.java b/5.leetcode/src/com/fanxb/common/Q274.java new file mode 100644 index 0000000..6f1b02c --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q274.java @@ -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})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q42.java b/5.leetcode/src/com/fanxb/common/Q42.java index 4ee4926..5cfba89 100644 --- a/5.leetcode/src/com/fanxb/common/Q42.java +++ b/5.leetcode/src/com/fanxb/common/Q42.java @@ -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})); } } diff --git a/5.leetcode/src/com/fanxb/common/Q55.java b/5.leetcode/src/com/fanxb/common/Q55.java new file mode 100644 index 0000000..7d48575 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q55.java @@ -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})); + } +} From c8c466a8e0276651f93f686b3e2617817c9f9e44 Mon Sep 17 00:00:00 2001 From: fleyx Date: Thu, 21 Dec 2023 16:48:44 +0800 Subject: [PATCH 02/17] add --- 5.leetcode/src/com/fanxb/common/Q12.java | 68 ++++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q13.java | 45 ++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q12.java create mode 100644 5.leetcode/src/com/fanxb/common/Q13.java diff --git a/5.leetcode/src/com/fanxb/common/Q12.java b/5.leetcode/src/com/fanxb/common/Q12.java new file mode 100644 index 0000000..801749a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q12.java @@ -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)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q13.java b/5.leetcode/src/com/fanxb/common/Q13.java new file mode 100644 index 0000000..f904ed4 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q13.java @@ -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 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")); + } +} From cbb59d2f9c2d49dcf92e3a8c3b0c6dc279dabe25 Mon Sep 17 00:00:00 2001 From: fleyx Date: Tue, 2 Jan 2024 11:06:27 +0800 Subject: [PATCH 03/17] add --- 5.leetcode/src/com/fanxb/common/Q58.java | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q58.java diff --git a/5.leetcode/src/com/fanxb/common/Q58.java b/5.leetcode/src/com/fanxb/common/Q58.java new file mode 100644 index 0000000..370c90d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q58.java @@ -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")); + } +} From 9612be20a57abe01a778d90075a6a151ae6f31fd Mon Sep 17 00:00:00 2001 From: fleyx Date: Tue, 2 Jan 2024 17:02:16 +0800 Subject: [PATCH 04/17] add --- 5.leetcode/src/com/fanxb/common/Q14.java | 47 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q151.java | 28 ++++++++++++++ 5.leetcode/src/com/fanxb/common/Q6.java | 37 +++++++++++++++++- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q14.java create mode 100644 5.leetcode/src/com/fanxb/common/Q151.java diff --git a/5.leetcode/src/com/fanxb/common/Q14.java b/5.leetcode/src/com/fanxb/common/Q14.java new file mode 100644 index 0000000..f4851f4 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q14.java @@ -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.isEmpty()) return ""; + res = temp.toString(); + } + return res; + } + + public static void main(String[] args) { + Q14 instance = new Q14(); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q151.java b/5.leetcode/src/com/fanxb/common/Q151.java new file mode 100644 index 0000000..2623c3e --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q151.java @@ -0,0 +1,28 @@ +package com.fanxb.common; + +import java.util.Stack; + +public class Q151 { + public String reverseWords(String s) { + Stack stack = new Stack<>(); + StringBuilder temp = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == ' ') { + if (!temp.isEmpty()) { + stack.push(temp.toString()); + temp = new StringBuilder(); + } + } else { + temp.append(s.charAt(i)); + } + } + if (!temp.isEmpty()) 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) { + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q6.java b/5.leetcode/src/com/fanxb/common/Q6.java index 7501dea..59cc923 100644 --- a/5.leetcode/src/com/fanxb/common/Q6.java +++ b/5.leetcode/src/com/fanxb/common/Q6.java @@ -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)); } } From 6657ad170777d4543fba8722934401da8578bf24 Mon Sep 17 00:00:00 2001 From: fleyx Date: Wed, 3 Jan 2024 17:26:41 +0800 Subject: [PATCH 05/17] add --- 5.leetcode/src/com/fanxb/common/Q68.java | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q68.java diff --git a/5.leetcode/src/com/fanxb/common/Q68.java b/5.leetcode/src/com/fanxb/common/Q68.java new file mode 100644 index 0000000..fdd4d34 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q68.java @@ -0,0 +1,32 @@ +package com.fanxb.common; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created with IntelliJ IDEA + * + * @author fanxb + * Date: 2020/6/11 9:56 + */ +public class Q68 { + public List fullJustify(String[] words, int maxWidth) { + int length = words.length, int leftWidth = maxWidth; + List lineWord = new ArrayList<>(); + for (int i = 0; i < length; i++) { + if (lineWord.isEmpty()) { + lineWord.add(words[i]); + leftWidth -= words[i].length(); + } else { + if (leftWidth >= words[i].length() + 1){ + //可以放下 + }else{ + //hao + } + } + } + } + + public static void main(String[] args) { + } +} From e89cc7971670425c466e05ab19e592573e4994f9 Mon Sep 17 00:00:00 2001 From: fleyx Date: Thu, 4 Jan 2024 18:12:43 +0800 Subject: [PATCH 06/17] add --- 5.leetcode/src/com/fanxb/common/Q68.java | 33 ++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/5.leetcode/src/com/fanxb/common/Q68.java b/5.leetcode/src/com/fanxb/common/Q68.java index fdd4d34..b475ca9 100644 --- a/5.leetcode/src/com/fanxb/common/Q68.java +++ b/5.leetcode/src/com/fanxb/common/Q68.java @@ -1,6 +1,7 @@ package com.fanxb.common; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; /** @@ -11,20 +12,42 @@ import java.util.List; */ public class Q68 { public List fullJustify(String[] words, int maxWidth) { - int length = words.length, int leftWidth = maxWidth; - List lineWord = new ArrayList<>(); + int length = words.length, leftWidth = maxWidth; + List lineWord = new LinkedList<>(); + List> lineList = new LinkedList<>(); for (int i = 0; i < length; i++) { if (lineWord.isEmpty()) { lineWord.add(words[i]); leftWidth -= words[i].length(); } else { - if (leftWidth >= words[i].length() + 1){ + if (leftWidth >= words[i].length() + 1) { //可以放下 - }else{ - //hao + lineWord.add(words[i]); + leftWidth -= words[i].length() + 1; + } else { + //放不下了,需要新开一行 + lineList.add(lineWord); + lineWord = new LinkedList<>(); + leftWidth = maxWidth; } } } + List res = new ArrayList<>(lineList.size()); + for (int i = 0; i < lineList.size() - 1; i++) { + addToRes(res, lineList.get(i), false); + } + addToRes(res, lineList.get(lineList.size() - 1), true); + return res; + } + + private void addToRes(List res, List line, boolean lastLine) { + StringBuilder temp = new StringBuilder(); + temp.append(line.get(0)); + if (lastLine) { + for (int i = 1; i < line.size(); i++) temp.append(' ').append(line.get(i)); + } else { + + } } public static void main(String[] args) { From 81df394d2cb5699ba2946c85d5a77d6e70ed48a4 Mon Sep 17 00:00:00 2001 From: fleyx Date: Mon, 8 Jan 2024 22:25:06 +0800 Subject: [PATCH 07/17] add --- 5.leetcode/src/com/fanxb/common/Q45.java | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q45.java diff --git a/5.leetcode/src/com/fanxb/common/Q45.java b/5.leetcode/src/com/fanxb/common/Q45.java new file mode 100644 index 0000000..8fdaf52 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q45.java @@ -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){ + steps++; + 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 Date: Mon, 8 Jan 2024 23:46:48 +0800 Subject: [PATCH 08/17] add --- 5.leetcode/src/com/fanxb/common/Q151.java | 4 +- 5.leetcode/src/com/fanxb/common/Q401.java | 1 - 5.leetcode/src/com/fanxb/common/Q45.java | 2 +- 5.leetcode/src/com/fanxb/common/Q68.java | 67 +++++++++++++---------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/5.leetcode/src/com/fanxb/common/Q151.java b/5.leetcode/src/com/fanxb/common/Q151.java index 2623c3e..8fcc19a 100644 --- a/5.leetcode/src/com/fanxb/common/Q151.java +++ b/5.leetcode/src/com/fanxb/common/Q151.java @@ -8,7 +8,7 @@ public class Q151 { StringBuilder temp = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { - if (!temp.isEmpty()) { + if (temp.length() > 0) { stack.push(temp.toString()); temp = new StringBuilder(); } @@ -16,7 +16,7 @@ public class Q151 { temp.append(s.charAt(i)); } } - if (!temp.isEmpty()) stack.push(temp.toString()); + if (temp.length() > 0) stack.push(temp.toString()); StringBuilder res = new StringBuilder(); res.append(stack.pop()); while (!stack.isEmpty()) res.append(" ").append(stack.pop()); diff --git a/5.leetcode/src/com/fanxb/common/Q401.java b/5.leetcode/src/com/fanxb/common/Q401.java index 5b78a0b..ad90b6d 100644 --- a/5.leetcode/src/com/fanxb/common/Q401.java +++ b/5.leetcode/src/com/fanxb/common/Q401.java @@ -1,6 +1,5 @@ package com.fanxb.common; -import com.sun.tools.jconsole.JConsoleContext; import java.util.*; diff --git a/5.leetcode/src/com/fanxb/common/Q45.java b/5.leetcode/src/com/fanxb/common/Q45.java index 8fdaf52..97a365b 100644 --- a/5.leetcode/src/com/fanxb/common/Q45.java +++ b/5.leetcode/src/com/fanxb/common/Q45.java @@ -18,7 +18,7 @@ public class Q45 { maxIndex = Math.max(maxIndex, i+nums[i]); //走到一步的边界了,已经找到下一跳的起点 if(i==end){ - steps++; + step++; end = maxIndex; } } diff --git a/5.leetcode/src/com/fanxb/common/Q68.java b/5.leetcode/src/com/fanxb/common/Q68.java index b475ca9..2df2943 100644 --- a/5.leetcode/src/com/fanxb/common/Q68.java +++ b/5.leetcode/src/com/fanxb/common/Q68.java @@ -1,6 +1,7 @@ package com.fanxb.common; import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -12,44 +13,54 @@ import java.util.List; */ public class Q68 { public List fullJustify(String[] words, int maxWidth) { - int length = words.length, leftWidth = maxWidth; - List lineWord = new LinkedList<>(); - List> lineList = new LinkedList<>(); - for (int i = 0; i < length; i++) { - if (lineWord.isEmpty()) { - lineWord.add(words[i]); - leftWidth -= words[i].length(); - } else { - if (leftWidth >= words[i].length() + 1) { - //可以放下 - lineWord.add(words[i]); - leftWidth -= words[i].length() + 1; - } else { - //放不下了,需要新开一行 - lineList.add(lineWord); - lineWord = new LinkedList<>(); - leftWidth = maxWidth; - } + List res = new ArrayList<>(); + List temp = new ArrayList<>(); + int size = words.length, lengthCount = 0, usedWidth = 0; + for (int i = 0; i < size; i++) { + lengthCount += words[i].length() + 1; + if (lengthCount > maxWidth + 1) { + //说明放不下了 + res.add(dealOne(temp, maxWidth, usedWidth, false)); + temp.clear(); + usedWidth = 0; + lengthCount = words[i].length() + 1; } + temp.add(words[i]); + usedWidth += words[i].length(); } - List res = new ArrayList<>(lineList.size()); - for (int i = 0; i < lineList.size() - 1; i++) { - addToRes(res, lineList.get(i), false); + if (!temp.isEmpty()) { + res.add(dealOne(temp, maxWidth, usedWidth, true)); } - addToRes(res, lineList.get(lineList.size() - 1), true); return res; } - private void addToRes(List res, List line, boolean lastLine) { - StringBuilder temp = new StringBuilder(); - temp.append(line.get(0)); - if (lastLine) { - for (int i = 1; i < line.size(); i++) temp.append(' ').append(line.get(i)); + private String dealOne(List temp, int maxWidth, int usedWidth, boolean lastOne) { + StringBuilder res = new StringBuilder(); + res.append(temp.get(0)); + int size = temp.size(); + if (size == 1) return res.append(" ".repeat(maxWidth - usedWidth)).toString(); + if (lastOne) { + for (int i = 1; i < size; i++) res.append(' ').append(temp.get(i)); + return res.append(" ".repeat(maxWidth - res.length())).toString(); } else { - + //每个间隙的长度 + int everyWidth = (maxWidth - usedWidth) / (size - 1); + //前几个间隙需要加一个空格 + int needPlusCount = (maxWidth - usedWidth) % (size - 1); + for (int i = 1; i < size; i++) { + res.append(" ".repeat(everyWidth)); + if (needPlusCount > 0) { + res.append(' '); + needPlusCount--; + } + res.append(temp.get(i)); + } } + return res.toString(); } public static void main(String[] args) { + Q68 q68 = new Q68(); + System.out.println(q68.fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16).toString()); } } From 0a6b066776ca3e3710b394e9fbb59a5f3cfa8f71 Mon Sep 17 00:00:00 2001 From: fleyx Date: Wed, 10 Jan 2024 17:27:57 +0800 Subject: [PATCH 09/17] add --- 5.leetcode/src/com/fanxb/common/Q125.java | 38 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q209.java | 27 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q3.java | 27 +++++++++++++++- 5.leetcode/src/com/fanxb/common/Q392.java | 35 +++++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q125.java create mode 100644 5.leetcode/src/com/fanxb/common/Q209.java create mode 100644 5.leetcode/src/com/fanxb/common/Q392.java diff --git a/5.leetcode/src/com/fanxb/common/Q125.java b/5.leetcode/src/com/fanxb/common/Q125.java new file mode 100644 index 0000000..0b8667b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q125.java @@ -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='a' && ci<='z') || (ci>='0' && ci<='9'); + } + + public static void main(String[] args) { + int[] prices = {7, 1, 5, 3, 6, 4}; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q209.java b/5.leetcode/src/com/fanxb/common/Q209.java new file mode 100644 index 0000000..e5550c2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q209.java @@ -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}; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q3.java b/5.leetcode/src/com/fanxb/common/Q3.java index b7f19c9..9954a4d 100644 --- a/5.leetcode/src/com/fanxb/common/Q3.java +++ b/5.leetcode/src/com/fanxb/common/Q3.java @@ -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")); } } diff --git a/5.leetcode/src/com/fanxb/common/Q392.java b/5.leetcode/src/com/fanxb/common/Q392.java new file mode 100644 index 0000000..4d1e2d2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q392.java @@ -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; + } + + public static void main(String[] args) { + } +} From 08407ea39c3b9d9e0c3d8f9877fdd74c04a75bca Mon Sep 17 00:00:00 2001 From: fleyx Date: Fri, 12 Jan 2024 19:56:36 +0800 Subject: [PATCH 10/17] add --- 5.leetcode/src/com/fanxb/common/Q54.java | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q54.java diff --git a/5.leetcode/src/com/fanxb/common/Q54.java b/5.leetcode/src/com/fanxb/common/Q54.java new file mode 100644 index 0000000..658ee1b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q54.java @@ -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 spiralOrder(int[][] matrix) { + List 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()); + } +} From 007a62df5154e3843f569bf591f77bdf854d8c3a Mon Sep 17 00:00:00 2001 From: fleyx Date: Sun, 21 Jan 2024 16:53:14 +0800 Subject: [PATCH 11/17] add --- 5.leetcode/src/com/fanxb/common/Q1.java | 10 +++++++++ 5.leetcode/src/com/fanxb/common/Q128.java | 26 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q14.java | 2 +- 5.leetcode/src/com/fanxb/common/Q202.java | 25 ++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q219.java | 20 +++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q228.java | 20 +++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q242.java | 24 +++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q49.java | 25 ++++++++++++++++++++++ 8 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q128.java create mode 100644 5.leetcode/src/com/fanxb/common/Q202.java create mode 100644 5.leetcode/src/com/fanxb/common/Q219.java create mode 100644 5.leetcode/src/com/fanxb/common/Q228.java create mode 100644 5.leetcode/src/com/fanxb/common/Q242.java create mode 100644 5.leetcode/src/com/fanxb/common/Q49.java diff --git a/5.leetcode/src/com/fanxb/common/Q1.java b/5.leetcode/src/com/fanxb/common/Q1.java index 5046235..30e46b7 100644 --- a/5.leetcode/src/com/fanxb/common/Q1.java +++ b/5.leetcode/src/com/fanxb/common/Q1.java @@ -25,6 +25,16 @@ public class Q1 { return null; } + public int[] twoSum1(int[] nums, int target) { + Map 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; + } + /** * 更优,一次循环即可 * diff --git a/5.leetcode/src/com/fanxb/common/Q128.java b/5.leetcode/src/com/fanxb/common/Q128.java new file mode 100644 index 0000000..8b1f2a9 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q128.java @@ -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 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) { + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q14.java b/5.leetcode/src/com/fanxb/common/Q14.java index f4851f4..f11a439 100644 --- a/5.leetcode/src/com/fanxb/common/Q14.java +++ b/5.leetcode/src/com/fanxb/common/Q14.java @@ -35,7 +35,7 @@ public class Q14 { if (res.charAt(j) == strs[i].charAt(j)) temp.append(res.charAt(j)); else break; } - if (temp.isEmpty()) return ""; + if (temp.length() == 0) return ""; res = temp.toString(); } return res; diff --git a/5.leetcode/src/com/fanxb/common/Q202.java b/5.leetcode/src/com/fanxb/common/Q202.java new file mode 100644 index 0000000..f0a4861 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q202.java @@ -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 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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q219.java b/5.leetcode/src/com/fanxb/common/Q219.java new file mode 100644 index 0000000..8338e1a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q219.java @@ -0,0 +1,20 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q219 { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Map 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) { + } + +} diff --git a/5.leetcode/src/com/fanxb/common/Q228.java b/5.leetcode/src/com/fanxb/common/Q228.java new file mode 100644 index 0000000..1735ff1 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q228.java @@ -0,0 +1,20 @@ +package com.fanxb.common; + +import java.util.LinkedList; +import java.util.List; + +public class Q228 { + public List summaryRanges(int[] nums) { + List 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q242.java b/5.leetcode/src/com/fanxb/common/Q242.java new file mode 100644 index 0000000..d8572ba --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q242.java @@ -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}}; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q49.java b/5.leetcode/src/com/fanxb/common/Q49.java new file mode 100644 index 0000000..ef0bee7 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q49.java @@ -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> groupAnagrams(String[] strs) { + Map> 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) { + } +} From 3c231d2345385158acda65eb2638941bae273116 Mon Sep 17 00:00:00 2001 From: fleyx Date: Wed, 24 Jan 2024 22:22:44 +0800 Subject: [PATCH 12/17] add --- 5.leetcode/src/com/fanxb/common/Q11.java | 26 +++++++++++ 5.leetcode/src/com/fanxb/common/Q15.java | 42 +++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q150.java | 38 +++++++++++++++ 5.leetcode/src/com/fanxb/common/Q167.java | 13 +++++- 5.leetcode/src/com/fanxb/common/Q205.java | 22 +++++++++ 5.leetcode/src/com/fanxb/common/Q290.java | 34 ++++++++++++++ 5.leetcode/src/com/fanxb/common/Q36.java | 56 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q383.java | 26 +++++++++++ 5.leetcode/src/com/fanxb/common/Q48.java | 35 ++++++++++++++ 5.leetcode/src/com/fanxb/common/Q73.java | 29 ++++++++++++ 5.leetcode/src/com/fanxb/common/Q76.java | 30 ++++++++++++ 11 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q11.java create mode 100644 5.leetcode/src/com/fanxb/common/Q15.java create mode 100644 5.leetcode/src/com/fanxb/common/Q150.java create mode 100644 5.leetcode/src/com/fanxb/common/Q205.java create mode 100644 5.leetcode/src/com/fanxb/common/Q290.java create mode 100644 5.leetcode/src/com/fanxb/common/Q36.java create mode 100644 5.leetcode/src/com/fanxb/common/Q383.java create mode 100644 5.leetcode/src/com/fanxb/common/Q48.java create mode 100644 5.leetcode/src/com/fanxb/common/Q73.java diff --git a/5.leetcode/src/com/fanxb/common/Q11.java b/5.leetcode/src/com/fanxb/common/Q11.java new file mode 100644 index 0000000..c5bfa59 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q11.java @@ -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(); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q15.java b/5.leetcode/src/com/fanxb/common/Q15.java new file mode 100644 index 0000000..494cbf3 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q15.java @@ -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> threeSum(int[] nums) { + Arrays.sort(nums); + List> res = new ArrayList<>(); + Set 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(); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q150.java b/5.leetcode/src/com/fanxb/common/Q150.java new file mode 100644 index 0000000..e90e546 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q150.java @@ -0,0 +1,38 @@ +package com.fanxb.common; + + +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 static void main(String[] args) { + System.out.println(new Q150().maxPoints(new int[][]{{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q167.java b/5.leetcode/src/com/fanxb/common/Q167.java index 338e175..7e07bd8 100644 --- a/5.leetcode/src/com/fanxb/common/Q167.java +++ b/5.leetcode/src/com/fanxb/common/Q167.java @@ -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}; diff --git a/5.leetcode/src/com/fanxb/common/Q205.java b/5.leetcode/src/com/fanxb/common/Q205.java new file mode 100644 index 0000000..22ea749 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q205.java @@ -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}; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q290.java b/5.leetcode/src/com/fanxb/common/Q290.java new file mode 100644 index 0000000..969d8b8 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q290.java @@ -0,0 +1,34 @@ +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 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 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) { + System.out.println(new Q290().firstBadVersion(16)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q36.java b/5.leetcode/src/com/fanxb/common/Q36.java new file mode 100644 index 0000000..7910b25 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q36.java @@ -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)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q383.java b/5.leetcode/src/com/fanxb/common/Q383.java new file mode 100644 index 0000000..5b6d989 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q383.java @@ -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) { + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q48.java b/5.leetcode/src/com/fanxb/common/Q48.java new file mode 100644 index 0000000..cd076be --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q48.java @@ -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)); + } + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q73.java b/5.leetcode/src/com/fanxb/common/Q73.java new file mode 100644 index 0000000..4715ffe --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q73.java @@ -0,0 +1,29 @@ +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) { + System.out.println(new Q73().minDistance("horse", "ros")); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q76.java b/5.leetcode/src/com/fanxb/common/Q76.java index bea1a4e..26c52f2 100644 --- a/5.leetcode/src/com/fanxb/common/Q76.java +++ b/5.leetcode/src/com/fanxb/common/Q76.java @@ -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")); From f72bb3069af5ae29151ee9b23476eb987e2ded51 Mon Sep 17 00:00:00 2001 From: fleyx Date: Mon, 19 Feb 2024 21:20:38 +0800 Subject: [PATCH 13/17] add --- 5.leetcode/src/com/fanxb/common/Q150.java | 1 - 5.leetcode/src/com/fanxb/common/Q290.java | 1 - 5.leetcode/src/com/fanxb/common/Q56.java | 27 ++++++++++ 5.leetcode/src/com/fanxb/common/Q57.java | 61 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q73.java | 1 - 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q56.java create mode 100644 5.leetcode/src/com/fanxb/common/Q57.java diff --git a/5.leetcode/src/com/fanxb/common/Q150.java b/5.leetcode/src/com/fanxb/common/Q150.java index e90e546..2123433 100644 --- a/5.leetcode/src/com/fanxb/common/Q150.java +++ b/5.leetcode/src/com/fanxb/common/Q150.java @@ -33,6 +33,5 @@ public class Q150 { } public static void main(String[] args) { - System.out.println(new Q150().maxPoints(new int[][]{{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}})); } } diff --git a/5.leetcode/src/com/fanxb/common/Q290.java b/5.leetcode/src/com/fanxb/common/Q290.java index 969d8b8..0d167cc 100644 --- a/5.leetcode/src/com/fanxb/common/Q290.java +++ b/5.leetcode/src/com/fanxb/common/Q290.java @@ -29,6 +29,5 @@ public class Q290 { } public static void main(String[] args) { - System.out.println(new Q290().firstBadVersion(16)); } } diff --git a/5.leetcode/src/com/fanxb/common/Q56.java b/5.leetcode/src/com/fanxb/common/Q56.java new file mode 100644 index 0000000..deccf9d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q56.java @@ -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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q57.java b/5.leetcode/src/com/fanxb/common/Q57.java new file mode 100644 index 0000000..12efcee --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q57.java @@ -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); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q73.java b/5.leetcode/src/com/fanxb/common/Q73.java index 4715ffe..7021ce7 100644 --- a/5.leetcode/src/com/fanxb/common/Q73.java +++ b/5.leetcode/src/com/fanxb/common/Q73.java @@ -24,6 +24,5 @@ public class Q73 { } public static void main(String[] args) { - System.out.println(new Q73().minDistance("horse", "ros")); } } From ff9af65feb7e721cecebd7b8be4001e31185e3ab Mon Sep 17 00:00:00 2001 From: fleyx Date: Mon, 19 Feb 2024 23:40:06 +0800 Subject: [PATCH 14/17] add --- 5.leetcode/src/com/fanxb/common/Q150.java | 27 +++++++++ 5.leetcode/src/com/fanxb/common/Q155.java | 28 +++++++++ 5.leetcode/src/com/fanxb/common/Q224.java | 72 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q71.java | 25 ++++++++ 4 files changed, 152 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q155.java create mode 100644 5.leetcode/src/com/fanxb/common/Q224.java create mode 100644 5.leetcode/src/com/fanxb/common/Q71.java diff --git a/5.leetcode/src/com/fanxb/common/Q150.java b/5.leetcode/src/com/fanxb/common/Q150.java index 2123433..90d9bb1 100644 --- a/5.leetcode/src/com/fanxb/common/Q150.java +++ b/5.leetcode/src/com/fanxb/common/Q150.java @@ -1,6 +1,8 @@ package com.fanxb.common; +import java.util.Stack; + public class Q150 { public void gameOfLife(int[][] board) { int m = board.length, n = board[0].length; @@ -32,6 +34,31 @@ public class Q150 { } } + + public int evalRPN(String[] tokens) { + Stack 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) { } } diff --git a/5.leetcode/src/com/fanxb/common/Q155.java b/5.leetcode/src/com/fanxb/common/Q155.java new file mode 100644 index 0000000..353699b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q155.java @@ -0,0 +1,28 @@ +package com.fanxb.common; + +import java.util.LinkedList; + +public class Q155 { + private LinkedList 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]; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q224.java b/5.leetcode/src/com/fanxb/common/Q224.java new file mode 100644 index 0000000..ace54b2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q224.java @@ -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 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 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 strings = new ArrayList<>(); + while (!stack.isEmpty()) + strings.add(stack.pop()); + return cal(strings); + } + + private Integer cal(List 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")); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q71.java b/5.leetcode/src/com/fanxb/common/Q71.java new file mode 100644 index 0000000..4da4230 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q71.java @@ -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 stack = new Stack<>(); + List 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()); + } +} From 1403b5a7eba6cedb476289cdf33c5300699eff3c Mon Sep 17 00:00:00 2001 From: fleyx Date: Mon, 11 Mar 2024 23:34:45 +0800 Subject: [PATCH 15/17] add --- 5.leetcode/src/com/fanxb/common/Q138.java | 17 ++++++ 5.leetcode/src/com/fanxb/common/Q141.java | 33 +++++++++++ 5.leetcode/src/com/fanxb/common/Q2.java | 44 +++++++++++---- 5.leetcode/src/com/fanxb/common/Q21.java | 30 ++++++++++ 5.leetcode/src/com/fanxb/common/Q92.java | 67 +++++++++++++++++++++++ 5 files changed, 180 insertions(+), 11 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q141.java create mode 100644 5.leetcode/src/com/fanxb/common/Q21.java create mode 100644 5.leetcode/src/com/fanxb/common/Q92.java diff --git a/5.leetcode/src/com/fanxb/common/Q138.java b/5.leetcode/src/com/fanxb/common/Q138.java index 947f82c..9566de4 100644 --- a/5.leetcode/src/com/fanxb/common/Q138.java +++ b/5.leetcode/src/com/fanxb/common/Q138.java @@ -43,4 +43,21 @@ public class Q138 { } return res.next; } + + public Node newCopy(Node head) { + Map 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); + } } diff --git a/5.leetcode/src/com/fanxb/common/Q141.java b/5.leetcode/src/com/fanxb/common/Q141.java new file mode 100644 index 0000000..bb7b36d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q141.java @@ -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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q2.java b/5.leetcode/src/com/fanxb/common/Q2.java index f865af8..11c63dd 100644 --- a/5.leetcode/src/com/fanxb/common/Q2.java +++ b/5.leetcode/src/com/fanxb/common/Q2.java @@ -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)); } } diff --git a/5.leetcode/src/com/fanxb/common/Q21.java b/5.leetcode/src/com/fanxb/common/Q21.java new file mode 100644 index 0000000..caf228b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q21.java @@ -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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q92.java b/5.leetcode/src/com/fanxb/common/Q92.java new file mode 100644 index 0000000..79b6dc6 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q92.java @@ -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; + } +} From 85737c7e8db6fb9702180f9501179bfdf540d7ac Mon Sep 17 00:00:00 2001 From: fleyx Date: Mon, 18 Mar 2024 20:48:12 +0800 Subject: [PATCH 16/17] add --- 5.leetcode/src/com/fanxb/common/Q25.java | 72 +++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/5.leetcode/src/com/fanxb/common/Q25.java b/5.leetcode/src/com/fanxb/common/Q25.java index a999912..c0c4e75 100644 --- a/5.leetcode/src/com/fanxb/common/Q25.java +++ b/5.leetcode/src/com/fanxb/common/Q25.java @@ -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); } } From 4984a2b5975e921214a3f1c2ae78eb86bfbeca20 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sat, 23 Mar 2024 20:50:44 +0800 Subject: [PATCH 17/17] add --- 5.leetcode/src/com/fanxb/common/Q33.java | 53 ++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q68.java | 68 +++++++----------------- 2 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q33.java diff --git a/5.leetcode/src/com/fanxb/common/Q33.java b/5.leetcode/src/com/fanxb/common/Q33.java new file mode 100644 index 0000000..be42f69 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q33.java @@ -0,0 +1,53 @@ +package com.fanxb.common; + +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 static void main(String[] args) { + System.out.println(new Q33().search(new int[]{1, 3}, 0)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q68.java b/5.leetcode/src/com/fanxb/common/Q68.java index 2df2943..ae2e305 100644 --- a/5.leetcode/src/com/fanxb/common/Q68.java +++ b/5.leetcode/src/com/fanxb/common/Q68.java @@ -1,10 +1,5 @@ package com.fanxb.common; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - /** * Created with IntelliJ IDEA * @@ -12,55 +7,30 @@ import java.util.List; * Date: 2020/6/11 9:56 */ public class Q68 { - public List fullJustify(String[] words, int maxWidth) { - List res = new ArrayList<>(); - List temp = new ArrayList<>(); - int size = words.length, lengthCount = 0, usedWidth = 0; - for (int i = 0; i < size; i++) { - lengthCount += words[i].length() + 1; - if (lengthCount > maxWidth + 1) { - //说明放不下了 - res.add(dealOne(temp, maxWidth, usedWidth, false)); - temp.clear(); - usedWidth = 0; - lengthCount = words[i].length() + 1; - } - temp.add(words[i]); - usedWidth += words[i].length(); - } - if (!temp.isEmpty()) { - res.add(dealOne(temp, maxWidth, usedWidth, true)); - } - return res; - } - - private String dealOne(List temp, int maxWidth, int usedWidth, boolean lastOne) { - StringBuilder res = new StringBuilder(); - res.append(temp.get(0)); - int size = temp.size(); - if (size == 1) return res.append(" ".repeat(maxWidth - usedWidth)).toString(); - if (lastOne) { - for (int i = 1; i < size; i++) res.append(' ').append(temp.get(i)); - return res.append(" ".repeat(maxWidth - res.length())).toString(); - } else { - //每个间隙的长度 - int everyWidth = (maxWidth - usedWidth) / (size - 1); - //前几个间隙需要加一个空格 - int needPlusCount = (maxWidth - usedWidth) % (size - 1); - for (int i = 1; i < size; i++) { - res.append(" ".repeat(everyWidth)); - if (needPlusCount > 0) { - res.append(' '); - needPlusCount--; + 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; } - res.append(temp.get(i)); } } - return res.toString(); + return nums[l] < target ? l + 1 : l; } public static void main(String[] args) { - Q68 q68 = new Q68(); - System.out.println(q68.fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16).toString()); + System.out.println(new Q68().searchInsert(new int[]{1, 3, 5, 6}, 5)); } }