avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1814. Statistics the number of good pairs in an array One question daily

  2024-01-01        
字数统计: 391字   |   阅读时长: 2min

topic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1814. Statistics the number of good pairs in an array
medium
86
company
Superior Uber
Give you an array nums ,The array contains only non -negative integers。definition rev(x) The value is an integer x The results obtained by the digital bits are reversed。for example rev(123) = 321 , rev(120) = 21 。We call the lattering pair of the following conditions (i, j) yes OK :

0 <= i < j < nums.length
nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
Please return the number of bidding pairs。Because the result may be great,Please right 109 + 7 Take a surplus Back。



Exemplary example 1:

enter:nums = [42,11,1,97]
Output:2
explain:Two coordinates right:
- (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 。
Exemplary example 2:

enter:nums = [13,10,35,24,76]
Output:4


hint:

1 <= nums.length <= 105
0 <= nums[i] <= 109

Thinking

这道题没想到yesHash table,还以为yesDouble pointer。
It has been done for a long time,Unexpectedly,Just look at the problem and solve the problem,I also understand。
总之就yes,好对子其实就yes two i - res(i) The same number!
Word gamesa feeling of:

42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121 。
不就yes 42-res(42) == 97 - res(97) ??

when*cnt[i-res(i)]*When there is no existence,Will return0.

ans += cnt[i-j]

Thus, incnt[i-j]have2次值的hour候才Will return到ansmiddle
because
cnt[i-j]=1
hour,yes不会经过ansAssociated

cnt[i-j] += 1

solution:

·ElementnumsW - rev(num[il)
·Calculate the number of times per element
·Calculate the difference between the number of times each time
@Lazy


code show as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
def countNicePairs(self, nums: List[int]) -> int:
def res(num: int) -> int:
return int(str(num)[::-1])
cnt = Counter()
ans = 0
for i in nums:
j = res(i)
# whencnt[i-res(i)]When there is no existence,Will return0.
ans += cnt[i-j]
# Thus, incnt[i-j] have2次值的hour候才Will return到ansmiddle
# becausecnt[]=1hour,yes不会经过ansAssociated
cnt[i-j] += 1
return ans % (10 ** 9 + 7)
  • Python
  • Hash table
  • solved
  • Double pointer

扫一扫,分享到微信

微信分享二维码
1798You can construct the maximum number of continuity One question daily
1801-1803 Liech buckle novice!
目录
  1. 1. topic
  2. 2. Thinking
  3. 3. solution:
  4. 4. code show as below:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号