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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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...
摘要:注意因為方法輸入的是一個正則表達式所以不能直接用,而是要用,而的要轉義,所有要用代碼按照進行分割比對相應的子串如果某個版本號更長,判斷其多余部分是否是,如果不是,則較長的較大,否則是一樣的。 Compare Version Numbers Compare two version numbers version1 and version2. If version1 > version2...
摘要:題目要求也就是說,比較版本號。思路一利用通過方法將版本通過分隔開,然后將每一段版本從轉化為進行比較思路二自己實現轉化為自己實現將轉化為,可以通過循環的方式。這是一個基本的算法。 題目要求 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...
摘要:首先找整數部分的坐標段,和都指向初值,令和一直向后遍歷到小數點為止。然后用將的整數段轉化為數值,進行比較若結果為大于或小于關系,直接返回結果若結果為相等,進行小數部分的比較。 Problem Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < v...
閱讀 3069·2021-09-28 09:43
閱讀 902·2021-09-08 09:35
閱讀 1441·2019-08-30 15:56
閱讀 1183·2019-08-30 13:00
閱讀 2732·2019-08-29 18:35
閱讀 1829·2019-08-29 14:07
閱讀 3432·2019-08-29 13:13
閱讀 1333·2019-08-29 12:40