public boolean isAnagram1(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int[] alpha=new int[26];
for(int i=0;i<s.length();i++){
alpha[s.charAt(i)-'a']++;
alpha[t.charAt(i)-'a']--;
}
for(int j:alpha){
if(j!=0){
return false;
}
}
return true;
}
public boolean isAnagram2(String s, String t) {
char[] sChars = s.toCharArray();
char[] tChars = t.toCharArray();
Arrays.sort(sChars);
Arrays.sort(tChars);
return Arrays.equals(sChars, tChars);
}
public boolean isAnagram3(String s, String t) {
Map<Character,Integer> map=new HashMap<>();
char[] sChars=s.toCharArray();
char[] tChars=t.toCharArray();
for(char ch:sChars){
map.put(ch,map.getOrDefault(ch,0)+1);
}
for(char ch:tChars){
Integer count=map.get(ch);
if(count==null){
return false;
}else if(count>1){
map.put(ch,count-1);
}else{
map.remove(ch);
}
}
return map.isEmpty();
}
public int[] intersection1(int[] nums1, int[] nums2) {
Set<Integer> resSet=new HashSet<>();
Set<Integer> set=new HashSet<>();
for(int i:nums1){
set.add(i);
}
for(int j:nums2){
if(set.contains(j)){
resSet.add(j);
}
}
int[] res=new int[resSet.size()];
int i=0;
for(int j:resSet){
res[i]=j;
i++;
}
return res;
}
//双指针
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> resSet=new HashSet<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i=0,j=0;
while(i<nums1.length&&j<nums2.length){
if(nums1[i]==nums2[j]){
resSet.add(nums1[i]);
i++;
j++;
}else if(nums1[i]>nums2[j]){
j++;
}else if(nums1[i]<nums2[j]){
i++;
}
}
int[] res=new int[resSet.size()];
i=0;
for(int z:resSet){
res[i]=z;
i++;
}
return res;
}
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;
}
public int[] twoSum(int[] nums, int target) {
int[] res=new int[2];
if(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;
}