成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 844. Backspace String Compare

DobbyKim / 756人閱讀

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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71939.html

相關(guān)文章

  • [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 評(píng)論0 收藏0
  • [Leetcode] Compare Version Numbers 比較版本號(hào)

    摘要:注意因?yàn)榉椒ㄝ斎氲氖且粋€(gè)正則表達(dá)式所以不能直接用,而是要用,而的要轉(zhuǎn)義,所有要用代碼按照進(jìn)行分割比對相應(yīng)的子串如果某個(gè)版本號(hào)更長,判斷其多余部分是否是,如果不是,則較長的較大,否則是一樣的。 Compare Version Numbers Compare two version numbers version1 and version2. If version1 > version2...

    FrozenMap 評(píng)論0 收藏0
  • leetcode165. Compare Version Numbers

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

    Mike617 評(píng)論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...

    趙春朋 評(píng)論0 收藏0
  • [LeetCode] Compare Version Numbers

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

    jzman 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

DobbyKim

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<