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. The record of Rf module debugging (1)

    In order to improve engineering English writing ability,start from today,record my daily work of  el ...

  2. 深度学习(七十一)darknet 源码阅读

    深度学习(七十一)darknet 源码阅读

  3. Linux udhcp client (udhcpc) get IP at anytime

    /*************************************************************************************** * Linux udh ...

  4. Linux下下载JDK

    需要加特殊的前缀,不然无法下载文件 . 例如JDK8 U131 wget -c --header "Cookie: oraclelicense=accept-securebackup-coo ...

  5. Jenkins构建常见问题

    最近在用jenkins搭建.NET自动编译发布环境时遇到的一些问题,解释不一定都对,仅记录以备后用. 1.MSBUILD : error MSB1008: 只能指定一个项目 Build a Visua ...

  6. JavaScript异步编程__“回调地狱”的一些解决方案

    异步编程在JavaScript中非常重要.过多的异步编程也带了回调嵌套的问题,本文会提供一些解决“回调地狱”的方法. setTimeout(function () { console.log('延时触 ...

  7. 文件和I/O流

    版权声明:本文为[博主](https://zhangkn.github.io)原创文章,未经博主同意不得转载.https://creativecommons.org/licenses/by-nc-sa ...

  8. Ant build.xml相关属性详解

    关键字: ant build.xml Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序 ...

  9. nginx rewrite规则实例讲解

    一.正则表达式匹配,其中: * ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文 ...

  10. 全是干货!UI设计的30条黄金准则!

    http://www.wex5.com/portfolio-items/js-1/ 全是干货!UI设计的30条黄金准则!   总的来说,好的UI界面有几个特征:简洁.便利.目标明确.人性化.字面上看这 ...