Question
Solution
题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一个cow,注:数字对与位置对优先,一个号码不能重复判断.
思路:构造map结构,遍历实现
Java实现:实现的不漂亮,好歹能通过
public String getHint(String secret, String guess) { Mapmap = new HashMap<>(); for (int i=0; i cowsList = new ArrayList<>(); // for count cows // count bulls for (int i=0; i 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
public String getHint(String secret, String guess) { int bulls = 0; int cows = 0; int[] numbers = new int[10]; for (int i = 0; i0) 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; i0) cows++; } } return bulls + "A" + cows + "B";}