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

边栏推荐
- Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
- Roll out! Enlightenment!
- Yuancosmos game farmersworld farmers world - core content of the second conference in China!
- Work and leisure suggestions of old programmers
- [CF559E]Gerald and Path
- ZABBIX alarm execute remote command
- An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
- Blue Bridge Cup real question: score statistics
- Draw drawing process of UI drawing process
- 因子分析怎么计算权重?
猜你喜欢

Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
![[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out](/img/f3/20b73f3545cdd17b9fbc52bf493ab4.jpg)
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out

MySQL connection tools

How to retrieve the password for opening Excel files

NSI packaging script add file details

540. Single element in ordered array

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

Oracle TRUNC function processing date format

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

The method of real-time tracking the current price of London Silver
随机推荐
Terms related to K line
Redis主从实现10秒检查与恢复
Penetration practice vulnhub range Nemesis
Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Mysql database design
D. Yet Another Minimization Problem
Three dimensional anti-terrorism Simulation Drill deduction training system software
Database - MySQL advanced SQL statement (I)
Apache iceberg source code analysis: schema evolution
Smart factory digital management system software platform
Happy new year | 202112 monthly summary
Fix the black screen caused by iPhone system failure
PCL learning materials
transform. Forward and vector3 Differences in the use of forward
Zabbix报警执行远程命令
MES production equipment manufacturing execution system software
Small exercise -- subnet division and summary
APK签名流程介绍[通俗易懂]
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out