leetcode 2442.反转之后不同整数的数目

1.题目要求:

c 复制代码
给你一个由 正 整数组成的数组 nums 。

你必须取出数组中的每个整数,反转其中每个数位,并将反转后得到的数字添加到数组的末尾。这一操作只针对 nums 中原有的整数执行。

返回结果数组中 不同 整数的数目。

 

示例 1:

输入:nums = [1,13,10,12,31]
输出:6
解释:反转每个数字后,结果数组是 [1,13,10,12,31,1,31,1,21,13] 。
反转后得到的数字添加到数组的末尾并按斜体加粗表示。注意对于整数 10 ,反转之后会变成 01 ,即 1 。
数组中不同整数的数目为 6(数字 1、10、12、13、21 和 31)。
示例 2:

输入:nums = [2,2,2]
输出:1
解释:反转每个数字后,结果数组是 [2,2,2,2,2,2] 。
数组中不同整数的数目为 1(数字 2)。
 

提示:

1 <= nums.length <= 105
1 <= nums[i] <= 106
c 复制代码
//把数字反转的函数
int reverse(int n){
    int number = n;
    int count = 0;//
    while(number){
        count++; 
        number /= 10;
    }
    number = n;
    int sum = 0;
    while(number){
        int temp = number % 10;
        sum +=  (temp * pow(10,count - 1));
        count--;
        number /= 10;
    }
    return sum;
}
int countDistinctIntegers(int* nums, int numsSize) {
    int* number = (int*)malloc(sizeof(int) * numsSize * 2);//设立二倍长度数组,用来存放数组
    memcpy(number,nums,sizeof(int) * numsSize);
    int j = 0;
    j = numsSize;
    //开始进行数字反转
    for(int i = 0;i < numsSize;i++){
        int ret = reverse(nums[i]);
        number[j] = ret;
        j++;
    }
    //用哈希函数来判断不同的整数数目
    int* temp = (int*)malloc(sizeof(int) * 1000000);
    memset(temp,0,sizeof(int) * 1000000);
    for(int i = 0;i < numsSize * 2;i++){
        int index = number[i] - 1;
        temp[index] = number[i];
    }
    int count = 0;
    for(int i = 0;i < 1000000;i++){
        if(temp[i] != 0){
            count++;
        }
    }
    return count;
}

大家如果觉得好的话,不妨给个免费的赞吧,谢谢了

相关推荐
wheeldown42 分钟前
【C语言】(指针系列3)数组指针+函数指针+typedef+函数数组指针+转移表
c语言·数据结构·算法
小比卡丘1 小时前
C语言进阶版第8课—指针(2)
c语言·开发语言·算法
NeVeRMoRE_20241 小时前
【数据结构和算法实践-树-LeetCode107-二叉树的层序遍历Ⅱ】
数据结构·算法·leetcode
2301_778411941 小时前
数据结构----树
数据结构·算法
山脚ice1 小时前
【Hot100】LeetCode—169. 多数元素
算法·leetcode
fhvyxyci1 小时前
【数据结构初阶】队列接口实现及用队列实现栈超详解
c语言·数据结构
小珑也要变强1 小时前
数据结构.
数据结构
茹亦如风1 小时前
【408 数据结构】第2章 线性表
数据结构
长风清留扬1 小时前
Python “集合” 100道实战题目练习,巩固知识、检查技术
数据结构·python·面试·跳槽·学习方法·集合·改行学it
programmergo2 小时前
CMS、G1、ZGC
java·jvm·算法