当前位置:网站首页>540. Single element in ordered array / 1684 Count the number of consistent strings
540. Single element in ordered array / 1684 Count the number of consistent strings
2022-07-01 18:35:00 【PI Qiliang】
540. A single element in an ordered array 【 Medium question 】【 A daily topic 】
Ideas :
- I compare dishes , When writing, I didn't expect how logn Complexity , Just look straight from front to back , My complexity is n, Problem solving binary search is very good , To study the .
- Define the target number cur, The initial value defaults to the first element value , Define count variables cnt, The initial value is 0.
- Traverse nums, If cnt Less than 2, Then it means that this number pair has not been counted , Then if the current number is equal to cur, that cnt++, If not equal to cur, So it means that cur The number has no matching number ( because nums Is ordered , The current element is no longer equal to cur 了 , Then the latter is more unlikely to equal cur); If cnt Not less than 2, Then it must be at least equal to 2 Of , Then it will be explained at this time cur Its matching number has been found , So will cur Update to current element ,cnt Updated to 1.
- In special cases, when nums The length is 1 when , Then it's impossible to for The answer is returned inside the loop , So you should return externally at this time nums The value of this unique element in .
Code :
class Solution {
public int singleNonDuplicate(int[] nums) {
int cur = nums[0],cnt = 0;
for (int num : nums) {
if (cnt<2){
if (num == cur){
cnt++;
}else {
return cur;
}
}else {
cur = num;
cnt = 1;
}
}
return cur;
}
}
when :
when 1ms, If the time complexity does not meet the requirements, it will not be posted .
1684. Count the number of consistent strings 【 Simple questions 】
Ideas :
- First define a hash set take allowed The characters are stored , Then define int Type variable ans, The initial value is 0.
- Traversal string array words Every string of word, Traverse the current word Every character of , If the current character is in set Does not exist in the , So similar flag bit flag( The default is true) Set as false And exit the character cycle of the current string . If flag by true, Then the current string word And allowed Strings are similar , that ans++.
- Finally back to ans that will do .
Code :
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
Set<Character> set = new HashSet<>();
for (char ch : allowed.toCharArray()){
set.add(ch);
}
int ans = 0;
for (String word : words){
boolean flag = true;
for (char ch : word.toCharArray()) {
if (!set.contains(ch)){
flag = false;
break;
}
}
if (flag){
ans++;
}
}
return ans;
}
}
when :
At present, there is no official solution , Time is not fast either , Make do with it .

边栏推荐
- Search 2D matrix 2
- Find all missing numbers in the array
- A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
- The method of real-time tracking the current price of London Silver
- Explain in detail the process of realizing Chinese text classification by CNN
- Data warehouse (3) star model and dimension modeling of data warehouse modeling
- Sanfeng cloud 0215 I often use
- (6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
- Terms related to K line
- Work and leisure suggestions of old programmers
猜你喜欢

Mysql database design

Blue Bridge Cup real topic: the shortest circuit

Cassette helicopter and alternating electric field magnetic manometer DPC

Mujoco model learning record

必看,时间序列分析

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes

Common design parameters of solid rocket motor

Rotation order and universal lock of unity panel

Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
随机推荐
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
MySQL connection tools
The latest intelligent factory MES management system software solution
Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
Penetration practice vulnhub range Nemesis
The method of real-time tracking the current price of London Silver
Mujoco's biped robot Darwin model
SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
Subnet division and summary
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Talk about the favorite tools used by project managers
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
Opencv map reading test -- error resolution
Mujoco model learning record
Detailed explanation of ArrayList expansion
Definition of rotation axis in mujoco
js如何将带有分割符的字符串转化成一个n维数组
Draw drawing process of UI drawing process
Three dimensional anti-terrorism Simulation Drill deduction training system software
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes