Day 27 - Detect Capital

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~


520. Detect Capital

Question

We define the usage of capitals in a word to be right when one of the following cases holds:

  • All letters in this word are capitals, like "USA".
  • All letters in this word are not capitals, like "leetcode".
  • Only the first letter in this word is capital, like "Google".

Given a string word, return true if the usage of capitals in it is right.


Example

Example1

Input: word = "USA"
Output: true

Example2

Input: word = "FlaG"
Output: false

Constraints

  • 1 <= word.length <= 100
  • word consists of lowercase and uppercase English letters.

解题

题目

首先先简单的翻译一下题目
给一个字串,判断是不是符合题目的规则。

  • 字母全大写
  • 字母全小写
  • 字母只有字首大写

Think

作法大致上是这样

  • 先把第一位的字母抓出来判断字首是大写还小写。
  • 如果是大写:
    • 代表剩下的字母必须都是大写或都是小写
    • 减少跑的回圈次数,从头跟从尾往中间判断是不是都是大写或都是小写
  • 如果是小写:
    • 代表剩下的字母必须都是小写
    • 一样从头跟从尾往中间判断是不是都是小写

Code

Python

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x = str(x)
        left = 0
        right = len(x)-1
        
        while left < right:
            if x[left] != x[right]:
                return False
            
            left += 1
            right -= 1
            
        return True

Result

  • Python

大家明天见/images/emoticon/emoticon29.gif


<<:  Day30 - 模型复杂度分析

>>:  找LeetCode上简单的题目来撑过30天啦(DAY27)

Vue出一个展开 / 隐藏 功能

今天练习的主题是用Vue实现列表的展开与隐藏功能 会分为两个范例让大家做演练 范例一 先将isSho...

第31天~

这个的前一篇是~https://ithelp.ithome.com.tw/articles/1024...

Day21 javascript 阵列方法(完)

今天继续来讲阵列,想说都剩没多少,不如就一次介绍完呗,这样以後翻笔记才不会翻半天还找不到东西(大家应...

Day 16 JavaScript boxing vs unboxing

boxing: 封装可以让原始型态的资料暂时转成物件,这样他才可以使用属性或方法。 遇到使用字面值(...

[FGL] 吸星大法 - IMPORT之 2: 带入JAVA或其他FGL套件

前一篇IMPORT中,提到Genero Package中有提供一些预先制作的功能套件可用。 可是面对...