返回介绍

solution / 1300-1399 / 1317.Convert Integer to the Sum of Two No-Zero Integers / README_EN

发布于 2024-06-17 01:03:20 字数 4204 浏览 0 评论 0 收藏 0

1317. Convert Integer to the Sum of Two No-Zero Integers

中文文档

Description

No-Zero integer is a positive integer that does not contain any 0 in its decimal representation.

Given an integer n, return _a list of two integers_ [a, b] _where_:

  • a and b are No-Zero integers.
  • a + b = n

The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.

 

Example 1:

Input: n = 2
Output: [1,1]
Explanation: Let a = 1 and b = 1.
Both a and b are no-zero integers, and a + b = 2 = n.

Example 2:

Input: n = 11
Output: [2,9]
Explanation: Let a = 2 and b = 9.
Both a and b are no-zero integers, and a + b = 9 = n.
Note that there are other valid answers as [8, 3] that can be accepted.

 

Constraints:

  • 2 <= n <= 104

Solutions

Solution 1

class Solution:
  def getNoZeroIntegers(self, n: int) -> List[int]:
    for a in range(1, n):
      b = n - a
      if "0" not in str(a) + str(b):
        return [a, b]
class Solution {
  public int[] getNoZeroIntegers(int n) {
    for (int a = 1;; ++a) {
      int b = n - a;
      if (!(a + "" + b).contains("0")) {
        return new int[] {a, b};
      }
    }
  }
}
class Solution {
public:
  vector<int> getNoZeroIntegers(int n) {
    for (int a = 1;; ++a) {
      int b = n - a;
      if ((to_string(a) + to_string(b)).find('0') == -1) {
        return {a, b};
      }
    }
  }
};
func getNoZeroIntegers(n int) []int {
  for a := 1; ; a++ {
    b := n - a
    if !strings.Contains(strconv.Itoa(a)+strconv.Itoa(b), "0") {
      return []int{a, b}
    }
  }
}

Solution 2

class Solution:
  def getNoZeroIntegers(self, n: int) -> List[int]:
    def f(x):
      while x:
        if x % 10 == 0:
          return False
        x //= 10
      return True

    for a in range(1, n):
      b = n - a
      if f(a) and f(b):
        return [a, b]
class Solution {
  public int[] getNoZeroIntegers(int n) {
    for (int a = 1;; ++a) {
      int b = n - a;
      if (f(a) && f(b)) {
        return new int[] {a, b};
      }
    }
  }

  private boolean f(int x) {
    for (; x > 0; x /= 10) {
      if (x % 10 == 0) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  vector<int> getNoZeroIntegers(int n) {
    auto f = [](int x) {
      for (; x; x /= 10) {
        if (x % 10 == 0) {
          return false;
        }
      }
      return true;
    };
    for (int a = 1;; ++a) {
      int b = n - a;
      if (f(a) && f(b)) {
        return {a, b};
      }
    }
  }
};
func getNoZeroIntegers(n int) []int {
  f := func(x int) bool {
    for ; x > 0; x /= 10 {
      if x%10 == 0 {
        return false
      }
    }
    return true
  }
  for a := 1; ; a++ {
    b := n - a
    if f(a) && f(b) {
      return []int{a, b}
    }
  }
}

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

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

发布评论

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