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

边栏推荐
- Is it safe to open a securities account? Is there any danger
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- Relationship between sensor size, pixel, dpi resolution, inch and millimeter
- Subnet division and summary
- [noip2015] jumping stone
- Talk about the favorite tools used by project managers
- How to retrieve the password for opening Excel files
- Data query language (DQL)
- Penetration practice vulnhub range Nemesis
- Blue Bridge Cup real problem: word analysis
猜你喜欢

Yuancosmos game farmersworld farmers world - core content of the second conference in China!
![[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

Rotation order and universal lock of unity panel

Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day

Localization through custom services in the shuttle application

Cassette helicopter and alternating electric field magnetic manometer DPC

Classpath classpath

Bernoulli distribution (a discrete distribution)

Penetration practice vulnhub range Keyring

Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
随机推荐
Batch export all pictures in PPT in one second
Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?
Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
PTA year of birth
On the language internationalization of Yongzhong Office
必看,时间序列分析
Session layer of csframework, server and client (1)
Rotation order and universal lock of unity panel
Search 2D matrix 2
Localization through custom services in the shuttle application
The latest intelligent factory MES management system software solution
Database - MySQL advanced SQL statement (I)
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
[CF559E]Gerald and Path
Find all missing numbers in the array
Product service, operation characteristics
Irradiance, Joule energy, exercise habits
Basic usage of shell script
Talk about the favorite tools used by project managers
Zabbix报警执行远程命令