242. 有效的字母异位词
class Solution {
public boolean isAnagram(String s, String t) {
int[] count = new int[26];
for(char c : s.toCharArray())
count[c-'a']++;
for(char d : t.toCharArray())
count[d-'a']--;
for(int i : count)
{
if(i!=0) return false;
}
return true;
}
}
349. 两个数组的交集
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1 == null || nums2==null) return new int[0];
Set<Integer> set1 = new HashSet<>();
Set<Integer> ans_set = new HashSet<>();
for(int i :nums1) set1.add(i);
for(int i:nums2)
{
if(set1.contains(i))
ans_set.add(i);
}
int[] ans_arr = new int[ans_set.size()];
int index=0;
for(int i : ans_set)
{
ans_arr[index++]=i;
}
return ans_arr;
}
}
202. 快乐数
class Solution {
public boolean isHappy(int n) {
Set<Integer> ans = new HashSet<>();
while(n!=1&&!ans.contains(n))
{
ans.add(n);
n=Nextnumber(n);
}
return n==1;
}
private int Nextnumber(int n)
{
int ans=0;
while(n>0)
{
int temp = n%10;
ans+=temp*temp;
n=n/10;
}
return ans;
}
}
1. 两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] ans = new int[2];
if(nums==null||nums.length==0) return ans;
Map<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++)
{
int temp= target-nums[i];
if(map.containsKey(temp))
{
ans[1]=i;
ans[0]=map.get(temp);
}
map.put(nums[i],i);
}
return ans;
}
}
454. 四数相加 II
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> map = new HashMap<>();
int count=0;
int temp;
for(int i:nums1)
{
for(int j:nums2)
{
temp= i+j;
if(map.containsKey(temp))
{
map.put(temp,map.get(temp)+1);
}
else
map.put(temp,1);
}
}
for(int i:nums3)
{
for(int j:nums4)
{
temp=i+j;
if(map.containsKey(0-temp))
count+=map.get(0-temp);
}
}
return count;
}
}
383. 赎金信
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] count = new int[26];
for(int i=0;i<magazine.length();i++)
{
count[magazine.charAt(i)-'a']++;
}
for(int j=0;j<ransomNote.length();j++)
{
count[ransomNote.charAt(j)-'a']--;
if(count[ransomNote.charAt(j)-'a']<0) return false;
}
return true;
}
}
15. 三数之和
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length;i++)
{
if(nums[i]>0) return ans;
if(i>0&&nums[i-1]==nums[i]) continue;
int left=i+1,right=nums.length-1;
while(left<right)
{
if(nums[left]+nums[i]+nums[right]>0)
right--;
else if(nums[left]+nums[i]+nums[right]<0)
left++;
else
{
List<Integer> temp = new ArrayList<>();
temp.add(nums[left]);
temp.add(nums[i]);
temp.add(nums[right]);
ans.add(temp);
while(left<right&&nums[left]==nums[left+1])
left++;
while(left<right&&nums[right]==nums[right-1])
right--;
left++;
right--;
}
}
}
return ans;
}
}
18.四数之和
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for(int i=0; i<nums.length;i++)
{
if(i>0&&nums[i-1]==nums[i])
continue;
for(int j = i+1; j<nums.length;j++)
{
if(j>i+1&&nums[j-1]==nums[j])
continue;
int left = j+1, right= nums.length-1;
while(left<right)
{
if(nums[i]+nums[j]+nums[left]+nums[right]>target)
right--;
else if(nums[i]+nums[j]+nums[left]+nums[right]<target)
left++;
else
{
List<Integer> temp = new ArrayList<>();
temp = Arrays.asList(nums[i],nums[j],nums[left],nums[right]);
ans.add(temp);
while(left<right&&nums[left]==nums[left+1])
left++;
while(left<right&&nums[right]==nums[right-1])
right--;
left++;
right--;
}
}
}
}
return ans;
}
}