[lintcode medium] Delete digits

Delete Digits

Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer.

Find the smallest integer after remove k digits.

N <= 240 and k <= N,

Example

Given an integer A = "178542", k = 4

return a string "12"

思路很直接,删掉K个数,需要注意的是,要按从前到后的顺序删,从前往后遍历,如果前面一个数比后面的数大,则删掉。删掉K个数。则留下的是最小的数。

由于string是不能直接操作,需要使用StringBuilder 来存储变化的string

还需要注意两点:

1、最后一个数的处理

2、如果删掉的数留下的前面是0,返回时需要从第一个不是0的数返回子串。

public class Solution {
    /**
     *@param A: A positive integer which has N digits, A is a string.
     *@param k: Remove k digits.
     *@return: A string
     */
    public String DeleteDigits(String A, int k) {
        // write your code here

        if(A==null || A.length()==0 || k<0 || k>A.length()) return "";

        StringBuilder sb=new StringBuilder(A);
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<sb.length();j++)
            {
                if(j==sb.length()-1||sb.charAt(j)>sb.charAt(j+1))
                {
                    sb.deleteCharAt(j);
                    break;
                }
            }
        }

        int i=0;
        for(;i<sb.length();i++)
        {
            if(sb.charAt(i)!=‘0‘)
            break;
        }

        return sb.substring(i,sb.length());

    }
}
时间: 2024-10-13 13:19:07

[lintcode medium] Delete digits的相关文章

LintCode &quot;Delete Digits&quot;

Greedy: remove earliest down-edge: like "54", "97". class Solution { public: /** *@param A: A positive integer which has N digits, A is a string. *@param k: Remove k digits. *@return: A string */ string DeleteDigits(string A, int k) {

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

Delete Digits

Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer. Find the smallest integer after remove k digit

[LeetCode]Delete Digits

题目 Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer. Make this new positive integers as small as

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium] digit counts

Digit Counts Count the number of k's between 0 and n. k can be 0 - 9. Example if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12) class Solution { /* * param k : As description. * param n : As description. * r

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium]Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return 2147483647 Example Given dividend = 100 and divisor = 9, return 11. public class Solution { /** * @param dividend the dividend