Notice
Recent Posts
Recent Comments
- Today
- Total
01-11 08:45
Tags
- λ°±μλ
- λ¬Έλ²
- MST
- μ‘Έμ μν
- Algorithm
- νλ‘κ·Έλλ¨Έμ€
- java
- λ€μ΅μ€νΈλΌ
- OOP
- μλ£κ΅¬μ‘°
- array
- tree
- μΈν΄
- pytorch
- PS
- μλ°μμ μ
- λ°±μ€
- ꡬν
- spring
- Graph
- BFS
- database
- μλ°
- CS
- 그리λ
- λ°μ΄ν°λ² μ΄μ€
- 벨λ§ν¬λ
- μμμ λ ¬
- dp
- leetcode
Link
Partially Committed
[μκ° μ½λ μ±λ¦°μ§ μμ¦1] 3μ§λ² λ€μ§κΈ° λ³Έλ¬Έ
π₯ Algorithm || λ¬Έμ νμ΄/PS
[μκ° μ½λ μ±λ¦°μ§ μμ¦1] 3μ§λ² λ€μ§κΈ°
WonderJay 2022. 7. 4. 18:15728x90
λ°μν
SMALL
https://programmers.co.kr/learn/courses/30/lessons/68935
μμ°μ n μ 3μ§λ² μΌλ‘ λ³νν λ€, μλ€λ₯Ό λ€μ§κ³ λ€μ 10μ§λ²μΌλ‘ λ°κΎΈμ΄ 리ν΄ν΄μΌνλ€.
vector<int> third λ₯Ό μ μνκ³ , n μ΄ 0λ³΄λ€ ν΄ λκΉμ§ n μ 3μΌλ‘ λλ λͺ«μ third μ push_back νκ³ n /= 3 μΌλ‘ μ λ°μ΄νΈ νλ κ²μ λ°λ³΅νλ©΄ third μλ 3μ§λ²μΌλ‘ λ³νν λ€ λ€μ§μ μκ° μ μ₯λλ€.
μ΄μ third λ₯Ό κ±°κΎΈλ‘ μννλ©° 10μ§λ²μΌλ‘ λ³ννκΈ° μν΄ int i = 1 μ μ μνκ³ ,
LSB λΆν° i λ₯Ό κ³±ν΄μ answer μ λνλ€. κ·Έλ¦¬κ³ i *= 3 μΌλ‘ μ λ°μ΄νΈνλ κ²μ λ°λ³΅νλ©΄ λλ€.
[C++]
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 0;
vector<int> third;
while (n > 0)
{
third.push_back(n%3);
n /= 3;
}
int i = 1;
for (vector<int>::reverse_iterator iter = third.rbegin();
iter != third.rend(); iter++)
{
answer += (i * (*iter));
i *= 3;
}
return answer;
}
728x90
λ°μν
LIST
'π₯ Algorithm || λ¬Έμ νμ΄ > PS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μκ° μ½λ μ±λ¦°μ§ μμ¦1] λ κ° λ½μμ λνκΈ° (0) | 2022.07.04 |
---|---|
[Summer/Winter Coding(~2018)] μμ° (0) | 2022.07.04 |
[μκ° μ½λ μ±λ¦°μ§ μμ¦2] μ½μμ κ°μμ λ§μ (0) | 2022.07.04 |
[2019 KAKAO BLIND RECRUITMENT] μ€ν¨μ¨ (0) | 2022.07.04 |
[μ°ΎμλΌ νλ‘κ·Έλλ° λ§μμ€ν°] ν°μΌλͺ¬ (0) | 2022.07.03 |
Comments