Files
LeetCode-Go/website/content/ChapterFour/0500.Keyboard-Row.md
2020-08-09 00:39:24 +08:00

62 lines
1.3 KiB
Markdown
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [500. Keyboard Row](https://leetcode.com/problems/keyboard-row/)
## 题目
Given a List of words, return the words that can be typed using letters of **alphabet** on only one row's of American keyboard like the image below.
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/keyboard.png)
**Example**:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
**Note**:
1. You may use one character in the keyboard more than once.
2. You may assume the input string will only contain letters of alphabet.
## 题目大意
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如上图所示。
## 解题思路
- 给出一个字符串数组,要求依次判断数组中的每个字符串是否都位于键盘上的同一个行,如果是就输出。这也是一道水题。
## 代码
```go
package leetcode
import "strings"
func findWords500(words []string) []string {
rows := []string{"qwertyuiop", "asdfghjkl", "zxcvbnm"}
output := make([]string, 0)
for _, s := range words {
if len(s) == 0 {
continue
}
lowerS := strings.ToLower(s)
oneRow := false
for _, r := range rows {
if strings.ContainsAny(lowerS, r) {
oneRow = !oneRow
if !oneRow {
break
}
}
}
if oneRow {
output = append(output, s)
}
}
return output
}
```