标签:offer length ++ ack stack cdc empty res har
Repeatedly remove all adjacent, repeated characters in a given string from left to right.
No adjacent characters should be identified in the final string.
Examples
public class Solution { public String deDup(String input) { // Write your solution here char[] charArr = input.toCharArray(); LinkedList<Character> stack = new LinkedList<>(); for (int i = 0; i < charArr.length; i++) { char cur = charArr[i]; if (!stack.isEmpty() && stack.peekFirst() == cur) { while (i + 1 < charArr.length && charArr[i] == charArr[i + 1]) { i += 1; } stack.pollFirst(); } else { stack.offerFirst(cur); } } char[] res = new char[stack.size()]; for (int i = res.length - 1; i >= 0; i--) { res[i] = stack.pollFirst(); } return new String(res); } }
[LC] 82. Remove Adjacent Repeated Characters IV
标签:offer length ++ ack stack cdc empty res har
原文地址:https://www.cnblogs.com/xuanlu/p/12330552.html