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(2^n). Space: O(n).

AC Java:

 1 public class Solution {
 2     public List<String> findStrobogrammatic(int n) {
 3         return findHelper(n,n);
 4     }
 5
 6     private List<String> findHelper(int m, int n){
 7         if(m == 0){
 8             return new ArrayList<String>(Arrays.asList(""));
 9         }
10         if(m == 1){
11             return new ArrayList<String>(Arrays.asList("0", "1", "8"));
12         }
13         List<String> base = findHelper(m-2, n);
14         List<String> res = new ArrayList<String>();
15         for(String s : base){
16             if(m != n){
17                 res.add("0" + s + "0");
18             }
19             res.add("1" + s + "1");
20             res.add("8" + s + "8");
21             res.add("6" + s + "9");
22             res.add("9" + s + "6");
23         }
24         return res;
25     }
26 }

类似Strobogrammatic Number

时间: 2024-12-15 08:40:30

LeetCode Strobogrammatic Number II的相关文章

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode &quot;Strobogrammatic Number III&quot;

It can be solved based on the code from "Strobogrammatic Number II". The idea is pretty straight forward - binary search the boundaries. class Solution { int go_cnt(int n, bool bInner) { int ret = 0; switch (n) { case 0: ret = 1; // ""

[LeetCode] 247. Strobogrammatic Number II 对称数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"

Strobogrammatic Number II -- LeetCode

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"

[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",&quo

leetcode Ugly Number II

题目连接 https://leetcode.com/problems/ugly-number-ii/ Ugly Number II Description Write a program to find the $n_{th}$ ugly number. Ugly numbers are positive numbers whose prime factors only include $2, 3, 5.$ For example,$ 1, 2, 3, 4, 5, 6, 8, 9, 10, 12

[LeetCode] Strobogrammatic Number 对称数

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string. For example, the numbers "69", "

LeetCode Strobogrammatic Number

原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is r

leetcode Single Number II python

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for eachint in nums: if