• Hash Table Mock


    242. Valid Anagram

    Solved

    Easy

    Topics

    Companies

    Given two strings s and t, return true if t is an anagram of s, and false otherwise.

    An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

    1. class Solution {
    2. public boolean isAnagram(String s, String t) {
    3. if(s.length() != t.length()){
    4. return false;
    5. }
    6. Map map = new HashMap<>();
    7. for(int i = 0; i < s.length(); i++){
    8. map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);
    9. }
    10. for(int i = 0; i < s.length(); i++){
    11. map.put(t.charAt(i),map.getOrDefault(t.charAt(i),0)-1);
    12. if(map.get(t.charAt(i))<0){
    13. return false;
    14. }
    15. }
    16. return true;
    17. }
    18. }
    1. class Solution {
    2. public boolean isAnagram(String s, String t) {
    3. if(s.length() != t.length()){
    4. return false;
    5. }
    6. int[] counts = new int[26];
    7. char[] chrs = s.toCharArray();
    8. char[] chrt = t.toCharArray();
    9. for(int i = 0; i < s.length(); i++){
    10. counts[chrs[i]-'a']++;
    11. counts[chrt[i]-'a']--;
    12. }
    13. for(int i = 0; i<26; i++){
    14. if(counts[i] != 0){
    15. return false;
    16. }
    17. }
    18. return true;
    19. }
    20. }

    349. Intersection of Two Arrays

    Solved

    Easy

    Topics

    Companies

    Given two integer arrays nums1 and nums2, return an array of their 

    intersection

    . Each element in the result must be unique and you may return the result in any order.

    1. class Solution {
    2. public int[] intersection(int[] nums1, int[] nums2) {
    3. Set set1 = new HashSet<>();
    4. Set resSet = new HashSet<>();
    5. //遍历数组1
    6. for (int i : nums1) {
    7. set1.add(i);
    8. }
    9. //遍历数组2的过程中判断哈希表中是否存在该元素
    10. for (int i : nums2) {
    11. if (set1.contains(i)) {
    12. resSet.add(i);
    13. }
    14. }
    15. int[] arr = new int[resSet.size()];
    16. int j = 0;
    17. for(int i : resSet){
    18. arr[j++] = i;
    19. }
    20. return arr;
    21. }
    22. }
    1. class Solution {
    2. public int[] intersection(int[] nums1, int[] nums2) {
    3. List list = new ArrayList();
    4. int[] counts = new int[1001];
    5. for(int i = 0; i < nums1.length; i++){
    6. counts[nums1[i]] = 1;
    7. }
    8. for(int i = 0; i < nums2.length; i++){
    9. if(counts[nums2[i]] == 1){
    10. list.add(nums2[i]);
    11. counts[nums2[i]] = 2;
    12. }
    13. }
    14. int[] ans = new int[list.size()];
    15. for(int i = 0; i < list.size();i++){
    16. ans[i] = list.get(i);
    17. }
    18. return ans;
    19. }
    20. }

    202. Happy Number

    Solved

    Easy

    Topics

    Companies

    Write an algorithm to determine if a number n is happy.

    happy number is a number defined by the following process:

    • Starting with any positive integer, replace the number by the sum of the squares of its digits.
    • Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
    • Those numbers for which this process ends in 1 are happy.

    Return true if n is a happy number, and false if not.

    Example 1:

    Input: n = 19
    Output: true
    Explanation:
    12 + 92 = 82
    82 + 22 = 68
    62 + 82 = 100
    12 + 02 + 02 = 1
    1. class Solution {
    2. public boolean isHappy(int n) {
    3. Set set = new HashSet();
    4. while(n != 1 && !set.contains(n)){
    5. set.add(n);
    6. n = getNextNumber(n);
    7. }
    8. return n == 1;
    9. }
    10. private int getNextNumber(int n){
    11. int res = 0;
    12. while(n > 0){
    13. int temp = n % 10;
    14. res += temp * temp;
    15. n = n/10;
    16. }
    17. return res;
    18. }
    19. }

    1. Two Sum

    Solved

    Easy

    Topics

    Companies

    Hint

    Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

    You may assume that each input would have exactly one solution, and you may not use the same element twice.

    You can return the answer in any order.

    1. class Solution {
    2. public int[] twoSum(int[] nums, int target) {
    3. Map map = new HashMap();
    4. for(int i = 0; i < nums.length; i++){
    5. if(map.containsKey(target-nums[i])){
    6. return new int[]{i,map.get(target-nums[i])};
    7. }
    8. map.put(nums[i],i);
    9. }
    10. return null;
    11. }
    12. }

    383. Ransom Note

    Solved

    Easy

    Topics

    Companies

    Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

    Each letter in magazine can only be used once in ransomNote.

    1. class Solution {
    2. public boolean canConstruct(String ransomNote, String magazine) {
    3. int[] counts = new int[26];
    4. for(int i = 0; i < magazine.length(); i++){
    5. counts[magazine.charAt(i)-'a']++;
    6. }
    7. for(int i = 0; i < ransomNote.length(); i++){
    8. counts[ransomNote.charAt(i)-'a']--;
    9. if(counts[ransomNote.charAt(i)-'a'] < 0){
    10. return false;
    11. }
    12. }
    13. return true;
    14. }
    15. }
    1. class Solution {
    2. public boolean canConstruct(String ransomNote, String magazine) {
    3. if (ransomNote.length() > magazine.length()) {
    4. return false;
    5. }
    6. Map map = new HashMap<>();
    7. for (char ch : magazine.toCharArray()) {
    8. map.put(ch, map.getOrDefault(ch, 0) + 1);
    9. }
    10. for (char ch : ransomNote.toCharArray()) {
    11. map.put(ch, map.getOrDefault(ch, 0) - 1);
    12. if (map.get(ch) < 0) {
    13. return false;
    14. }
    15. }
    16. return true;
    17. }
    18. }

    454. 4Sum II

    Solved

    Medium

    Topics

    Companies

    Given four integer arrays nums1nums2nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:

    • 0 <= i, j, k, l < n
    • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
    1. class Solution {
    2. public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
    3. HashMap map = new HashMap<>();
    4. for(int num1: nums1){
    5. for(int num2: nums2){
    6. map.put(num1+num2, map.getOrDefault(num1+num2, 0) + 1);
    7. }
    8. }
    9. int res = 0;
    10. for(int num3: nums3){
    11. for(int num4: nums4){
    12. res += map.getOrDefault(-(num3+num4), 0);//只写get可能会返回null
    13. }
    14. }
    15. return res;
    16. }
    17. }

    49. Group Anagrams

    Solved

    Medium

    Topics

    Companies

    Given an array of strings strs, group the anagrams together. You can return the answer in any order.

    An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

    Example 1:

    Input: strs = ["eat","tea","tan","ate","nat","bat"]
    Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
    1. class Solution {
    2. public List> groupAnagrams(String[] strs) {
    3. Map> map = new HashMap>();
    4. for (String str : strs) {
    5. char[] array = str.toCharArray();
    6. Arrays.sort(array);
    7. String key = new String(array);
    8. List list = map.getOrDefault(key, new ArrayList());
    9. list.add(str);
    10. map.put(key, list);
    11. }
    12. return new ArrayList>(map.values());
    13. }
    14. }
    1. class Solution {
    2. public List> groupAnagrams(String[] strs) {
    3. Map> map = new HashMap>();
    4. for (String str : strs) {
    5. int[] counts = new int[26];
    6. int length = str.length();
    7. for (int i = 0; i < length; i++) {
    8. counts[str.charAt(i) - 'a']++;
    9. }
    10. // 将每个出现次数大于 0 的字母和出现次数按顺序拼接成字符串,作为哈希表的键
    11. StringBuffer sb = new StringBuffer();
    12. for (int i = 0; i < 26; i++) {
    13. if (counts[i] != 0) {
    14. sb.append((char) ('a' + i));
    15. sb.append(counts[i]);
    16. }
    17. }
    18. String key = sb.toString();
    19. List list = map.getOrDefault(key, new ArrayList());
    20. list.add(str);
    21. map.put(key, list);
    22. }
    23. return new ArrayList>(map.values());
    24. }
    25. }

    128. Longest Consecutive Sequence

    Solved

    Medium

    Topics

    Companies

    Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

    You must write an algorithm that runs in O(n) time.

    Example 1:

    Input: nums = [100,4,200,1,3,2]
    Output: 4
    Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
    

    Example 2:

    Input: nums = [0,3,7,2,5,8,4,6,0,1]
    Output: 9
    1. class Solution {
    2. public int longestConsecutive(int[] nums) {
    3. Set numSet = new HashSet();
    4. for (int num : nums) {
    5. numSet.add(num);
    6. }
    7. int longestLength = 0;
    8. for (int num : numSet) {
    9. if (!numSet.contains(num - 1)) {
    10. int currentNum = num;
    11. int currentLength = 1;
    12. while (numSet.contains(currentNum + 1)) {
    13. currentNum += 1;
    14. currentLength += 1;
    15. }
    16. longestLength = Math.max(longestLength, currentLength);
    17. }
    18. }
    19. return longestLength;
    20. }
    21. }

  • 相关阅读:
    bin-editor-next实现josn序列化
    C语言——结构体(位段)、联合体、枚举
    手部关键点检测4:Android实现手部关键点检测(手部姿势估计)含源码 可实时检测
    WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
    【PyCharm中PIL/Pillow的安装】
    汽车产业与技术链分析
    JVM垃圾回收算法
    宝塔部署node项目
    acme在同一台服务器上设置多个Ali_key实现自动ssl申请和续期
    Placement Rules 使用文档
  • 原文地址:https://blog.csdn.net/weixin_65279006/article/details/137409470