当前位置:网站首页>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 .
边栏推荐
- House raiding
- CV 06 demonstrates backgroundworker
- PCR validation of basic biological experiments in [life sciences]
- Graduation project - campus old thing recycling system based on stm32
- [data analysis and visualization] key points of data drawing 5- the problem of error line
- JS multiple judgment writing
- Professional database management software: Valentina Studio Pro for Mac
- Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
- 遍历数组,删除某元素,直到删除为止
- Using binary heap to implement priority queue
猜你喜欢
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
[data analysis and visualization] key points of data drawing 4- problems of pie chart
[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge
Professional database management software: Valentina Studio Pro for Mac
03 认识第一个view组件
Special topic I of mathematical physics of the sprint strong foundation program
AAR packaging and confusion
Leetcode 926. Flip string to monotonically increasing [prefix and]
專業的數據庫管理軟件:Valentina Studio Pro for Mac
[reading paper] generate confrontation network Gan
随机推荐
How to destroy a fragment- How to destroy Fragment?
04路由跳转并携带参数
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
[reading papers] transformer miscellaneous notes, especially miscellaneous
重定向设置参数-RedirectAttributes
Exam23 named windows and simplified paths, grayscale conversion
Delphi implements adding a column of serial number to the CXGRID list
Data processing in detailed machine learning (II) -- Feature Normalization
[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP
Why does it feel that most papers still use RESNET as the backbone network rather than densenet?
[data analysis and visualization] key points of data drawing 12- importance of chart notes
[data analysis and visualization] key points of data drawing 11- precautions for radar chart
Mp4 playback
Sans certificate generation
[dest0g3 520 orientation] dest0g3, which has been a long time since we got WP_ heap
05 tabbar navigation bar function
[data analysis and visualization] key points of data drawing 10- construction of legend
01 初识微信小程序
[deep learning] fast Reid tutorial
Welcome to blog navigation