代码长了些,但还是比较简单的

 public class Solution {
public String[] findWords(String[] words) {
List<String> ansList = new ArrayList<String>();
Map<Character, Integer> charMap = new HashMap<Character, Integer>();
charMap.put('q', 1);charMap.put('w', 1);charMap.put('e', 1);charMap.put('r', 1);charMap.put('t', 1);
charMap.put('y', 1);charMap.put('u', 1);charMap.put('i', 1);charMap.put('o', 1);charMap.put('p', 1);
charMap.put('a', 2);charMap.put('s', 2);charMap.put('d', 2);charMap.put('f', 2);charMap.put('g', 2);
charMap.put('h', 2);charMap.put('j', 2);charMap.put('k', 2);charMap.put('l', 2);charMap.put('z', 3);
charMap.put('x', 3);charMap.put('c', 3);charMap.put('v', 3);charMap.put('b', 3);charMap.put('n', 3);
charMap.put('m', 3);
for(int i = 0; i < words.length; i++) {
boolean isSame = true;
Character c = new Character(words[i].charAt(0));
int pos = charMap.get(Character.toLowerCase(c));
for (int j = 1; j < words[i].length(); j++) {
c = new Character(words[i].charAt(j));
if (pos != charMap.get(Character.toLowerCase(c))) {
isSame = false;
break;
}
}
if (isSame == true)
ansList.add(words[i]);
}
String[] ans = new String[ansList.size()];
for (int i = 0; i < ansList.size(); i++) {
ans[i] = ansList.get(i);
}
return ans;
}
}

LeetCode: Keyboard Row的更多相关文章

  1. LeetCode——Keyboard Row

    LeetCode--Keyboard Row Question Given a List of words, return the words that can be typed using lett ...

  2. [LeetCode] Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  3. 46. leetcode 500. Keyboard Row

    500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...

  4. Leetcode#500. Keyboard Row(键盘行)

    题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...

  5. Week4 - 500.Keyboard Row & 557.Reverse Words in a String III

    500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...

  6. LeetCode 500. Keyboard Row (键盘行)

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  7. [LeetCode] 500. Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  8. 【LeetCode】500. Keyboard Row 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...

  9. leetcode算法: Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

随机推荐

  1. 基于GitLab的前端Assets发布体系

    以SVN+RMS为核心的发布系统,对前端开发的影响上来看,存在以下问题: 覆盖式的发布,容易导致线上问题. js一旦发布,就有可能被任意其他页面使用.被引用的越多,就越重要.一旦核心js出现故障,影响 ...

  2. Vsphere日记02.ESXi5.5.configuration

    2.Vsphere ESXi 5.5 configuration 步骤1:启动 ESXI 服务器 步骤2:按 F2 进入用户登录界面 输入用户名及密码,进入参数设置界面.密码为我安装时候设置的&quo ...

  3. void bind(String sName,Object object);――绑定:把名称同对象关联的过程

    void bind(String sName,Object object);――绑定:把名称同对象关联的过程 void rebind(String sName,Object object);――重新绑 ...

  4. hdu 4587(割点的应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 思路:题目的意思很简单,就是删除任意2个节点以及关联的边,求图的最大连通分量数.我们知道删除割点 ...

  5. python3 - property的使用

    传统的绑定属性值,会把属性暴露出去,而且无法检查参数是否合法,如下: class Test(object): def  __int__(self,age): self.age = age 为了检查参数 ...

  6. python学习【第十篇】单例设计模式

    单例设计模式 目的:让类创建对象,在系统中只有唯一的实例,让每一次创建的对象返回的内存地址都是相同的. __new__方法 使用类名创建对象时,python解释器首先会调用__new__方法为对象分配 ...

  7. python学习【第六篇】python迭代器与生成器

    一.什么是迭代器 迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代(只能往后走不能往前退) 可迭代对象:实现了迭代器 ...

  8. 【转】linux 中fork()函数详解

    在看多线程的时候看到了这个函数,于是学习了下,下面文章写的通俗易懂,于是就开心的看完了,最后还是很愉快的算出了他最后一个问题. linux 中fork()函数详解 一.fork入门知识 一个进程,包括 ...

  9. DataTable数据导出到Excel,并发送到客户端进行下载

    本代码实现思路是:页面显示和导出分开,导出的数据和用于页面显示的是同一查询数据方式,所以也是同样的数据,只是在导出数据时从数据库重新捞了一次数据.此导出数据方式会先将数据保存到Excel中,然后将创建 ...

  10. 【原创】学习CGLIB动态代理中遇到的问题

    代码清单1 CGLIB动态代理 package wulj.proxy.cglibProxy; import java.lang.reflect.Method; import net.sf.cglib. ...