返回介绍

solution / 2400-2499 / 2442.Count Number of Distinct Integers After Reverse Operations / README_EN

发布于 2024-06-17 01:03:06 字数 4394 浏览 0 评论 0 收藏 0

2442. Count Number of Distinct Integers After Reverse Operations

中文文档

Description

You are given an array nums consisting of positive integers.

You have to take each integer in the array, reverse its digits, and add it to the end of the array. You should apply this operation to the original integers in nums.

Return _the number of distinct integers in the final array_.

 

Example 1:

Input: nums = [1,13,10,12,31]
Output: 6
Explanation: After including the reverse of each number, the resulting array is [1,13,10,12,31,1,31,1,21,13].
The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1.
The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).

Example 2:

Input: nums = [2,2,2]
Output: 1
Explanation: After including the reverse of each number, the resulting array is [2,2,2,2,2,2].
The number of distinct integers in this array is 1 (The number 2).

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 106

Solutions

Solution 1: Hash Table

First, we use a hash table to record all integers in the array. Then, we traverse each integer in the array, reverse it, and add the reversed integer to the hash table. Finally, we return the size of the hash table.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.

class Solution:
  def countDistinctIntegers(self, nums: List[int]) -> int:
    s = set(nums)
    for x in nums:
      y = int(str(x)[::-1])
      s.add(y)
    return len(s)
class Solution {
  public int countDistinctIntegers(int[] nums) {
    Set<Integer> s = new HashSet<>();
    for (int x : nums) {
      s.add(x);
    }
    for (int x : nums) {
      int y = 0;
      while (x > 0) {
        y = y * 10 + x % 10;
        x /= 10;
      }
      s.add(y);
    }
    return s.size();
  }
}
class Solution {
public:
  int countDistinctIntegers(vector<int>& nums) {
    unordered_set<int> s(nums.begin(), nums.end());
    for (int x : nums) {
      int y = 0;
      while (x) {
        y = y * 10 + x % 10;
        x /= 10;
      }
      s.insert(y);
    }
    return s.size();
  }
};
func countDistinctIntegers(nums []int) int {
  s := map[int]struct{}{}
  for _, x := range nums {
    s[x] = struct{}{}
  }
  for _, x := range nums {
    y := 0
    for x > 0 {
      y = y*10 + x%10
      x /= 10
    }
    s[y] = struct{}{}
  }
  return len(s)
}
function countDistinctIntegers(nums: number[]): number {
  const n = nums.length;
  for (let i = 0; i < n; i++) {
    nums.push(Number([...(nums[i] + '')].reverse().join('')));
  }
  return new Set(nums).size;
}
use std::collections::HashSet;
impl Solution {
  pub fn count_distinct_integers(nums: Vec<i32>) -> i32 {
    let mut set = HashSet::new();
    for i in 0..nums.len() {
      let mut num = nums[i];
      set.insert(num);
      set.insert({
        let mut item = 0;
        while num > 0 {
          item = item * 10 + (num % 10);
          num /= 10;
        }
        item
      });
    }
    set.len() as i32
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文