当前位置:网站首页>Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
2022-07-03 23:11:00 【Rong AI holiday】
The finger of the sword Offer The first 4 God Search algorithm ( Simple )
The finger of the sword Offer 03. Repeated numbers in an array
Title Description
Find the repeated numbers in the array .
At a length of n Array of nums All the numbers in 0~n-1 Within the scope of . Some numbers in the array are repeated , But I don't know how many numbers are repeated , I don't know how many times each number has been repeated . Please find any duplicate number in the array .
Example 1
Input :
[2, 3, 1, 0, 2, 5, 3]
Output :2 or 3
Be careful
When no result is returned , Need to return null value
Code
c++ edition
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
int n = nums.size();
map<int,int> mp;
int t = -1;
for(int i=0;i<n;i++){
if(mp[nums[i]]){
t = nums[i];
break;
}else{
mp[nums[i]]=1;
}
}
if(t!=-1)return t;
else{
return {
{
}};
}
}
};
The finger of the sword Offer 53 - I. Look up numbers in the sort array I
Title Description
Count the number of times a number appears in the sort array .
Example 1
Input : nums = [5,7,7,8,8,10], target = 8
Output : 2
Example 2
Input : nums = [5,7,7,8,8,10], target = 6
Output : 0
Code
c++ edition
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
int f = 0;
for(int i=0;i<n;i++){
if(nums[i]==target){
f++;
}
}
return f;
}
};
python edition
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
f = 0
for i in range(n):
if nums[i]==target :
f = f+1
return f
The finger of the sword Offer 53 - II. 0~n-1 Missing numbers in
Title Description
A length of n-1 All numbers in the incremental sort array of are unique , And every number is in the range 0~n-1 within . In scope 0~n-1 Internal n There are and only one number is not in the array , Please find out the number .
Example 1
Input : [0,1,3]
Output : 2
Example 2
Input : [0,1,2,3,4,5,6,7,9]
Output : 8
Code
c++ edition
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int f = -1;
int vis[10010]={
0};
for(int i=0;i<n;i++){
vis[nums[i]] = 1;
}
//return n;
for(int i=0;i<=n;i++){
if(!vis[i]){
f = i;
}
}
return f;
}
};
python edition
class Solution:
def missingNumber(self, nums: List[int]) -> int:
vis = [0]*10010
n = len(nums)
for i in range(n):
vis[nums[i]] = 1
for i in range(n+1):
if not vis[i] :
return i
If this article is helpful to my friends , I hope you can give me some praise and support ~ Thank you very much. ~
边栏推荐
- Creation of the template of the password management software keepassdx
- Day30-t540-2022-02-14-don't answer by yourself
- Unity shader visualizer shader graph
- Recursion and recursion
- Fluent learning (5) GridView
- Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
- 540. Single element in ordered array
- Pyqt5 sensitive word detection tool production, operator's Gospel
- Comparable interface and comparator interface
- Learning methods of zynq
猜你喜欢
Es6~es12 knowledge sorting and summary
SDMU OJ#P19. Stock trading
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Qtoolbutton - menu and popup mode
Fluent learning (5) GridView
In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
A preliminary study on the middleware of script Downloader
Hcip day 15 notes
Shell script three swordsman awk
随机推荐
In 2022, 6G development has indeed warmed up
Blue Bridge Cup -- guess age
How the computer flushes the local DNS cache
pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
AST (Abstract Syntax Tree)
Errors taken 1 Position1 argument but 2 were given in Mockingbird
Quick one click batch adding video text watermark and modifying video size simple tutorial
Programming language (1)
Fluent learning (4) listview
Current detection circuit - including op amp current scheme
Ppt image processing
ADB related commands
Format cluster and start cluster
Live app source code, jump to links outside the station or jump to pages inside the platform
Text replacement demo
在恒泰证券开户怎么样?安全吗?
2022.02.14
2.14 summary
1 Introduction to spark Foundation
Loop compensation - explanation and calculation of first-order, second-order and op amp compensation