Problem:

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

Analysis:

This problem is easy. It is just a reverse process of checking a Strobogrammatic number. But I have made following mistakes at this process. 

Mistake 1:
Wrongly use return when forking search branch. (This would cause other search branches were ignored!)
if (len != n - 2)
cur = num + cur + num;
else
return; Mistake 2:
Wrongly pass around String's reference, without create a new one.
Usually I use String in following way.
helper (..., cur + 'c', ...)
It works totally fine. But this time, I have used the reference in following way, which is totally wrong!
helper(..., cur, ...) To pass around string reference, we should be very careful. Since it is not primitive type, but it was conveniently used as a primitive type. When we use a string, we actually pass it's reference around.
1. Only when we make a change over it, the instance of the old string was discarded and a new string was created.
2. Only when we use a new reference and use "=" sign to copy the content. If we do not pass the new intance as an argument, many problems could occur! 2.1 get the wrong length of the string. Even the cur was actually changed, we keep on get it's original length 0. Thus there is no stop for this program.
...
searchPath(n, nums, "", 0, ret);
..
private void searchPath(int n, char[] nums, String cur, List<String> ret) {
int len = cur.length();
if (len == n) {
ret.add(cur);
return;
}
...
} Errors:
Overflow 2.2 Get wrong result.
To fix above problem, I have tried to pass around the cur's length. But it actually still wrong!! Wrong solution : (update on the same cur all the time)
public class Solution {
public List<String> findStrobogrammatic(int n) {
if (n < 0)
throw new IllegalArgumentException("n is negative!");
List<String> ret = new ArrayList<String> ();
if (n == 0)
return ret;
char[] nums = {'0', '1', '6', '8', '9'};
if (n % 2 == 1) {
for (char num : nums) {
searchPath(n, nums, num + "", 1, ret);
}
} else{
searchPath(n, nums, "", 0, ret);
}
return ret;
} private void searchPath(int n, char[] nums, String cur, int cur_len, List<String> ret) {
if (cur_len == n) {
ret.add(cur);
return;
}
for (char num : nums) {
if (num == '0') {
if (cur_len != n - 2)
cur = num + cur + num;
else
continue;
} else{
if (num == '9')
cur = '9' + cur + '6';
else if (num == '6')
cur = '6' + cur + '9';
else
cur = num + cur + num;
}
searchPath(n, nums, cur, cur_len+2, ret);
}
}
} Error cases:
Input:
2
Output:
["11","6119","861198","98611986"]
Expected:
["11","69","88","96"] As you can see, the "cur" was passed around all the time. Actually we use the same instance all the time.
Fix method:
String next_cur;
for (char num : nums) {
if (num == '0') {
if (len != n - 2)
next_cur = num + cur + num;
else
continue;
} else{
if (num == '9')
next_cur = '9' + cur + '6';
else if (num == '6')
next_cur = '6' + cur + '9';
else
next_cur = num + cur + num;
}
searchPath(n, nums, next_cur, ret);
} Mistake 3:
Another mistake I have made is that I fail to consider "6" and "9" could not be put in the middle when n is odd.
Error case:
Input:
3
Output:
["101","609","808","906","111","619","818","916","161","669","868","966","181","689","888","986","191","699","898","996"]
Expected:
["101","111","181","609","619","689","808","818","888","906","916","986"]

Solution:

public class Solution {
public List<String> findStrobogrammatic(int n) {
if (n < 0)
throw new IllegalArgumentException("n is negative!");
List<String> ret = new ArrayList<String> ();
if (n == 0)
return ret;
char[] core_num = {'0', '1', '8'};
char[] nums = {'0', '1', '6', '8', '9'};
if (n % 2 == 1) {
for (char num : core_num) {
searchPath(n, nums, num + "", ret);
}
} else{
searchPath(n, nums, "", ret);
}
return ret;
} private void searchPath(int n, char[] nums, String cur, List<String> ret) {
int len = cur.length();
if (len == n) {
ret.add(cur);
return;
}
String next_cur;
for (char num : nums) {
if (num == '0') {
if (len != n - 2)
next_cur = num + cur + num;
else
continue;
} else{
if (num == '9')
next_cur = '9' + cur + '6';
else if (num == '6')
next_cur = '6' + cur + '9';
else
next_cur = num + cur + num;
}
searchPath(n, nums, next_cur, ret);
}
}
}

[LeetCode#247] Strobogrammatic Number II的更多相关文章

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

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

  2. 247. Strobogrammatic Number II

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

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

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

  4. [LeetCode] 246. Strobogrammatic Number 对称数

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

  5. [LeetCode] 248. Strobogrammatic Number III 对称数III

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

  6. [LeetCode] 248. Strobogrammatic Number III 对称数之三

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

  7. LeetCode 246. Strobogrammatic Number

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

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

  9. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

随机推荐

  1. C++中const关键字详解

    1.什么是const? const意味着是常量类型,被const修饰的变量或对象是不能被修改和更新的,当然在某些情况下,我们可以偷梁换柱的改变它. 2.为什么要引入const? 最初的目的是为了取代预 ...

  2. iOS 23 种设计模式

    设计模式主要分三个类型:创建型.结构型和行为型. 其中创建型有: 一.Singleton,单例模式:保证一个类只有一个实例,并提供一个访问它的全局访问点 二.Abstract Factory,抽象工厂 ...

  3. html5音频、视频

    1.插入一个视频:<video src="test.webm" width="800" height="600"></vi ...

  4. 使用parseJSON代替eval

    有些程序员如果没有很好的在javascript中解析json数据,往往会直接eval把json转成js对象,这时候如果json的数据中包含了被注入的恶意数据,则可能导致代码注入的问题. 正确的做法是分 ...

  5. [转载]C# FTP操作工具类

    本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...

  6. 如何将jsp中<input>设为只读

    将一个input变为只读,可以使用 readonly 属性 和 disabled 属性.  用disabled 属性时,文字显示为灰色.  下面的两种方法都是可以的: <input id =&q ...

  7. cf 359C

    stl 里的map使用   然后就是快速幂取余 #include <cstdio> #include <cstring> #include <algorithm> ...

  8. Linux重启inotify配置max_user_watches无效被恢复默认值8192的正确修改方法

    Linux下Rsync+inotify-tools实现数据实时同步中有一个重要的配置就是设置Inotify的max_user_watches值,如果不设置,当遇到大量文件的时候就会出现出错的情况. 一 ...

  9. 转:[gevent源码分析] 深度分析gevent运行流程

    [gevent源码分析] 深度分析gevent运行流程 http://blog.csdn.net/yueguanghaidao/article/details/24281751 一直对gevent运行 ...

  10. POJ 3318 Matrix Multiplication(矩阵乘法)

    题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...