class Solution {
public boolean isAnagram(String s, String t) {
int[] record = new int[26];
for (int i = 0; i < s.length(); i++) {
record[ s.charAt(i) - 'a' ] ++;
}
for (int i = 0; i < t.length(); i++) {
record[ t.charAt(i) - 'a' ] --;
if (record[ t.charAt(i) - 'a' ] == -1) return false;
}
for (int i : record) {
if (i != 0) {
return false;
}
}
return true;
}
}
349. 两个数组的交集
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[] record = new int[1001];
int index = 0;
Set<Integer> tem_result = new HashSet<Integer>();
for (int i = 0; i < nums1.length; i++){
record[ nums1[i] ] = 1;
}
for (int i = 0; i <nums2.length; i++) {
if (record[nums2[i]] == 1) {
tem_result.add(nums2[i]);
}
}
int[] result = new int[tem_result.size()];
for (int i : tem_result) {
result[index++ ] = i;
}
return result;
}
}
202. 快乐数
class Solution {
public boolean isHappy(int n) {
Set<Integer> record = new HashSet<Integer>();
while(true) {
if ( n == 1) return true;
if ( record.contains(n) ) return false;
else record.add(n);
n = getNextNumber(n);
}
}
private int getNextNumber(int n) {
int sum = 0;
while (n != 0) {
sum += (n%10) * (n%10);
n = n/10;
}
return sum;
}
}
1. 两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
break;
}
map.put(nums[i], i);
}
return res;
}
}