From b8cbf111fee71c2194f7684feddf842ad5b27540 Mon Sep 17 00:00:00 2001 From: whaifree Date: Thu, 19 Sep 2024 01:04:12 +0800 Subject: [PATCH] =?UTF-8?q?```=E6=B7=BB=E5=8A=A0=E6=96=B0=E7=9A=84LeetCode?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=92=8C=E6=95=B0=E7=BB=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决了新的LeetCode字符串和数组问题,包括字符串转换、罗马数字转换、插入区间、寻找最长公共前缀和判断回文字符串。这些解决方案涵盖了各种字符串操作和数组处理技巧,体现了在面对不同算法挑战时的灵活性和效率。 ``` --- .../java/cn/whaifree/interview/Jinshan.java | 45 ++++++++ .../whaifree/leetCode/Array/LeetCode57.java | 80 ++++++++++++++ .../whaifree/leetCode/String/LeetCode12.java | 92 ++++++++++++++++ .../whaifree/leetCode/String/LeetCode125.java | 49 +++++++++ .../whaifree/leetCode/String/LeetCode14.java | 73 +++++++++++++ .../whaifree/leetCode/String/LeetCode58.java | 42 ++++++++ .../whaifree/leetCode/String/LeetCode6.java | 101 ++++++++++++++++++ 7 files changed, 482 insertions(+) create mode 100644 src/main/java/cn/whaifree/interview/Jinshan.java create mode 100644 src/main/java/cn/whaifree/leetCode/Array/LeetCode57.java create mode 100644 src/main/java/cn/whaifree/leetCode/String/LeetCode12.java create mode 100644 src/main/java/cn/whaifree/leetCode/String/LeetCode125.java create mode 100644 src/main/java/cn/whaifree/leetCode/String/LeetCode14.java create mode 100644 src/main/java/cn/whaifree/leetCode/String/LeetCode58.java create mode 100644 src/main/java/cn/whaifree/leetCode/String/LeetCode6.java diff --git a/src/main/java/cn/whaifree/interview/Jinshan.java b/src/main/java/cn/whaifree/interview/Jinshan.java new file mode 100644 index 0000000..46a77a2 --- /dev/null +++ b/src/main/java/cn/whaifree/interview/Jinshan.java @@ -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; + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Array/LeetCode57.java b/src/main/java/cn/whaifree/leetCode/Array/LeetCode57.java new file mode 100644 index 0000000..6134c9d --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Array/LeetCode57.java @@ -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 list = new ArrayList<>(Arrays.asList(intervals)); + list.add(newInterval); + list.sort(new Comparator() { + @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]); + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode12.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode12.java new file mode 100644 index 0000000..c1090ed --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode12.java @@ -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 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)); + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode125.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode125.java new file mode 100644 index 0000000..2f561cc --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode125.java @@ -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; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode14.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode14.java new file mode 100644 index 0000000..99e50fa --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode14.java @@ -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); + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode58.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode58.java new file mode 100644 index 0000000..76c8fc6 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode58.java @@ -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; + + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode6.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode6.java new file mode 100644 index 0000000..951fbf8 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode6.java @@ -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 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(); + } + } +}