242. Valid Anagram
Given two strings s and t, return true if t is an anagram of s , and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
题目解析:
- 使用map统计字符串中字符的频率
- 因为字符串中只包含小写英文字母,所以也可以使用数组来记录字符的频率
代码:
1. 使用hashmap
class Solution {
public boolean isAnagram(String s, String t) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (int i = 0; i < t.length(); i++) {
char c = t.charAt(i);
map.put(c, map.getOrDefault(c, 0) - 1);
}
for (int v : map.values()) {
if (v != 0) {
return false;
}
}
return true;
}
}
2. 使用数组
class Solution {
public boolean isAnagram(String s, String t) {
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a'] += 1;
}
for (int i = 0; i < t.length(); i++) {
count[t.charAt(i) - 'a'] -= 1;
}
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
return false;
}
}
return true;
}
}
349. Intersection of Two Arrays
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
题目解析:
- 可以使用两个set求交集
s1.retainAll(s2), 也可以使用一个set存重复结果
代码:
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> result = new HashSet<>();
Set<Integer> set = new HashSet<>();
for (int val : nums1) {
set.add(val);
}
for (int val : nums2) {
if (set.contains(val)) {
result.add(val);
}
}
return result.stream().mapToInt(e -> e.intValue()).toArray();
}
}
202. Happy Number
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
- Starting with any positive integer, replace the number by the sum of the squares of its digits.
- Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
- Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
题目解析:
- 重复计算过程的结果有两种:1 和 重复无限循环
- 所以需要set判断是否发生循环
代码:
class Solution {
public boolean isHappy(int n) {
int val = n;
Set<Integer> set = new HashSet<>();
while (!set.contains(val)) {
set.add(val);
String s = String.valueOf(val);
val = 0;
for (int i = 0; i < s.length(); i++) {
// val += Math.pow(Integer.parseInt(String.valueOf(s.charAt(i))), 2);
val += Math.pow(Character.getNumericValue(s.charAt(i)), 2);
}
}
if (val == 1) {
return true;
}
return false;
}
}
1. Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
题目解析:
- 使用hashmap存储
代码:
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (!map.containsKey(nums[i])) {
map.put(target - nums[i], i);
} else {
return new int[]{map.get(nums[i]), i};
}
}
return null;
}
}