Skip to content

Commit bcaf090

Browse files
solves #2710: Remove Trailing Zeros From a String in java
1 parent 6e61696 commit bcaf090

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: README.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@
826826
| 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings) | [![Java](assets/java.png)](src/MinimumStringLengthAfterRemovingSubstrings.java) | |
827827
| 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome) | [![Java](assets/java.png)](src/LexicographicallySmallestPalindrome.java) | |
828828
| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates) | [![Java](assets/java.png)](src/BuyTwoChocolates.java) | |
829-
| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string) | | |
829+
| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string) | [![Java](assets/java.png)](src/RemoveTrailingZerosFromAString.java) | |
830830
| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length) | | |
831831
| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation) | | |
832832
| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street) | | |

Diff for: src/RemoveTrailingZerosFromAString.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://leetcode.com/problems/remove-trailing-zeros-from-a-string
2+
// T: O(|s|)
3+
// S: O(|s|)
4+
5+
public class RemoveTrailingZerosFromAString {
6+
public String removeTrailingZeros(String num) {
7+
for (int i = num.length() - 1; i >= 0 ; i--) {
8+
if (num.charAt(i) != '0') {
9+
return num.substring(0, i + 1);
10+
}
11+
}
12+
return "";
13+
}
14+
}

0 commit comments

Comments
 (0)