From 90320f9b276530bff60fd5ede6ab7232834d129c Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 10 May 2024 19:42:44 +0800 Subject: [PATCH] update java 125, progress --- data/progress.txt | 1 + data/to_review.txt | 9 ++++++++ .../LeetCodeJava/String/ValidPalindrome.java | 23 +++++++++++++++++++ .../src/main/java/dev/workspace3.java | 21 +++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/data/progress.txt b/data/progress.txt index 48b9fb04..bd237989 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240510: 125 20240314: 647 20240313: 338,347,371,417(again),424(again),435,572(again) 20240312: 297(again),300,322(again),323 diff --git a/data/to_review.txt b/data/to_review.txt index 7909e635..f9b82e78 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,3 +1,12 @@ +2024-07-04 -> ['125'] +2024-06-13 -> ['125'] +2024-05-31 -> ['125'] +2024-05-23 -> ['125'] +2024-05-18 -> ['125'] +2024-05-15 -> ['125'] +2024-05-13 -> ['125'] +2024-05-12 -> ['125'] +2024-05-11 -> ['125'] 2024-05-07 -> ['338,347,371,417(again),424(again),435,572(again)'] 2024-05-06 -> ['297(again),300,322(again),323'] 2024-05-05 -> ['261(again!!!),268,269(again),271,295(again)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java b/leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java index cc643604..3cda834e 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java +++ b/leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java @@ -24,6 +24,29 @@ public boolean isPalindrome(String s) { return true; } + StringBuilder sb = new StringBuilder(); + for (int idx = 0; idx < s.length(); idx++){ + if (Character.isLetterOrDigit(s.charAt(idx))){ + sb.append(String.valueOf(s.charAt(idx)).toUpperCase()); + } + } + + // NOTE : should use equals (so compare value but not memory address) + /** + * The issue in your code is with the comparison of strings using ==. + * In Java, you should use the equals method to compare strings for equality. + * Here's the corrected code: + */ + return sb.toString().equals(sb.reverse().toString()); + } + + // V0' + public boolean isPalindrome_0(String s) { + + if (s == null || s.length() == 0){ + return true; + } + String sUpper = ""; for (int idx = 0; idx < s.length(); idx++){ if (Character.isLetterOrDigit(s.charAt(idx))){ diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index 94e06e91..15f17cff 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -19,6 +19,27 @@ public static void main(String[] args) { for (ThreadInfo threadInfo : threadInfos) { System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName()); } + } + + // LC 125 + public boolean isPalindrome(String s) { + + if (s == null || s.length() == 0){ + return true; + } + String s_updated = ""; + StringBuilder sb = new StringBuilder(); + //String[] s_split = s.split(","); + char[] array = s.toCharArray(); + for (char x : array){ + String x_ = Character.toString(x); + if (x_ != null && x_ != "" && Character.isAlphabetic(x)){ + sb.append(x_.toLowerCase()); + } + } + System.out.println(sb.toString()); + return sb.toString() == sb.reverse().toString(); } + }