首页 > 其他分享 >leetcode-500-easy

leetcode-500-easy

时间:2022-10-31 20:56:19浏览次数:68  
标签:map ch String int easy put words leetcode 500

Keyboard Row

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.

In the American keyboard:

the first row consists of the characters "qwertyuiop",
the second row consists of the characters "asdfghjkl", and
the third row consists of the characters "zxcvbnm".

Example 1:

Input: words = ["Hello","Alaska","Dad","Peace"]
Output: ["Alaska","Dad"]
Example 2:

Input: words = ["omk"]
Output: []
Example 3:

Input: words = ["adsdf","sfd"]
Output: ["adsdf","sfd"]
Constraints:

1 <= words.length <= 20
1 <= words[i].length <= 100
words[i] consists of English letters (both lowercase and uppercase).

思路一:存储字母所在的位置,共分为三类,最后挨个遍历单词,查看单词的字符是否都处于都一个位置即可

public String[] findWords(String[] words) {
    String first = "qwertyuiop";
    String second = "asdfghjkl";
    String third = "zxcvbnm";

    Map<Character, Integer> map = new HashMap<>();
    for (int i = 0; i < first.length(); i++) {
        char ch = first.charAt(i);
        map.put(ch, 1);
        map.put(Character.toUpperCase(ch), 1);
    }
    for (int i = 0; i < second.length(); i++) {
        char ch = second.charAt(i);
        map.put(ch, 2);
        map.put(Character.toUpperCase(ch), 2);
    }
    for (int i = 0; i < third.length(); i++) {
        char ch = third.charAt(i);
        map.put(ch, 3);
        map.put(Character.toUpperCase(ch), 3);
    }

    List<String> result = new ArrayList<>();
    out:
    for (String word : words) {
        int c = map.get(word.charAt(0));
        for (int i = 1; i < word.length(); i++) {
            if (map.get(word.charAt(i)) != c) continue out;
        }
        result.add(word);
    }

    return result.toArray(new String[0]);
}

标签:map,ch,String,int,easy,put,words,leetcode,500
From: https://www.cnblogs.com/iyiluo/p/16845752.html

相关文章

  • leetcode-1356-easy
    SortIntegersbyTheNumberOf1BitsYouaregivenanintegerarrayarr.Sorttheintegersinthearrayinascendingorderbythenumberof1'sintheirbinar......
  • Codeforces - 839C - Journey(图论 + 概率 + 搜索、*1500)
    839C-Journey(⇔源地址)目录839C-Journey(⇔源地址)tag题意思路错误思路正解AC代码错误次数:2tag⇔图论、⇔概率、⇔搜索、⇔*1500题意在七......
  • AI人脸检测识别EasyCVR视频融合平台告警预案的配置操作与使用
    我们在前期的文章中为大家介绍了EasyCVR新增的告警预案功能,感兴趣的用户可以戳这篇文章:《AI人脸检测智能视频融合平台EasyCVR新增告警预案功能》。  告警预案可以根......
  • EasyCode
    文章目录​​前提准备​​​​1、idea安装easycode插件​​​​本地安装​​​​在线安装(推荐)​​​​2、idea添加数据库​​​​一、easyCode表生成​​​​单表生成:选择......
  • EasyCode
    文章目录​​前提准备​​​​1、idea安装easycode插件​​​​本地安装​​​​在线安装(推荐)​​​​2、idea添加数据库​​​​一、easyCode表生成​​​​单表生成:选择......
  • leetcode-1-easy
    TwoSumGivenanarrayofintegersnumsandanintegertarget,returnindicesofthetwonumberssuchthattheyadduptotarget.Youmayassumethateachinp......
  • C++&Python 描述 LeetCode 1.两数之和
    C++&Python描述LeetCode1.两数之和  大家好,我是亓官劼(qíguānjié),在【亓官劼】公众号、、GitHub、B站、华为开发者论坛等平台分享一些技术博文。放弃不难,但坚持......
  • SpringBoot如何用easyPOI导出excel文件
    在工作中,经常需要我们用Java代码导出一些数据,保存在Excel中。这是非常实用的Excel导出功能,如果我们用SpringBoot结合EasyPOI框架,可以非常方便地实现这个功能。在maven项目中......
  • dp-leetcode152
    动态规划问题,存在重叠子问题/***<p>给你一个整数数组<code>nums</code>&nbsp;,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数......
  • 1500套HTML+CSS+JS网页设计期末课程大作业 web前端开发技术 web课程设计 网页规划与设
    一、1500套HTML期末学生结课大作业作品(HTML+CSS+JS)这8年来做了1000多套(HTML+CSS+JS)网页设计的学生期末大作业,都是给学生定制的都符合学校或者学生考试期末作业的水平,都......