본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 1317. Convert Integer to the Sum of Two No-Zero Integers

반응형

https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

간단 구현 문제였습니다.

📕 풀이방법

📔 풀이과정

1 ~ n/2까지 for loop를 수행하면서 i 와 n - i에 0이 포함되어있는지 확인해 정답을 구할 수 있습니다.


📕 Code

📔 C++

class Solution {
public:
    bool hasNoZero(int num) {
      while(num > 0) {
        if (num % 10 == 0) return false;
        num /= 10;
      }
      return true;
    }

    vector<int> getNoZeroIntegers(int n) {
        for(int i = 1; i <= n/2; i++) {
            if(hasNoZero(i) && hasNoZero(n-i)) return {i, n-i};
        }
        return {};
    }
};

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.