当前位置:网站首页>2021-12-19: find the missing numbers in all arrays. Give you an n
2021-12-19: find the missing numbers in all arrays. Give you an n
2022-06-23 22:06:00 【Fuda scaffold constructor's daily question】
2021-12-19: Find all the missing numbers in the array .
Here's one for you n Array of integers nums , among numsi In the interval 1, n Inside . Please find out all in 1, n Range but not in nums Number in , And return the result in the form of array .
Advanced : You can use no extra space and the time complexity is O(n) Solve this problem in the case of ? You can assume that the returned array is not included in the extra space .
Power button 448.
answer 2021-12-19:
Subscript loop . strive for i Place i+1.
Time complexity :O(N).
Extra space complexity :O(1).
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
nums := []int{4, 3, 2, 7, 8, 2, 3, 1}
ret := findDisappearedNumbers(nums)
fmt.Println(ret)
}
func findDisappearedNumbers(nums []int) []int {
ans := make([]int, 0)
if len(nums) == 0 {
return ans
}
N := len(nums)
for i := 0; i < N; i++ {
// from i Set out , Play subscript loop
walk(nums, i)
}
for i := 0; i < N; i++ {
if nums[i] != i+1 {
ans = append(ans, i+1)
}
}
return ans
}
func walk(nums []int, i int) {
for nums[i] != i+1 { // Constantly from i deliver goods
nexti := nums[i] - 1
if nums[nexti] == nexti+1 {
break
}
nums[i], nums[nexti] = nums[nexti], nums[i]
}
}The results are as follows :
边栏推荐
- How to deal with high memory in API gateway how to maintain API gateway
- Basic concepts and common methods of syntactic dependency analysis
- Completely open source and permanently free, this markdown editor is really fragrant!
- Selenium batch query athletes' technical grades
- Ten thousand words! Understand the inheritedwidget local refresh mechanism
- Ffmpeg for audio and video commands
- Find My资讯|苹果可能会推出第二代AirTag,试试伦茨科技Find My方案
- 微信小程序中发送网络请求
- HDLBits-&gt; Circuits-&gt; Arithmetic Circuitd-&gt; 3-bit binary adder
- The transaction code mp83 at the initial level of SAP retail displays a prediction parameter file
猜你喜欢

ACL2022 | MVR:面向开放域检索的多视角文档表征

Selenium批量查询运动员技术等级

数据可视化之:没有西瓜的夏天不叫夏天

实验五 模块、包和库

使用 Provider 改造屎一样的代码,代码量降低了2/3!

嵌入式开发:嵌入式基础——重启和重置的区别

MySQL de duplication query only keeps one latest record

Polar cycle graph and polar fan graph of high order histogram

Smart cockpit SOC competition upgraded, and domestic 7Nm chips ushered in an important breakthrough

智能座舱SoC竞争升级,国产7nm芯片迎来重要突破
随机推荐
Tencent cloud database tdsql elite challenge Q & A (real-time update)
Supplement to fusionui form component
北大、加州伯克利大學等聯合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基於未標記數據的結構化知識的領域自適應文本分類)
Data visualization: summer without watermelon is not summer
What causes the applet SSL certificate to expire? How to solve the problem when the applet SSL certificate expires?
智能座舱SoC竞争升级,国产7nm芯片迎来重要突破
After easydss is configured with domain name / public IP, it will always prompt for troubleshooting problems that do not exist in the service
Experiment 5 module, package and Library
Make it simple. This wave of global topology is quite acceptable!
Installation and use of Minio
Common commands for cleaning up kubernetes cluster resources
嵌入式开发:嵌入式基础——重启和重置的区别
To develop AI face comparison, how to output multiple faces with comparative similarity?
How do API gateways set up dynamic routing? What are the benefits of dynamic routing?
DM sub database and sub table DDL "pessimistic coordination" mode introduction - tidb tool sharing
How to provide value for banks through customer value Bi analysis
Outlook開機自啟+關閉時最小化
Open source C # WPF control library --newbeecoder UI User Guide (II)
Peking University, University of California Berkeley and others jointly | domain adaptive text classification with structured knowledge from unlabeled data (Domain Adaptive Text Classification Based o
The latest research progress of domain generalization from CVPR 2022