当前位置:网站首页>2160. 拆分数位后四位数字的最小和
2160. 拆分数位后四位数字的最小和
2022-06-25 06:41:00 【AlbertOS】
引入
给你一个四位 正 整数 n u m num num 。请你使用 n u m num num 中的 数位 ,将 n u m num num 拆成两个新的整数 n e w 1 new1 new1 和 n e w 2 new2 new2 。
n e w 1 new1 new1 和 n e w 2 new2 new2 中可以有 前导 0 ,且 num 中 所有 数位都必须使用。
比方说,给你 num = 2932 ,你拥有的数位包括:两个 2 ,一个 9 和一个 3 。一些可能的 [new1, new2] 数对为 [22, 93],[23, 92],[223, 9] 和 [2, 329] 。
请你返回可以得到的 new1 和 new2 的 最小 和。
示例
输入:num = 2932
输出:52
解释:可行的 [new1, new2] 数对为 [29, 23] ,[223, 9] 等等。
最小和为数对 [29, 23] 的和:29 + 23 = 52 。
输入:num = 4009
输出:13
解释:可行的 [new1, new2] 数对为 [0, 49] ,[490, 0] 等等。
最小和为数对 [4, 9] 的和:4 + 9 = 13 。
题解
这个题目的意思看示例就明白一点了,是一个四位的正整数,就说明在这个整数中找到两个最小的当做第一和第二个数对的十位,剩下的就是两者的个位,这样就是最小和。
很明显的贪心法,我们将当前较小的数值放在较高位,用数组存储 n u m num num的每个的数值,然后升序排序,则最小和为 10 ∗ ( d i g [ 0 ] + d i g [ 1 ] ) + ( d i g [ 2 ] + d i g [ 3 ] ) 10 * (dig[0]+dig[1])+(dig[2]+dig[3]) 10∗(dig[0]+dig[1])+(dig[2]+dig[3])
class Solution {
public:
int minimumSum(int num) {
vector<int> digits;
//将每个数值位存入数组
while (num) {
digits.push_back(num % 10);
num /= 10;
}
//数组升序排序
sort(digits.begin(), digits.end());
return 10 * (digits[0] + digits[1]) + digits[2] + digits[3];
}
};
边栏推荐
- 一次弄清楚 Handler 可能导致的内存泄漏和解决办法
- [batch dos-cmd command - summary and summary] - commands related to Internet access and network communication (Ping, Telnet, NSLOOKUP, ARP, tracert, ipconfig)
- C# 读取web上的xml
- Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
- PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering
- Construction of occupancy grid map
- Vscode official configuration synchronization scheme
- CPDA|数据分析师成长之路如何起步?
- Hisilicon 3559 sample parsing: Vio
- Chuantu microelectronics ca-if1051 can-fd transceiver
猜你喜欢

Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer

NPM install reports an error: gyp err! configure error

El input to add words to the tail
![[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)](/img/8c/7345b4e8a55dcf11d18ac113df4fc2.png)
[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)

Fairmot yolov5s to onnx

How to use printf of 51 single chip microcomputer

数据可视化没有重点怎么办?

This year, I graduated

ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性

Keil and Proteus joint commissioning
随机推荐
IAR compiler flashback
NPM install reports an error: gyp err! configure error
Ns32f103c8t6 can perfectly replace stm32f103c8t6
Four software 2021-10-14 suitable for beginners to draw PCB
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
Distributed quorum NWR of the alchemy furnace of the Supreme Master
如何用svn新建属于自己的分支
php入门基础记录
基于激光雷达的林业调查常用术语及含义锦集
判断用户是否是第一次进入某个页面
Pit encountered by pytorch: why can't l1loss decrease during model training?
函数模板_类模板
Redis learning notes
shell小技巧(一百三十四)简单的键盘输入记录器
Collection of common terms and meanings in forestry investigation based on lidar
一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
Ca-is1200u current detection isolation amplifier has been delivered in batch
“空间转换”显著提升陡崖点云的地面点提取质量
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
Function template_ Class template