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)的更多相关文章

  1. LeetCode 1021. 删除最外层的括号(Remove Outermost Parentheses)

    1021. 删除最外层的括号 1021. Remove Outermost Parentheses 题目描述 有效括号字符串为空 ("")."(" + A + ...

  2. 【Leetcode_easy】1021. Remove Outermost Parentheses

    problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完

  3. 【leetcode】1021. Remove Outermost Parentheses

    题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...

  4. 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

  5. [Swift]LeetCode1021. 删除最外层的括号 | Remove Outermost Parentheses

    A valid parentheses string is either empty (""), "(" + A + ")", or A + ...

  6. LeetCode.1021-删除最外面的括号(Remove Outermost Parentheses)

    这是小川的第380次更新,第408篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第242题(顺位题号是1021).有效的括号字符串为空(""),&qu ...

  7. Leetcode 1021. Remove Outermost Parentheses

    括号匹配想到用栈来做: class Solution: def removeOuterParentheses(self, S: str) -> str: size=len(S) if size= ...

  8. LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号

    https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...

  9. [LeetCode] To Lower Case 转为小写

    Implement function ToLowerCase() that has a string parameter str, and returns the same string in low ...

随机推荐

  1. vue2.x学习笔记(十二)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12592256.html. 组件基础 组件化是vue的一个重要特性,也是vue学习中非常重要的一个知识点. 基础示例 ...

  2. python3爬虫 爬取动漫视频

    起因 因为本人家里有时候网速不行,所以看动漫的时候播放器总是一卡一卡的,看的太难受了.闲暇无聊又F12看看.但是动漫网站却无法打开控制台.这就勾起了我的兴趣.正好反正无事,去寻找下视频源. 但是这里事 ...

  3. python3_learn 实现文件夹内批量对图片重命名

    初衷 练习Python,提高动手能力. 珍藏的壁纸文件夹名命有点乱. 可以学习下一些基础的库 开始(.jpg,无筛选) First 首先找到OS库,寻找可以遍历文件名的.找到了OS.walk() os ...

  4. Java中的二分查找

    二分查找:(折半查找) 前提:数组必须是有序的. 思想:每次都猜中间的那个元素,比较大或者小,就能减少一半的元素.思路:A:定义最小索引,最大索引. B:比较出中间索引 C:拿中间索引的值和要查找的元 ...

  5. 21.SpringCloud实战项目-后台题目类型功能(网关、跨域、路由问题一文搞定)

    SpringCloud实战项目全套学习教程连载中 PassJava 学习教程 简介 PassJava-Learning项目是PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. ...

  6. java算法-二分法查找实现

    什么是二分法查找 首先,使用二分法查找的前提是:被查找的数组已排好序 具体实现: 假如有一组数为3,12,24,36,55,68,75,88要查给定的值24.可设三个变量front,mid,end分别 ...

  7. Git速查表大全

  8. Win10桌面美化

    捯饬了几个小时终于捯饬好了,没什么特效,就是看起来干净了许多. 用到的小软件: 链接:https://pan.baidu.com/s/1_PSTn0JZ22ZGiMDOdvdWEw提取码:329c 1 ...

  9. 线上Bug无法复现怎么办?老司机教你一招,SpringBoot远程调试不用愁!

    前言 在部署线上项目时,相信大家都会遇到一个问题,线上的 Bug 但是在本地不会复现,多么无奈. 此时最常用的就是取到前端传递的数据用接口测试工具测试,比如 POSTMAN,复杂不,难受不? 今天陈某 ...

  10. http与web

    在看<图解http>的时候,忽然有一个想法冒出来. web与http是什么关系? http服务器与web服务器有区别?为什么用以搞混了? web的定义:它是一种基于超文本和HTTP的.全球 ...