Day07-(哈希表,双指针的使用,难度逐层升高)

一、454. 四数相加 II - 力扣(LeetCode)

        a、暴力解法

                四个for循环嵌套!!!时间复杂度O(n^4),不推荐哈。

        b、分部循环

                将nums1和nums2凑一组,再将nums3和nums4凑一组,两两for循环得出一组数字,存入map容器(为什么要用map容器:算出的第一组数字里不仅需要记录数字的数值还要记录该数字的个数用以计算最后组合的个数)

class Solution 
{
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) 
    {
        Map map = new HashMap<>();
        int count = 0;
        int n = nums1.length;

        // 计算nums1和nums2所有元素和的组合,并存储在哈希表中
        for (int i = 0; i < n; i++)
         {
            for (int j = 0; j < n; j++) 
            {
                int sum = nums1[i] + nums2[j];
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }

        // 遍历nums3和nums4,查找哈希表中是否有相反数存在
        for (int i = 0; i < n; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                int sum = nums3[i] + nums4[j];
                count += map.getOrDefault(-sum, 0);
            }
        }

        return count;
    }
}

二、383. 赎金信 - 力扣(LeetCode)

       这题的思路同Day06第一题242. 有效的字母异位词 - 力扣(LeetCode)类似,此处不讲解。

        a、map容器:

class Solution
{
    public boolean canConstruct(String ransomNote, String magazine)
    {
        Map map = new HashMap<>();
        for(int i = 0 ; i < magazine.length() ; i++)
        {
            map.put(magazine.charAt(i),map.getOrDefault(magazine.charAt(i),0) +1 );
        }
        for(int i = 0 ; i < ransomNote.length() ; i++)
        {
            if(map.containsKey(ransomNote.charAt(i)) && map.get(ransomNote.charAt(i))!=0)
            {
                map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i)) -1 );
            }
            else
            {
                return false;
            }
        }
        return true;
    }
}

        b、数组容器:

class Solution
{
    public boolean canConstruct(String ransomNote, String magazine)
    {
        int[] arr = new int[26];
        for(int i = 0 ; i < magazine.length() ; i++)
        {
            arr[magazine.charAt(i) - 'a']++;
        }
        for(int i = 0 ; i < ransomNote.length() ; i++)
        {   
            if(arr[ransomNote.charAt(i) - 'a'] != 0)
            {
                arr[ransomNote.charAt(i) - 'a']--;
            }
            else
            {
                return false;
            }
            
        }
        return true;
    }
}

三、15. 三数之和 - 力扣(LeetCode)

        a、暴力解法:三层for循环遍历所有组合

        b、双指针法:一层for循环,用一组双指针替代两层for循环。left指针从for循环下标index下一个开始,right从数组最后开始。难点(去重,剪枝)

class Solution
{
    public List> threeSum(int[] nums)
    {
        
        List> res = new ArrayList>();
        Arrays.sort(nums);
        for(int i = 0 ; i < nums.length - 2 ; i++)
        {           
            if(nums[i] > 0)
            {
                return res;
            }
            if(i > 0 && nums[i] == nums[i-1] )
            {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while(left  < right)
            {
                int sum = nums[i] + nums[left] + nums[right];
                if(sum < 0)
                {
                    left++;
                }
                else if(sum > 0)
                {
                    right--;
                }
                else
                {
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    while (left < right && nums[left] == nums[left + 1]) 
                    {
                        left++;
                    }
                    while (left < right && nums[right] == nums[right - 1])
                    {
                        right--;    
                    } 
                    left++;
                    right--;
                }
            }
        }


        return res;
    }
}

        这题的思路同样可以使用在Day06的1. 两数之和 - 力扣(LeetCode)中,但是在今天这题求的是数值,而Day06这题需要求的是下标,因此需要多一步骤处理。

class Solution 
{
    public int[] twoSum(int[] nums, int target) 
    {
        int[] res = new int[2];  
        int[][] newNums = new int[nums.length][2];
        for(int i = 0 ; i < nums.length ; i++)
        {
            newNums[i][0] = nums[i];
            newNums[i][1] = i;
        }
        Arrays.sort(newNums, (a, b) -> Integer.compare(a[0], b[0]));
        for(int i = 0 ; i < nums.length ; i++)
        {
            int j = newNums.length - 1;          
            while(i < j)
            {
                int sum = newNums[i][0] + newNums[j][0];
                if(sum > target)
                {
                    j--;
                }
                else if(sum < target)
                {
                    break;
                }
                else
                {
                    res[0] = newNums[i][1];
                    res[1] = newNums[j][1];
                    return res;
                }
            }
            
        }
        return res;
    }
}

四、18. 四数之和 - 力扣(LeetCode)

        完全类似上一题,加一层for循环处理第一个数,然后最后三个数当三数之和来做,要注意剪枝和去重步骤。

class Solution 
{
    public List> fourSum(int[] nums, int target) 
    {
        List list = new ArrayList<>();
        List> ans = new ArrayList<>();
        Arrays.sort(nums);
        for(int i = 0; i < nums.length ; i++)
        {
            for(int j = i + 1 ; j < nums.length ; j++)
            {
                if (nums[i] > target && nums[i] >= 0) 
                {
                    break;
                }
                if(i > 0 && nums[i] == nums[i-1] )
                {
                    continue;
                }
                if( j >i+1 && nums[j] == nums[j-1])
                {
                    continue;
                }
                int left = j+1;
                int right = nums.length - 1;
                while(left < right)
                {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum < target)
                    {
                        left++;
                    }
                    else if(sum > target)
                    {
                        right--;
                    }
                    else
                    {
                        ans.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while(left < right && nums[left] == nums[left + 1])
                        {
                            left++;
                        }
                        while(left < right && nums[right] == nums[right - 1])
                        {
                            right--;
                        }
                        left++;
                        right--;
                    }
                }
            }
        }
        return ans; 
    }
}

你可能感兴趣的:(散列表,数据结构,java,算法,leetcode)