返回介绍

solution / 0200-0299 / 0248.Strobogrammatic Number III / README_EN

发布于 2024-06-17 01:04:02 字数 4923 浏览 0 评论 0 收藏 0

248. Strobogrammatic Number III

中文文档

Description

Given two strings low and high that represent two integers low and high where low <= high, return _the number of strobogrammatic numbers in the range_ [low, high].

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

 

Example 1:

Input: low = "50", high = "100"
Output: 3

Example 2:

Input: low = "0", high = "0"
Output: 1

 

Constraints:

  • 1 <= low.length, high.length <= 15
  • low and high consist of only digits.
  • low <= high
  • low and high do not contain any leading zeros except for zero itself.

Solutions

Solution 1

class Solution:
  def strobogrammaticInRange(self, low: str, high: str) -> int:
    def dfs(u):
      if u == 0:
        return ['']
      if u == 1:
        return ['0', '1', '8']
      ans = []
      for v in dfs(u - 2):
        for l, r in ('11', '88', '69', '96'):
          ans.append(l + v + r)
        if u != n:
          ans.append('0' + v + '0')
      return ans

    a, b = len(low), len(high)
    low, high = int(low), int(high)
    ans = 0
    for n in range(a, b + 1):
      for s in dfs(n):
        if low <= int(s) <= high:
          ans += 1
    return ans
class Solution {
  private static final int[][] PAIRS = {{1, 1}, {8, 8}, {6, 9}, {9, 6}};
  private int n;

  public int strobogrammaticInRange(String low, String high) {
    int a = low.length(), b = high.length();
    long l = Long.parseLong(low), r = Long.parseLong(high);
    int ans = 0;
    for (n = a; n <= b; ++n) {
      for (String s : dfs(n)) {
        long v = Long.parseLong(s);
        if (l <= v && v <= r) {
          ++ans;
        }
      }
    }
    return ans;
  }

  private List<String> dfs(int u) {
    if (u == 0) {
      return Collections.singletonList("");
    }
    if (u == 1) {
      return Arrays.asList("0", "1", "8");
    }
    List<String> ans = new ArrayList<>();
    for (String v : dfs(u - 2)) {
      for (var p : PAIRS) {
        ans.add(p[0] + v + p[1]);
      }
      if (u != n) {
        ans.add(0 + v + 0);
      }
    }
    return ans;
  }
}
using ll = long long;

class Solution {
public:
  const vector<pair<char, char>> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};

  int strobogrammaticInRange(string low, string high) {
    int n;
    function<vector<string>(int)> dfs = [&](int u) {
      if (u == 0) return vector<string>{""};
      if (u == 1) return vector<string>{"0", "1", "8"};
      vector<string> ans;
      for (auto& v : dfs(u - 2)) {
        for (auto& [l, r] : pairs) ans.push_back(l + v + r);
        if (u != n) ans.push_back('0' + v + '0');
      }
      return ans;
    };

    int a = low.size(), b = high.size();
    int ans = 0;
    ll l = stoll(low), r = stoll(high);
    for (n = a; n <= b; ++n) {
      for (auto& s : dfs(n)) {
        ll v = stoll(s);
        if (l <= v && v <= r) {
          ++ans;
        }
      }
    }
    return ans;
  }
};
func strobogrammaticInRange(low string, high string) int {
  n := 0
  var dfs func(int) []string
  dfs = func(u int) []string {
    if u == 0 {
      return []string{""}
    }
    if u == 1 {
      return []string{"0", "1", "8"}
    }
    var ans []string
    pairs := [][]string{{"1", "1"}, {"8", "8"}, {"6", "9"}, {"9", "6"}}
    for _, v := range dfs(u - 2) {
      for _, p := range pairs {
        ans = append(ans, p[0]+v+p[1])
      }
      if u != n {
        ans = append(ans, "0"+v+"0")
      }
    }
    return ans
  }
  a, b := len(low), len(high)
  l, _ := strconv.Atoi(low)
  r, _ := strconv.Atoi(high)
  ans := 0
  for n = a; n <= b; n++ {
    for _, s := range dfs(n) {
      v, _ := strconv.Atoi(s)
      if l <= v && v <= r {
        ans++
      }
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文