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

This commit is contained in:
huangge1199 2021-06-07 11:31:34 +08:00
parent cb3995e293
commit b26dc421bb
2 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,80 @@
//给你一个数组 nums 数组中只包含非负整数定义 rev(x) 的值为将整数 x 各个数字位反转得到的结果比方说 rev(123) = 321 r
//ev(120) = 21 我们称满足下面条件的下标对 (i, j) 好的
//
//
// 0 <= i < j < nums.length
// nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
//
//
// 请你返回好下标对的数目由于结果可能会很大请将结果对 109 + 7 取余 后返回
//
//
//
// 示例 1
//
// 输入nums = [42,11,1,97]
//输出2
//解释两个坐标对为
// - (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
//
//
// 示例 2
//
// 输入nums = [13,10,35,24,76]
//输出4
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 105
// 0 <= nums[i] <= 109
//
// Related Topics 数组 哈希表
// 👍 13 👎 0
package leetcode.editor.cn;
import java.util.HashMap;
import java.util.Map;
//1814:统计一个数组中好对子的数目
public class CountNicePairsInAnArray{
public static void main(String[] args) {
//测试代码
Solution solution = new CountNicePairsInAnArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int countNicePairs(int[] nums) {
Map<Integer, Long> map = new HashMap<>();
for (int num : nums) {
int key = num - revert(num);
if (map.containsKey(key)) {
map.put(key, map.get(key) + 1);
} else {
map.put(key, Long.parseLong("1"));
}
}
long count = 0;
for (int key : map.keySet()) {
long value = map.get(key);
if (value > 1) {
count += value * (value - 1) / 2;
}
}
return (int)(count % (Math.pow(10, 9) + 7));
}
private int revert(int num){
String str = ""+num;
str = new StringBuilder(str).reverse().toString();
return Integer.parseInt(str);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,35 @@
<p>给你一个数组 <code>nums</code> ,数组中只包含非负整数。定义 <code>rev(x)</code> 的值为将整数 <code>x</code> 各个数字位反转得到的结果。比方说 <code>rev(123) = 321</code>  <code>rev(120) = 21</code> 。我们称满足下面条件的下标对 <code>(i, j)</code> 是 <strong>好的</strong> </p>
<ul>
<li><code>0 &lt;= i &lt; j &lt; nums.length</code></li>
<li><code>nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])</code></li>
</ul>
<p>请你返回好下标对的数目。由于结果可能会很大,请将结果对 <code>10<sup>9</sup> + 7</code> <b>取余</b> 后返回。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>nums = [42,11,1,97]
<b>输出:</b>2
<b>解释:</b>两个坐标对为:
- (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 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums = [13,10,35,24,76]
<b>输出:</b>4
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li></div></div>\n<div><li>👍 13</li><li>👎 0</li></div>