https://programmers.co.kr/learn/courses/30/lessons/12932
< ์์ฐ์ ๋ค์ง์ด ๋ฐฐ์ด๋ก ๋ง๋ค๊ธฐ >
โ ๋์ ํ์ด ( charAt() ,toCharArray() ์ฌ์ฉ)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.*;
class Solution {
public static int[] solution(long n) {
String temp ="";
char[] cha = new char[temp.length()];
temp+=n;
int k=0;
int[] answer = new int[temp.length()];
cha = temp.toCharArray();
for(int i=temp.length()-1; i>=0 ; i--)
{
answer[k]=(int)cha[i]-48;
k++;
}
return answer;
}
}
//char ๋ int ํ๋ณํ ํ ๋ ์์คํค์ฝ๋๋ก
|
cs |
โ ์๊ฐ ๊ณผ์
- ์ ์ํ ํ์ ์ด n ์ String์ผ๋ก ๋ฐ๊ฟ์ charAt() ํจ์๋ก ์๋ผ์ผ ๊ฒ ๋ค.
- ๋ฐ๋ณต๋ฌธ์ ๋๋ฆด ๋๋ ์ญ๋ฐฉํฅ์ผ๋ก ๋๋ ค์ผ ๊ฒ ๋ค.
( โป ์ํ์ฐฉ์ค charAt() ์์ int ๋ก ๋ณํ ๋ 48์ ๋นผ์ค์ผ ํ๋ ๊ฑธ ๊น๋จน์๋ค..)
โ ๋์ ํ์ด ( substring() , parseInt( ) ์ฌ์ฉ)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Solution {
public static int[] solution(long n) {
String temp ="";
temp += n;
int answer[]= new int [temp.length()];
int k=0;
for(int i =temp.length()-1; i>=0; i--) //5 4 3 2 1
{
answer[k] = Integer.parseInt(temp.substring(i,i+1));//(4,5),(3,4),(2,3)
k++;
}
return answer;
}
}
|
cs |
( โป ์ํ์ฐฉ์ค : substring() ์ ๋ฒ์ ์ ํด ์ฃผ๋๋ฐ ์ค๋ ๊ฑธ๋ ธ๋ค .)
- substring(์์์ธ๋ฑ์ค,๋์ธ๋ฑ์ค)
์ธ๋ฑ์ค : 01234
: String temp ="12345"
substring(4,5) ="5"
substring(3,4) ="4"
:
substring(0,1)="1"
โ ๋ค๋ฅธ์ฌ๋ ํ์ด
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Solution {
public int[] solution(long n) {
String a = "" + n;
int[] answer = new int[a.length()];
int cnt=0;
while(n>0) {
answer[cnt]=(int)(n%10);
n/=10;
System.out.println(n);
cnt++;
}
return answer;
}
}
|
cs |