首页 > 其他分享 >哈希表 part 1

哈希表 part 1

时间:2023-08-25 13:57:08浏览次数:35  
标签:return target int num item part 哈希 type

相关阅读:https://docs.qq.com/doc/DUEtFSGdreWRuR2p4

当遇到了要快速判断一个元素是否出现集合里的时候,就需要考虑哈希法。

1. 两数之和

def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        d = {} 
        #x 为当前数字 j 为下标
        for j, x in enumerate(nums) :
            #target - x 为当前的数字与target的差
            if target - x in d :
                #返回字典中target-x的位置和当前x的下标j 
                return [d[target-x], j]
            else: 
                #如果差值不存在当前字典中,将j存入字典的value中
                d[x] = j 

242: 

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        d = {} 
        for item in s: 
            if item in d: 
                d[item] += 1 
            else: 
                d[item] = 1 
            
        d1 = {} 
        for item in t: 
            if item in d1: 
                d1[item] += 1 
            else:
                d1[item] = 1
        
        if d == d1 :
            return True
        else: 
            return False

349: 

class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        d={} 
        for i in nums1:
            if i not in d:
                d[i] = 1 
        res=set() 
        for num in nums2:
            if num in d:
                res.add(num) 
                
        return list(res)

202.

#建立一个集合,用来装已经算过的数,然后一个循环:
#如果 n == 1 return true 
#else: n在集合中出现过,说明进入死循环, return false 
#and 将n 放入集合中,进入下一次循环 
def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        record = set() 
        while n not in record :
            record.add(n) 
            new_num = 0 
            n_str = str(n) 
            for in n_str:
                new_num += int(i)**2 
            
            if new_num == 1 :
                return True 
            else: 
                n = new_num
        
        return False 

 

标签:return,target,int,num,item,part,哈希,type
From: https://www.cnblogs.com/yuhao0451/p/17656726.html

相关文章

  • 创建第一个Django app-part5
    自动化测试开始第一个测试首先有一个bugpython3manage.pyshell创建一个测试来暴露这个bug将下面的代码写入polls应用里的tests.py文件内点击查看代码fromdjango.testimportTestCase#Createyourtestshere.importdatetimefromdjango.utilsimporttim......
  • Python数据结构:哈希表
    哈希散列(哈希)是电脑科学中一种对资料的处理方法,通过某种特定的函数/算法(称为散列函数/算法)将要检索的项与用来检索的索引(称为散列,或者散列值)关联起来,生成一种便于搜索的数据结构(称为散列表)。哈希表是什么哈希表(散列表)是根据键(Key)直接访问内存存储位置的数据结构。根据键(Key)值......
  • [代码随想录]Day26-回溯算法part06
    题目:332.重新安排行程思路:其实这里已经是图的部分了,回溯应该也可以。Hierholzer算法解决欧拉问题代码:funcfindItinerary(tickets[][]string)[]string{var(m=map[string][]string{}res[]string)for_,ticket:=rangeticket......
  • day15 - 二叉树 part02
    102. 二叉树的层序遍历详解/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*right;*TreeNode():val(0),left(nullptr),right(nullptr){}*TreeNode(intx):val(x),left(nullp......
  • Vue学习笔记:Pinia Part02 Store
    在Pinia中有option和setup两种写法OptionStore与Vue的选项式API类似,我们也可以传入一个带有 state、actions 与 getters 属性的Option对象exportconstuseCounterStore=defineStore('counter',{state:()=>({count:0}),getters:{double:(state)......
  • [代码随想录]Day25-回溯算法part05
    题目:491.递增子序列思路:核心问题——同层去重,这一题不能够重新排序因此不可以用i>index&&nums[i]==nums[i-1]来去重,而是每一层开一个map来判断该数是否出现过代码:var(res[][]intpath[]int)funcfindSubsequences(nums[]int)[][]int{res=make(......
  • 什么是 Spartacus Storefront B2B store 的 My Company 菜单
    Spartacus是一个基于Angular的开源JavaScriptweb应用,与SAPCommerceCloud的RESTAPI进行交互。它的目标是提供一个稳定、可靠、可扩展的前端解决方案,让开发者能够创建全功能的商店,同时避免了与后端系统的紧密耦合。其中,B2Bstore是一个专门为B2B交易设计的商店,而My......
  • 哈希,列表,集合,有序集合,慢查询,pipeline,发布订阅,bitmap位图,Hyperloglog
    目录1哈希类型2列表类型3集合类型4有序集合(zset)5慢查询6pipeline与事务7发布订阅8Bitmap位图9HyperLogLog1哈希类型###1---hget,hset,hdelhgetkeyfield#获取hashkey对应的field的value时间复杂度为o(1)hsetkeyfieldvalue#设置hashkey对应的field的value......
  • day14 - 二叉树part01
    144. 二叉树的前序遍历详解/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*right;*TreeNode():val(0),left(nullptr),right(nullptr){}*TreeNode(intx):val(x),left(nullp......
  • Vue学习笔记:Pinia Part01
    介绍Pinia是Vue的专属状态管理库,它允许你跨组件或页面共享状态。如果你熟悉组合式API的话,你可能会认为可以通过一行简单的 exportconststate=reactive({}) 来共享一个全局状态。对于单页应用来说确实可以,但如果应用在服务器端渲染,这可能会使你的应用暴露出一些安全漏洞......