This commit is contained in:
fanxb 2022-01-26 16:48:12 +08:00
parent f630b021d0
commit 5b53c56fd1
8 changed files with 258 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -0,0 +1,32 @@
package com.fanxb.common;
import java.util.LinkedList;
import java.util.Queue;
public class Q225 {
private Queue<Integer> 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();
}
}

View File

@ -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<Character> 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"));
}
}

View File

@ -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}));
}
}

View File

@ -0,0 +1,38 @@
package com.fanxb.common;
import java.util.Arrays;
import java.util.List;
public class Q539 {
public int findMinDifference(List<String> 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")));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}