当前位置:网站首页>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 .
边栏推荐
- Sum of three numbers
- Penetration practice vulnhub range Nemesis
- Distributed task queue: Celery usage record
- Common design parameters of solid rocket motor
- 聊聊项目经理最爱使用的工具
- Depth first search - DFS (burst search)
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- Detailed explanation of ArrayList expansion
- A wonderful time to buy and sell stocks
- [acnoi2022] color ball
猜你喜欢
Find all missing numbers in the array
12种数据量纲化处理方式
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Fix the black screen caused by iPhone system failure
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Cassette helicopter and alternating electric field magnetic manometer DPC
Classpath classpath
C# SelfHost WebAPI (2)
PTA year of birth
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out
随机推荐
Blue Bridge Cup real question: score statistics
Check log4j problems using stain analysis
Force buckle day33
What is web application security testing technology?
Operation of cmake under win
Financial judgment questions
Rust language - cargo, crates io
Product service, operation characteristics
Work and leisure suggestions of old programmers
C# SelfHost WebAPI (2)
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
Session layer of csframework, server and client (1)
golang中的select详解
Zabbix报警执行远程命令
To improve the efficiency of office collaboration, trackup may be the best choice
NSI packaging script add file details
Data query language (DQL)
Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
Small exercise -- subnet division and summary
Common design parameters of solid rocket motor