mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #72 from zqh1059405318/master
添加0202.快乐数,0242.有效的字母异味词,0349.两个数组的交集 Java版本。
This commit is contained in:
@ -84,7 +84,28 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public boolean isHappy(int n) {
|
||||
Set<Integer> 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:
|
||||
|
||||
|
@ -85,7 +85,25 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
```java
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
@ -75,6 +75,7 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
```Java
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
Reference in New Issue
Block a user