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 ...
随机推荐
- 2019-08-02【机器学习】有监督学习之分类 SVC算法 实例(上证指数跌涨预测)
样本: 代码:有几处与教程不同,自行修改 import pandas as pd import numpy as np from sklearn import svm from sklearn imp ...
- Salesforce Admin考题解析 | 流程自动化考题与知识点拓展
[题目1] A record is modified on 1/1/2008. It meets criteria for a time-based workflow rule; this rule ...
- G - Messy codeforces1262C
题目大意: 输入n和m,n是n个字符,m是m个前缀.对前缀的规定可以配对的括号.比如(),,((()))等等.在输入n个括号字符,对这个n个字符,通过交换使其满足m个前缀.交换次数不限,规则想当与re ...
- vue2.x学习笔记(十三)
接着前面的内容:https://www.cnblogs.com/yanggb/p/12595860.html. 组件的注册 注册组件有一些规范约定与注意事项. 组件名的命名规范 在注册一个组件的时候, ...
- Soul Android app 悬浮view以及帖子中view的联动刷新逆向分析
Soul app是我司的竞品,对它的语音音乐播放同步联动的逻辑很感兴趣,于是就开启了一波逆向分析. 下面看代码,以及技术分析,直接步入正轨,哈哈. 我们根据https://github.com/xin ...
- redis: Zset有序集合类型(七)
存值:zadd myset 1 one 取值:zrange myset 0 -1 127.0.0.1:6379> zadd myset 1 one #存值 分值为1 (integer) 1 12 ...
- Jmeter系列(6)- test plan测试计划详细讲解
如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html 测试计划的作用 测试计划描述了Jmet ...
- 2019-2020-1 20199308《Linux内核原理与分析》第八周作业
<Linux内核分析> 第七章 可执行程序工作原理 7.1 知识点 1.目标文件:编译器生成的文件,"目标"指平台,它决定了编译器使用的机器指令集. 2.目标文件格式: ...
- python学习21之高级特性
'''''''''1.切片(1)谁可以进行切片操作?——列表,元组,字符串(2)切片有以下几种操作'''#[a:b]:取从下标为a的元素开始,到下标为b-1的元素结束L=['aa','bb','cc' ...
- java学习(第一篇)
Java 简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式 ...