202.Happy Number(C++)
Problem description:
Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
Diffculty:Easy
class Solution {
public:bool isHappy(int n) {
bool bResult = false;
int sum = 0;
int tmpnum = n;
do
{
int bit1 = tmpnum % 10;
tmpnum = tmpnum/10;
int bit2 = tmpnum % 10;
sum = bit1 * bit1 + bit2 * bit2;
tmpnum = tmpnum/10;
while(tmpnum != 0)
{
bit1 = tmpnum % 10;
sum += (bit1 * bit1);
tmpnum = tmpnum / 10;
}
tmpnum = sum;
}while(tmpnum >= 10);
sum = tmpnum;
if (sum == 1 || sum == 7)
{
bResult = true;
}
return bResult;
}
};
提交成功后,会有一个Submission Details报告。同样的代码,每次重新提交,显示的运行结果和效率排名都有不同,感觉没什么参考价值。