• 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 like the image below.

    American keyboard

    Example 1:

    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.

    大致意思是输入一个数组,找到其中是由同一行字母组成的字符串组成新的数组

    思路很简单,使用hashmap存储一个键值对,键是每个字母,值是1,2,3。对于输入的字符串判断每个字符的hashmap的值是否一样即可

    public String[] findWords(String[] words) {
           ArrayList<String> list = new ArrayList();
                String arr1[] = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"};
                String arr2[] = {"a", "s", "d", "f", "g", "h", "j", "k", "l"};
                String arr3[] = {"z", "x", "c", "v", "b", "n", "m"};
                HashMap temp = new HashMap();
                for(String s:arr1)
                {
                    temp.put(s, 1);
                }
                 for(String s:arr2)
                {
                     temp.put(s, 2);
                }
                 for(String s:arr3)
                {
                     temp.put(s, 3);
                }
                for(int i=0;i<words.length;i++)
                {
                    int judge = 0;
                    for(int j=0;j<words[i].length();j++)
                    {
                        if (temp.get("" + words[i].toLowerCase().charAt(0)) != temp.get("" + words[i].toLowerCase().charAt(j))) 
                        {
                            judge = 1;
                            break;
                        }
                    }
                    if(judge != 1)
                    {
                        list.add(words[i]);
                    }
                }
                String[] ans = new String[list.size()];
                return (String[]) list.toArray(ans);
        }
  • 相关阅读:
    命令行中邮件的收发
    关于location对象
    正则表达式
    一家初创公司的 CTO 应当做什么?
    移动数据网络质量的国家奖牌榜
    MFQ&PPDCS测试分析和测试设计框架l学习记录
    Python学习笔记之基本语法学习1
    《用Python做HTTP接口测试》学习感悟
    我的中台的理解
    中台与平台的区别
  • 原文地址:https://www.cnblogs.com/icysnow/p/8176982.html
Copyright © 2020-2023  润新知