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. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects

    The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...

  2. 5分钟python爬虫案例,手把手教爬取国内外最新疫情历史数据

    俗话说的好,“授之以鱼不如授之以渔”,所以小编今天就把爬疫情历史数据的方法分享给你们. 基本思路:分析腾讯新闻“抗肺炎”版块,采用“倒推法”找到疫情数据接口,然后用python模拟请求,进而保存疫情历 ...

  3. stand up meeting 1/7/2016

    part 组员                今日工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云 调研下滑条的存在问题,尝试替换方案     6 全面实行替换 ...

  4. windows编译动态链接库,dll+lib的形式

    之前一直在linux上做开发,没怎么关注过windows上如何编译动态链接库.不过一直存疑,为什么windows上的动态链接库是.dll配合.lib使用的,这个又是怎么生成的呢,通过一段时间的查资料和 ...

  5. .Net Core主机配置

    Host:(主机)负责web应用程序的启用和生成期管理,配置服务器和请求处理管道. 主机配置日志,依赖注入关系,实际上是一个封装了应用资源的对象. 创建主机生成器-〉配置主机-〉创建主机-〉运行主机. ...

  6. ASP.NET Core Razor Pages 初探

    最近新建 Asp.net Core MVC 项目的时候不小心选错了个模板,发现了一种新的项目模板.它使用cshtml视图模板,但是没有Controller文件夹.后来才发现这是ASP.NET Core ...

  7. php 常量的使用

    我们来看下直接的例子 <?php //定义常量 //常量不可被删除 //常量一旦被定义,就无法重新置换 //常量一旦定义,就不能对他第二次定义,否则会报错! define('MYCONSTANT ...

  8. Inno Setup 删除文件夹 DelTree

    Pascal Scripting: DelTree Prototype: function DelTree(const Path: String; const IsDir, DeleteFiles, ...

  9. c++中set 的用法

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  10. Nginx四层代理

    Nginx支持四层代理 http://nginx.org/en/docs/stream/ngx_stream_core_module.html 该ngx_stream_core_module模块自1. ...