[Leetcode] 248. Strobogrammatic Number III 解题报告

来源:互联网 发布:淘宝不能下载 编辑:程序博客网 时间:2024/06/11 17:44

题目

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.

For example,
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.

Note:
Because the range might be a large number, the low and high numbers are represented as string.

思路

在《[Leetcode] 247. Strobogrammatic Number II 解题报告》的基础上稍作改进就可以了。也就是枚举从low的长度到high的长度之间的符合条件的数,一旦发现它在low和high的范围之内,就更新计数器。然而,总觉得这种解法不是最优的,因为从直观上来说我们可能不需要枚举所有符合条件的数。等后面有空再想想具体做法吧。

代码

class Solution {public:    int strobogrammaticInRange(string low, string high) {        int n1 = low.length(), n2 = high.length();        int ret = 0;        for (int i = n1; i <= n2; ++i) {            dfs(low, high, i, "", ret);        }        return ret;    }private:    void dfs(string &low, string &high, int n, string str, int &result) {          if(n == 0) {              if (stol(str) >= stol(low) && stol(str) <= stol(high)) {                ++result;            }            return;        }          if(n % 2 != 0) {            for(auto val : same) {                  dfs(low, high, n-1, val, result);              }          }          else {              for(int i = (n == 2) ? 1 : 0; i < two.size(); ++i) {                  dfs(low, high, n-2, two[i].first + str + two[i].second, result);              }          }      }      vector<string> same{"0", "1", "8"};        vector<pair<char,char>> two{{'0','0'},{'1','1'},{'6','9'},{'8','8'},{'9','6'}};};

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大众速腾的不锈钢圈被碰花了怎么办 公司卡着生育险不给怎么办 济南图书馆借书超过期限了怎么办 淘宝解绑支付宝支付密码不对怎么办 微博支付宝支付密码忘记了怎么办 图虫签约师通过了认证标识怎么办 签证做假工作证明资料被拒怎么办 在广州办个建设厅电工证怎么办 水利考的五大员证到有效期怎么办 额头注射玻尿酸吸收后不平怎么办 施工员证书挂靠拿不回来怎么办 森林公安未转政法编制的人员怎么办 北京的限行新政策外地车怎么办 报考二级建造师工作年限不够怎么办 郑州航院图书馆密码忘了怎么办 无锡妇幼预约挂号过号了怎么办 云南建筑八大员考试没通过怎么办 订车ax7一个月提不到车怎么办 提车时间到了却没车怎么办 一汽大众速腾气囊灯亮该怎么办呢? 幼儿园上课时候电脑上的课件怎么办 黑米紫薯红豆粥不好煮怎么办 母狗生了小狗后不吃东西没奶怎么办 狗给扑倒了主人不想负责怎么办 山东政务网个人中心账号忘了怎么办 网易博客忘记登入名和密码了怎么办 奶水不够怎么办怎样让奶水变多 和初恋分手多年又爱上初恋该怎么办 分手六年的初恋想要和我复合怎么办 不小心把手机里的视频删了怎么办 网签过了中介不配合过户怎么办 房屋被中介恶意网签了怎么办 重介质选矿块矿品位低怎么办 去泰国酒吧选小姐只会说中文怎么办 脚被窝烫伤后发炎里面有龙怎么办 脚被窝烫了发炎有龙怎么办 辣椒落花的药喷到孩子嘴巴了怎么办 香炉里的小米生虫子了怎么办 香炉里面放的小米生虫子了怎么办 肉肉上长满了白色的小虫子怎么办 多肉上面有白色的虫子怎么办