标签:ica ace input continue trail div col pac put
Given a string, remove all leading/trailing/duplicated empty spaces.
Assumptions:
Examples:
public class Solution { public String removeSpaces(String input) { // Write your solution here if (input == null || input.length() == 0) { return input; } char[] charArr = input.toCharArray(); int slow = 0; for (int i = 0; i < charArr.length; i++) { if (charArr[i] == ‘ ‘ && (i == 0 || charArr[i - 1] == ‘ ‘)) { continue; } charArr[slow++] = charArr[i]; } // slow > 0 for case " " if (slow > 0 && charArr[slow - 1] == ‘ ‘) { return new String(charArr, 0, slow - 1); } return new String(charArr, 0, slow); } }
标签:ica ace input continue trail div col pac put
原文地址:https://www.cnblogs.com/xuanlu/p/12777409.html