当前位置:网站首页>Leetcode 1814 count nice pairs in an array (recommended by map)
Leetcode 1814 count nice pairs in an array (recommended by map)
2022-06-11 01:39:00 【_ TCgogogo_】
You are given an array nums that consists of non-negative integers. Let us define rev(x) as the reverse of the non-negative integer x. For example, rev(123) = 321, and rev(120) = 21. A pair of indices (i, j) is nice if it satisfies all of the following conditions:
0 <= i < j < nums.lengthnums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
Return the number of nice pairs of indices. Since that number can be too large, return it modulo 109 + 7.
Example 1:
Input: nums = [42,11,1,97] Output: 2 Explanation: The two pairs are: - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121. - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.
Example 2:
Input: nums = [13,10,35,24,76] Output: 4
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 109
Topic link :https://leetcode.com/problems/count-nice-pairs-in-an-array/
The main idea of the topic : Give an array , Find the number of pairs of formulas that meet the conditions of the topic
Topic analysis : encounter nums[i]+rev(nums[j])==nums[j]+rev(nums[i]) such i and j For problems on both sides of the equation, first consider putting the same variables aside ( I remember that there is a problem that the condition is to find nums[j]-nums[i]==j-i The logarithmic , It is essentially the same as this question ), So there is nums[j]-rev(nums[j])== nums[i]-rev(nums[i]), Hash it
I can't pass the penultimate set of data in another way , The problem is found through the binary test set , And an interesting conclusion , Interested parties can refer to the following links :
https://leetcode.com/problems/count-nice-pairs-in-an-array/discuss/1757293
57ms, Time beats 60%
class Solution {
public int countNicePairs(int[] nums) {
Map<Integer, Integer> mp = new HashMap<>();
int ans = 0;
for (int num : nums) {
int key = num - calRev(num);
int val = mp.getOrDefault(key, 0);
ans = (ans + val) % 1000000007;
mp.put(key, val + 1);
}
return ans;
}
private int calRev(int x) {
int ans = 0;
while (x > 0) {
ans = ans * 10 + x % 10;
x /= 10;
}
return ans;
}
}边栏推荐
- Beijing Mentougou District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
- Beijing Dongcheng District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
- Makefile:1860: recipe for target ‘cmake_ check_ build_ system‘ failed make: *** [cmake_check_build_syst
- 复利的保险理财产品怎么样?可以买吗?
- [chess life] 01 life is like chess
- Beijing Pinggu District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
- threejs:两点坐标绘制贝赛尔曲线遇到的坑
- Role of handlermethodargumentresolver + use case
- Conda安装Pytorch后numpy出现问题
- threejs:流光效果封装
猜你喜欢

2.1 ros+px4 simulation - Fixed Point flight control

Detailed explanation of classic papers on OCR character recognition

Conda安装Pytorch后numpy出现问题

nodejs中使用mySql数据库

threejs:两点坐标绘制贝赛尔曲线遇到的坑

Once you know these treasure websites, you can't live without them!!!

PX4从放弃到精通(二十四):自定义机型

Docking of express bird system

How to write this with data and proc without SQL

中间件_Redis_05_Redis的持久化
随机推荐
SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)
Digital IC Design self-study ing
Current limiting and download interface request number control
部分 力扣 LeetCode 中的SQL刷题整理
OCR文字识别经典论文详解
Some tips for programmers to deal with stress
Introduction to the subsidy fund for leading technological innovation of Beijing enterprises, with a subsidy of 5million yuan
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
SAS factor analysis (proc factor process, factor rotation and regression method for factor score function)
Role of handlermethodargumentresolver + use case
2022年高考加油 那年我的高考如此兴奋
Norme de soutien à la culture des entreprises de haute technologie du district de Mentougou à Beijing, subvention de 100 000 RMB
2.1、ROS+PX4仿真---定点飞行控制
Garbled code when the command parameter contains% in VisualStudio debugging
项目_基于网络爬虫的疫情数据可视化分析
Web3生态去中心化金融平台——Sealem Finance
Introduction to the application process of Shenzhen China Patent Award, with a subsidy of 1million yuan
Clean up the broken artifacts data (.lastUpdated files) and reload the project. Problem resolution
Tencent Cloud Database tdsql - Dajia comments | The Past, Present and Future of basic software
中间件_Redis_05_Redis的持久化