class Solution {
public int[] solution(String s) {
int[] answer = new int[2];
int one = 0;
while(s.length()>1){
for(int i=0; i<s.length();i++){
if(s.charAt(i)=='0')
answer[1]++;
else
one++;
}
s = Integer.toBinaryString(one);
answer[0]++;
}
return answer;
}
}
When you run, all but test2 appears as timeout. I don't know why. Thank you to the teachers for your answer.
java
I know that the question changes every time you take the skill check test, so it's hard to know what the question is if you tell me it's question number 2. Write down the title of the question and it will help you answer!
Separately, I guess it's code that takes a binary string and does something. Integer.toBinaryString
converts the binary number to decimal, and s = Integer.toBinaryString(one);
appears to be doing one
as a binary number.
while
-Let's check the progress of the loop s.length() > 1
. This condition is when Integer.toBinaryString(one)
has a length of 1 or greater, in other words, when one
has a binary representation of 1 or greater. Therefore, if one
is 2 or more, i.e. 10(2) or more binary, while
will continue.
However, there is no decrease in one
in the while
loop. one
will only increase, so if a situation other than s.charAt(i) == '0'
occurs twice, it will be s.length() > 1
from then on and will spin indefinitely.
© 2024 OneMinuteCode. All rights reserved.