```添加新的LeetCode字符串和数组问题解决方案

解决了新的LeetCode字符串和数组问题,包括字符串转换、罗马数字转换、插入区间、寻找最长公共前缀和判断回文字符串。这些解决方案涵盖了各种字符串操作和数组处理技巧,体现了在面对不同算法挑战时的灵活性和效率。
```
This commit is contained in:
whaifree 2024-09-19 01:04:12 +08:00
parent 93d200414a
commit b8cbf111fe
7 changed files with 482 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package cn.whaifree.interview;
import java.util.Scanner;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/18 19:01
* @注释
*/
public class Jinshan {
/**
* 带k头牛
*
*
* n中选择k头 n*m^k
*
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int res = 1;
for (int k = 1; k <= n; k++) {
long choice = Cfactorial(n, k);
long tmp = (long) (choice * Math.pow(m, k));
res += tmp;
}
System.out.println(res);
}
public static long factorial(int n) {
return (n > 1) ? n * factorial(n - 1) : 1;
}
public static long Cfactorial(int n, int m) {
long a = factorial(n);
return (n >= m) ? (a / factorial(n - m)) / factorial(m) : 0;
}
}

View File

@ -0,0 +1,80 @@
package cn.whaifree.leetCode.Array;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/18 23:37
* @注释
*/
public class LeetCode57 {
@Test
public void test() {
Solution solution = new Solution();
int[][] insert = solution.insert(new int[][]{{1, 3}, {6, 9}}, new int[]{2, 5});
for (int[] ints : insert) {
System.out.println(Arrays.toString(ints));
}
// [[1,2],[3,5],[6,7],[8,10],[12,16]]
int[][] insert1 = solution.insert(new int[][]{{1, 2}, {3, 5}, {6, 7}, {8, 10}, {12, 16}}, new int[]{4, 8});
for (int[] ints : insert1) {
System.out.println(Arrays.toString(ints));
}
}
class Solution {
/**
* | | | |
| |
*
* | | | |
* | |
*
* | | | |
* | |
*
* | | | |
* | |
*
*
* | |
* | |
* | |
* | |
* | |
* | |
* @param intervals
* @param newInterval
* @return
*/
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> list = new ArrayList<>(Arrays.asList(intervals));
list.add(newInterval);
list.sort(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
for (int i = 1; i < list.size(); i++) {
int[] now = list.get(i);
int[] before = list.get(i - 1);
if (now[0] <= before[1]) {
before[1] = Math.max(before[1], now[1]);
list.remove(i);
i--;
}
}
return list.toArray(new int[list.size()][2]);
}
}
}

View File

@ -0,0 +1,92 @@
package cn.whaifree.leetCode.String;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/18 21:43
* @注释
*/
public class LeetCode12 {
class Solution1 {
int[] nums = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] romans = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
public String intToRoman(int num) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < nums.length; i++) {
int v = nums[i];
String c = romans[i];
while (num >= v) {
num -= v;
stringBuilder.append(c);
}
if (num == 0) { // 效率
break;
}
}
return stringBuilder.toString();
}
}
class Solution {
static Map<Integer,String> map = new HashMap<>();
static {
map.put(1,"I");
map.put(2,"II");
map.put(3,"III");
map.put(4,"IV");
map.put(5,"V");
map.put(6,"VI");
map.put(7,"VII");
map.put(8,"VIII");
map.put(9,"IX");
map.put(10,"X");
map.put(20,"XX");
map.put(30,"XXX");
map.put(40,"XL");
map.put(50,"L");
map.put(60,"LX");
map.put(70,"LXX");
map.put(80,"LXXX");
map.put(90,"XC");
map.put(100,"C");
map.put(200,"CC");
map.put(300,"CCC");
map.put(400,"CD");
map.put(500,"D");
map.put(600,"DC");
map.put(700,"DCC");
map.put(800,"DCCC");
map.put(900,"CM");
map.put(1000,"M");
map.put(2000,"MM");
map.put(3000,"MMM");
}
public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
String x = String.valueOf(num);
for (int i = 0; i < x.length(); i++) {
char c = x.charAt(i);
if (c == '0') {
continue;
}
int v = (int) ((c - '0') * Math.pow(10, x.length() - i - 1));
sb.append(map.get(v));
}
return sb.toString();
}
}
@Test
public void test() {
Solution solution = new Solution();
System.out.println(solution.intToRoman(1994));
}
}

