当前位置:网站首页>LeetCode_ 2342_ Maximum sum of digits and equal pairs
LeetCode_ 2342_ Maximum sum of digits and equal pairs
2022-07-23 13:51:00 【Fitz1318】
Topic link
Title Description
I'll give you a subscript from 0 Starting array nums , The elements in the array are just Integers . Please choose two subscripts i and j(i != j), And nums[i] The digits and And nums[j] The digit sum of is equal .
Please find all the subscripts that meet the conditions i and j , Find out and return to nums[i] + nums[j] What you can get Maximum .
Example 1:
Input :nums = [18,43,36,13,7]
Output :54
explain : Number pairs that meet the conditions (i, j) by :
- (0, 2) , The digit sum of the two numbers is 9 , Add up to get 18 + 36 = 54 .
- (1, 4) , The digit sum of the two numbers is 7 , Add up to get 43 + 7 = 50 .
So the maximum sum that can be obtained is 54 .
Example 2:
Input :nums = [10,12,19,14]
Output :-1
explain : There is no number pair that satisfies the condition , return -1 .
Tips :
1 <= nums.length <= 1051 <= nums[i] <= 109
Their thinking
- First, sort the array , Then write a function to get the sum of each digit of each integer
- The initial maximum value is
-1, Traversal array , Use a hash table to record , Every time if the digit sum is updated , You can put it directly into the currentnums[i], To ensure that what you put in and get is the largest
AC Code
class Solution {
public int maximumSum(int[] nums) {
Arrays.sort(nums);
int ans = -1;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int k = getSum(nums[i]);
if (map.containsKey(k)) {
ans = Math.max(nums[i] + map.get(k), ans);
}
map.put(k, nums[i]);
}
return ans;
}
private static int getSum(int n) {
int sum = 0;
while (n > 0) {
int tmp = n % 10;
sum += tmp;
n = n / 10;
}
return sum;
}
}
边栏推荐
- freemarker
- Ti single chip millimeter wave radar 1642 code walk through (0) - General Outline
- Kotlin - suspend function
- Light chain dissection / tree chain dissection
- C#:in、out、ref关键字
- LeetCode_2342_数位和相等数对的最大和
- Hardware system architecture of 4D millimeter wave radar
- 数据库系统原理与应用教程(051)—— MySQL 查询(十三):DML 语句中使用查询
- 接口测试-简单的接口自动化测试Demo
- 数据库系统原理与应用教程(038)—— MySQL 的索引(四):使用 EXPLAIN 命令分析索引
猜你喜欢

Point target simulation of SAR imaging (II) -- matlab simulation

Introduction to radar part vii 4 SAR system design

Redis常用命令

Chapter II relational database after class exercises

Interface test - simple interface automation test demo

Qt Creator .pro文件根据kit添加对应库

Method of entering mathematical formula into mark down document

4D antenna array layout design

Color recognition of regions of interest in pictures and videos based on OpenCV

SparkSQL设计及入门,220722,
随机推荐
LeetCode_491_递增子序列
欧洲“气荒”对中国有哪些影响?
数据库系统原理与应用教程(052)—— MySQL 的数据完整性(十四):交叉表查询(行列转换)
Point target simulation of SAR imaging (III) -- Analysis of simulation results
Unity关于本地加载图片涉及webrequest或者byte
KingbaseES DENSE_RANK 函数用法
Talking about the CPU type of anroid device and the placement directory of so files
微信小程序--动态设置导航栏颜色
如何保证消息的可靠传输?如果消息丢了会怎么办
浅谈Anroid设备的CPU类型以及so文件的放置目录
ROS中引用和输出消息类型
C#:in、out、ref关键字
基于OpenCV实现对图片及视频中感兴趣区域颜色识别
关于this指针
挖财开户风险性大吗,安全吗?
数据库系统原理与应用教程(051)—— MySQL 查询(十三):DML 语句中使用查询
ROS2自学笔记:URDF机器人建模
微服务重点
QT creator.Pro file adds the corresponding library according to the kit
Reference and output message types in ROS