首页 技术 正文
技术 2022年11月7日
0 收藏 751 点赞 964 浏览 1659 个字

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时的计算量了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905