算法--哈希表

有效的字母异位词

/**
 * @Classname IsAnagram
 * @Description 有效的字母异位词: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
 * @Date 2022/7/5 16:22
 * @Created by 6407002802
 */
public class IsAnagram {
    public boolean isAnagram(String s, String t) {
        int[] records = new int[26];
        if (s.length() != t.length()) return false;
        for (int i = 0; i < s.length(); i++) {
            records[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            records[t.charAt(i) - 'a']--;
        }
        for (int i = 0; i < records.length; i++) {
            if (records[i] > 0) {
                return false;
            }
        }
        return true;
    }

    public boolean isAnagram2(String s, String t) {
        int[] records = new int[26];
        if (s.length() != t.length()) return false;
        for (char c : s.toCharArray()) {
            records[c - 'a']++;
        }
        for (char c : t.toCharArray()) {
            records[c - 'a']--;
        }
        for (int record : records) {
            if (record > 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        final IsAnagram isAnagram = new IsAnagram();
        final boolean anagram = isAnagram.isAnagram2("ate", "tae");
        System.out.println(anagram);
    }
}

两数之和, 三数之和, 四数之和

public class NumberSum {
    /***
     * @Title twoSum
     * @Description 两数之和
     *      给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标
     * @Author 6407002802
     * @Date 2022/7/6
     */
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        if (nums == null || nums.length < 2) return result;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            final int temp = target - nums[i];
            if (map.containsKey(temp)) {
                result[0] = i;
                result[1] = map.get(temp);
                break;
            }
            map.put(nums[i], i);
        }
        return result;
    }

    /**
     * @Description 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
     * 注意:答案中不可以包含重复的三元组。
     * @Title threeSum
     * @Date 2022/7/6
     */
    public List<List<Integer>> threeSum(int[] nums) {
        return null;
    }

    /***
     * @Description 给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):
     *
     *     0 <= a, b, c, d < n
     *     a、b、c 和 d 互不相同
     *     nums[a] + nums[b] + nums[c] + nums[d] == target
     * 你可以按 任意顺序 返回答案 。
     * @Title fourSum
     * @Date 2022/7/7
     */
    public List<List<Integer>> fourSum(int[] nums, int target) {
        final List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length < 4) return result;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < nums.length; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                int c = j + 1, d = nums.length - 1;
                while (c < d) {
                    long sum = (long)nums[i] + nums[j] + nums[c] + nums[d];
                    if (sum < target) {
                        c++;
                    } else if (sum > target) {
                        d--;
                    } else {
                        result.add(Arrays.asList(nums[i], nums[j], nums[c], nums[d]));
                        while (c < d && nums[c] == nums[c + 1]) c++;
                        while (c < d && nums[d] == nums[d - 1]) d--;
                        c++;
                        d--;
                    }
                }
            }
        }
        return result;
    }


    /***
     * @Title fourSumCount
     * @Description 四数相加: 给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
    1.0 <= i, j, k, l < n
    2.nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
     * @Date 2022/7/6
     */
    public int fourNumberAdd(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> tempMap = new HashMap<>();
        int temp;
        for (int a : nums1) {
            for (int b : nums2) {
                temp = a + b;
                if (tempMap.containsKey(temp)) tempMap.put(a + b, tempMap.get(temp) + 1);
                else tempMap.put(temp, 1);
            }
        }
        int count = 0;

        for (int c : nums3) {
            for (int d : nums4) {
                temp = -c - d;
                if (tempMap.containsKey(temp)) {
                    count += tempMap.get(temp);
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        final NumberSum numberSum = new NumberSum();
        //final int[] ints = numberSum.twoSum(new int[]{1, 2, 3, 4, 5, 6, 7, 9}, 10);
        //System.out.println(Arrays.toString(ints));

        //final int i = numberSum.fourNumberAdd(new int[]{1, 2}, new int[]{-2, -1}, new int[]{-1, 2}, new int[]{0, 2});
        //System.out.println(i);

        final List<List<Integer>> lists = numberSum.fourSum(new int[]{1000000000, 1000000000, 1000000000, 1000000000},
                -294967296);
        lists.forEach(list -> System.out.println(list));

    }
}

赎金信


/**
 * @Classname RansomLetter
 * @Description 赎金信: 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom
 * 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
 * @Date 2022/7/6 16:13
 * @Created by 6407002802
 */
public class RansomLetter {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] chs = new int[26];
        for (char c : magazine.toCharArray()) {
            chs[c - 'a'] += 1;
        }

        for (char c : ransomNote.toCharArray()) {
            chs[c - 'a'] -= 1;
        }
        for (int count : chs) {
            if (count < 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        final RansomLetter ransomLetter = new RansomLetter();
        System.out.println(ransomLetter.canConstruct("aa", "aab"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值