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 = ; i < ; i++) {
Domino d(box[*i], box[*i+]);
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 = ;
for (int i = ; i < ; i++)
hash_value = hash_value * + v[i].first* + 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;
}
};

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

  1. 【BZOJ2124】等差子序列 树状数组维护hash值

    [BZOJ2124]等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N ...

  2. HashMap的底层实现以及解决hash值冲突的方式

    class HashMap<K,V> extends AbstractMap<K,V> HashMap  put() HashMap  get() 1.put() HashMa ...

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

    对于一些大文件,有时会需要计算部分内容的Hash,下面的函数计算了 文件头尾各1M,中间跳跃100M取10K 以及文件大小的Hash值 public static String CalHash(Str ...

  4. Java 获取字符串Hash值

    Java 生成字符串的Hash值: /** * A hashing method that changes a string (like a URL) into a hash suitable for ...

  5. 【转】Java计算文件的hash值

    原文地址:http://blog.csdn.net/qq_25646191/article/details/78863110 如何知道一个文件是否改变了呢?当然是用比较文件hash值的方法,文件has ...

  6. MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值

    F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...

  7. C#实现像Git那样计算Hash值

    从Git Tip of the Week: Objects一文中得知,Git是这样计算提交内容的Hash值的: Hash算法用的是SHA1 计算前,会在内容前面添加"blob 内容长度\0& ...

  8. 同样的输入,为什么Objects.hash()方法返回的hash值每次不一样?

    背景 开发过程中发现一个问题,项目中用Set保存AopMethod对象用于去重,但是发现即使往set中添加相同内容的对象,每次也能够添加成功. AopMethod类的部分代码如下: public cl ...

  9. hash值

    任何类都继承public int hashCode()方法,该方法返回的值是通过将该对象的内部地址转换为一个整数来实现的,hash表的主要作用就是在对对象进行散列的时候作为key输入.我们需要每个对象 ...

随机推荐

  1. Win7操作系统安装IE10提示“安装前需要更新与安装程序版本”

    安装IE10浏览器时提示错误的 Internet Explorer安装程序版本 故障现象: Win7操作系统在安装IE10浏览器时会弹出对话框,提示错误的Ieternet Explorer 安装程序版 ...

  2. 深入了解 Session 与 Cookie

    Java —— 深入了解 Session 与 Cookie Java web   了解Cookie和Session   定义 1.很通俗的来讲,Cookie就是浏览器的缓存,就是用户访问网站时保存在浏 ...

  3. Nginx(Windows)

    Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器. 反向代理方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将 ...

  4. busybox microcom Segmentation fault

    /********************************************************************************* * busybox microco ...

  5. It is the courage

    It is the reality that a society which becomes lower and becomes weak.Believe it or not,I think it i ...

  6. threejs精灵平面Sprite(类似tip效果)

    效果图:   let center = this.cube.position.clone(), size = this.cube.geometry.boundingBox.getSize(), sca ...

  7. .NET移动开发环境搭建

    开发工具:Xamarin Studio 社区版 下载地址 http://www.monodevelop.com/download/ 操作系统要求:Windows7及以上..NET Framework4 ...

  8. StringUtils.isEmpty()和isBlank()的区别

    一.概述 两种判断字符串是否为空的用法都是在程序开发时常用的,相信不少同学在这种简单的问题上也吃过亏,到底有什么区别,使用有什么讲究,带着问题往下看. 二.jar包 commons-lang3-3.5 ...

  9. spring boot打包部署到Linux环境

    打包部署说白了就两步:打包.部署.废话不多说,直接拿spring boot自动生成的项目骨架,再添加一个文件用来演示: package com.crocodile.springboot; import ...

  10. Top Android App使用的组件(应用)

    Top Android App使用的组件   唱吧_462 smack:de.measite.smack:??? ???:org.apache:??? smack:org.jivesoftware.s ...