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();
    }
}

总结

此题思路很简单,遍历给定字符串,如果字母为大写字母,则变为小写字母后用一个字符串变量存起来,否则直接将小写字母或其他字符存起来。
Note:
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,否则什么都不做。

Note:
1.此题又是用StringBuilder类进行字符连接,返回值需要用toString()方法转为字符串。

原文地址:https://www.cnblogs.com/victorxiao/p/11094571.html

时间: 2024-11-06 23:41:40

LeetCode--To Lower Case & Remove Outermost Parentheses (Easy)的相关文章

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

这是小川的第380次更新,第408篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第242题(顺位题号是1021).有效的括号字符串为空(""),"("+ A +")"或A + B,其中A和B是有效的括号字符串,+表示字符串连接.例如,"","()","(())()"和"(()(()))"都是有效的括号字符串. 如果有效的括号字符串S是非空

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

https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution 1 class Solution { 2 public String removeOuterParentheses(String S) { 3 char[] chars = S.toCharArray(); 4 int flag = 0; 5 StringBuilder result = new StringBuilder(); 6 7 for

LeetCode 1021 Remove Outermost Parentheses

class Solution { public String removeOuterParentheses(String S) { int outer = 0; int inner = 0; char[] intput = S.toCharArray(); char[] output = new char[intput.length]; int i = 0; for (char c: intput) { if (outer != 0) { if (inner == 0) { if (')' ==

[LeetCode] To Lower Case 转为小写

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: "L

leetcode Remove Invalid Parentheses

题目连接 https://leetcode.com/problems/remove-invalid-parentheses/ Remove Invalid Parentheses Description Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may co

[LeetCode][JavaScript]Remove Invalid Parentheses

Remove Invalid Parentheses Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()"

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not