From f1fa019b4a65a69bf92caf1041c05661add307c2 Mon Sep 17 00:00:00 2001 From: zqh1059405318 <1059405318@qq.com> Date: Thu, 13 May 2021 10:16:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4242=E9=A2=98java=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index be553c5a..6c5a90ce 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -85,7 +85,24 @@ public: Java: +```java +public boolean isAnagram(String s, String t) { + int[] record = new int[26]; + for (char c : s.toCharArray()) { + record[c - 'a'] += 1; + } + for (char c : t.toCharArray()) { + record[c - 'a'] -= 1; + } + for (int i : record) { + if (i != 0) { + return false; + } + } + return true; +} +``` Python: From 447262eeffd5e08c266b14e6ec52b24372efebaf Mon Sep 17 00:00:00 2001 From: zqh1059405318 <1059405318@qq.com> Date: Thu, 13 May 2021 11:31:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E7=AC=AC202?= =?UTF-8?q?=EF=BC=8C242=EF=BC=8C349=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 21 ++++++++++++++++++ problems/0242.有效的字母异位词.md | 27 ++++++++++++----------- problems/0349.两个数组的交集.md | 24 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index d6b1cdd2..8c0dd1e7 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -84,7 +84,28 @@ public: Java: +```java +class Solution { + public boolean isHappy(int n) { + Set record = new HashSet<>(); + while (n != 1 && !record.contains(n)) { + record.add(n); + n = getNextNumber(n); + } + return n == 1; + } + private int getNextNumber(int n) { + int res = 0; + while (n > 0) { + int temp = n % 10; + res += temp * temp; + n = n / 10; + } + return res; + } +} +``` Python: diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 6c5a90ce..89186620 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -86,22 +86,23 @@ public: Java: ```java -public boolean isAnagram(String s, String t) { - int[] record = new int[26]; - for (char c : s.toCharArray()) { - record[c - 'a'] += 1; - } - for (char c : t.toCharArray()) { - record[c - 'a'] -= 1; - } - for (int i : record) { - if (i != 0) { - return false; +class Solution { + public boolean isAnagram(String s, String t) { + int[] record = new int[26]; + for (char c : s.toCharArray()) { + record[c - 'a'] += 1; } + for (char c : t.toCharArray()) { + record[c - 'a'] -= 1; + } + for (int i : record) { + if (i != 0) { + return false; + } + } + return true; } - return true; } - ``` Python: diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index c196b467..195dbad5 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -75,7 +75,31 @@ public: Java: +```java +class Solution { + public int[] intersection(int[] nums1, int[] nums2) { + Set record = new HashSet(); + Set unique = new HashSet(); + for (int num : nums1) { + record.add(num); + } + + for (int num : nums2) { + if (record.contains(num)) { + unique.add(num); + } + } + + int[] res = new int[unique.size()]; + int index = 0; + for (int num : unique) { + res[index++] = num; + } + return res; + } +} +``` Python: