HT299BullsAndCows
来源:互联网 发布:java 生成汉字五笔码 编辑:程序博客网 时间:2024/06/06 00:57
Method1
- Use two arrays, calculate the bulls and cows separately.
Method2
- HashMap: Use of hashmap can identify bulls quickly, but for cows, we have to eliminate existing bulls from both strings, which is a problem.
- 做了半天才想起来hashset不可duplicate, 因此并不适用于重复数字
- 于是用了LinkedList和HashMap, 一下是当时写的思路
* Put secret in HashMap, and guess in LinkedLIst * Find the bulls and delete them in both HashMap and HashSet * Cannot use same method fow cows, because cannot remove element in hashMap by values. Must by keys
后来发现, 一旦删除linkedlist的元素, index就跟着变,再有index作为key就不对了.因此, 对于HashMap来讲,一旦用别人的index相关作为key或者value,都要注意index不可随意增减!
- 最后才想起来怎么不用两个HashMap呢? 分分钟写完…
- One Failed Case:
- ”1234” and “0111” Pay attention that when you find cows, break the second loop, or it will return 0A3B instead of 0A1B
Method 3
- Other’s solution: To get the cows, use ascii array to calculate the frequency of each number.
Method 4
- Use the HashMap to accumulate frequency
- One optimization of the above
- cows = all - bulls
- My post https://discuss.leetcode.com/topic/67559/b-all-a-hashmap-one-pass-java-solution-with-explanation
0 0
- HT299BullsAndCows
- (抄)写给新手程序员的一封信
- LL141LinkedListCycle
- 清除浮动引起的高度塌陷的7种方法
- JavaScript 对比
- Java垃圾回收机制(GC)详解----垃圾回收GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部
- HT299BullsAndCows
- 实验驱动开发与响应式编程 ---- File Watcher的技术实现----File Watcher,是由我开发的一个文件监控工具(见:File Watcher,不只是一个工具),关于它的具体需求以
- 为东北师范而备考----第二次备考,我又回到了长春。那么这次我准备得就比上次要充分很多了。其实考东北师范大学的软件工程专业,相比于清华,是要容易很多的。因为四门考试内容是政治、英语一、数学二以及C程序
- (转)应该知道的Linux技巧-陈皓
- IDA6.6远程调试Android应用程序的一点总结
- 通俗理解卷积神经网络----1 前言 2012年我在北京组织过8期machine learning读书会,那时“机器学习”非常火,很多人都对其抱有巨大的热情。当我2013年再次来到北京时,有
- [转]十个你可能不曾用过的Linux命令
- [转]惹恼程序员的十件事-陈皓
- [转]其实Unix很简单-陈皓