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的更多相关文章

  1. 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 用了第一种方式, ...

  2. [Swift]LeetCode247.对称数 II $ Strobogrammatic Number II

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

  3. [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 ...

  4. Single Number i and ii

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 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  ...

  9. [Swift]LeetCode264.丑数 II | Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

随机推荐

  1. [转帖]vivado & VS2013工具

    来源:http://bbs.csdn.net/topics/380057699 添加OpenCV库后,MFC在Debug模式下调试,提示应用程序无法正常启动(0xc000007b). 解决方法:在环境 ...

  2. for循环,如何结束多层for循环

    采用标签方式跳出,指定跳出位置, a:for(int i=0;i<n;i++) { b:for(int j=0;j<n;j++) { if(n=0) { break a; } } }

  3. 主成份分析PCA

    Data Mining 主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有6 ...

  4. javascript--15条规则解析JavaScript对象布局(__proto__、prototype、constructor)

    大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...

  5. cmd 命令行下复制、粘贴的快捷键

    1.单击左下角“开始”菜单,选择“运行”,输入“cmd”. 2.在弹出的cmd窗口的标题栏上点击“右键”,选择“属性”. 3.在弹出的对话框中选择“选项”这个选项卡,在“编辑选项”区域中勾选“快速编辑 ...

  6. PHP中定义常量的几种方式与区别

    [问]在php中定义常量时,const与define的区别? [答]使用const使得代码简单易读,const本身就是一个语言结构,而define是一个函数.另外const在编译时要比define快很 ...

  7. photoshop cc 版本安装失败解决办法

    好久没有碰ps,看了下在ps版本都到cc了.忍不住也想尝试最新版本,但是安装出现了很多问题,导致我花了很多时间才搞定,现在分享给大家几点经验吧. Exit Code: Please see speci ...

  8. 简单实现tab标签页切换

    常见面试题: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  9. 移动Web开发,4行代码检测浏览器是否支持position:fixed

    不废话,直接上代码 var div = document.createElement('div'); div.style.cssText = 'display:none;position:fixed; ...

  10. [HOWTO] Install Sphinx for A Script Pro

    Hi, Here's a small howto on installing Sphinx Search (http://sphinxsearch.com/) and configuring it t ...