c++ - 在LintCode上碰到了一题largest-number
天蓬老师
天蓬老师 2017-04-17 13:41:22
0
2
749
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
小葫芦

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:

  1. If the current character is not '0', store the current character in res;

  2. 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;

  3. 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.

刘奇

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]

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!