当前位置:网站首页>Hash table: whether alien languages are sorted
Hash table: whether alien languages are sorted
2022-06-13 02:45:00 【Zeng Qiang】
subject
Hashtable : Whether alien languages are sorted
https://leetcode-cn.com/problems/lwyVBB/
Their thinking
1. Two conditions for judging word order
Suppose there are two words ,word1 and word2.
word1 and word2 When the letters are not equal for the first time ,word1[i] < word2[i], So these two words are ordered , Otherwise it is disordered .
If two words have equal letters , The length of the preceding word is greater than that of the following word , So these two words are not ordered .
2. Save the index reference hash table indicating the size of letters .
We use the keys of the hash table to save the letters of the words , The value of the hash table holds the sequence number of the letters of the word .
The smaller the serial number , Indicates that the smaller the letter .
The length of the hash table is 26, because order The sequence string represents at most 26 Letters .
3. Compare letter sizes
From the hash table according to the key ( Letter -‘a’), You can get the size of letters directly .
The specific code is as follows :
Code
class Solution {
public boolean isAlienSorted(String[] words, String order) {
int[] orderArray = new int[order.length()];
for(int i = 0; i < order.length(); i++) {
orderArray[order.charAt(i) - 'a'] = i;
}
for(int i = 0; i < words.length - 1; i++) {
String w1 = words[i];
String w2 = words[i+1];
boolean isSorted = isSorted(w1, w2, orderArray);
if(!isSorted){
return false;
}
}
return true;
}
private boolean isSorted(String word1, String word2, int[] order) {
int i = 0;
for(; i < word1.length() && i < word2.length(); ++i) {
char char1 = word1.charAt(i);
char char2 = word2.charAt(i);
if(order[char1 - 'a'] < order[char2 - 'a']) {
return true;
}
if(order[char1 - 'a'] > order[char2 - 'a']) {
return false;
}
}
return i == word1.length();
}
}
summary
This topic examines the application of hash tables . Determine letter size , You can use hash tables / The array stores the letters and their sizes .
边栏推荐
- Opencv 08 demonstrates the effect of opening and closing operations of erode, dilate and morphological function morphologyex.
- OneNote User Guide (1)
- Surpass the strongest variant of RESNET! Google proposes a new convolution + attention network: coatnet, with an accuracy of 89.77%!
- Redis multiple servers share one
- [deep learning] fast Reid tutorial
- Introduction to facial expression recognition system - Technical Paper Edition
- [data analysis and visualization] key points of data drawing 11- precautions for radar chart
- Queuing theory, game theory, analytic hierarchy process
- How to select fund products? What kind of fund is a good fund?
- Find the number of permutations
猜你喜欢

Principle and steps of principal component analysis (PCA)

OneNote使用指南(一)

OneNote User Guide (1)

Summary of innovative ideas of transformer model in CV

Graduation project - campus old thing recycling system based on stm32

在IDEA使用C3P0連接池連接SQL數據庫後卻不能顯示數據庫內容
![[data analysis and visualization] key points of data drawing 8- use of circular bar chart](/img/1f/232f2f0134867eeec3f1cfe005b2b5.jpg)
[data analysis and visualization] key points of data drawing 8- use of circular bar chart

Use of OpenCV 12 findcircuits and drawcircuits
![[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP](/img/4e/a5c6b1a8880209f89d6bf252ff889a.jpg)
[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP

OpenCVSharpSample04WinForms
随机推荐
Principle and steps of principal component analysis (PCA)
[data analysis and visualization] key points of data drawing 6- too many data groups
[data and Analysis Visualization] data operation in D3 tutorial 3-d3
Implementing fillet in custom dialog
Ijkplayer source code ---setdatasource
[reading papers] deep learning face representation from predicting 10000 classes. deepID
wx.createSelectorQuery()在components获取Dom节点的使用
Data warehouse notes | 5 factors that need attention for customer dimension modeling
SANs证书生成
Opencv 9 resize size change rotate rotate blur mean (blur)
CV 06 demonstrates backgroundworker
Opencvsharp4 pixel read / write and memory structure of color image and gray image
04 route jump and carry parameters
MySQL index
02 optimize the default structure of wechat developer tools
遍历数组,删除某元素,直到删除为止
05 tabbar navigation bar function
Retrofit easy to use
Prometheus安装并注册服务
[common tools] pyautogui tutorial