1. 程式人生 > >LeetCode之路:520. Detect Capital

LeetCode之路:520. Detect Capital

一、引言

這道題有關於處理字元的大小寫問題,對於熟悉字元的大小寫處理函式非常有幫助。

這裡粘出題目資訊:

Given a word, you need to judge whether the usage of capitals in it is right or not.
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 if it has more than one letter, like “Google”.
Otherwise, we define that this word doesn’t use capitals in a right way.

這道題看上去應該比較簡單,重點是我們如何快速的分析題意,以最簡潔的方式解出題目來。

二、看看我的分析

按照題目意思,合法的大寫使用情況有幾種呢:

合法情況 單詞第 1 位 單詞第 1 位之外
全部大寫 U U
全部小寫 L L
第 1 位大寫其後全部小寫 U L

注:這裡用 U 表示大寫,用 L 表示小寫

這裡可以看到,有三種情況可以判定使用合法,那麼在程式碼中我們就要判斷三種情況嗎?

當然不是,這樣肯定可以做出來,但是太過於複雜,不如我們換個角度思考,我們來考慮不合法的情況:

非法情況 單詞第 1 位 單詞第 1 位之外
第 1 位小寫,其後位置有大寫字母 L 存在 U 字母
第 1 位大寫,其後位置有大寫字母也有小寫字母 U 存在 U 和 L 字母

那麼,我們需要判斷的情況,就由 3 種簡化成了 2 中。

根據上述分析,程式碼如下:

// my solution2, runtime = 9 ms
class Solution2 {
public:
    bool detectCapitalUse(string word) {
        if (word.size() == 1) return true;
        if
(islower(word[0])) { for (auto i : word) if (isupper(i)) return false; return true; } else { bool hasUpper = false; bool hasLower = false; for (size_t i = 1; i < word.size(); ++i) { if (islower(word[i])) hasLower = true; else hasUpper = true; } if (hasUpper && hasLower) return false; else return true; } } };

那麼,還有沒有其他的思路呢?

三、看看別人的三行程式碼

當然有其他的思路了。

上述方法中,我一直在考慮如何在遍歷中判斷大小寫,並且直接返回結果。

這裡有一種方法,就是記錄單詞中的大寫字母的個數,與上述分析一樣的判斷,判斷是否合法。

直接看程式碼直觀些:

// other's solution , runtime = 9 ms
class Solution3 {
public:
    bool detectCapitalUse(string word) {
        int count = 0;
        for (auto i : word) if (isupper(i)) ++count;
        return !count || count == word.size() || isupper(word[0]) && count == 1;
    }
};

其實呢,判斷邏輯都是差不多的,只是判斷根據換成了大寫字母的個數與單詞總數的比較,儘管 runtime 差不多,但是很明顯這種方法更加的直觀、簡潔。

追求程式碼的簡潔是我的愛好,To be Continue!

相關推薦

LeetCode520. Detect Capital

一、引言 這道題有關於處理字元的大小寫問題,對於熟悉字元的大小寫處理函式非常有幫助。 這裡粘出題目資訊: Given a word, you need to judge whether the usage of capitals in it is r

[LeetCode&Python] Problem 520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the

Leetcode PHP題解--D81 520. Detect Capital

D81 520. Detect Capital 題目連結 520. Detect Capital 題目分析 給定一個單詞,判斷其使

LeetCode - 520. Detect Capital

style detect inpu lower fine whether span string har Given a word, you need to judge whether the usage of capitals in it is right or not.

leetcode-520-Detect Capital

ons emp ctc con miss 很慢 字符 other 都是 題目描述: Given a word, you need to judge whether the usage of capitals in it is right or not. We defin

LeetCode-520. Detect Capital

520.Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to

LeetCode 520. Detect Capital

520. Detect CapitalGiven a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals

LeetCode520 Detect Capital

題目大意 判斷一個單詞的大小寫運用是否正確。全部大寫、全部小寫和首字母大寫是正確的寫法。 解法一 分三種情況: 首字母小寫,如果之後有一個大寫字母,則返回false。 首字母大寫,

Unity3d修煉遊戲開發中,3d數學知識的練習【1】(不斷更新.......)

turn tor rdo pre 長度 scrip 縮放 unity3d float #pragma strict public var m_pA : Vector3 = new Vector3(2.0f, 4.0f, 0.0f); public var m_pB :

我的R主成分分析

log -1 plot code style 9.png ngs alt 顯示 主成分分析是利用降維的方法,在損失很少信息量很少的前提下 X1 X2 X3 X4 X5 X6 X7 X8 1 90342 52

Java學習不走彎路,就是捷徑

下載地址 下載 何事 系統 也有 包括 軟件公司 項目管理師 應用 1.如何學習程序設計? Java是一種平臺,也是一種程序設計語言,如何學好程序設計不僅僅適用於Java,對C++等其他程序設計語言也一樣管用。有編程高手認為,JAVA也好C也好沒什麽分別,拿來就用。為什麽他

angular踩坑初探webpack

match 了解 contex tex component logs pen erro nco 之前費了一番力氣安裝好了angular開發環境,後面的幾天都是在angular中文官網上看文檔,照著英雄教程一步一步操作,熟悉了angular的一些基本特性,這部分沒

leetcode

strong class -1 pac == lag ace style dig 寫的不是很好,僅記錄自己所寫的,僅供參考。 第七題: Reverse digits of an integer. Example1: x = 123, return 321 Example2

pythonpython基礎3

bar 匿名函數 發送 函數式 edit 系統 概念 作用域 opened ---恢復內容開始--- 本節內容 1. 函數基本語法及特性 2. 參數與局部變量 3. 返回值 嵌套函數 4.遞歸 5.匿名函數 6.函數式編程介紹 7.高階函數 8.內置函數 溫故知新 1.

Angular4.0踩坑探索子路由和懶加載

ati clas per 而是 配置 trap child property one 參考文章: Angular4路由快速入門 http://www.jianshu.com/p/e72c79c6968e Angular2文檔學習的知識點摘要——Angular模塊(NgMo

小白成長初識python(三) -----------python內置函數

urn locals for lte mod pre 最大 變量 ascii   剛才看了一下竟然還有人看我的博客^ - ^ 廢話不多說,上代碼,有不對的勿噴,畢竟我真的是小白 # python 內置函數整理# 返回一個數的絕對值# a = abs(-10)# print(

小白成長初識python(五) --python裝飾器

pytho ret 成了 通過 是把 代碼 rgs 得到 ## flag = "=======================裝飾器============================="#定義的裝飾器函數# def outer(func):# def inne

翻譯(六)——T-SQL的進階超過基礎的2級水平寫子查詢

相關 完整 圖像 reg 想要 試驗 releases 驗證 不用 T-SQL的進階之路:超過基礎的2級水平:寫子查詢 格雷戈裏·拉森(Gregory Larsen),2016/01/01(第一次出版:2014/01/29) 該系列 這篇文章是樓梯系列的

Python學習集合的使用

元素 int pda car ren 添加 brush 存在 對稱 集合關系測試: list_1=[1,4,5,7,3,6,7,9] list_1=set(list_1) #去重 list_2=set([2,6,0,66,22,8]) print(list_1,typ

Python學習文件操作

append 句柄 enc pre light logs utf highlight 追加內容 文件基本操作: 打開、讀取、追加、關閉 #data = open("yesterday",encoding="utf-8").read() #打開並讀取文件 f=open("y