首页 > 其他分享 >[LeetCode] 2129. Capitalize the Title

[LeetCode] 2129. Capitalize the Title

时间:2024-03-11 12:44:08浏览次数:16  
标签:lowercase word title 2129 LeetCode words letters Capitalize remaining

You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:
If the length of the word is 1 or 2 letters, change all letters to lowercase.
Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
Return the capitalized title.

Example 1:
Input: title = "capiTalIze tHe titLe"
Output: "Capitalize The Title"
Explanation:
Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.

Example 2:
Input: title = "First leTTeR of EACH Word"
Output: "First Letter of Each Word"
Explanation:
The word "of" has length 2, so it is all lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

Example 3:
Input: title = "i lOve leetcode"
Output: "i Love Leetcode"
Explanation:
The word "i" has length 1, so it is lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

Constraints:
1 <= title.length <= 100
title consists of words separated by a single space without any leading or trailing spaces.
Each word consists of uppercase and lowercase English letters and is non-empty.

将标题首字母大写。

给你一个字符串 title ,它由单个空格连接一个或多个单词组成,每个单词都只包含英文字母。请你按以下规则将每个单词的首字母 大写 :
如果单词的长度为 1 或者 2 ,所有字母变成小写。
否则,将单词首字母大写,剩余字母变成小写。
请你返回 大写后 的 title 。

思路

判断每个单词的长度,然后根据情况做不同的操作。考察对字符串的拼接和大小写的转换。

复杂度

时间O(n)
空间O(n) - stringbuilder

代码

Java实现

class Solution {
    public String capitalizeTitle(String title) {
        String[] words = title.split(" ");
        StringBuilder sb = new StringBuilder();
        for (String word : words) {
            int n = word.length();
            if (n <= 2) {
                sb.append(word.toLowerCase());
            } else {
                sb.append(word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase());
            }
            sb.append(" ");
        }
        return sb.toString().trim();
    }
}

标签:lowercase,word,title,2129,LeetCode,words,letters,Capitalize,remaining
From: https://www.cnblogs.com/cnoodle/p/18065833

相关文章

  • leetcode 528/ LCR 071 按权重随机选择
    leetcode528/LCR071按权重随机选择528.按权重随机选择LCR071.按权重随机选择题目描述给定一个正整数数组w,其中w[i]代表下标i的权重(下标从0开始),请写一个函数pickIndex,它可以随机地获取下标i,选取下标i的概率与w[i]成正比。例如,对于w=[1,3],挑选下标0......
  • LeetCodeHot100 283. 移动零 11. 盛最多水的容器 15. 三数之和 42. 接雨水
    283.移动零https://leetcode.cn/problems/move-zeroes/description/?envType=study-plan-v2&envId=top-100-likedpublicvoidmoveZeroes(int[]nums){intr=0;for(inti=0;i<nums.length;i++){if(nums[i]!=0){......
  • LeetCode 128.最长连续序列 Python题解
    leetcode128题最长连续序列分享解题思路,使用哈希表算法......
  • 【LeetCode】整数转罗马数字 C语言 | 此刻,已成艺术(bushi)
    Problem:12.整数转罗马数字目录思路解题方法复杂度Code思路暴力破解+转换解题方法由思路可知复杂度时间复杂度:$O(n)$空间复杂度:$O(1)$Codechar*intToRoman(intnum){char*s=(char*)malloc(sizeof(char)*4000),*p=s;while(num>0)......
  • LeetCodeHot100 283. 移动零 11. 盛最多水的容器 42. 接雨水 15. 三数之和
    283.移动零https://leetcode.cn/problems/move-zeroes/description/?envType=study-plan-v2&envId=top-100-likedpublicvoidmoveZeroes(int[]nums){intr=0;for(inti=0;i<nums.length;i++){if(nums[i]!=0){......
  • 代码随想录算法训练营day17 | leetcode 110. 平衡二叉树、257. 二叉树的所有路径、404
    目录题目链接:110.平衡二叉树-简单题目链接:257.二叉树的所有路径-简单题目链接:404.左叶子之和-简单题目链接:110.平衡二叉树-简单题目描述:给定一个二叉树,判断它是否是平衡二叉树示例1:输入:root=[3,9,20,null,null,15,7]输出:true示例2:输入:root=[1,2,2,3,3,null,nul......
  • 2024-03-08 leetcode写题记录
    目录2024-03-08leetcode写题记录27.移除元素题目链接题意解法179.最大数题目链接题意解法75.颜色分类题目链接题意解法2024-03-08leetcode写题记录27.移除元素题目链接27.移除元素题意给你一个数组\(nums\)和一个值\(val\),你需要原地移除所有数值等于\(val\)的元素,并......
  • 【LeetCode】977. 有序数组的平方
    题目:977.有序数组的平方解题思路:分析题目,左侧负数的平方可能超过右侧正数的平方,所以考虑使用双指针法,从左右向中间遍历最大值将遍历结果放入新创建的数组中,返回数组由于该问题的传入数组大小不确定,故只能使用动态数组创建方法,malloc方法导入<math.h>,使用abs绝对值比较函数,......
  • 【LeetCode】209. 长度最小的子数组
    题目:209.长度最小的子数组解题思路:初始化最小长度,设置为最大值,当最小长度变小时,该值更新设置left和right指针,left指针用于记录左边界,当求和sum大于target时,左指针右移;right指针记录右边界,当求和sum小于target时,右指针右移,继续寻找符合要求的子字符串。当右边界符合题目要求......
  • 2023-03-07 leetcode写题记录
    2023-03-07leetcode写题记录目录2023-03-07leetcode写题记录148.排序链表题目链接题意解法归并排序56.合并区间题目链接题意解法复健中,第一次重新写链表题。写链表题需要注意下面这些事项:写链表时,可以把链表理解成一个数,只不过这个数有特殊含义,代表着一个地址;"->"是对地......