LeetCode(Map)804. Unique Morse Code Words

问题

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:

‘a’ maps to “.-”, ‘b’ maps to “-…”, ‘c’ maps to “-.-.”, and so on. For convenience, the full table for the 26 letters of the English alphabet is given below:

[“.-”,“-…”,“-.-.”,“-…”,“.”,“…-.”,“–.”,“…”,“…”,“.—”,“-.-”,“.-…”,“–”,“-.”,“—”,“.–.”,“–.-”,“.-.”,“…”,“-”,“…-”,“…-”,“.–”,“-…-”,“-.–”,“–…”] Given an array of strings words where each word can be written as a concatenation of the Morse code of each letter.

For example, “cab” can be written as “-.-…–…”, which is the concatenation of “-.-.”, “.-”, and “-…”. We will call such a concatenation the transformation of a word. Return the number of different transformations among all words we have.

Example 1:

Input: words = [“gin”,“zen”,“gig”,“msg”]

Output: 2

Explanation: The transformation of each word is:

“gin” -> “–…-.”

“zen” -> “–…-.”

“gig” -> “–…–.”

“msg” -> “–…–.”

There are 2 different transformations: “–…-.” and “–…–.”.

Example 2:

Input: words = [“a”]

Output: 1

Constraints:

1 <= words.length <= 100 1 <= words[i].length <= 12

代码

class Solution {

public int uniqueMorseRepresentations(String[] words) {

String[] tokens = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};

Set set = new HashSet<>();//1.使用Hashset是因为没有重复元素

for ( String word : words ) {//2.遍历数组

StringBuilder sb = new StringBuilder();//使用 StringBuilder是可以添加不限长度的元素

for ( int i = 0; i < word.length(); i++ ) {//ASC2码小写字母位置是连续的,所以 ‘字母’-’a’=字母和a的距离,此题就对应morse数组中对应字母的索引,并添加到StringBUilder

sb.append( tokens[word.charAt(i)-'a'] );//int index=c-'a';sb.append(morseCode[index]);

}

set.add( sb.toString() );//转换成String添加到hashset

}

return set.size();//3.求hashset的size也就是不重复的元素个数

}

}

