LeetCode Strobogrammatic Number II
2024-10-19 17:31:37
原题链接在这里: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;
}
}
最新文章
- socket编程学习step1
- WebStrom 10 注册码(转)
- Dom终
- Recyclerview使用系列教材
- 美团在Redis上踩过的一些坑-目录(本人非美团)(转)
- jQuery 的 live() 方法对 hover 事件的处理
- JavaScript 字符串函数 之查找字符方法(一)
- server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh
- 基于 Aliexpress API 的小程序 : 批量 Copy 产品到不同的店铺
- React翻译官网文档之JSX
- MySQL视图了解
- javaScript基础的基础
- 拉勾网爬取全国python职位并数据分析薪资,工作经验,学历等信息
- VS下.net开发常用扩展、配置
- vue的定位
- mongodb查看操作记录方法以及用户添加删除权限修改密码
- C++之 模板Template的使用
- annovar积累
- Java中对话框的弹出
- Zend Studio使用综述
热门文章
- 【python游戏编程之旅】第六篇---pygame中的Sprite(精灵)模块和加载动画
- hadoop datanode 挂机恢复后,多复制的块删除的问题
- java线程详解
- <;构建之法>;第十一章、十二章有感
- 江西理工大学南昌校区acm选拔赛题解
- C++11 feature: move constructor
- CDOJ 435 (SCOI 2011) 糖果 Label:差分约束系统
- 神奇的莫比乌斯带(mobius)
- URAL 1501. Sense of Beauty(记忆化搜索)
- GridView合并表头、多重表头(转)