[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ด์ง๋ณํ๋ฐ๋ณตํ๊ธฐ (์๋ฐ java)
1. ๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/70129
2. ํ์ด
๋ฌธ์์ด ํ๋์ฉ ์ฝ์ผ๋ฉด์ 0์ ํ์ธํ๋ฉด์ ํ์์ง๋ง
replaceAll ํตํด์ 0์ ์ ๊ฑฐํ๊ณ ๊ธธ์ด ๋น๊ตํ๋ ๊ฒ ๋ ํจ์จ์ ์ธ ๊ฒ ๊ฐ๋ค.
3. ์ฝ๋
๋์ ํ์ด
class Solution {
public int[] solution(String s) {
int answer_cnt = 0;
int answer_zero = 0;
int x_len = s.length();
while(x_len>1){
String temp = "";
for(int i=0;i<s.length();i++){
char schar = s.charAt(i);
if(schar-'0'!= 0) temp += schar;
else answer_zero ++;
}
answer_cnt++;
x_len = temp.length();
s = Integer.toBinaryString(x_len);
}
int[] answer = {answer_cnt,answer_zero};
return answer;
}
}
์ฐธ๊ณ ์ฝ๋
class Solution {
public int[] solution(String s) {
int[] answer = new int[2];
int temp;
while( !s.equals("1") ) {
answer[1] += s.length();
s = s.replaceAll("0", "");
temp = s.length();
s = Integer.toBinaryString(temp);
//System.out.println("s : " + s );
answer[0]++;
answer[1] -= temp;
}
return answer;
}
}
'๐ป Coding Problems Solving > Array | String | Loop' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์์ด ๋๋ง์๊ธฐ (์๋ฐ java) (0) | 2022.12.01 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ซ์์ ํํ (์๋ฐ java) (0) | 2022.11.26 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฌธ์์ด ๋ง๋ค๊ธฐ (์๋ฐ java) (0) | 2022.11.24 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ต๋๊ฐ๊ณผ ์ต์๊ฐ (์๋ฐ java) (0) | 2022.11.24 |
[LeetCode] Product of Array Expect Self (0) | 2022.07.09 |
์ต๊ทผ๋๊ธ