当前位置:网站首页>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 .
边栏推荐
- Simple use of leaflet - offline map scheme
- Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
- Matlab: find the inner angle of n-sided concave polygon
- CDN single page reference of indexbar index column in vant framework cannot be displayed normally
- Redis multiple servers share one
- Leetcode 473. Match to square [violence + pruning]
- [data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3
- Word splitting problem
- Pycharm and Anaconda ultra detailed installation and configuration tutorial
- Data processing in detailed machine learning (II) -- Feature Normalization
猜你喜欢

Prometheus安装并注册服务

Superficial understanding of conditional random fields

Ijkplayer source code ---packetqueue

在IDEA使用C3P0連接池連接SQL數據庫後卻不能顯示數據庫內容

Matlab: find the inner angle of n-sided concave polygon

Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx

A real-time target detection model Yolo

OpenCVSharpSample04WinForms

AAR packaging and confusion

02 optimize the default structure of wechat developer tools
随机推荐
The weight of the input and textarea components of the applet is higher than that of the fixed Z-index
Find the number of permutations
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
Opencv 07, pixel read, change and bitmap write
[life science] DNA extraction of basic biological experiments
Modify the color of El input, textarea and El checkbox when they are disabled
Rough understanding of wechat cloud development
Node uses post to request req Pit with empty body
String: number of substring palindromes
redis 多个服务器共用一个
Mp4 playback
OneNote User Guide (1)
专业的数据库管理软件:Valentina Studio Pro for Mac
wx. Createselectorquery() gets the usage of DOM nodes in components
Android lightweight cache processing
04路由跳转并携带参数
House raiding
How did you spend your winter vacation perfectly?
[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3
[data analysis and visualization] key points of data drawing 12- importance of chart notes