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?
Solutionclass 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
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...
摘要:注意因?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...
摘要:題目要求也就是說,比較版本號(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...
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...
摘要:首先找整數(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...
閱讀 3103·2021-09-28 09:43
閱讀 936·2021-09-08 09:35
閱讀 1469·2019-08-30 15:56
閱讀 1217·2019-08-30 13:00
閱讀 2762·2019-08-29 18:35
閱讀 1854·2019-08-29 14:07
閱讀 3481·2019-08-29 13:13
閱讀 1357·2019-08-29 12:40