[LeetCode#247] Strobogrammatic Number II
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的更多相关文章
- [LeetCode] 247. Strobogrammatic Number II 对称数II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- 247. Strobogrammatic Number II
题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...
- 247. Strobogrammatic Number II输出所有对称数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
- [LeetCode] 246. Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 248. Strobogrammatic Number III 对称数III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 248. Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode 246. Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
- [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 ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
随机推荐
- iOS常见问题(4)
一.非ARC内存管理问题. 有些同学在创建项目的时候忘记点ARC了,导致一些成员属性都莫名其妙的释放了.然后出现了一系列莫名其妙的错误. 在滚动UITableView的时候出现野指针错误. 一出现这些 ...
- sharepoint mysite and upgrade topics
My Sites overview (SharePoint Server 2010)http://technet.microsoft.com/en-us/library/ff382643(v=offi ...
- 如何在64位的Windows中安裝PLSQLDEVELOPER 8
先到 Oracle 官網下載Oracle Database 11g Release 2 Client (11.2.0.1.0) for Microsoft Windows (x64) ,接者依照以下步 ...
- bzoj 1295: [SCOI2009]最长距离 暴力+bfs最短路
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1295 题解: 对每个点暴力跑一遍bfs,看能够到达的最远位置,这里如果有障碍物则距离为1 ...
- Java多线程——<一>概述、定义任务
一.概述 为什么使用线程?从c开始,任何一门高级语言的默认执行顺序是“按照编写的代码的顺序执行”,日常开发过程中写的业务逻辑,但凡不涉及并发的,都是让一个任务顺序执行以确保得到想要的结果.但是,当你的 ...
- 2014 Multi-University Training Contest 8
官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uzj7.html Area of Mushroom http://acm.hdu.edu.cn/s ...
- Finite State Machine
Contents [hide] 1 Description 2 Components 3 C# - FSMSystem.cs 4 Example Description This is a Dete ...
- properties配置应用,为什么需要使用properties文件
在项目中我们常常会使用Constants常量类,达到系统全局配置的目的. 但是有些常量需要动态的配置,如果项目上线后,每次修改Constants.java然后再编译,再上传Constants.clas ...
- hdu 4578 Transformation 线段树
没什么说的裸线段树,注意细节就好了!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> ...
- hdu 1271 整数对
看了别人的解题报告a了, 大致思路就是 A=a+b*10^k+c*10^(k+1) B=a+c*10^k (在A中取出一位数后) N=A+B=2*a+b*10^k+11*c*10^k 这样就好做了,再 ...