LeetCode题解之Rotated Digits

本文介绍了一个用于计算在1到N范围内,有多少个数字在旋转180度后变成不同的数字的算法。通过遍历每个数字并检查其是否包含3、4、7这些不能旋转的数字,以及是否包含2、5、6、9这些旋转后会变成不同数字的元素,来判断一个数字是否为好数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、题目描述

 

2、代码

 1 int rotatedDigits(int N) {
 2         int res = 0;
 3         
 4         for (int i = 1; i <= N; i++) {
 5             if (isGood(i)) {
 6                 res++;
 7             }
 8         }
 9         
10         return res;
11     }
12     
13     bool isGood(int x) 
14     {
15         int rx = x;
16         vector<int> nums;
17         while (x) {
18             int r = x % 10;
19             nums.push_back(r);
20             x = x / 10;
21         }    
22         
23         for (vector<int>::iterator it = nums.begin(); it != nums.end(); it++ ) {
24             if (*it == 3 || *it == 4 || *it == 7)
25                 return false;
26             if (*it == 0 || *it == 1 || *it == 8)
27                 continue;
28             
29             if (*it == 2) {
30                 *it = 5;
31                 continue;
32             }
33             
34             if (*it == 5) {
35                 *it = 2;
36                 continue;
37             }
38             
39             if (*it == 6) {
40                 *it = 9;
41                 continue;
42             }
43             
44             if (*it == 9) {
45                 *it = 6;
46                 continue;
47             }
48         }
49         
50         int good  = 0;
51         for (vector<int>::reverse_iterator ite = nums.rbegin(); ite != nums.rend(); ite++) {
52             good = good * 10  + *ite; 
53         }
54         
55         return good != rx;
56     }

 

转载于:https://www.cnblogs.com/wangxiaoyong/p/10420756.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值