1047--Remove All Adjacent Duplicates In String

public class RemoveAllAdjacentDuplicatesInString {
    /*
    解法一:栈
     */
    public String removeDuplicates(String S) {
        Stack<Character> stack=new Stack<>();
        for (char c:S.toCharArray()){
            if (stack.isEmpty()||c!=stack.peek())
                stack.push(c);
            else
                stack.pop();
        }
        StringBuilder stringBuilder=new StringBuilder();
        for (Character character:stack)
            stringBuilder.append(character);
        return stringBuilder.toString();
    }
    /*
    解法二:StringBuilder模拟栈。
     */
    public String removeDuplicates2(String S) {
     StringBuilder stringBuilder=new StringBuilder();
     int length=0;
     for (char c:S.toCharArray()){
        if (length!=0&&c==stringBuilder.charAt(length-1))
            stringBuilder.deleteCharAt(length-- -1);
        else {
            stringBuilder.append(c);
            length++;
        }
     }
     return stringBuilder.toString();
    }
}

原文地址:https://www.cnblogs.com/zhangyuhao/p/11485464.html

时间: 2024-10-07 03:22:48

1047--Remove All Adjacent Duplicates In String的相关文章

1047. Remove All Adjacent Duplicates In String做题报告

题目链接: Remove All Adjacent Duplicates In String 题目大意: 删除字符串中的所有相邻字符 做题报告: (1)该题涉及的算法与数据结构 栈 (2)自己的解答思路+代码+分析时间和空间复杂度 Input: "abbaca" Output: "ca"          思路:使用栈,对字符串遍历,进行入栈出栈操作.如果栈空或者遍历到的该字符与栈顶元素不同则入栈,否则(即遍历到的该字符与栈顶元素相同)出栈.最后,栈存的字符就是我们

【leetcode】1209. Remove All Adjacent Duplicates in String II

题目如下: Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them causing the left and the right side of the deleted substring to concatenate together. We repeatedly make k duplicate removals on 

LeetCode 1209. Remove All Adjacent Duplicates in String II

原题链接在这里:https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/ 题目: Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them causing the left and the right side of the delet

[Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String

In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the startin

做题报告模板

题目链接: Remove All Adjacent Duplicates In String 题目大意: .... 做题报告: (1)该题涉及的算法与数据结构 ... (2)自己的解答思路+代码+分析时间和空间复杂度 (3)大神们的解答思路+代码+分析时间和空间复杂度 时间和空间复杂度: 时间复杂度:O( ) 空间复杂度:O( ) (4)比较自己想的和参考答案的区别 原文地址:https://www.cnblogs.com/Aiahtwo/p/12228711.html

[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&

[LeetCode][JavaScript]Remove Duplicate Letters

Remove Duplicate Letters Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results

c# Char &amp;&amp; string

char 支持的方法 字符串 声明字符串 String str = [null]; 可以用此方法声明一个空字符串 连接字符串 str +"" + str1; 比较两个字符串 Compare 静态方法 返回int 比较两个字符串是否相等,最常用的2个重载方法 Int Compare(string a,string b) Int Compare(string a,string b ,bool ignorCase) 第三方参数是true 忽略大小写 String.Compare("

jQuery源码

/*! * jQuery JavaScript Library v1.8.3 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2012 jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: Tue Nov 13 20