LeetCode Bulls and Cows (简单题)

题意:

  给出两个数字,输出(1)有多少位是相同的(2)有多少位不在正确的位置上。

思路:

  扫一遍,统计相同的,并且将两串中不同的数的出现次数分别统计起来,取小者之和就是第2个答案了。

 1 class Solution {
 2 public:
 3     string getHint(string secret, string guess) {
 4         const int N=10;
 5         int *cnt1=new int[N];memset(cnt1,0,sizeof(int)*N);
 6         int *cnt2=new int[N];memset(cnt2,0,sizeof(int)*N);
 7            int    right=0, wrong=0;
 8         for(int i=0; i<secret.size(); i++)
 9         {
10             if(secret[i]==guess[i])    right++;
11             else
12             {
13                 cnt1[secret[i]-‘0‘]++;
14                 cnt2[guess[i]-‘0‘]++;
15             }
16         }
17         for(int i=0; i<N; i++)
18             wrong+=min(cnt1[i],cnt2[i]);
19         return to_string(right)+"A"+to_string(wrong)+"B";
20     }
21 };

AC代码

时间: 2024-08-03 00:44:09

LeetCode Bulls and Cows (简单题)的相关文章

[LeetCode] Bulls and Cows 公母牛游戏

You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the corr

[LeetCode] Bulls and Cows

Bulls and Cows You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits

LeetCode Missing Number (简单题)

题意: 给一个含有n个整数的数组,数组中的元素应该是0-n.现在缺了其中某1个,找出缺少的那个整数? 思路: 0-n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和. 1 int missingNumber(vector<int>& nums) 2 { 3 int sum=0; 4 for(int i=0; i<nums.size(); i++) 5 sum+=i+1-nums[i]; 6 return sum; 7 } AC代码

LeetCode Valid Anagram (简单题)

题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序后逐个比对,复杂度O(nlogn). 第一种方法: 1 class Solution { 2 public: 3 bool isAnagram(string s,string t) 4 { 5 if(s.size()!=t.size()) return false; 6 int cnt[26][2]

LeetCode Move Zeroes (简单题)

题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. 1 class Solution { 2 public: 3 void moveZeroes(vector<int>& nums) { 4 int idx=0; 5 for(int i=0; i<nums.size(); i++) 6 if(nums[i]!=0) 7 nums[idx++]=nums[i];

【Leetcode】Bulls and Cows

题目链接:https://leetcode.com/problems/bulls-and-cows/ 题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hin

leetcode笔记:Bulls and Cows

一. 题目描写叙述 You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said gues

LeetCode:Bulls and Cows - 猜数字游戏

1.题目名称 Bulls and Cows(猜数字游戏) 2.题目地址 https://leetcode.com/problems/bulls-and-cows/ 3.题目内容 英文:You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend g

Leetcode动态规划【简单题】

目录 Leetcode动态规划[简单题] 53. 最大子序和 题目描述 思路分析 复杂度分析 70.爬楼梯 题目描述 思路分析 复杂度分析 121.买卖股票的最佳时机 题目描述 思路分析 复杂度分析 303.区域和检索-数组不可变 题目描述 思路分析 复杂度分析 Leetcode动态规划[简单题] 动态规划(Dynamic programming,简称DP),是一种把原问题分解为相对简单的子问题的方式求解复杂问题的方法.动态规划相较于递归,拥有更少的计算量. 53. 最大子序和 题目描述 给定一