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;
}

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

相关推荐
薛定谔的悦1 分钟前
嵌入式设备OTA升级实战:从MQTT命令到自动重启的全流程解析
linux·算法·ota·ems
杰克尼8 分钟前
知识点总结--01
数据结构·算法
cici1587418 分钟前
图像匹配算法:灰度相关法、相位相关法与金字塔+相位相关法
算法
佚名ano20 分钟前
支持向量机SVM的简单推导过程
算法·机器学习·支持向量机
云泽80822 分钟前
蓝桥杯算法精讲:倍增思想与离散化深度剖析
算法·职场和发展·蓝桥杯
m0_5698814722 分钟前
基于C++的数据库连接池
开发语言·c++·算法
.select.29 分钟前
c++ auto
开发语言·c++·算法
2401_8845632433 分钟前
C++中的访问者模式高级应用
开发语言·c++·算法
智者知已应修善业38 分钟前
【51单片机用两个定时计数器级联实现定时】2023-04-12
c语言·经验分享·笔记·算法·51单片机
君义_noip39 分钟前
信息学奥赛一本通 1613:打印文章
c++·算法·信息学奥赛·csp-s