LeetCode-500. Keyboard Row
Posted 码上哈希
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-500. 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
public class Solution { public String[] findWords(String[] words) { if (words == null) return new String[0]; String[] r = {"qwertyuiop", "asdfghjkl", "zxcvbnm"}; List<String> rets = new ArrayList<String>(); for (String ws : words) { String w = ws.toLowerCase(); char first = w.charAt(0); int in = 0; while (in < 3) { if (r[in].contains(first+"")) break; in ++; } boolean all = true; for (int i=1; i<w.length(); i++) { if (!r[in].contains(w.charAt(i)+"")) { all = false; break; } } if (all) { rets.add(ws); } } return rets.toArray(new String[0]); } }
以上是关于LeetCode-500. Keyboard Row的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 500. Keyboard Row (键盘行)
leetcode 500. 键盘行(Keyboard Row)