One liner using BigInteger and String.Reverse

I overcomplicated the solution here just to come up with an one-liner: reverse the string using String.Reverse, convert to number (BigInteger), to string, then reverse again. Code is below, cheers, ACC.

Remove Trailing Zeros From a String - LeetCode

2710. Remove Trailing Zeros From a String
Easy

Given a positive integer num represented as a string, return the integer num without trailing zeros as a string.

 

Example 1:

Input: num = "51230100"
Output: "512301"
Explanation: Integer "51230100" has 2 trailing zeros, we remove them and return integer "512301".

Example 2:

Input: num = "123"
Output: "123"
Explanation: Integer "123" has no trailing zeros, we return integer "123".

 

Constraints:

  • 1 <= num.length <= 1000
  • num consists of only digits.
  • num doesn't have any leading zeros.
Accepted
20,066
Submissions
25,307

public string RemoveTrailingZeros(string num)
{
    return new string(BigInteger.Parse(new string(num.Reverse().ToArray())).ToString().Reverse().ToArray());
}

Comments

Popular posts from this blog

Changing the root of a binary tree

ProjectEuler Problem 719 (some hints, but no spoilers)

The Power Sum, a recursive problem by HackerRank