算法:和为 K 的子数组 + 转换成小写字母

  • Post author:
  • Post category:其他



和为K的子数组


题目

给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k 的连续子数组的个数 。

示例 1:

输入:nums = [1,1,1], k = 2

输出:2

示例 2:

输入:nums = [1,2,3], k = 3

输出:2

提示:

1 <= nums.length <= 2 * 10^4

-1000 <= nums[i] <= 1000

-10^7 <= k <= 10^7

代码

class Solution {
    public int subarraySum(int[] nums, int k) {
        int count = 0;
        for (int start = 0;start < nums.length;start++) {
            int sum = 0;
            for (int end = start;end >= 0;end--) {
                sum += nums[end];
                if (sum == k) {
                    count++;
                } 
            }
        }
        return count;
    }
}

执行结果


转换成小写字母

题目

给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。

示例 1:

输入:s = “Hello”

输出:”hello”

示例 2:

输入:s = “here”

输出:”here”

示例 3:

输入:s = “LOVELY”

输出:”lovely”

提示:

1 <= s.length <= 100

s 由 ASCII 字符集中的可打印字符组成

代码

class Solution {
    public String toLowerCase(String s) {
        return s.toLowerCase(); 
    }
}

执行结果

来源:力扣(LeetCode)

链接:https://leetcode.cn/problems/subarray-sum-equals-k



版权声明:本文为Elena_qi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。