HT299BullsAndCows

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值