carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode2710: Remove Trailing Zeros From a String #437

Open carloscn opened 11 months ago

carloscn commented 11 months ago

Description

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:

carloscn commented 11 months ago

Analysis

pub fn remove_trailing_zeros(num: &str) -> String
{
    let mut ret:String = String::new();
    if num.len() < 1 {
        return ret;
    }

    ret = num.trim_end_matches('0').to_string();

    return ret;
}
carloscn commented 11 months ago

Code

https://review.gerrithub.io/c/carloscn/structstudy/+/1173353 https://github.com/carloscn/structstudy/commit/910a260e79c73527ffb98a74ccda7af7c0a4c10c