当前位置:网站首页>[leetcode weekly race -- hash table number pairs] 6164. Maximum sum of digit and equal number pairs
[leetcode weekly race -- hash table number pairs] 6164. Maximum sum of digit and equal number pairs
2022-07-22 16:36:00 【alone_ yue】
List of articles
Leetcode 6164. The maximum sum of digits and equal pairs
1. Problem description
2. Solution
Solution 1 : violence
Brutally traverse all pairs , Then judge whether each number pair is equal to the digit sum , If equal , Update Max ( Some big examples , It's overtime )
class Solution {
Integer max = Integer.MIN_VALUE;
public void judge(int a, int b){
String str1 = Integer.toString(a);
String str2 = Integer.toString(b);
Integer sum1 = 0, sum2 = 0;
for(char c : str1.toCharArray()){
sum1 += Integer.parseInt(String.valueOf(c));
}
for(char c : str2.toCharArray()){
sum2 += Integer.parseInt(String.valueOf(c));
}
if(sum1==sum2) max = Math.max(max, a+b);
}
public int maximumSum(int[] nums) {
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
judge(nums[i], nums[j]);
}
}
return Math.max(max, -1);
}
}
Solution 2 : Hashtable
Hash store [key: Digital sum (9),value: Numbers (18) ], There are digits and the same , Just update the maximum value , And leave larger numbers in map Inside , It is convenient for the following digits and the same , Can be added to the largest number , Update Max .
class Solution {
public int numSum(int a){
String str = Integer.toString(a);
int sum = 0;
for(char c : str.toCharArray()){
sum += Integer.parseInt(String.valueOf(c));
}
return sum;
}
public int maximumSum(int[] nums) {
int max = Integer.MIN_VALUE;
HashMap<Integer, Integer> map = new HashMap<>();
for(int i : nums){
int key = numSum(i);
if(!map.containsKey(key)){
map.put(key, i);
}else{
int value = map.get(key);
max = Math.max(max, value+i);
if(i>value){
map.put(key, i);
}
}
}
return Math.max(max, -1);
}
}
边栏推荐
- sql server2008数据库查询admin密码
- 查论文、期刊的排名等的方法
- Process and thread interview questions
- What is SCM? What are the components of SCM?
- Methods of checking the ranking of papers and periodicals
- 盒马两大供应链中心启用 多业态商品创新研发“有后台”
- MP查询条件
- Informatics Olympiad all in one 1977: [08noip popularization group] stereogram | Luogu p1058 [noip2008 popularization group] stereogram
- C server NFS shared folder building and uploading image files
- AT5662 [AGC040D] Balance Beam(二分)
猜你喜欢
The two supply chain centers of HEMA launched the "background" of innovative research and development of multi format commodities
NFT exchange contract development tutorial (solidity & hardhat)
ftp服务器搭建部署与C#实现ftp文件的上传
AlterNET脚本编写,用户界面设计功能
AcWing_ 11. Number of solutions for knapsack problem_ dp
MP查询条件
Mock模拟数据,并发起get,post请求(保姆级教程,一定能成功)
Glide source code analysis
sftp创建
Popular science | how to create a Dao?
随机推荐
ARC110F Esoswap
嵌入式IDE原理 OpenOCD介绍 以及stlink如何连接stm32板子
Android interview: 2022 please keep this experience of Netease Android development and Tiktok e-commerce Android engineers
交换机与路由器技术:OSPF路由重分发、OSPF的NSSA区域和OSPF虚链路
Network layer interview questions
AcWing_ 11. Number of solutions for knapsack problem_ dp
【解决方案】解决ImportError: Library “GLU“ not found.问题
How far can TTL, RS232 and 485 transmit?
The two supply chain centers of HEMA launched the "background" of innovative research and development of multi format commodities
nested subqueries
Rosen's QT journey 98 QML tab control tabview
Leetcode 172. zero after factorial
Write a function in C language to delete the spaces in the string and return the number of spaces
This easy-to-use office network optimization tool is free
Hybrid混合开发与JSBridge
At5662 [agc040d] balance beam (two points)
How to configure webrtc protocol for low latency playback on easycvr platform v2.5.0 and above?
When the easycvr platform cascades, there is an error prompt. What is the reason why the port is unreachable?
JVM memory model: classification and acquisition of class loaders
Window startup add / close