How to get the least number after deleting k digits from the input number

前端 未结 11 1222
灰色年华
灰色年华 2020-12-11 05:34

For example, if the input number is 24635, the least number is 23 after deleting any 3 digits.

It\'s not the same as taking the two smalles

11条回答
  •  猫巷女王i
    2020-12-11 05:47

    Very simple Algorithm come to mine mind

    1. Consider input number as string
    2. Start backward looping from number 9 to 0
    3. check if 9 exist in given number(in java we can do it with indexOf()), if exists remove. check if number of removed digit are equal to no of input digits to b removed . If not , check 9 existence gaain till it does not exist (indexOf() will return -1 under java) to see if same digit is repeated . Now repeat if for 8 till expected no of digit are removed
    4. Till now we have removed digit which could contribute to larger number
    5. Now loop over from 0 to 9 and check if each digit present in number got in step 3 and rearrange it in ascending order

    For example :-

    Given number is 24635 and no of digit to remove are 2

    1. Start from 9, you find first digit to be removed is 6 then 5
    2. Remaining number is 243
    3. Start loop from 0 to 9 rearrange it in ascending order like 234

提交回复
热议问题