LeetCode - 709. To Lower Case

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lewky_liu/article/details/91344821

题目

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"

解题思路

这道题很简单,就是单纯遍历字符串的每个字符,如果是大写字母就变成小写字母,每个小写字母都比对应的大写字母的ASCII码大32。代码如下:

class Solution {
    public String toLowerCase(String str) {
        if (str == null || "".equals(str)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (c >= 65 && c <= 90) {
                c += 32;
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

如果不记得A或者Z的ASCII码是多少,也可以直接写成下面这样:

class Solution {
    public String toLowerCase(String str) {
        if (str == null || "".equals(str)) {
            return "";
        }
        int n = 'a' - 'A';
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (c >= 'A' && c <= 'Z') {
                c += n;
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

下面是时间与内存的消耗:

Runtime: 0 ms
Memory Usage: 33.1 MB

相关链接

展开阅读全文

BUY LOW, BUY LOWER

10-10

DescriptionnnThe advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems' advice: n "Buy low; buy lower"nnEach time you buy a stock, you must purchase it at a lower price than the previous time you bought it. The more times you buy at a lower price than before, the better! Your goal is to see how many times you can continue purchasing at ever lower prices. nnYou will be given the daily selling prices of a stock (positive 16-bit integers) over a period of time. You can choose to buy stock on any of the days. Each time you choose to buy, the price must be strictly lower than the previous time you bought stock. Write a program which identifies which days you should buy stock in order to maximize the number of times you buy. nnHere is a list of stock prices: n Day 1 2 3 4 5 6 7 8 9 10 11 12nnPrice 68 69 54 64 68 64 70 67 78 62 98 87nnnThe best investor (by this problem, anyway) can buy at most four times if each purchase is lower then the previous purchase. One four day sequence (there might be others) of acceptable buys is: nDay 2 5 6 10nnPrice 69 68 64 62nInputnn* Line 1: N (1 <= N <= 5000), the number of days for which stock prices are given nn* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers. nOutputnnTwo integers on a single line: n* The length of the longest sequence of decreasing prices n* The number of sequences that have this length (guaranteed to fit in 31 bits) nnIn counting the number of solutions, two potential solutions are considered the same (and would only count as one solution) if they repeat the same string of decreasing prices, that is, if they "look the same" when the successive prices are compared. Thus, two different sequence of "buy" days could produce the same string of decreasing prices and be counted as only a single solution. nSample Inputnn12n68 69 54 64 68 64 70 67 78 62n98 87nSample Outputnn4 2 问答

没有更多推荐了,返回首页