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; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.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...
摘要:注意因为方法输入的是一个正则表达式所以不能直接用,而是要用,而的要转义,所有要用代码按照进行分割比对相应的子串如果某个版本号更长,判断其多余部分是否是,如果不是,则较长的较大,否则是一样的。 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...
阅读 3054·2021-09-28 09:43
阅读 887·2021-09-08 09:35
阅读 1427·2019-08-30 15:56
阅读 1160·2019-08-30 13:00
阅读 2715·2019-08-29 18:35
阅读 1814·2019-08-29 14:07
阅读 3416·2019-08-29 13:13
阅读 1319·2019-08-29 12:40