LeetCode_500. Keyboard Row

500. Keyboard Row

Easy

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.

Example:

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.
package leetcode.easy;

public class KeyboardRow {
	private static void print_arr(String[] strs) {
		for (String str : strs) {
			System.out.print(str + " ");
		}
		System.out.println();
	}

	private java.util.HashSet<Character> set1 = new java.util.HashSet<Character>();
	private java.util.HashSet<Character> set2 = new java.util.HashSet<Character>();
	private java.util.HashSet<Character> set3 = new java.util.HashSet<Character>();

	public String[] findWords(String[] words) {
		char[] chs1 = { ‘q‘, ‘w‘, ‘e‘, ‘r‘, ‘t‘, ‘y‘, ‘u‘, ‘i‘, ‘o‘, ‘p‘ };
		char[] chs2 = { ‘a‘, ‘s‘, ‘d‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘l‘ };
		char[] chs3 = { ‘z‘, ‘x‘, ‘c‘, ‘v‘, ‘b‘, ‘n‘, ‘m‘ };
		boolean flag1 = false;
		boolean flag2 = false;
		boolean flag3 = false;
		java.util.LinkedList<String> result = new java.util.LinkedList<String>();
		int j = 0;
		for (int i = 0; i < chs1.length; i++) {
			set1.add(chs1[i]);
		}
		for (int i = 0; i < chs2.length; i++) {
			set2.add(chs2[i]);
		}
		for (int i = 0; i < chs3.length; i++) {
			set3.add(chs3[i]);
		}
		for (int i = 0; i < words.length; i++) {
			String current = words[i].toLowerCase();
			flag1 = false;
			flag2 = false;
			flag3 = false;
			for (j = 0; j < current.length(); j++) {
				char ch = current.charAt(j);
				if (set1.contains(ch)) {
					flag1 = true;
				} else if (set2.contains(ch)) {
					flag2 = true;
				} else if (set3.contains(ch)) {
					flag3 = true;
				}

				if ((flag1 && !flag2 && !flag3) || (!flag1 && flag2 && !flag3) || (!flag1 && !flag2 && flag3)) {
					continue;
				} else {
					break;
				}
			}
			if (j == current.length()) {
				if ((flag1 && !flag2 && !flag3) || (!flag1 && flag2 && !flag3) || (!flag1 && !flag2 && flag3)) {
					result.add(words[i]);
				}
			}
		}
		return result.toArray(new String[0]);
	}

	@org.junit.Test
	public void test() {
		String[] words = { "Hello", "Alaska", "Dad", "Peace" };
		print_arr(findWords(words));
	}
}

原文地址:https://www.cnblogs.com/denggelin/p/12120570.html

时间: 2024-08-26 17:15:25

LeetCode_500. Keyboard Row的相关文章

Keyboard Row

1. Title500. Keyboard Row2. Http addresshttps://leetcode.com/problems/keyboard-row/?tab=Description3. The question 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 ima

Leetcode#500. Keyboard Row(键盘行)

题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 注意: 你可以重复使用键盘上同一字符. 你可以假设输入的字符串将只包含字母. 思路 把键盘中的字母和其所在行数放到map中,然后比较一个字符串中是否都来自一行.

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. Example 1: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["A

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 like the image below. Example 1: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["A

[LeetCode] 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. Example 1: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["A

leetcode算法: 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"]

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 like the image below. Example 1: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["A

3.Keyboard Row

leetcode地址:https://leetcode.com/problems/keyboard-row/description/ 题意:给你一个字符串数组,输出一个新的数组,这个新的数组中的每一个字符串所有的字符属于键盘上的同一行. 1 public String[] findWords(String[] words) { 2 String[] tmp = new String[]{"qwertyuiop","asdfghjkl","zxcvbnm&q

18.Keyboard Row

题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 判断一棵二叉树是否平衡二叉树. 判断平衡二叉树,只需计算它