2023-09-04力扣每日一题

链接:

2605. 从两个数字数组里生成最小数字

题意:

两个数组都只包含1-9的数字,求一个最小数,两个数组内都要有它的其中一位

解:

要么是个位数要么是十位数,存一下数量和两边的最小数即可

实际代码:

c++ 复制代码
#include<bits/stdc++.h>
using namespace std;
int minNumber(vector<int>& nums1, vector<int>& nums2)
{
    short book[10];for(auto& b:book) b=0;
    
    int min1=INT_MAX,min2=INT_MAX;
    for(auto num:nums1) { book[num]++;min1=min(min1,num); }
    for(auto num:nums2) { book[num]++;min2=min(min2,num); }
    
    for(int i=1;i<=9;i++) if(book[i]>=2) return i;
    
    return min(min1,min2)*10+max(min1,min2);
}
int main()
{
	int a,b,num;cin>>a>>b;
	vector<int> nums1, vector<int> nums2;
	while(a--)
	{
		cin>>num;
		nums1.push_back(num);
	}
	while(b--)
	{
		cin>>num;
		nums2.push_back(num);
	}
	int ans=minNumber(num1,num2);
	cout<<ans<<endl;
	return 0;
}

限制:

  • 1 <= nums1.length, nums2.length <= 9
  • 1 <= nums1[i], nums2[i] <= 9
  • 每个数组中,元素 互不相同 。
相关推荐
蛋蛋的就会顺顺的9 小时前
hot100——矩阵
java·数据结构·算法·leetcode·力扣
想吃火锅100513 小时前
【leetcode】238.除了自身以外数组的乘积js
javascript·算法·leetcode
Tisfy15 小时前
LeetCode 0020.有效的括号:栈匹配
算法·leetcode·字符串·题解·括号匹配
Navigator_Z15 小时前
LeetCode //C - 1276. Number of Burgers with No Waste of Ingredients
c语言·算法·leetcode
想吃火锅100516 小时前
【leetcode】189.轮转数组js
开发语言·javascript·leetcode
All for pursuit.18 小时前
【贪心-2】253.会议室 II
数据结构·c++·算法·leetcode
All for pursuit.1 天前
【分治-3】347.前K个高频元素
数据结构·c++·算法·leetcode
All for pursuit.1 天前
【二分查找-2】34.在排序数组中查找元素的第一个和最后一个位置
数据结构·c++·算法·leetcode
blxr_2 天前
力扣hot100爬楼梯https://leetcode.cn/problems/climbing-stairs/
算法·leetcode·职场和发展
Navigator_Z2 天前
LeetCode //C - 1275. Find Winner on a Tic Tac Toe Game
c语言·算法·leetcode