当前位置:网站首页>Algorithm -- maximum number after parity exchange (kotlin)
Algorithm -- maximum number after parity exchange (kotlin)
2022-06-21 17:36:00 【Xiaomi technology Android R & D caoxinyu】
subject
Give you a positive integer num . You can exchange num in Parity Any two digits of the same ( namely , Are odd or even ).
Return to exchange arbitrarily After that num Of Maximum It may be worth .
Example 1:
Input :num = 1234
Output :3412
explain : Exchange numbers 3 And number 1 , The result is 3214 .
Exchange numbers 2 And number 4 , The result is 3412 .
Be careful , There may be other exchange sequences , But it can be proved that 3412 Is the maximum possible value .
Be careful , You can't exchange numbers 4 And number 1 , Because they have different parity .
Example 2:
Input :num = 65875
Output :87655
explain : Exchange numbers 8 And number 6 , The result is 85675 .
Exchange numbers 5 And number 7 , The result is 87655 .
Be careful , There may be other exchange sequences , But it can be proved that 87655 Is the maximum possible value .
Tips :
1 <= num <= 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/largest-number-after-digit-swaps-by-parity
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Solutions
Convert numbers to strings numStr;
establish 2 A priority queue , Separate storage num Odd and even numbers in , And they are arranged in reverse order ;
Replace the original odd or even numbers in the arranged order .
resolvent
fun largestInteger(num: Int): Int {
if (num == 0) {
return 0
}
var q1 = PriorityQueue(Collections.reverseOrder<Int>())
var q2 = PriorityQueue(Collections.reverseOrder<Int>())
val charArray = num.toString().toCharArray()
charArray.forEach {
val value = it - '0'
when(value % 2){
0 ->{
q1.offer(value)}
else ->{
q2.offer(value)}
}
}
var result = 0
charArray.forEach {
val value = it - '0'
result = when(value % 2){
0 ->{
result* 10 + q1.poll()
}
else ->{
result* 10 + q2.poll()
}
}
}
return result
}
summary
1. Pay attention to the examination It is exchanged according to the parity of a single number , Not location
2. When splicing numbers ,result = result* 10 + q1.poll() instead of result += result* 10 + q1.poll()
边栏推荐
- 欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
- Implementation of decode function in GP
- Android kotlin class delegation by, by lazy key
- win32com 操作excel
- Three color mark removal method
- JetPack compose 状态提升(二)
- Kotlin annotation declaration and use
- 程序员进修之路
- IEC62133与EN62133有何区别?主要测试哪些项目?
- Template: p6114 [template] Lyndon Decomposition & runs (string)
猜你喜欢

【数据集】|BigDetection

加速云原生应用落地,焱融 YRCloudFile 与天翼云完成兼容性认证

Qt5 knowledge: string list qstringlistmodel

AS 3744.1标准中提及ISO8191测试,两者测试一样吗?

Matlab中xticks函数

Detailed explanation of Fisher information quantity detection countermeasure sample code

How can aggressive programmers improve R & D efficiency Live broadcast Preview

一招教你通过焱融 SaaS 数据服务平台+ELK 让日志帮你做决策

「运维有小邓」Active Directory批量修改用户

20 pyGame module to make a jumping ball game
随机推荐
#Vscode工具#
BM19 寻找峰值
一些细节
Fragment与Activity 传值
「运维有小邓」Active Directory批量修改用户
IEC62133与EN62133有何区别?主要测试哪些项目?
go corn定时任务简单应用
kotlin 注解声明与使用
Go corn timing task simple application
【数据集】|BigDetection
程序员进修之路
Implementation of decode function in GP
[MySQL learning notes 11] sort query
Force deduction solution summary 1108-ip address invalidation
RTMP webrtc protocol OpenSSL installation
【mysql学习笔记13】查询语句综合练习
贝叶斯公式的两种理解
一招教你通过焱融 SaaS 数据服务平台+ELK 让日志帮你做决策
The node server res.end() writes Chinese, and the solution to the problem of garbled code in the client
shamir