public int uniqueMorseRepresentations(String[] words) {

String[] arr = new String[]{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

Set set = new HashSet<>();

for (String s : words) {

StringBuilder sb = new StringBuilder();

for (char ch : s.toCharArray()) sb.append(arr[ch - 'a']);//ASC2码小写字母位置是连续的,所以 ‘字母’-’a’=字母和a的距离,此题就对应morse数组中对应字母的索引

set.add(sb.toString());

}

return set.size();

}

LeetCode(Map)2006. Count Number of Pairs With Absolute Difference K

问题

Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.

The value of |x| is defined as:

x if x >= 0. -x if x < 0.

Example 1:

Input: nums = [1,2,2,1], k = 1

Output: 4

Explanation: The pairs with an absolute difference of 1 are:

[1,2,2,1]

[1,2,2,1]

[1,2,2,1]

[1,2,2,1]

Example 2:

Input: nums = [1,3], k = 3

Output: 0

Explanation: There are no pairs with an absolute difference of 3.

Example 3:

Input: nums = [3,2,1,5,4], k = 2

Output: 3

Explanation: The pairs with an absolute difference of 2 are:

[3,2,1,5,4]

[3,2,1,5,4]

[3,2,1,5,4]

Constraints:

1 <= nums.length <= 200 1 <= nums[i] <= 100 1 <= k <= 99

代码

class Solution {

public int countKDifference(int[] nums, int k) {

int result =0;//1.定义result初始为0

for (int i = 0; i < nums.length; i++) {//2.for循环,两数相减取绝对值,如果等于k,result基础上加1

for(int j=i+1; j

if(Math.abs(nums[i]-nums[j])==k) result+=1;

// if(nums[i]-nums[j]==k) result+=1;

// else if( nums[i]-nums[j]==-k) result+=1;

}

}

return result;//3.返回result结果

}

}

class Solution {

public int countKDifference(int[] nums, int k) {

Map map = new HashMap<>();//1.新建hashmap,定义result初始为0

int result = 0;

for(int i = 0;i< nums.length;i++){//2.for循环遍历

if(map.containsKey(nums[i]-k)){//map中是否有key减去k,说明有和其相减的数值,如果有,map.get(nums[i]-k)为1,也就是在result基础上加1

result += map.get(nums[i]-k);

}

if(map.containsKey(nums[i]+k)){//map中是否有key加k,说明有和其相减的数值,如果有,map.get(nums[i]+k)为1,也就是在result基础上加1

result += map.get(nums[i]+k);

}

map.put(nums[i],map.getOrDefault(nums[i],0)+1);//map中没有元素,map将数组元素作为key放入map,默认值为0,+1是为reusult加1做铺垫

}

return result;//3.返回reuslt

}

}

LeetCode(Map)1684. Count the Number of Consistent Strings

问题

You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.

Return the number of consistent strings in the array words.

Example 1:

Input: allowed = “ab”, words = [“ad”,“bd”,“aaab”,“baa”,“badab”]

Output: 2

Explanation: Strings “aaab” and “baa” are consistent since they only contain characters ‘a’ and ‘b’.

Example 2:

Input: allowed = “abc”, words = [“a”,“b”,“c”,“ab”,“ac”,“bc”,“abc”]

Output: 7

Explanation: All strings are consistent.

Example 3:

Input: allowed = “cad”, words = [“cc”,“acd”,“b”,“ba”,“bac”,“bad”,“ac”,“d”]

Output: 4

Explanation: Strings “cc”, “acd”, “ac”, and “d” are consistent.

Constraints:

1 <= words.length <= 104 1 <= allowed.length <= 26 1 <= words[i].length <= 10 The characters in allowed are distinct. words[i] and allowed contain only lowercase English letters.

代码

class Solution {

public int countConsistentStrings(String allowed, String[] words) {

int result =0;//1.定义result初始为0

Set set = new HashSet<>();//2.新建一个hashset,将allowed.toCharArray()存入set中

for(char a: allowed.toCharArray()){

set.add(a);

}

for(String w:words){//3.for循环遍历words,如果set包含words中char数组元素为ture,result+1,否者为false,result不变

boolean flag = true;

for(char c: w.toCharArray()){

if(!set.contains(c)){//注意:这是set.contains(c)是charter.contains(charter)

flag=false;

}

}

if(flag) result+=1;

}

return result;//4.返回result

}

}

LeetCode(Map)1832. Check if the Sentence Is Pangram

问题

A pangram is a sentence where every letter of the English alphabet appears at least once.

Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.

Example 1:

Input: sentence = “thequickbrownfoxjumpsoverthelazydog”

Output: true

Explanation: sentence contains at least one of every letter of the English alphabet.

Example 2:

Input: sentence = “leetcode”

Output: false

Constraints:

1 <= sentence.length <= 1000 sentence consists of lowercase English letters.

代码

class Solution {

public boolean checkIfPangram(String sentence) {

Set set = new HashSet<>();//1.利用set内没有重复元素

for(char c : sentence.toCharArray()) set.add(c);//2.将sentence存入set,如果长度等于26为true,否则为false

return set.size() == 26;

}

}

LeetCode(Map)2367. Number of Arithmetic Triplets

问题

You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met:

i < j < k, nums[j] - nums[i] == diff, and nums[k] - nums[j] == diff. Return the number of unique arithmetic triplets.

Example 1:

Input: nums = [0,1,4,6,7,10], diff = 3

Output: 2

Explanation:

(1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.

(2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.

Example 2:

Input: nums = [4,5,6,7,8,9], diff = 2

Output: 2

Explanation:

(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.

(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.

Constraints:

3 <= nums.length <= 200 0 <= nums[i] <= 200 1 <= diff <= 50 nums is strictly increasing.

代码

class Solution {

public int arithmeticTriplets(int[] nums, int diff) {

int result = 0;

Set set = new HashSet<>();//1.新建一个HashSet为set,通过for循环遍历数组添加到set中

for(int n:nums){

set.add(n);

}

for(int s:nums){//2.for循环遍历,set是否包含nums数组元素,元素+diff,元素+2*diff,如果有result加1

if(set.contains(s+diff)&&set.contains(s+2*diff)){

result+=1;

}

}

return result;//3.返回result

}

}

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: