当前位置:网站首页>Interview question 17.04 in leetcode Disappearing numbers and 27 Removing Elements
Interview question 17.04 in leetcode Disappearing numbers and 27 Removing Elements
2022-07-21 21:09:00 【m0_ sixty-three million seven hundred and eighty-four thousand 】
17.04. Array nums Contains from 0 To n All integers of , But one of them is missing . Please write code to find the missing integer . You have a way O(n) Is it finished in time ?
Example 1:
Input :[3,0,1]
Output :2
Example 2:
Input :[9,6,4,2,3,5,7,0,1]
Output :8source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/missing-number-lcci
The time complexity required in the question shall not exceed O(n), Then the realizable method can be : Find out first 0 To n The sum of all integers , Then subtract the array nums The elements in , What you get is the missing integer . The input is the array and the number of array elements .
int missingNumber(int* nums, int numsSize){
int x=numsSize*(numsSize+1)/2;
for(int i=0;i<numsSize;i++)
{
x=x-nums[i];
}
return x;
}
27. Remove elements
Give you an array nums And a value val, You need In situ Remove all values equal to val The elements of , And return the new length of the array after removal .
Don't use extra array space , You have to use only O(1) Additional space and In situ Modify input array .
The order of elements can be changed . You don't need to think about the elements in the array that follow the new length
Example 1:
Input :nums = [3,2,2,3], val = 3
Output :2, nums = [2,2]
explain : Function should return the new length 2, also nums The first two elements in are 2. You don't need to think about the elements in the array that follow the new length . for example , The new length returned by the function is 2 , and nums = [2,2,3,3] or nums = [2,2,0,0], It will also be seen as the right answer .
Example 2:Input :nums = [0,1,2,2,3,0,4,2], val = 2
Output :5, nums = [0,1,4,0,3]
explain : Function should return the new length 5, also nums The first five elements in are 0, 1, 3, 0, 4. Note that these five elements can be in any order . You don't need to think about the elements in the array that follow the new length .source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/remove-element
The question requires modification in place , The space complexity is O(1), Then you can use the double pointer method , Traversal array , If it is equal to val Value , So skip , If it's not equal, stay . Return is not equal to val Number of elements of value .
int removeElement(int* nums, int numsSize, int val){
int j=0;
for(int i=0;i<numsSize;i++)
{
int num=nums[i];
if(num!=val)
{
nums[j]=num;
j++;
}
}
return j;
}
边栏推荐
- 易语言学习笔记(一)
- [vulnerability recurrence] redis unauthorized access to windows+linux utilization method
- Simulate the implementation of string functions and memory functions (strlen, strcpy, strstr, memcpy
- [reverse analysis] simple learning of C language pointer
- Redis(六) - Redis企业实战之商户查询缓存
- Cannot convert from "boost:: shared\u PTR < pcl:: rangeimage >" to "const std:: shared\u PTR < const pcl:: pointcloud < pcl:: pointwit"
- Redis(五) - Redis企业实战之短信登录
- [authority promotion] search ideas and utilization methods of raising rights exp
- 基于Merkle Tree为NFT添加白名单功能(翻译)
- PawningShop:一种NFT抵押借贷的实现
猜你喜欢
H5 online chat room
Easy language learning notes (4) -- JS decryption, graphic verification code, slider, fishbone multithreading
Buuctf (WEB class)
Write a sushi MasterChef in Solana
TP5 import Excel to database
File operation (C language)
Méthode d'autorisation MSSQL
易语言学习笔记(三)
How to match multiple point clouds step by step
PCL runtime ucrtbased Exception thrown by DLL
随机推荐
并发编程(二十七) - JUC之原子类
Learn MySQL in one day --- learning notes (I)
Redis(二) - Redis安装说明
Attack and defense World Web Zone difficulty level: 2 (upload1, web2, web_php_include, supersqli, warmup)
Fossage 2.0-metaforce force force chain operation race tutorial
Deep understanding of pointers (bubble sorting simulation implements qsort, and function pointers implement callback functions)
Pikachu character injection for Day1 POC and exp learning
[vulnerability recurrence] cve-2022-22954 VMware workspace one access vulnerability analysis
Web security -- File Inclusion (local inclusion, remote inclusion)
Moher college webshell file upload analysis (questions 3-5)
文件操作上(C语言)
[intranet penetration] cobaltstrike traffic encryption
【權限提昇】 MSSQL提權方法
SushiSwap的SushiMaker和SushiBar解读
[reverse analysis] static analysis of malicious code
Solidity中的mapping为何不提供按照key遍历的功能?
Compile and run typescript with vscode plug-in coderunner. When the output has Chinese, there is garbled code
Rust short note: Cargo specifies the dependent version
易语言学习笔记(四)--js解密,图形验证码,滑块,鱼刺多线程
Initial CSDN