leetcode 1047. Remove All Adjacent Duplicates In String | 1047. 删除字符串中的所有相邻重复项(Java)
生活随笔
收集整理的這篇文章主要介紹了
leetcode 1047. Remove All Adjacent Duplicates In String | 1047. 删除字符串中的所有相邻重复项(Java)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
題解
簡單題,不寫思路了,一看就明白。
class Solution {public String removeDuplicates(String s) {boolean valid = true;while (valid) {valid = false;int len = s.length();for (int i = 0; i < len-1; i++) {if (s.charAt(i) == s.charAt(i + 1)) {s = removeCharAt(s, i);len = s.length();valid = true;}}}return s;}public static String removeCharAt(String s, int pos) {return s.substring(0, pos) + s.substring(pos + 2);} }總結
以上是生活随笔為你收集整理的leetcode 1047. Remove All Adjacent Duplicates In String | 1047. 删除字符串中的所有相邻重复项(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode 274, 275. H
- 下一篇: leetcode 485,487,100