高洛峰2017-04-17 13:43:22
You can divide this code into two parts. Under normal circumstances, the code ends after executing the following for loop:
for (int i = 0; i < s_num.size(); ++i) {
tmp_res += s_num[i];
}
For example, sort [1, 20, 23, 4, 8] according to cmp and get [8, 4, 23, 20, 1]. Using a for loop to connect them into "8423201" is the final answer.
However, there are always special circumstances. What if the array is all 0? For example, [0, 0, 0, 0]. At this time, tmp_res needs to be processed: traversing tmp_res, there are three situations:
If the current character is not '0', store the current character in res;
If the current character is '0', but there is a character other than '0' in tmp_res, the current character is also stored in res;
If the current character is '0', and all characters before the current character are '0', no processing will be performed.
After the traversal is completed, if the flag is found to be false, it means that the array is all 0, so just save a '0' in res.
怪我咯2017-04-17 13:43:22
The function of this part is.. If all bits are 0, for example, the input is [0,0,0,0], let this function return 0 instead of 0000
flag
indicates whether there is a non-0 bit on the left side of tmp_res[i]