当前位置:网站首页>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. ~
边栏推荐
- Recursion and recursion
- Classification and extension of OC
- 33 restrict the input of qlineedit control (verifier)
- Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
- Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
- [untitled]
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Pyqt5 sensitive word detection tool production, operator's Gospel
- C summary of knowledge point definitions, summary notes
猜你喜欢
QT creator source code learning note 05, how does the menu bar realize plug-in?
webAssembly
Pat grade A - 1164 good in C (20 points)
2 spark environment setup local
Flutter internationalized Intl
A preliminary study on the middleware of script Downloader
Harbor integrated LDAP authentication
2022.02.13
Fluent learning (4) listview
Gorilla/mux framework (RK boot): add tracing Middleware
随机推荐
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
STM32 multi serial port implementation of printf -- Based on cubemx
Buuctf, misc: n solutions
Programming language (1)
Learning notes of raspberry pie 4B - IO communication (SPI)
Can you draw with turtle?
Sort merge sort
ThreadLocal function, scene and principle
Take you to master the formatter of visual studio code
Fluent learning (4) listview
Simple solution of m3u8 file format
540. Single element in ordered array
1 Introduction to spark Foundation
Why should enterprises do more application activities?
What are the common computer problems and solutions
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
炒股开户佣金优惠怎么才能获得,网上开户安全吗
SDMU OJ#P19. Stock trading
Qtoolbutton available signal
Opengauss database log management guide