[Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
Strobogrammatic Number
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string.
For example, the numbers "69", "88", and "818" are all strobogrammatic.
分析:
找出中心对称的阿拉伯数字串,解释型题目,其中0,1,8本身是中心对称的,69互相中心对称
代码:
bool isStrobogrammatic(string num) {
int i = , j = int(num.length()) - ;
while(i < j) {
if((num[i] == '' && num[j] == '') || (num[i] == '' && num[j] == '') || (num[i] == num[j] && (num[i] == '' || num[i] == '' || num[i] == ''))) {
i++;
j--;
}
else
return false;
}
//如果i大于j,则为偶数串,直接return true;i等与j,则判断num[i]本身是否是中心对称
return i > j ? true : (num[i] == '' || num[i] == '' || num[i] == '');
}
Strobogrammatic Number II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Find all strobogrammatic numbers that are of length = n.
For example, given n = 2, return ["11","69","88","96"]
.
分析:
跟I类似,主要问题还是在与代码解释,由于要列出所有可能的答案,递归的复杂度是至少的,所以就用递归吧,DFS, BFS都行
代码:
void dfs(vector<string> &result, string str, int i, int j) {
if(i == -) {
result.push_back(str);
return;
}
if(i == j) {
i--;
j++;
dfs(result, str + '', i, j);
dfs(result, str + '', i, j);
dfs(result, str + '', i, j);
}
else {
i--;
j++;
if(i != -)
dfs(result, '' + str + '', i, j);
dfs(result, '' + str + '', i, j);
dfs(result, '' + str + '', i, j);
dfs(result, '' + str + '', i, j);
dfs(result, '' + str + '', i, j);
}
return;
}
vector<string> findCertainStrobogrammatic(int num) {
vector<string> result;
dfs(result, "", (num - )/, num/);
return result;
}
Strobogrammatic Number III
The idea is similar to Strobogrammatic Number II: generate all those in-range strobogrammatic numbers and count.
分析:
与两个边界中任何一个等长的字符串要进行逐个验证满足要求。最初的想法为了减少时间复杂度,长度n(n > 1)介于两者之间的直接用个数函数计算:n为偶数时,count(n) = 4 * 5^(n/2 -1);n为奇数时,count(n) = 12 * 5^(n/2 - 1)。但问题在于,n足够大时,O(n * 5^n)与O(5^n)基本没差别,所以如果采用逐个验证的方法,也就不必在乎小于n时的计算量了。
[Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III的更多相关文章
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- [Swift]LeetCode247.对称数 II $ Strobogrammatic Number II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- Single Number i and ii
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Number of Islands II
Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- 305. Number of Islands II
题目: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand ...
- [Swift]LeetCode264.丑数 II | Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
随机推荐
- mac 下安装安卓模拟器
mac下的安卓模拟器——bluestacks 下载:http://pan.baidu.com/s/1kVvZyYz 该安卓模拟器除了能网络下载apk,本地还能安装apk,但是与win版不同,安装不是那 ...
- 借用Toad 生成表空间的使用量图示
图示产生方法 图示(tablespace uage)如下
- asp.net发布和更新网站
我们一般使用ftp软件来更新网站,而更新之前的一个步骤就是发布项目.以下将讲解asp.net mvc如何发布网站. 打开项目 右键点击项目,选择“发布” 第一次发布前,需要配置一下发布配置文件:点击” ...
- SGU 132.Another Chocolate Maniac
时间限制:0.25s 空间限制:4M 题目: Bob非常喜欢巧克力,吃再多也觉得不够.当他的父母告诉他将要买很多矩形巧克力片为他庆祝生日时,他的喜悦是能被理解的.巧克力都是 2x1 或 1x2 的矩形 ...
- JQuery 绑定回车事件 兼容ie8,ie9
$("#form-search").find('#search-query').bind('keypress', function(e) { var keycode; if(win ...
- CSAPP LAB: Buffer Overflow
这是CSAPP官网上的著名实验,通过注入汇编代码实现堆栈溢出攻击.实验材料可到我的github仓库 https://github.com/Cheukyin/CSAPP-LAB/ 选择buffer-ov ...
- PAT - 基础 - 最大公约数和最小公倍数
题目: 本题要求两个给定正整数的最大公约数和最小公倍数. 输入格式: 输入在一行中给出2个正整数M和N(<=1000). 输出格式: 在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1 ...
- Javascript闭包函数快速上手
闭包函数是什么?在开始学习的闭包的时候,大家很能都比较难理解.就从他的官方解释来说,都是比较概念化的. 不过我们也还是从闭包的含义出发. 闭包是指函数有自由独立的变量.换句话说,定义在闭包中的函数可以 ...
- php框架练习
框架的作用: 功能:操作数据库 模版引擎smarty 分析,应该具备什么功能? a:具体配置文件 ,配置文件的读取功能 b:数据库处理类 c:目录结构 先建一个includes文件放入,配置文件(co ...
- 基于cx_freeze编译PyQt4程序(numpy & scipy)
当开发完成PyQt4程序后,需要提供给他人使用,这时最好的办法是将Python程序编译成exe文件. 通常我采用cx_freeze完成这个工作,即编写setup.py文件,执行python setup ...