leetcode-844 Backspace String Compare
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
andT
only contain lowercase letters and'#'
characters.
想法:使用两个栈,分别处理字符串S和字符串T。对于字符串S而言,逐个遍历该字符串,如果字符不为#,入栈,如果为#,则执行栈的pop()操作。对字符串T也执行同样的操作。最后再比较两个栈中元素是否相等即可
public: bool backspaceCompare(string S, string T) { std::stack<char> s; std::stack<char> t; for(char i : S){ if(i != '#'){ s.push(i); }else if(!s.empty() && i == '#'){ s.pop(); } } for(char j : T){ if(j != '#'){ t.push(j); }else if(!t.empty() && j == '#'){ t.pop(); } } return s == t; } };
leetcode-844 Backspace String Compare的更多相关文章
- [LeetCode] 844. Backspace String Compare 退格字符串比较
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- 【Leetcode_easy】844. Backspace String Compare
problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...
- 【LeetCode】844. Backspace String Compare 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...
- [LeetCode&Python] Problem 844. Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- 844. Backspace String Compare判断删除后的结果是否相等
[抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...
- 844. Backspace String Compare
class Solution { public: bool backspaceCompare(string S, string T) { int szs=S.size(); int szt=T.siz ...
- [LeetCode] 844. Backspace String Compare_Easy tag: Stack **Two pointers
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- [LeetCode] Backspace String Compare 退格字符串比较
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- LeetCode算法题-Backspace String Compare(Java实现)
这是悦乐书的第327次更新,第350篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第197题(顺位题号是844).给定两个字符串S和T,如果两个字符串都输入到空文本编辑器 ...
- LeetCode - Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
随机推荐
- Points on Cycle (hdu1700,几何)
Points on Cycle Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- JDBC、mybatis、hibernate连接数据库
JDBC连接数据库五步骤: 一.加载驱动 Class.forName(“com.mysql.jdbc.Driver”); 二.建立连接 Connection conn = DriverManager. ...
- Java - "JUC线程池" ThreadPoolExecutor原理解析
Java多线程系列--“JUC线程池”02之 线程池原理(一) ThreadPoolExecutor简介 ThreadPoolExecutor是线程池类.对于线程池,可以通俗的将它理解为"存 ...
- 前端SEO
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
- python基础训练题1-列表操作
1,在列表末尾添加一个值 >>> l = [ 10, 20 ] >>> l [10, 20] >>> l.append( 'ghostwu' ) ...
- POJ3268(KB4-D spfa)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23426 Accepted: 1069 ...
- python学习之老男孩python全栈第九期_第一次周末考试题(over)第三次添加完毕
day 6python基础数据类型考试题 考试时间:两个半小时 满分100分(80分以上包含80分及格) 一,基础题. 1. 简述变量命名规范(3分) 答:(1) 变量为数字,字母以及下划线的任意组合 ...
- js-数字、字符串、布尔值的转换方式
来自JavaScript秘密花园 1.转换为字符串 '' + 10 === '10'; // true 将一个值加上空字符串可以轻松转换为字符串类型. 2.字符串转换为数字 +'010' === 10 ...
- xxl-job 实现高可用
xxl-job-Admin是一个服务调度中心,管理所有的任务的触发. 1.如果xxl-job-Admin平台如果宕机了,该如何处理? 需要搭建集群. 2.xxl-job-Admin 如何搭建集群? 使 ...
- SD从零开始45-46
[原创] SD从零开始45 运输流程的控制 运输业务场景的例子Examples 一个公司可使用不同的运输业务场景,通过不同的处理类型或者运输方式来刻画: 要模型化这些不同的装运,你可以在配置中定义装运 ...