Skip to content

Latest commit

 

History

History
40 lines (36 loc) · 1.36 KB

README.md

File metadata and controls

40 lines (36 loc) · 1.36 KB

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.

Related Topics:
String

Solution 1.

// OJ: https://leetcode.com/problems/remove-trailing-zeros-from-a-string
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    string removeTrailingZeros(string s) {
        while (s.back() == '0') s.pop_back();
        return s;
    }
};