-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1342. Number of Steps to Reduce a Number to Zero #76
Comments
再麻煩 @twy30 大大 給予命名建議 orz 我這次答題是參考 [ 從LeetCode學演算法 - 87 Bitwise Operation (6) ] 感謝大大們 orz public int NumberOfSteps(int inputNumber)
{
// 答案 = inputNumber 的bit ‘1’數量 + inputNumber 的長度 - 1
string binaryInputNumber = Convert.ToString(inputNumber, 2);
int stepsToReduceInputNumber = binaryInputNumber.Length -1;
for (int i = 0; i < binaryInputNumber.Length; ++i)
{
if (binaryInputNumber[i] == '1')
{
++stepsToReduceInputNumber;
}
}
return stepsToReduceInputNumber;
} |
string binaryInputNumber = Convert.ToString(inputNumber, 2); 因為這裡是從
int stepsToReduceInputNumber = binaryInputNumber.Length -1; 這裡雖然 如果要寫成 或著也可以寫成 |
感謝 @twy30 大大的指導 orz 怎麼都沒有其他人出來寫一波 XD |
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
請參考「刷 LeetCode 練習命名」 #69 😊
The text was updated successfully, but these errors were encountered: