299. Bulls and Cows - LeetCode

Question

299. Bulls and Cows

Solution

题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一个cow,注:数字对与位置对优先,一个号码不能重复判断.

思路:构造map结构,遍历实现

Java实现:实现的不漂亮,好歹能通过

public String getHint(String secret, String guess) {
    Map<Character, Index> map = new HashMap<>();
    for (int i=0; i<secret.length(); i++) {
        Index idx = map.get(secret.charAt(i));
        if (idx == null) {
            idx = new Index();
            map.put(secret.charAt(i), idx);
        }
        idx.add(i);
    }

    int bulls = 0;
    int cows = 0;
    List<Character> cowsList = new ArrayList<>(); // for count cows
    // count bulls
    for (int i=0; i<guess.length(); i++) {
        Index idx = map.get(guess.charAt(i));
        if (idx != null) { // check digits
            if (idx.isBull(i)) {
                bulls++;
            } else {
                cowsList.add(guess.charAt(i));
            }
        }
    }
    // count cows
    for (char c : cowsList) {
        Index idx = map.get(c);
        if (idx.isCow()) {
            cows++;
        }
    }
    return bulls + "A" + cows + "B";
}

class Index {
    List<Integer> idxList;
    int count;

    // constructor
    public Index() {
        idxList = new ArrayList<>();
        count = 0;
    }

    void add(int x) {
        idxList.add(x);
        count++;
    }

    boolean isCow() {
        return count-- > 0;
    }

    boolean isBull(int x) {
        for (int tmp : idxList) {
            if (x == tmp) {
                count--;
                return true;
            }
        }
        return false;
    }
}

Ref

https://leetcode.com/problems/bulls-and-cows/discuss/74621/One-pass-Java-solution

public String getHint(String secret, String guess) {
    int bulls = 0;
    int cows = 0;
    int[] numbers = new int[10];
    for (int i = 0; i<secret.length(); i++) {
        int s = Character.getNumericValue(secret.charAt(i));
        int g = Character.getNumericValue(guess.charAt(i));
        if (s == g) bulls++;
        else {
            if (numbers[s] < 0) cows++;
            if (numbers[g] > 0) cows++;
            numbers[s] ++;
            numbers[g] --;
        }
    }
    return bulls + "A" + cows + "B";
}
public String getHint(String secret, String guess) {
    int bulls = 0;
    int cows = 0;
    int[] numbers = new int[10];
    for (int i = 0; i<secret.length(); i++) {
        if (secret.charAt(i) == guess.charAt(i)) bulls++;
        else {
            if (numbers[secret.charAt(i)-'0']++ < 0) cows++;
            if (numbers[guess.charAt(i)-'0']-- > 0) cows++;
        }
    }
    return bulls + "A" + cows + "B";
}

转载于:https://my.oschina.net/yysue/blog/1919875

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值