Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 오늘도 우라라 펫 공략
- C언어
- install opencv-4.4.0 on ubuntu 22.04
- 기초
- 프로그래밍
- 오늘도 우라라 펫
- 데이터 베이스
- ubuntu
- ros
- 오늘도 우라라 공략
- 리눅스
- 등차수열
- mariaDB
- LeetCode
- 환경설정
- Linux
- JungOl
- topic
- while
- C++
- Subscribe
- 마리아 DB
- 반복문
- 그랑사가
- 오늘도 우라라
- MSG
- mysql
- 우분투
- publish
- 토픽
Archives
- Today
- Total
하루의 쉼터
[LeetCode] 288. Add Digits 본문
반응형
Question
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Input: 38
Output: 2
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
Since 2 has only one digit, return it.
Solution.h //Hearder
#include<iostream>
#include<windows.h>
class Solution
{
private :
int input_num;
public:
int addDigits(int num);
int inputData();
void outputData(int num);
};
Solution.cpp
#include "Solution.h"
int Solution::addDigits(int num)
{
while (true) {
if(num < 10)
return num;
num=(num/10)+(num%10);
}
return num;
}
int Solution::inputData()
{
std::cout << "input : ";
std::cin >> input_num;
if (input_num < 0) {
std::cout << "error"<<std::endl;
std::system("cls");
inputData();
}
return input_num;
}
void Solution::outputData(int num)
{
std::cout << "output : " << num<<std::endl;
std::system("pause");
}
Result
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Add Digits.
Memory Usage: 6.3 MB, less than 27.32% of C++ online submissions for Add Digits.
Anchangun - Overview
어제보다 나은 오늘. Anchangun has 3 repositories available. Follow their code on GitHub.
github.com
반응형
'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 53. Maximum Subarray (0) | 2020.11.30 |
---|---|
[LeetCode] 21. Merge Two Sorted Lists (0) | 2020.11.24 |
[LeetCode] 136. Single Number (0) | 2020.11.23 |
[LeetCode] 20. Valid Parentheses (0) | 2020.11.17 |
[LeetCode] 1. Two Sum (0) | 2020.11.17 |
Comments