首页 > 其他分享 >[LeetCode] 1814. Count Nice Pairs in an Array

[LeetCode] 1814. Count Nice Pairs in an Array

时间:2023-01-17 01:22:34浏览次数:60  
标签:1814 Count Pairs nums int rev num 11 10

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.length
  • nums[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 <= 105
  • 0 <= nums[i] <= 109

统计一个数组中好对子的数目。

给你一个数组 nums ,数组中只包含非负整数。定义 rev(x) 的值为将整数 x 各个数字位反转得到的结果。比方说 rev(123) = 321 , rev(120) = 21 。我们称满足下面条件的下标对 (i, j) 是 好的 :

0 <= i < j < nums.length
nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
请你返回好下标对的数目。由于结果可能会很大,请将结果对 109 + 7 取余 后返回。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-nice-pairs-in-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题思路类似 two sum。注意题目描述,题目让你找的是有多少对下标(i, j)满足 nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]),其中 rev() 函数是把这个数字翻转过来。

这个题目很像 two sum,也是找两数之和,只不过等号右边也是两数之和。我们可以把这个等式转换成 nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]),这样我们找的就是是否存在两个不同的下标,对应的两个数字的 nums[i] - rev(nums[i]) 相等。这里为了保险起见,计算结果我用了 long 型避免溢出。

时间O(n * logC) - rev()函数的复杂度是 logC

空间O(n)

Java实现

 1 class Solution {
 2     public int countNicePairs(int[] nums) {
 3         int MOD = (int) Math.pow(10, 9) + 7;
 4         long count = 0;
 5         HashMap<Integer, Integer> map = new HashMap<>();
 6         for (int num : nums) {
 7             int rev = helper(num);
 8             if (map.containsKey(num - rev)) {
 9                 count += map.get(num - rev);
10             }
11             map.put(num - rev, map.getOrDefault(num - rev, 0) + 1);
12         }
13         return (int) (count % MOD);
14     }
15     
16     private int helper(int num) {
17         int res = 0;
18         while (num != 0) {
19             res = res * 10 + num % 10;
20             num /= 10;
21         }
22         return res;
23     }
24 }

 

two sum题目总结

LeetCode 题目总结

标签:1814,Count,Pairs,nums,int,rev,num,11,10
From: https://www.cnblogs.com/cnoodle/p/17056805.html

相关文章

  • CountDownLatch的使用
    一、介绍CountDownLatch是一个计数的闭锁,作用与CyclicBarrier有点儿相似。在API中是这样描述的:用给定的计数初始化CountDownLatch。由于调用了countDown()方法,所以......
  • java CountDownLatch用法 主线程等待子线程执行完后再执行
    这里记录一下下面这种情况:主线程需要等待多个子线程执行完后再执行。我们先看一下下面的场景:packagecom.java4all.mypoint;importjava.util.concurrent.CountDownLatch;/*......
  • hdu: Count(矩阵快速幂)
    ProblemDescriptionFarmerJohn有n头奶牛.某天奶牛想要数一数有多少头奶牛,以一种特殊的方式:第一头奶牛为1号,第二头奶牛为2号,第三头奶牛之后,假如当前奶牛是第n头,那......
  • MySQL中的COUNT(*)和COUNT(col)
    ​另一篇:differencebetweencount(1)andcount(*) 看看人们是如何使用COUNT(*)和COUNT(col)的,看起来大多数人都认为它们是同义词,只是使用他们喜欢的,而在性能甚至查询......
  • Count Pairs With XOR in a Range
    CountPairsWithXORinaRangeGivena(0-indexed) integerarray nums andtwointegers low and high ,returnthenumberofnicepairs.Anicepair is......
  • [LeetCode] 1807. Evaluate the Bracket Pairs of a String
    Youaregivenastring s thatcontainssomebracketpairs,witheachpaircontaininga non-empty key.Forexample,inthestring "(name)is(age)yearsold",......
  • 解决 OpenAI‘s API is not available in your country
    解决 OpenAI'sAPIisnotavailableinyourcountry.根据百度翻译,英文大意为:OpenAI的API在您所在的国家/地区不可用。根据显示,需要魔法网络  好了,我们可以看......
  • Hadoop单击模式运行wordcount例子
    1、进入Hadoop安装目录cd/zwy/soft/hadoop-2.7.12、创建文件夹inputmkdirinput3、写一段文字到文件file.txtecho"helloworldhellohadoop">file.txt4、移动文件file.tx......
  • SELECT COUNT(*) 会造成全表扫描?回去等通知吧
    本文已经收录到Github仓库,该仓库包含计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设计模式、架构、校招......
  • Codeforces 1671 F Permutation Counting 题解
    题目链接把\(p_i>p_{i+1}\)的位置个数称为间隔数首先想到一个暴力做法。从小到大挨个添加1-n中的每个数,注意到添加数i时,只能添加到当前序列的最后11个位置中,否则逆序对数......