当前位置:网站首页>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 .

边栏推荐
- Depth first search - DFS (burst search)
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- MES production equipment manufacturing execution system software
- JS how to convert a string with a delimiter into an n-dimensional array
- When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- Review Net 20th anniversary development and 51aspx growth
- MySQL connection tools
- Subnet division and summary
- The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
猜你喜欢

Force buckle day33

Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application

Review Net 20th anniversary development and 51aspx growth

因子分析怎么计算权重?

Search 2D matrix 2

Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week

Penetration practice vulnhub range Nemesis

Cloud computing - make learning easier

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

Extract the compressed package file and retrieve the password
随机推荐
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
What is web application security testing technology?
Cassette helicopter and alternating electric field magnetic manometer DPC
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Is Alipay wallet convenient to use?
Is it safe to open a securities account? Is there any danger
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
(6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
[CF559E]Gerald and Path
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
Key points on February 15, 2022
主成分之综合竞争力案例分析
Subnet division and summary
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
Glidefast consulting was selected as the elite partner of servicenow in 2022
网上股票开户安全吗?是否可靠?
Opencv map reading test -- error resolution
Calculation of intersection of two line segments