View File

@ -0,0 +1,49 @@
package cn.whaifree.leetCode.String;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/18 23:28
* @注释
*/
public class LeetCode125 {
public static void main(String[] args) {
System.out.println((int) 'A');
System.out.println((int) 'Z');
System.out.println((int) 'a');
System.out.println((int) 'z');
System.out.println(new Solution().isPalindrome("A man, a plan, a canal: Panama"));
}
static class Solution {
public boolean isPalindrome(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
// 大写字符转换为小写字符并移除所有非字母数字字符
// if (c >= 'A' && c <= 'Z') {
// sb.append((char) (c + 32));
// } else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// sb.append(c);
// }
if (Character.isLetterOrDigit(c)) {
sb.append(Character.toLowerCase(c));
}
}
return isHuiWen(sb.toString());
}
public boolean isHuiWen(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
}

View File

@ -0,0 +1,73 @@
package cn.whaifree.leetCode.String;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/18 22:09
* @注释
*/
public class LeetCode14 {
@Test
public void test() {
Solution solution = new Solution();
String[] strs = {"d", "c"};
String s = solution.longestCommonPrefix(strs);
System.out.println(s);
}
class Solution {
public String longestCommonPrefix(String[] strs) {
String min = strs[0];
for (String s : strs) {
if (s.length() < min.length()) {
min = s;
}
}
int index = 0;
while (index < min.length()) {
char c = strs[0].charAt(index);
for (int i = 1; i < strs.length; i++) {
if (strs[i].charAt(index) != c) {
return strs[0].substring(0, index);
}
}
index++;
}
return min;
}
}
@Test
public void test2() {
Solution2 solution = new Solution2();
String[] strs = {"flower","flow","flight"};
String s = solution.longestCommonPrefix(strs);
System.out.println(s);
}
class Solution2 {
public String longestCommonPrefix(String[] strs) {
String str = strs[0];
for (int i = 1; i < strs.length; i++) {
str = common(str, strs[i]);
}
return str;
}
public String common(String a, String b) {
int len = Math.min(a.length(), b.length());
for (int i = 0; i < len; i++) {
if (a.charAt(i) != b.charAt(i)) {
return a.substring(0, i);
}
}
return a.substring(0, len);
}
}
}

View File

@ -0,0 +1,42 @@
package cn.whaifree.leetCode.String;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/18 22:04
* @注释
*/
public class LeetCode58 {
@Test
public void test() {
Solution1 solution = new Solution1();
int i = solution.lengthOfLastWord(" fly me to the moon ");
System.out.println(i);
}
class Solution {
public int lengthOfLastWord(String s) {
String trim = s.trim();
String[] split = trim.split(" ");
return split[split.length - 1].length();
}
}
class Solution1 {
public int lengthOfLastWord(String s) {
s = s.trim();
int len = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) != ' ') {
len++;
}else {
break;
}
}
return len;
}
}
}

View File

@ -0,0 +1,101 @@
package cn.whaifree.leetCode.String;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/9/18 22:25
* @注释
*/
public class LeetCode6 {
@Test
public void test() {
Solution solution = new Solution();
String s = solution.convert("AB", 1);
System.out.println(s);
}
class Solution {
/**
* 4
*
* 1234321234321
* @param s
* @param numRows
* @return
*/
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
List<StringBuilder> lines = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
lines.add(new StringBuilder());
}
int i = 0, flag = -1;
for(char c : s.toCharArray()) {
lines.get(i).append(c);
if(i == 0 || i == numRows -1) flag = - flag;
i += flag;
}
StringBuilder res = new StringBuilder();
for (StringBuilder row : lines) {
res.append(row);
}
return res.toString();
}
}
class Solution1 {
/**
* 4
*
* 1234321234321
* @param s
* @param numRows
* @return
*/
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
int[] nums = new int[s.length()];
boolean up = true;
int index = 1;
for (int i = 0; i < nums.length; i++) {
if (index == numRows|| index == 1) {
up = !up;
}
nums[i] = index;
if (up) {
index--;
}else {
index++;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= numRows; i++) {
for (int j = 0; j < nums.length; j++) {
if (nums[j] == i) {
sb.append(s.charAt(j));
}
}
}
return sb.toString();
}
}
}