题目:

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

Hint:

    1. Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.

链接: http://leetcode.com/problems/strobogrammatic-number-ii/

题解:

求所有长度为n的strobogrammatic number。这题一开始的思路是用DFS + Backtracking。还要处理一些特殊的边界条件,比如n的长度为奇数和偶数,以及最外层不能为两个'0'等等,代码写得很拖沓。 Discuss区有不少好的代码,二刷时一定要思考清楚再进行优化。这里我主要是从中心向两边添加,而discuss区大家大部分都是从两边向中心递归,所以我的代码还需要回溯,不够简练。

Time Complexity - O(2n), Space Complexity - O(n)。

public class Solution {
public List<String> findStrobogrammatic(int n) {
if(n < 1)
return new ArrayList<String>();
List<String> res = new ArrayList<>();
Map<Character, Character> map = new HashMap<>();
map.put('0', '0');
map.put('1', '1');
map.put('6', '9');
map.put('8', '8');
map.put('9', '6'); StringBuilder sb = new StringBuilder();
int position = (n % 2 == 0) ? 0 : 1;
findStrobogrammatic(res, sb, map, n, position); return res;
} private void findStrobogrammatic(List<String> res, StringBuilder sb, Map<Character, Character> map, int n, int position) {
if(sb.length() > n)
return;
if(sb.length() == n) {
res.add(sb.toString());
return;
} if(position == 1) {
for(char c : map.keySet()) {
if(c == '6' || c == '9')
continue;
sb.append(c);
findStrobogrammatic(res, sb, map, n, position + 1);
sb.setLength(0);
}
} else {
for(char c : map.keySet()) {
if(n - sb.length() == 2 && c == '0')
continue;
sb.insert(0, c);
sb.append(map.get(c));
findStrobogrammatic(res, sb, map, n, position + 2);
sb.deleteCharAt(0);
sb.deleteCharAt(sb.length() - 1);
}
}
}
}

二刷:

两种思路:

  1. 和一刷一样,用dfs + backtracking
  2. 求一半String的permutation,剪去一些invalid case,再补上另外一半。也就是使用跟267. Palindrome Permutation II类似的方法。

Java:

Reference:

https://leetcode.com/discuss/50412/ac-clean-java-solution

https://leetcode.com/discuss/50377/my-concise-java-solution-using-dfs

https://leetcode.com/discuss/52277/accepted-java-solution-using-recursion

https://leetcode.com/discuss/53144/my-concise-iterative-java-code

https://leetcode.com/discuss/68215/simple-java-solution-without-recursion

247. Strobogrammatic Number II的更多相关文章

  1. [LeetCode#247] Strobogrammatic Number II

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

  2. 247. Strobogrammatic Number II输出所有对称数字

    [抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...

  3. [LeetCode] 247. Strobogrammatic Number II 对称数II

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

  4. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  5. [LeetCode] Strobogrammatic Number II 对称数之二

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

  6. LeetCode Strobogrammatic Number II

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...

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

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

  8. Strobogrammatic Number II -- LeetCode

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

  9. 246. Strobogrammatic Number 上下对称的数字

    [抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...

随机推荐

  1. mikrotik/IPSec Dynamic End points Updater.rsc

    # IPSec Peer/Policy Updater for Dynamic WAN addresses # ============================================ ...

  2. How to disable Passwords must meet complexity requirements[windows 7]

    The Password complexity is a Local Policy setting named "Passwords must meet complexity require ...

  3. 使用 Visual Studio Team Test 进行单元测试和java中的测试

    C#中test测试地 方法一. 1.从NUnit官网(http://www.nunit.org/index.php)下载最新版本NUnit,当前版本为NUnit2.5.8. 2.安装后,在VS2008 ...

  4. Oracle利用数据泵迁移用户

    一.利用数据泵将数据导出 1.1.确定字符集: select * from v$nls_parameters; 或 select userenv('language') from dual; 1.2. ...

  5. WPF与DevExpress之旅-序言

    随着.NET技术的发展,从之前的WINFORM转向到WPF是我们技术改革的必然趋势.WPF能给人带来震撼的视觉体验,也能更加规范我们的开发模式,与传统的WINFORM开发来说具有革命性的意义.DevE ...

  6. 深入剖析——float之个人见解

    浮动的原本作用仅仅是为了实现文字的环绕效果. 以下分别是html与css代码,显示效果如下图.因为两个div使用了float浮动属性,所以脱离了标准文档流.让父元素撑开高度,我们需要清除浮动. < ...

  7. 1100. Mars Numbers (20)

    People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...

  8. cocos2dx中的触摸事件及触摸优先级

    1.只有CCLayer及其派生类才有触摸功能. 2.开启触摸 setTouchEnable(true); 3.设置触摸模式,单点,多点(仅IOS支持) setTouchMode(kCCTouchesO ...

  9. C# ASP .NET WEB方向和WPF方向,我该如何去选择

    一个2012年南航毕业学软件的学生,该如何去选择我的职业方向? 2011年11分月份,我被老师介绍在南京珠江路华丽国际大厦工作,开发一个大型国际物流平台,公司的开发人员比较少,设计网站的是高校的老师, ...

  10. JAVA里的String、Timestamp、Date相互转换(转)

    转自:http://blog.sina.com.cn/s/blog_6675493d0100lbfl.html Timestamp转化为String: SimpleDateFormat df = ne ...