首页 > 其他分享 >leetcode-2160-easy

leetcode-2160-easy

时间:2022-10-30 12:24:29浏览次数:90  
标签:arr 23 int 2160 num easy new2 new1 leetcode

Minimum Sum of Four Digit Number After Splitting Digits

You are given a positive integer num consisting of exactly four digits. Split num into two new integers new1 and new2 by using the digits found in num. Leading zeros are allowed in new1 and new2, and all the digits found in num must be used.

For example, given num = 2932, you have the following digits: two 2's, one 9 and one 3. Some of the possible pairs [new1, new2] are [22, 93], [23, 92], [223, 9] and [2, 329].
Return the minimum possible sum of new1 and new2.

 

Example 1:

Input: num = 2932
Output: 52
Explanation: Some possible pairs [new1, new2] are [29, 23], [223, 9], etc.
The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52.
Example 2:

Input: num = 4009
Output: 13
Explanation: Some possible pairs [new1, new2] are [0, 49], [490, 0], etc. 
The minimum sum can be obtained by the pair [4, 9]: 4 + 9 = 13.

思路一: 四个数字,组成两个数字,所以尽可能把大的数字放进位低,小的数字放进位高

public int minimumSum(int num) {
	int[] arr = new int[4];
	for (int i = 0; i < 4; i++) {
		arr[i] = num % 10;
		num /= 10;
	}

	Arrays.sort(arr);

	return arr[0] * 10 + arr[3] + arr[1] * 10 + arr[2];
}

标签:arr,23,int,2160,num,easy,new2,new1,leetcode
From: https://www.cnblogs.com/iyiluo/p/16840949.html

相关文章

  • leetcode-1748-easy
    SumofUniqueElementsYouaregivenanintegerarraynums.Theuniqueelementsofanarrayaretheelementsthatappearexactlyonceinthearray.Returnthe......
  • leetcode102-二叉树的层序遍历
    102.二叉树的层序遍历有两种实现方法。第一种是递归,第二种是队列实现。第一种是看了别人的代码写出来的,第二种是自己写的。这道题的不能直接把遍历得到的数字直接塞进res......
  • EasyExcel导出Date类型格式处理
    EasyExcel导出Date类型格式处理​ 如果在导出的excel中有date时间类型的字段,直接导出会报错org.apache.poi.ss.usermodel.Cell.setCellValue(Ljava/time/LocalDateTime;)......
  • 刷题 LeetCode二叉树2
    代码随想录LeetCode102.二叉树的层序遍历carl二叉树遍历#层序遍历#队列#广度优先思路队首是待访问节点,每访问一个节点,将其左子和右子加入队列细节如何知道某......
  • LeetCode 题解 | 1. 两数之和 Javascript 版
    题目给定一个整数数组nums 和一个整数目标值target,请你在该数组中找出和为目标值target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个......
  • LeetCode 题解 | 3. 无重复字符的最长子串 Javascript
    /***@param{string}str*@returnsnumber*思路:1.start与range组合成一个窗口,窗口内的子串就是当前最长不重复的字符串*2.range每次循环递增*......
  • LeetCode 题解|6. Z 字形变换
    /***@param{string}s*@param{number}numRows*@return{string}*/varconvert=function(s,numRows){//存储结果constrows=[];//指针下一......
  • LeetCode 题解|9. 回文数
    /***@param{number}x*@return{boolean}*/varisPalindrome=function(x){if(x<0){returnfalse;}letnum=x;letreverse=0;wh......
  • LeetCode 题解|7. 整数反转
    /***@param{number}x*@return{number}*/varreverse=function(x){letres=0;while(x!=0){res=res*10+(x%10);//划重点......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:旋转链表
    题目:给你一个链表的头节点head,旋转链表,将链表每个节点向右移动 k 个位置。 示例1:输入:head=[1,2,3,4,5],k=2输出:[4,5,1,2,3]示例2:输入:head=[0,1,2],k=4输出......