寻找hash值——把int array看成是一个整数

QUESTION:

Write a class DominoChecker that has a method called addBox(int[]) that takes a box of five dominoes, described as a list of 10 integers (explained after), adds it to a collection, and returns true if a box with the same dominoes was already in the collection and false otherwise. A box of dominoes is encoded as a list of 10 integers from 0 to 9, where a pair of numbers represent a domino. For example: 0,2,9,1,3,3,7,4,5,6 represents a box containing dominoes: (0,2); (9,1); (3,3); (7,4); (5,6).

SOLUTION:

typedef pair< int,int > Domino;

class DominoChecker {
   unordered_set< long long > hash;
   vector< vector< Domino > > boxes;

  public:
    bool addBox(const vector< int >& box) {
        vector< Domino > v;
        for (int i = 0; i < 5; i++) {
            Domino d(box[2*i], box[2*i+1]);
            if (d.first > d.second)
                swap(d.first, d.second); // order does not matter
            v.push_back(d);
        }
        sort(v.begin(), v.end());  // order does not matter here as well.
        long long hash_value = 0;
        for (int i = 0 ; i < 5; i++)
             hash_value = hash_value * 100 + v[i].first*10 + v[i].second; //把int  array看成是一个整数
        if (hash.find(hash_value) != hash.end())
            return false;
        hash.insert(hash_value);
        boxes.push_back(v); // i suppose we want to store them
        return true;
    }
};
时间: 2024-08-03 14:15:31

寻找hash值——把int array看成是一个整数的相关文章

hash值

任何类都继承public int hashCode()方法,该方法返回的值是通过将该对象的内部地址转换为一个整数来实现的,hash表的主要作用就是在对对象进行散列的时候作为key输入.我们需要每个对象的hash码尽可能不同.Object类提供的默认实现确实保证每个对象的hash码不同. 对于集合类HashMap,HashSet和Hash有关的类,是通过hash算法来散列对象的. 对于hashset判断是不是重复对象通过equals方法判断,两个对象equal相等的时候,hashcode的返回值一

IPVS基于应用层任意偏移字段HASH值的负载均衡算法

在比较早的那些年,我曾经写了一个负载均衡调度算法模块,是基于应用层协议包任意偏移量开始的一段固定长度的数据计算一个值,然后将这个值hash到不同的服务器.那时觉得没啥用,就没有再继续,直到前一段时间的一段思考以及前几天的一次预研.我决定作文以记之,以后说不定能用得着. 1.UDP服务的负载均衡 以前使用UDP的服务很少,虽然HTTP并没有说一定要是TCP,但事实上几乎没有UDP上的HTTP.但是随着网络可靠性的增加,网络集中控制机制与分布式优化技术的日益成熟,使用UDP的场合越来越多.     

WebQQ hash值获取 C#方法 2014/06/20

去年心血来潮,利用闲暇时间做了一个WebQQ的桌面软件,基本功能实现之后,就放那儿了.webQQ的协议时常更新,导致有些参数加密的方法要跟着更新,今天群里一朋友提供了一份最新的WebQQ hash的js,我转成了C#的方法,记在这里,希望对正在做webqq的朋友有所帮助. js方法 p=getqqhsahs(b,j) { for (var a = j + "password error", i = "", E = [];;) if (i.length <= a

JAVA-读取文件部分内容计算HASH值

对于一些大文件,有时会需要计算部分内容的Hash,下面的函数计算了 文件头尾各1M,中间跳跃100M取10K 以及文件大小的Hash值 public static String CalHash(String path) throws IOException { File file = new File(path); if (!file.canRead()) return ""; if (file.length() < 150 * 1024 * 1024) { return &qu

Java 获取字符串Hash值

Java 生成字符串的Hash值: /** * A hashing method that changes a string (like a URL) into a hash suitable for using as a * disk filename. */ public static String hashKeyForDisk(String key) { String cacheKey; try { final MessageDigest mDigest = MessageDigest.g

webQQ 中hash值的计算 python实现 2015年7月

目前,在获取群列表时,需要post一个hash值,计算函数在js中,具体位置详见注释部分. 另外,此部分更改的频率很快,应该很快就变了. #!/usr/bin/env python # -*- coding: UTF-8 -*- def getHashCode(b, j): """ get the hash num to achieve the grouplist info (record:gcode) source function: http://0.web.qstati

【转】Java计算文件的hash值

原文地址:http://blog.csdn.net/qq_25646191/article/details/78863110 如何知道一个文件是否改变了呢?当然是用比较文件hash值的方法,文件hash又叫文件签名,文件中哪怕一个bit位被改变了,文件hash就会不同. 比较常用的文件hash算法有MD5和SHA-1.我用的是MD5算法,java中,计算MD5可以用MessageDigest这个类. 下面是代码: [java] view plain copy package com.test;

Java 区块链BLOCKCHAIN中区块BLOCK的hash值的计算

Java 区块链中区块的hash值的计算 计算方法有多种,如,可以直接String拼接,也可以用stringbuffer,或者stringbuilder .这里采用了速度较快的stringbuilder,自己编程的时候可采用stringbuffer.其中index是区块BLOCK的索引,timestamp是区块BLOCK的时间戳,data是区块BLOCK内包含的数据,nonce为该区块的难度系数.总体计算代码如下: /** * 计算hash服务 * @param index 索引 * @para

Get the largest sum of contiguous subarray in an int array

When I finished reading this problem,I thought I could solve it by scan every single subarray in the array,and the time complexity is cubic.Every subarray could be the eventual one whose sum is the largest,so I did make a conclusion that the best tim