国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 844. Backspace String Compare

DobbyKim / 725人閱讀

Problem

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

Example 1:

Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".

Example 2:

Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".

Example 3:

Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".

Example 4:

Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".

Note:

1 <= S.length <= 200
1 <= T.length <= 200
S and T only contain lowercase letters and "#" characters.

Follow up:

Can you solve it in O(N) time and O(1) space?

Solution
class Solution {
    public boolean backspaceCompare(String S, String T) {
        return helper(S).equals(helper(T));
    }
    private String helper(String str) {
        int count = 0;
        String res = "";
        for (int i = str.length()-1; i >= 0; i--) {
            char ch = str.charAt(i);
            if (ch == "#") count++;
            else {
                if (count > 0) count--; //能不加就不加
                else res += ch; //非加不可的字符 那就加吧
            }
        }
        return res;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/71939.html

相關文章

  • [LeetCode] Compare Version Numbers

    Problem Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings are non-empty an...

    Alex 評論0 收藏0
  • [Leetcode] Compare Version Numbers 比較版本號

    摘要:注意因為方法輸入的是一個正則表達式所以不能直接用,而是要用,而的要轉義,所有要用代碼按照進行分割比對相應的子串如果某個版本號更長,判斷其多余部分是否是,如果不是,則較長的較大,否則是一樣的。 Compare Version Numbers Compare two version numbers version1 and version2. If version1 > version2...

    FrozenMap 評論0 收藏0
  • leetcode165. Compare Version Numbers

    摘要:題目要求也就是說,比較版本號。思路一利用通過方法將版本通過分隔開,然后將每一段版本從轉化為進行比較思路二自己實現轉化為自己實現將轉化為,可以通過循環的方式。這是一個基本的算法。 題目要求 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < ve...

    Mike617 評論0 收藏0
  • [LeetCode] 165. Compare Version Numbers

    Problem Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings are non-empty an...

    趙春朋 評論0 收藏0
  • [LeetCode] Compare Version Numbers

    摘要:首先找整數部分的坐標段,和都指向初值,令和一直向后遍歷到小數點為止。然后用將的整數段轉化為數值,進行比較若結果為大于或小于關系,直接返回結果若結果為相等,進行小數部分的比較。 Problem Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < v...

    jzman 評論0 收藏0

發表評論

0條評論

DobbyKim

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<