[LC] 82. Remove Adjacent Repeated Characters IV

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

  • "abbbaaccz" → "aaaccz" → "ccz" → "z"
  • "aabccdc" → "bccdc" → "bdc"
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);
  }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12330552.html

时间: 2024-10-08 19:25:55

[LC] 82. Remove Adjacent Repeated Characters IV的相关文章

LF.79.Remove Adjacent Repeated Characters I

Remove adjacent, repeated characters in a given string, leaving only one character for each group of such characters. Assumptions Try to do it in place.Examples "aaaabbbc" is transferred to "abc"Corner Cases If the given string is null

82. Remove Duplicates from Sorted List II &amp;&amp; i

题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 解析 [LeetCode] Rem

82. Remove Duplicates from Sorted List II(js)

82. Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example

LC.203. Remove Linked List Elements

230,82,83 是一类题 https://leetcode.com/problems/remove-linked-list-elements/description/Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 -

LeetCode(82): Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->

leetcode:82. Remove Duplicates from Sorted List II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50411033 题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have

Leetcode 82 Remove Duplicates from Sorted List 2

* Problem: * Given a sorted linked list, delete all nodes that have duplicate numbers * leaving only distinct numbers from the original list. * Solution: * Compare the current position with the previous and behind it. If it does not equal to them, th

LC 660. Remove 9 【lock, hard】

Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ... Given a positive integer n, you need to return the n-th integer after removing. Note that

[LC] 19. Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n w