LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)
709. To Lower Case(Easy)#
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
Input: "Hello"
Output: "hello"
Example 2:
Input: "here"
Output: "here"
Example 3:
Input: "LOVELY"
Output: "lovely"
Note:
there are many other characters,such as '&".
solution##
class Solution {
public String toLowerCase(String str) {
StringBuilder s = new StringBuilder();
for (int i=0; i<str.length(); i++)
{
if ('a' <= str.charAt(i) && str.charAt(i) <='z')
s.append(str.charAt(i));
else if ('A' <= str.charAt(i) && str.charAt(i) <='Z')
s.append((char)(str.charAt(i) - 'A' + 'a'));
else
s.append(str.charAt(i));
}
return s.toString();
}
}
总结##
此题思路很简单,遍历给定字符串,如果字母为大写字母,则变为小写字母后用一个字符串变量存起来,否则直接将小写字母或其他字符存起来。
Notes:
1.用StringBuilder类更省空间;
2.两个字符相加减得到的结果为int型数值,要转为字符必须用(char)强制转换,比如char c = (char)97,得到的结果为c=a;
3.字符拼接一般用StringBuilder类的append()方法;
1021. Remove Outermost Parentheses (Easy)#
A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.
Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
Example 1:
Input: "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
Example 2:
Input: "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
Example 3:
Input: "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".
Note:
S.length <= 10000
S[i] is "(" or ")"
S is a valid parentheses string
solution##
class Solution {
public String removeOuterParentheses(String S) {
StringBuilder s = new StringBuilder();
int k = 0;
for (int i=0; i<S.length(); i++)
{
if (S.charAt(i) == '(')
{
if (k > 0)
s.append('(');
k++;
}
if (S.charAt(i) == ')')
{
k--;
if (k > 0)
s.append(')');
}
}
return s.toString(); //return a string
}
}
总结##
此题我最初的想法是用栈,后来发现只需要用栈的思想就够了,只需用一个计数器k和一个字符串变量s。当遇到左括号时,先判断k>0是否成立,如果成立,则将左括号存入字符串s,否则什么都不做,随后计数器k++;当遇到右括号时,先计数器k--,再判断k>0是否成立,如果成立,则将右括号存入字符串s,否则什么都不做。
Notes:
1.此题又是用StringBuilder类进行字符连接,返回值需要用toString()方法转为字符串。
LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)的更多相关文章
- LeetCode 1021. 删除最外层的括号(Remove Outermost Parentheses)
1021. 删除最外层的括号 1021. Remove Outermost Parentheses 题目描述 有效括号字符串为空 ("")."(" + A + ...
- 【Leetcode_easy】1021. Remove Outermost Parentheses
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完
- 【leetcode】1021. Remove Outermost Parentheses
题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...
- 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- [Swift]LeetCode1021. 删除最外层的括号 | Remove Outermost Parentheses
A valid parentheses string is either empty (""), "(" + A + ")", or A + ...
- LeetCode.1021-删除最外面的括号(Remove Outermost Parentheses)
这是小川的第380次更新,第408篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第242题(顺位题号是1021).有效的括号字符串为空(""),&qu ...
- Leetcode 1021. Remove Outermost Parentheses
括号匹配想到用栈来做: class Solution: def removeOuterParentheses(self, S: str) -> str: size=len(S) if size= ...
- LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号
https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...
- [LeetCode] To Lower Case 转为小写
Implement function ToLowerCase() that has a string parameter str, and returns the same string in low ...
随机推荐
- 天天在用Redis,持久化方案你又知道哪些?
前言 文章首发于微信公众号[码猿技术专栏]:天天用Redis,持久化方案有哪些你知道吗? Redis目前已经成为主流的内存数据库了,但是大部分人仅仅是停留在会用的阶段,你真的了解Redis内部的工作原 ...
- 记录d3.js 力导向图的平移缩放,类似地图导航点击某一项移动到当前位置
项目中有用到d3.js用于图结构的查询, 需求如下: 右上角有个模糊搜索功能,查询出来的结果用列表展示 点击列表的某一列,要求画布移动到当前选中的节点的位置,基于画布正中间 搜索出来的结果列表展示用的 ...
- idea创建springboot工程,总出现响应超时问题,或者无法连接http://start.spring.io导致创建失败
问题描述如下: idea创建springboot工程,总出现响应超时问题,或者无法连接http://start.spring.io导致创建失败 从我出现此类问题几次的解决方案 依照解决效率分为一下三种 ...
- sqli-labs通关教程----41~50关
第四十关 与前几关一样,闭合变成') 插入数据 ?id=1') ;insert into users(id,username,password) values('17','aaa','bbb'); % ...
- JavaScript表达式和运算符 —— 基础语法(4)
JavaScript基础语法(4) 运算符 运算符用于将一 个或者多个值变成结果值. 使用运算符的值称为操作数,运算符和操作数的组合称为表达式 JS中的运算符可以分成下面几类: 算术运算符 逻辑运算符 ...
- Task启动方式及源码探究
启动Task有几种方式: 1.Task.Run() 2.new TaskFactory.StartNew() 3.var t=new Task(); t.start(); 平时用的最多是第一和第二种 ...
- cmd命令行中无pip命令的解决办法
cmd命令行中无pip命令的解决办法 只需简单的两步即可,按顺序执行以下命令(在cmd中): python -m ensurepip python -m pip install --upgrade p ...
- nginx日志、nginx日志切割、静态文件不记录日志和过期时间
2019独角兽企业重金招聘Python工程师标准>>> 12.10 Nginx访问日志 日志格式 vim /usr/local/nginx/conf/nginx.conf //搜索l ...
- 写给MongoDB开发者的50条建议Tip21
本系列文章翻译自<50 Tips and Tricks for MongoDB Developers>,暂时没有找到中文版,反正自己最近也在深入学习mongodb,所以正好拿来翻译一下.一 ...
- 用Swoole4 打造高并发的PHP协程Mysql连接池
码云代码仓库:https://gitee.com/tanjiajun/MysqlPool 代码仓库:https://github.com/asbectJ/swoole4.git 前言 在写这篇文章之前 ...