From 761469af167a14e4119ef8935867eeb2926e3404 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Sat, 13 Nov 2021 20:44:39 +0800 Subject: [PATCH 1/3] add: leetcode 0520 solution --- leetcode/0520.Detect-Capital/520.Detect Capital.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 leetcode/0520.Detect-Capital/520.Detect Capital.go diff --git a/leetcode/0520.Detect-Capital/520.Detect Capital.go b/leetcode/0520.Detect-Capital/520.Detect Capital.go new file mode 100644 index 000000000..c9710484d --- /dev/null +++ b/leetcode/0520.Detect-Capital/520.Detect Capital.go @@ -0,0 +1,13 @@ +package leetcode + +import "strings" + +func detectCapitalUse(word string) bool { + wLower := strings.ToLower(word) + wUpper := strings.ToUpper(word) + wCaptial := strings.ToUpper(string(word[0])) + strings.ToLower(string(word[1:])) + if wCaptial == word || wLower == word || wUpper == word { + return true + } + return false +} From 75d1e15eef1e1772acef1f1e9dcd4092239a55ab Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Sat, 13 Nov 2021 20:44:54 +0800 Subject: [PATCH 2/3] add: leetcode 0520 test --- .../520.Detect Capital_test.go | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 leetcode/0520.Detect-Capital/520.Detect Capital_test.go diff --git a/leetcode/0520.Detect-Capital/520.Detect Capital_test.go b/leetcode/0520.Detect-Capital/520.Detect Capital_test.go new file mode 100644 index 000000000..20635849b --- /dev/null +++ b/leetcode/0520.Detect-Capital/520.Detect Capital_test.go @@ -0,0 +1,45 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question520 struct { + para520 + ans520 +} + +// para 是参数 +type para520 struct { + word string +} + +// ans 是答案 +type ans520 struct { + ans bool +} + +func Test_Problem520(t *testing.T) { + + qs := []question520{ + + { + para520{"USA"}, + ans520{true}, + }, + + { + para520{"FlaG"}, + ans520{false}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 520------------------------\n") + + for _, q := range qs { + _, p := q.ans520, q.para520 + fmt.Printf("【input】:%v 【output】:%v\n", p, detectCapitalUse(p.word)) + } + fmt.Printf("\n\n\n") +} From 0ef19c8e0b4ee16350e505f0b3c5385bc3269dd6 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Sat, 13 Nov 2021 20:45:23 +0800 Subject: [PATCH 3/3] add: leetcode 0520 readme --- leetcode/0520.Detect-Capital/README.md | 65 ++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 leetcode/0520.Detect-Capital/README.md diff --git a/leetcode/0520.Detect-Capital/README.md b/leetcode/0520.Detect-Capital/README.md new file mode 100644 index 000000000..376ef59a5 --- /dev/null +++ b/leetcode/0520.Detect-Capital/README.md @@ -0,0 +1,65 @@ +# [520. Detect Capital](https://leetcode-cn.com/problems/detect-capital/) + + +## 题目 + +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 1:** + +``` +Input: word = "USA" +Output: true +``` + +**Example 2:** + +``` +Input: word = "FlaG" +Output: false +``` + +**Constraints:** + +- 1 <= word.length <= 100 +- word consists of lowercase and uppercase English letters. + +## 题目大意 + +我们定义,在以下情况时,单词的大写用法是正确的: + +全部字母都是大写,比如 "USA" 。 +单词中所有字母都不是大写,比如 "leetcode" 。 +如果单词不只含有一个字母,只有首字母大写,比如"Google" 。 + +给你一个字符串 word 。如果大写用法正确,返回 true ;否则,返回 false 。 + +## 解题思路 + +- 把word分别转换为全部小写wLower,全部大写wUpper,首字母大写的字符串wCaptial +- 判断word是否等于wLower,wUpper,wCaptial中的一个,如果是返回true,否则返回false + +## 代码 +```go +package leetcode + +import "strings" + +func detectCapitalUse(word string) bool { + wLower := strings.ToLower(word) + wUpper := strings.ToUpper(word) + wCaptial := strings.ToUpper(string(word[0])) + strings.ToLower(string(word[1:])) + if wCaptial == word || wLower == word || wUpper == word { + return true + } + return false +} +``` \ No newline at end of file