当前位置:网站首页>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 :
边栏推荐
- Using barcode software to make certificates
- How to use zero to build a computer room
- Take you through the distributed file system
- What causes the applet SSL certificate to expire? How to solve the problem when the applet SSL certificate expires?
- How to build an API gateway and how to maintain an API gateway?
- How to defend the security importance of API gateway
- Leetcode must review six lintcode (28348455116385)
- Detailed explanation of logical structure, physical structure and data operation
- Like playing a game? Take it and use it to build the park scene
- v-chart
猜你喜欢
Performance optimization of database 5- database, table and data migration

北大、加州伯克利大學等聯合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基於未標記數據的結構化知識的領域自適應文本分類)

Embedded development: embedded foundation -- the difference between restart and reset

微信小程序中发送网络请求

Introduction to scikit learn machine learning practice

Code implementation of CAD drawing online web measurement tool (measuring distance, area, angle, etc.)

Outlook開機自啟+關閉時最小化

CAD图在线Web测量工具代码实现(测量距离、面积、角度等)

Using the provider to transform the shit like code, the amount of code is reduced by 2/3!

Sending network request in wechat applet
随机推荐
ACL2022 | MVR:面向开放域检索的多视角文档表征
How to build an API gateway and how to maintain an API gateway?
Question: how to understand the network protocol and why the OSI reference model is divided into seven layers
KnowDA: All-in-One Knowledge Mixture Model for Data Augmentation in Few-Shot NLP(KnowDA:用于 Few-Shot NLP 中数据增强的多合一知识混合模型)
Find My资讯|苹果可能会推出第二代AirTag,试试伦茨科技Find My方案
What do you know about the 15 entry-level applets
【Proteus仿真】LCD1602+DS1307按键设置简易时钟
Find my information | Apple may launch the second generation airtag. Try the Lenz technology find my solution
Improve efficiency, take you to batch generate 100 ID photos with QR code
API gateway verification token the role of adding a new authentication token in API gateway
智能座舱SoC竞争升级,国产7nm芯片迎来重要突破
v-chart
BenchCLAMP:评估语义分析语言模型的基准
How to use the serial port assistant in STC ISP?
What is API gateway link tracking? What is the function of the line monitoring tool?
Lighthouse open source application practice: snipe it
Surprise! Edge computing will replace cloud computing??
Bluetooth chip | Renesas and Ti launch new Bluetooth chip, try Lenz st17h65 Bluetooth ble5.2 chip
Outlook开机自启+关闭时最小化
How to calculate individual income tax? You know what?