From 5b53c56fd152f648c3049dc3e8356a2b14e72e18 Mon Sep 17 00:00:00 2001 From: fanxb Date: Wed, 26 Jan 2022 16:48:12 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q1576.java | 32 +++++++++++++++ 5.leetcode/src/com/fanxb/common/Q1688.java | 28 ++++++++++++++ 5.leetcode/src/com/fanxb/common/Q225.java | 32 +++++++++++++++ 5.leetcode/src/com/fanxb/common/Q3.java | 45 ++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q300.java | 16 ++++++++ 5.leetcode/src/com/fanxb/common/Q539.java | 38 ++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q6.java | 45 ++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q7.java | 22 +++++++++++ 8 files changed, 258 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q1576.java create mode 100644 5.leetcode/src/com/fanxb/common/Q1688.java create mode 100644 5.leetcode/src/com/fanxb/common/Q225.java create mode 100644 5.leetcode/src/com/fanxb/common/Q3.java create mode 100644 5.leetcode/src/com/fanxb/common/Q539.java create mode 100644 5.leetcode/src/com/fanxb/common/Q6.java create mode 100644 5.leetcode/src/com/fanxb/common/Q7.java diff --git a/5.leetcode/src/com/fanxb/common/Q1576.java b/5.leetcode/src/com/fanxb/common/Q1576.java new file mode 100644 index 0000000..54633c3 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q1576.java @@ -0,0 +1,32 @@ +package com.fanxb.common; + +public class Q1576 { + public String modifyString(String s) { + char[] chars = s.toCharArray(); + //相对于a的asii值 + int last, next; + for (int i = 0; i < chars.length; i++) { + if (chars[i] != '?') { + continue; + } + last = i == 0 ? -1 : chars[i - 1] - 'a'; + next = i == chars.length - 1 || chars[i + 1] == '?' ? -1 : chars[i + 1] - 'a'; + if (last == -1) { + if (next == -1) { + chars[i] = 'a'; + } else { + chars[i] = (char) ((next + 1) % 26 + 'a'); + } + } else { + if (next == -1) { + chars[i] = (char) ((last + 1) % 26 + 'a'); + } else { + int big = Math.max(last, next); + int less = Math.min(last, next); + chars[i] = (char) (big - less > 1 ? ('a' + less + 1) : ('a' + (big + 1) % 26)); + } + } + } + return new String(chars); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q1688.java b/5.leetcode/src/com/fanxb/common/Q1688.java new file mode 100644 index 0000000..88d6799 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q1688.java @@ -0,0 +1,28 @@ +package com.fanxb.common; + +public class Q1688 { + private static int[] res = new int[201]; + + static { + res[1] = 0; + res[2] = 1; + //是否偶数 + boolean isTwo = false; + for (int i = 3; i <= 200; i++) { + if (isTwo) { + res[i] = i / 2 + res[i / 2]; + } else { + res[i] = (i - 1) / 2 + res[(i - 1) / 2 + 1]; + } + isTwo = !isTwo; + } + } + + public int numberOfMatches(int n) { + return res[n]; + } + + public static void main(String[] args) { + System.out.println(new Q1688().numberOfMatches(14)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q225.java b/5.leetcode/src/com/fanxb/common/Q225.java new file mode 100644 index 0000000..5e97529 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q225.java @@ -0,0 +1,32 @@ +package com.fanxb.common; + +import java.util.LinkedList; +import java.util.Queue; + +public class Q225 { + private Queue queue1; + + public Q225() { + this.queue1 = new LinkedList<>(); + } + + public void push(int x) { + int size = queue1.size(); + queue1.add(x); + while (size-- > 0) { + queue1.add(queue1.poll()); + } + } + + public int pop() { + return queue1.poll(); + } + + public int top() { + return queue1.peek(); + } + + public boolean empty() { + return queue1.isEmpty(); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q3.java b/5.leetcode/src/com/fanxb/common/Q3.java new file mode 100644 index 0000000..b7f19c9 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q3.java @@ -0,0 +1,45 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q3 { + public int lengthOfLongestSubstring(String s) { + int length = s.length(); + if (length <= 1) { + return length; + } + int res = 0; + //存储当前结果子串 + LinkedList characters = new LinkedList<>(); + //字符对应characters中的index + boolean[] existArr = new boolean[128]; + characters.add(s.charAt(0)); + existArr[s.charAt(0)] = true; + for (int i = 1; i < s.length(); i++) { + Character one = s.charAt(i); + if (existArr[one]) { + //这个字符出现过,当前的长度就是目前能找到的最大长度 + res = Math.max(res, characters.size()); + //去掉重复字符及该字符之前的所有字符 + while (true) { + Character temp; + if ((temp = characters.poll()) == one) { + break; + } + existArr[temp] = false; + } + //将当前字符串加入 + characters.add(one); + } else { + //这个字符没出现过,可以加到characters中 + characters.add(one); + existArr[one] = true; + } + } + return Math.max(characters.size(), res); + } + + public static void main(String[] args) { + System.out.println(new Q3().lengthOfLongestSubstring("bbbbbbbbbbbbbbbbb")); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q300.java b/5.leetcode/src/com/fanxb/common/Q300.java index 321d33c..eb90a1c 100644 --- a/5.leetcode/src/com/fanxb/common/Q300.java +++ b/5.leetcode/src/com/fanxb/common/Q300.java @@ -26,7 +26,23 @@ public class Q300 { return res; } + public int another(int[] nums) { + int n = nums.length, res = 1; + int[] dp = new int[n]; + for (int i = n - 1; i >= 0; i--) { + dp[i] = 1; + for (int j = i + 1; j < n; j++) { + if (nums[i] < nums[j]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + res = Math.max(res, dp[i]); + } + return res; + } + public static void main(String[] args) { System.out.println(new Q300().lengthOfLIS(new int[]{0, 1, 0, 3, 2, 3})); + System.out.println(new Q300().another(new int[]{0, 1, 0, 3, 2, 3})); } } diff --git a/5.leetcode/src/com/fanxb/common/Q539.java b/5.leetcode/src/com/fanxb/common/Q539.java new file mode 100644 index 0000000..aa99398 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q539.java @@ -0,0 +1,38 @@ +package com.fanxb.common; + +import java.util.Arrays; +import java.util.List; + +public class Q539 { + public int findMinDifference(List timePoints) { + String zero = "00:00"; + for (int i = 0; i < timePoints.size(); i++) { + if (timePoints.get(i).equals(zero)) { + timePoints.set(i, "24:00"); + } + } + timePoints.sort(String::compareTo); + int res = Integer.MAX_VALUE; + int cur = cal(timePoints.get(0)), next = 0; + for (int i = 0; i < timePoints.size() - 1; i++) { + next = cal(timePoints.get(i + 1)); + if (cur == next) { + return 0; + } + res = Math.min(res, next - cur); + cur = next; + } + //最后计算首尾的时间差 + res = Math.min(cal("24:00") - cur + cal(timePoints.get(0)), res); + return res; + } + + private int cal(String point) { + String[] nums = point.split(":"); + return Integer.parseInt(nums[0]) * 60 + Integer.parseInt(nums[1]); + } + + public static void main(String[] args) { + System.out.println(new Q539().findMinDifference(Arrays.asList("05:31", "22:08", "00:35"))); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q6.java b/5.leetcode/src/com/fanxb/common/Q6.java new file mode 100644 index 0000000..7501dea --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q6.java @@ -0,0 +1,45 @@ +package com.fanxb.common; + +public class Q6 { + public String convert(String s, int numRows) { + if (numRows == 1) { + return s; + } + int length = s.length(); + int circle = 2 * numRows - 2; + char[][] res = new char[numRows][Double.valueOf(Math.ceil(length / (circle * 1.0))).intValue() * (numRows - 1)]; + int y = 0, x = 0, count = 0; + for (int i = 0; i < length; i++) { + res[y][x] = s.charAt(i); + count++; + if (count == circle) { + //一轮循环结束,重置 + count = 0; + x++; + y--; + } else { + if (count < numRows) { + y++; + } else { + x++; + y--; + } + } + + } + char[] strs = new char[length]; + count = 0; + for (char[] temp : res) { + for (char one : temp) { + if (one != 0) { + strs[count++] = one; + } + } + } + return new String(strs); + } + + public static void main(String[] args) { + System.out.println(new Q6().convert("PAYPALISHIRING", 4)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q7.java b/5.leetcode/src/com/fanxb/common/Q7.java new file mode 100644 index 0000000..7f7aff3 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q7.java @@ -0,0 +1,22 @@ +package com.fanxb.common; + +public class Q7 { + public int reverse(int x) { + int res = 0; + int last = 0; + while (x != 0) { + last = res; + res = res * 10 + x % 10; + x = x / 10; + //判断是否溢出(原理:数字溢出后肯定和原来不一样,通过%10后和上一个值比较,一样说明未溢出) + if (res / 10 != last) { + return 0; + } + } + return res; + } + + public static void main(String[] args) { + System.out.println(new Q7().reverse(1234567)); + } +}