首页 > 其他分享 >不知道什么时候用Hash表,学完后,我得到了答案 ---> 判断一个元素是否出现 , 就要考虑哈希法

不知道什么时候用Hash表,学完后,我得到了答案 ---> 判断一个元素是否出现 , 就要考虑哈希法

时间:2022-11-27 23:55:18浏览次数:28  
标签:Hash int --- ++ length 哈希 new answer return

242.有效的字母异位词

怎么硕呢?
虽然我想到了可以用表去存每个字母的个数,所以一开始,我用了这种算法,
我将其锐评为: 傻子方法:用两个表
思路就是::建两个表,然后遍历对比

public boolean isAnagram1(String s, String t) {
        // 傻子方法:用两个表
        int[] sHashCount = new int[26], tHashCount = new int[26];
        if (s == null || t == null) {
            return false;
        }
        if (s.length() != t.length()) {
            return false;
        }

        for (int i = 0; i < s.length(); i++) {
            char charS = s.charAt(i);
            char charT = t.charAt(i);
            sHashCount[charS - 'a']++;
            tHashCount[charT - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (sHashCount[i] != tHashCount[i]) {
                return false;
            }
        }
        return true;
    }

然后看答案之后,我发现可以只用一个表:
思路:第一个串遍历时, 我的Hash表用 ' + ',第二个串用 ' - ', 然后看是否全是0

/**
     * 聪明人用的方法
     */
public boolean isAnagram(String s, String t) {
        int[] hashCount = new int[26];
        for (int i = 0; i < s.length(); i++) {
            hashCount[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            hashCount[t.charAt(i) - 'a']--;
        }
        for (int i : hashCount) {
            if (i == 0) {
                return false;
            }
        }
        return true;
    }

349. 两个数组的交集

public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for (int i : nums1) {
            set1.add(i);
        }
        for (int i : nums2) {
            if (set1.contains(i)) {
                set2.add(i);
            }
        }
//        Object[] array = set2.toArray();
//        int[] answer = new int[set2.size()];
//        for (int i = 0; i < set2.size(); i++) {
//            answer[i] = (int) array[i];
//        }

        return set2.stream().mapToInt(x -> x).toArray();
    }

202. 快乐数

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> hashSet = new HashSet<>();
        while (n != 1 && !hashSet.contains(n)) {
            hashSet.add(n);
            n = get(n);
        }
        return n==1;
    }

    public int get(int n) {
        int sum = 0;
        int tmp;
        while (n != 0) {
            tmp = n % 10;
            sum += tmp * tmp;
            n = n / 10;
        }
        return sum;
    }
}

1. 两数之和

public int[] twoSum(int[] nums, int target) {
        int[] answer = new int[2];
        if(nums == null || nums.length==0){
            return answer;
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int need = target - nums[i];
            if (map.containsKey(need)) {
                answer[0] = map.get(need);
                answer[1] = i;
                break;
            }
            map.put(nums[i], i);
        }
        return answer;
    }

标签:Hash,int,---,++,length,哈希,new,answer,return
From: https://www.cnblogs.com/Chain-Tian/p/16931063.html

相关文章

  • HTML基础-列表标签和表格标签
    HTML基础-列表标签和表格标签二、列表标签2.列表的应用场景Ø场景:在网页中按照行展示关联性的内容,如:新闻列表、排行榜、账单等Ø特点:按照行的方式,整齐显示内容Ø......
  • django-缓存
    缓存缓存的作用是缓解服务器压力,或者者说是数据库的压力,我们可以将一些常用的页面或数据放入缓存中,用户查询时,直接去缓存里面查,以此来缓解服务器压力 django提供的缓存......
  • 第15节-MySQL用户权限
    1、用户管理1.1、查询用户usemysql--5.6以下的版本selecthost,user,passwordfromuser;--5.7以上的版本selecthost,user,authentication_stringfromuser;......
  • 三. docker-compose 简介、基本命令及示例-1
    docker-compose使用简介、基于docker-compsoe实现Nginx+Java+Mysql服务部署docker-compose简介docker-compose项目是Docker官方的开源项目,负责实现对单机容器的快速编排,d......
  • DRF组件--认证
    DRF---认证功能自己定义一个认证类,其中类中必定要有authenticate方法classMyAuthentication():defauthenticate(self,request):token=request._reque......
  • 第四章-线程间的同步操作 4-4 使用同步操作简化代码
    1.Functionalprogrammingwithfutures使用future的函数式编程函数式编程指的是一种编程方式,其函数调用的结果只依赖于函数参数,而不依赖于任何其他外部状态。一个纯......
  • lc第319场周赛第三题-逐层排序二叉树所需的最少操作数目
    给你一个值互不相同的二叉树的根节点root。在一步操作中,你可以选择同一层上任意两个节点,交换这两个节点的值。返回每一层按严格递增顺序排序所需的最少操作数目......
  • 管理者能力模型学习-九五小庞
      一,业务管理1.制定计划和目标。对目标和计划的选择和制定,直接影响整个团队的工作成效,因此需要管理者对此有清晰的思考和明智的判断。2.过程监控。同样的工作由他......
  • 14-权限修饰符和代码块
    权限修饰符权限修饰符的分类理解权限修饰符的使用规则代码块局部代码块构造代码块当多个构造方法中有重复的代码时,我们可以写到构造代码......
  • 13-包和final
    包什么是包?使用其他类的规则小结final关键字final关键字的意思以及可以修饰的东西修饰后的效果被final修饰的父方法不能被子类重写使用场景......