mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
更新了第202,242,349题
This commit is contained in:
@ -84,7 +84,28 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
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:
|
Python:
|
||||||
|
|
||||||
|
@ -86,7 +86,8 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
```java
|
```java
|
||||||
public boolean isAnagram(String s, String t) {
|
class Solution {
|
||||||
|
public boolean isAnagram(String s, String t) {
|
||||||
int[] record = new int[26];
|
int[] record = new int[26];
|
||||||
for (char c : s.toCharArray()) {
|
for (char c : s.toCharArray()) {
|
||||||
record[c - 'a'] += 1;
|
record[c - 'a'] += 1;
|
||||||
@ -100,8 +101,8 @@ public boolean isAnagram(String s, String t) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
@ -75,7 +75,31 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int[] intersection(int[] nums1, int[] nums2) {
|
||||||
|
Set<Integer> record = new HashSet<Integer>();
|
||||||
|
Set<Integer> unique = new HashSet<Integer>();
|
||||||
|
|
||||||
|
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:
|
Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user