LeetCode Strobogrammatic Number II
原题链接在这里:https://leetcode.com/problems/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"]
.
题解:
base case 分奇数偶数两种. 拿到base case, 在每一个 string 前后加上"1", "1"; "8","8";...等等添加到新的res中去.
corner case是若是string长度大于1, "0"不能加在最前面. 所以用 m != n搞定.
Time Complexity: O(5^n), exponential.
Space: O(n/2 + 5^(n/2)), n/2层stack, 5^(n/2)是当前base的大小.
AC Java:
public class Solution {
public List<String> findStrobogrammatic(int n) {
return findHelper(n, n);
} private List<String> findHelper(int cur, int max){
if(cur == 0){
return new ArrayList<String>(Arrays.asList(""));
}
if(cur == 1){
return new ArrayList<String>(Arrays.asList("0", "1", "8"));
} List<String> res = new ArrayList<String>();
List<String> base = findHelper(cur-2, max);
for(String s : base){
if(cur != max){
res.add("0" + s + "0");
}
res.add("1" + s + "1");
res.add("8" + s + "8");
res.add("6" + s + "9");
res.add("9" + s + "6");
}
return res;
}
}
LeetCode Strobogrammatic Number II的更多相关文章
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
- Leetcode: Strobogrammatic Number III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [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] 247. Strobogrammatic Number II 对称数II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode#247] Strobogrammatic Number II
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- Strobogrammatic Number II -- LeetCode
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
随机推荐
- js 四舍五入保留二位小数
1. 最笨的办法....... [我就怎么干的.........] function get() { var s = 22.127456 + ""; var str = s.sub ...
- Codeforces Round #249 (Div. 2) A. Black Square
水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; ...
- 警惕自己,不断学习c++【转】
每天早上起床看一遍,时刻警惕自己,每天至少要浏览http://www.cplusplus.com 1.把C++当成一门新的语言学习(和C没啥关系!真的.):2.看<Thinking In C++ ...
- 我的Linux对拍脚本
本文用于Linux下bash的对拍脚本: brute为本目录的暴力程序.. pro为优化过的程序 mak造数据的.. #!/bin/bash while(true)do ./mak printf &q ...
- 一个不错的安卓下ssh客户端
1.使用安卓作为ssh客户端连接ssh服务器 软件名:JuiceSSH 版本 :1.4.8 大小 :4.22 M 百度网盘地址:JuiceSSH_1.4.8.apk 或 JuiceSSH_1 ...
- MAT(Memory Analyzer Tool)工具入门介绍
1.MAT是什么? MAT(Memory Analyzer Tool),一个基于Eclipse的内存分析工具,是一个快速.功能丰富的JAVA heap分析工具,它可以帮助我们查找内存泄漏和减少内存消耗 ...
- 【Go语言】I/O专题
本文目录 1.bytes包:字节切片.Buffer和Reader 1_1.字节切片处理函数 1_1_1.基本处理函数 1_1_2.字节切片比较函数 1_1_3.字节切片前后缀检查函数 1_1_4.字节 ...
- JSP内置对象---request和 response
<%@page import="java.net.URLEncoder"%> <%@page import="com.hanqi.web.CardDAO ...
- jquery中each遍历各种标签方法
这写天用到的遍历jquery each方法比较频繁 刚好有时间,就在这里记录一下 jquery用的是bootstrap的线上文件 不需要导入 <!DOCTYPE html><html ...
- jQuery对象和Dom对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...