Leetcode500 Keyboard Row
Posted chason95
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode500 Keyboard Row相关的知识,希望对你有一定的参考价值。
class Solution { public String[] findWords(String[] words) { HashSet<Character>[] rows = new HashSet[3]; ArrayList<String> result = new ArrayList<>(); String[] letters = {"qwertyuiopQWERTYUIOP","asdfghjklASDFGHJKL","zxcvbnmZXCVBNM"}; for(int i=0;i<3;i++) { rows[i] = new HashSet<Character>(); //这里容易忽略从而报错nullPointerException char[] temp = letters[i].toCharArray(); for(char l:temp) rows[i].add(l); } for(String word:words) { char[] ls = word.toCharArray(); int row; for(row=0;row<3;row++) if(rows[row].contains(ls[0])) break; boolean isOK = true; for(char l:ls) if(!rows[row].contains(l)) {isOK=false;break;} if(isOK) result.add(word); } String[] r = new String[result.size()]; for(int i=0;i<r.length;i++) { r[i]=result.get(i); } return r; } }
以上是关于Leetcode500 Keyboard Row的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 500. Keyboard Row (键盘行)