1. 程式人生 > >【LeetCode】520 Detect Capital

【LeetCode】520 Detect Capital

題目大意

判斷一個單詞的大小寫運用是否正確。全部大寫、全部小寫和首字母大寫是正確的寫法。

解法一

分三種情況:

  • 首字母小寫,如果之後有一個大寫字母,則返回false。
  • 首字母大寫,如果第二個字母小寫,則將type複製為1,其後如果有大寫字母,則返回false。
  • 首字母大寫,如果第二個字母大寫,則將type複製為2,其後如果有小寫字母,則返回false。
bool detectCapitalUse(string word) {
    bool isFirstUpper = isupper(word[0]);
    int type = 0;

    for (int i = 1
; i < word.length(); i++) { if (!isFirstUpper && isupper(word[i])) { return false; } else if (isFirstUpper && islower(word[i])) { // "Google" if (type == 0 || type == 1) type = 1; else return
false; } else if (isFirstUpper && isupper(word[i])) { // "USA" if (type == 0 || type == 2) type = 2; else return false; } } return true; }

解法二

分兩種情況:

  • 全部小寫與首字母大寫分為一種,其後如果有大寫字母則返回false。
  • 全部大寫為一種情況,其後如果有小寫字母則返回false。
bool detectCapitalUse2(string word) {
    if (word.length() <= 1)
        return true;
    if (islower(word[0]) || isupper(word[0]) && islower(word[1])) {
        for (int i = 1; i < word.length(); i++) {
            if (isupper(word[i]))
                return false;
        }
    }
    else {
        for (int i = 1; i < word.length(); i++) {
            if (islower(word[i]))
                return false;
        }
    }

    return true;
}

參考:

相關推薦

LeetCode520 Detect Capital

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

LeetCode520. 檢測大寫字母

1.題目 給定一個單詞,你需要判斷單詞的大寫使用是否正確。 我們定義,在以下情況時,單詞的大寫用法是正確的: 全部字母都是大寫,比如"USA"。 單詞中所有字母都不是大寫,比如"leetcode"。 如果單詞不只含有一個字母,只有首字母大寫, 比如 “Google”。 否則,

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&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-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

LeetCode之路:520. Detect Capital

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

Leetcode PHP題解--D81 520. Detect Capital

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

LeetCode091. Decode Ways

rom size etc oss following nbsp pan ron ann 題目: A message containing letters from A-Z is being encoded to numbers using the following map

LeetCode040. Combination Sum II

log bsp for ont end ati 無法 clas class 題目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinatio

LeetCode240. Search a 2D Matrix II

target ott arc rop win mat ive pty his 題目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the

LeetCode215. Kth Largest Element in an Array

distinct class ted ++ bsp order algo max git 題目: Find the kth largest element in an unsorted array. Note that it is the kth largest eleme

LeetCode169. Majority Element

turn end and else pear ive element emp bsp 題目: Given an array of size n, find the majority element. The majority element is the element t

LeetCode064. Minimum Path Sum

ive rom right ott path sum 處理 tom ber its 題目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri

LeetCode241. Different Ways to Add Parentheses

cto only leetcode save ++ ssi brush log ive 題目: Given a string of numbers and operators, return all possible results from computing all t

LeetCode039. Combination Sum

set sha leet delet als unique ati solution gin 題目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), fin

LeetCode053. Maximum Subarray

子序列 fin n) cto largest nbsp code ray ive 題目: Find the contiguous subarray within an array (containing at least one number) which has the

LeetCode022. Generate Parentheses

ret logs int false return 題解 gen cto solution 題目: Given n pairs of parentheses, write a function to generate all combinations of well-for

LeetCodePythonBinary Tree Inorder Traversal

nod 不知道 otto div ack return integer neu else Given a binary tree, return the inorder traversal of its nodes‘ values. For example: Gi