Leetcode Weekly – 171 – 1

Time:2020-10-18

1317. Convert an integer to the sum of two nonzero integers

Hi, everyone. I’m a pig. Welcome to the weekly leetcode quiz of the “baby can understand” series.

This is the first question in issue 171 and 1317 in the list of topics – “converting an integer into the sum of two nonzero integers.”

Title Description

Zero free integers are in decimal representationDoes not contain any 0Is a positive integer of.

Here is an integer n. please return oneA list of two integers [A, B]To meet the following requirements:

  • AandBThey are all nonzero integers
  • A + B = n

Topic data ensures at least one effective solution.

If there are multiple valid solutions, you can return any of them.

Example 1:

Input: n = 2
Output: [1,1]
Explanation: a = 1, B = 1. A + B = n and the decimal representation of a and B does not contain any 0.

Example 2:

Input: n = 11
Output: [2,9]

Example 3:

Input: n = 10000
Output: [19999]

Example 4:

Input: n = 69
Output: [1,68]

Example 5:

Input: n = 1010
Output: [11999]

Tips:

  • 2 <= n <= 10^4

Official difficulty

EASY

Solutions

The requirement of the title is, given an integer, you need to return a pair of integers that do not contain 0 as a sum.

After reading the question, my first reaction is whether there is any mathematical method to find the answer. Touch the pig nose, think for a while, did not think of any reliable method. It must be a little pig. It’s blue and thin

Next, I turned to the computer-based way. So the first reaction is to enumerate violence. Time complexity O (n), should be able to AC, then try it first.

enumeration

This idea is actually very direct, that is to traverse all possible addends on one side, and then determine whether they contain 0. Since there is no requirement for the returned solution, we can directly return to the first group of solutions. The specific process is as follows:

  1. Traverse all addends from 1.
  2. Determine whether the addend and the corresponding addend contain 0.
  3. Returns the first solution encountered.

But there’s a little bit of optimization here. On the one hand, in the process of judging 0, the rounding operation after dividing by 10 can be directly implemented by bit operation. On the other hand, for each integer, whether it contains 0 is an objective fact. So we can cache all test cases globally to avoid unnecessary calculation. And because the topic limits the scope of[2, 10^4]So we can use a fixed length array to record.

The specific codes are as follows:

const memo = new Uint8Array(10000);
const helper = x => {
  if (memo[x] !== 0) return memo[x] === 1;
  while (x > 0) {
    if (x % 10 === 0) { memo[x] = 2; return false; }
    x = x / 10 << 0;
  }
  memo[x] = 1;
  return true;
};
const getNoZeroIntegers = n => {
  let m = 0;
  while (n--) {
    if (helper(++m) && helper(n)) return [m, n];
  }
};

Non enumeration

If you only give a brute force solution, you will not be satisfied with it<

So after a lot of brainstorming,Piggy decided to play a few dishes of chicken first,Piggy put the idea back to the topic condition itself. If you want to change an integer into an integer that doesn’t contain 0, there are two possibilities. First, the number itself does not contain 0, so we wish it a happy new year. Second, one of the digits is 0. For this case, let’s analyze it in detail.

First of all, the zero can’t be the highest bit, because the title is given an ordinary integer. So if we want to turn it into another number, it’s either an increase or a decrease. Since we are looking for an adder, we can only choose minus. And since it is not a leader 0, subtraction is safe. The value of subtraction is determined based on the current position. For example, if it is 100 digits, it will be reduced by 10 digits. In this way, we replace 0 in the current position with a non-zero.

However, it should be noted that our pair of adders need to be added and subtracted synchronously, and they need to repeatedly verify whether they contain 0. The specific process is as follows:

  1. Put 1 andn - 1As the initial value.
  2. Determine whether they contain 0:

    • If included, find the position of 0, subtract the cardinality of the corresponding position, and return to step 2.
    • If not, wish it a happy new year.
  3. Output results.

Based on this process, we can implement code like this:

const helper = x => {
  let digit = 0;
  while (x > 0) {
    if (x % 10 === 0) break;
    x = x / 10 << 0;
    ++digit;
  }
  return 10 ** digit;
};
const getNoZeroIntegers = n => {
  let x = 1, y = n - 1;
  while (true) {
    let num = helper(y);
    if (num < y) {
      y -= num;
      x += num;
      continue;
    }
    num = helper(x);
    if (num > x) break;
    y -= num;
    x += num;
  }
  return [x, y];
};

This code temporarily beats 100%.

summary

The first question of weekly competition is to guarantee the minimum score. However, I think it’s OK to use enumeration for this problem. The non enumeration ideas provided in the following are just for the kids to have fun. Because I don’t know why, but I always think it’s a bit strange to deal with problems in this way.

Well, there may be a lot of friends who have already gone home or are stuck on their way home. Here or in advance to send Piggy’s sincere wishes, I hope you in the new year, hairline must be safe oh. Momada > is the most popular one<

Related links

  • Weekly contest 171 topic list
  • Repo
  • My segment fault column
  • My Zhihu column

Leetcode Weekly - 171 - 1