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. 免费获取一年 AVG Internet Security 2014 和 Antivirus Pro 2014

    华为版的 AVG 2014 系列出炉了,用过华为版 2013 系列的童鞋都知道是什么回事,内置一年多的序列号不用那么麻烦去找了. 下载地址: 内置的许可证是:IBY9X-ESYXT-W4BZQ-QI4 ...

  2. 201621123006 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词. 继承.接口.抽象类.多态.interfac.comparable 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一 ...

  3. Python基础学习----参数和返回值

    # 函数的参数和返回值 # 4种组合方式 # 1.无参无返 # def methodone(): # 2.无参有返 def methodtwo(): a=10 return a # 3.有参无返 # ...

  4. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 08

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2389] 版本控制:https://git.coding.net/liuyy08 ...

  5. react中findDOMNode

    在使用react过程中,大家有时会那么这里的findDomNode是做什么的呢? import { findDomNode } from 'react-dom'; 简单来说是用来得到实际Dom的,因为 ...

  6. Ubuntu文本编辑(vi和nano)命令

    vi是Unix世界里极为普遍的全萤幕文书编辑器,几乎可以说任何一台Unix机器都会提供这套软体就像windows的记事本一样. 键入 vi /etc/hosts 进入vi界面,把光标移动到文件未尾.按 ...

  7. 【Keras学习】Sequential模型

    序贯(Sequential)模型 序贯模型是多个网络层的线性堆叠,也就是“一条路走到黑”. 可以通过向Sequential模型传递一个layer的list来构造该模型: from keras.mode ...

  8. “之”字形输出二叉树 python

    “之”字形输出二叉树即第1层从左到右输出,第2层从右到左输出,第3层从左到右输出......第2*n层从右到左输出,第2*n+1层从左到右输出,如下图所示二叉树,“之”字形输出的结果为:1-3-2-4 ...

  9. Multiple Object Tracking using K-Shortest Paths Optimization简要

    参考文献:Multiple Object Tracking using K-Shortest Paths Optimization 核心步骤: 两步:一.detection 二.link detect ...

  10. 【传输协议】HttpClient基本使用

    最近工作中是做了一个handoop的hdfs系统的文件浏览器的功能,是利用webhdfs提供的rest api来访问hdfs来与hdfs进行交互的,其中大量使用HttpClient,之前一直很忙,没什 ...