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

最新文章

  1. Vimium使用快捷键总结
  2. EM算法(3):EM算法运用
  3. 索引的重载 str[&quot;name&quot;] str[i]
  4. paip.哈米架构CAO.txt
  5. 爬虫神器xpath的用法(四)
  6. Unable to execute dex: Multiple dex files define Lcom/kenai/jbosh/AbstractAttr
  7. Git 代码管理常用命令
  8. Java jdk环境变量配置
  9. 开发EXTMVC框架前需要了解的基础知识整理
  10. 国内YUM源收集
  11. 1523. K-inversions(K逆序对)
  12. CodeKata
  13. SPSS方差分析
  14. 编写程序输入一个5x5的矩阵,将最大元素与中心元素交换,并按行列对齐输出。
  15. 使用shell+awk完成Hive查询结果格式化输出
  16. Flood-it!
  17. git clean -fdx
  18. 32.APP后端处理表情的一些技巧
  19. Python反序列化 pickle
  20. [Inside HotSpot] C1编译器优化:条件表达式消除

热门文章

  1. Eclipse代码自动填充.
  2. oracle 导出导入数据
  3. 介绍一个小工具 Linqer
  4. 最简单的基于FFmpeg的移动端例子:IOS 推流器
  5. AFNetworking自带的解析图片的方法
  6. Codeforces 193D Two Segments 解题报告
  7. dede取得指定栏目的链接
  8. Android各种访问权限Permission详解
  9. Python自动化运维之27、Django(一)
  10. Unity问答——NGUI怎么使用按键模拟鼠标点击?