当前位置:网站首页>Go implements leetcode rotation array
Go implements leetcode rotation array
2022-07-02 05:00:00 【Jimmy_ jimi】
The goal is : Rotated array
Give you an array , Rotate the elements in the array to the right k A place , among k It is a non negative number .
Result output :
Let's start with the simplest loop change , Execution result display timeout , Optimized by the bank ,k After taking the mold, it still times out , Consider that it should be a waste of time to operate the array .
func rotate(nums []int, k int)[]int {
for i :=0;i<k;i++{
a := nums[(len(nums)-1)]
for j := len(nums)-1;j>0;j--{
nums[j] = nums[j-1]
}
nums[0]=a
}
return nums
}
Use temporary arrays , Calculate the position of each element , Then add it back to the new array .
There's a problem here ,leetcode The detection fails all the time , There should be no problem checking the logic , Then run the display on your computer without any problem . This is because he directly tested nums Array , Instead of line checking the return value .
func rotate(nums []int, k int)[]int {
var nums1 = make([]int,len(nums))
k = k%len(nums)
for j :=0;j<len(nums);j++ {
if j+k<len(nums) {
nums1[j+k]=nums[j]
}else {
nums1[(j+k)%len(nums)]=nums[j]
}
}
return nums1
}
After modification
Execution time :28 ms, In all Go Defeated in submission 26.28% Users of
Memory consumption :7.9 MB, In all Go Defeated in submission 45.76% Users of
func rotate(nums []int, k int){
var nums1 = make([]int,len(nums))
k %= len(nums)
for j :=0;j<len(nums);j++ {
if j+k<len(nums) {
nums1[j+k]=nums[j]
}else {
nums1[(j+k)%len(nums)]=nums[j]
}
}
for j :=0;j<len(nums);j++ {
nums[j] =nums1[j]
}
}
边栏推荐
- Analyze the space occupied by the table according to segments, clusters and pages
- 记录一次Unity 2020.3.31f1的bug
- Getting started with pytest -- description of fixture parameters
- Leetcode- insert and sort the linked list
- Markdown edit syntax
- Solution: the agent throws an exception error
- Learn BeanShell before you dare to say you know JMeter
- Oracle stored procedure and job task setting
- Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
- [understand one article] FD_ Use of set
猜你喜欢

How to recover deleted data in disk

培养中小学生对教育机器人的热爱之心

记录一次Unity 2020.3.31f1的bug

Starting from the classification of database, I understand the map database

MySQL table insert Chinese change? Solution to the problem of No

Record my pytorch installation process and errors

Rhcsa --- work on the third day

Embedded-c language-9-makefile/ structure / Consortium

Federal learning: dividing non IID samples according to Dirichlet distribution

List of common bugs in software testing
随机推荐
農業生態領域智能機器人的應用
fastText文本分类
Steam教育的实际问题解决能力
Mathematical knowledge -- understanding and examples of fast power
从数组中找出和为目标的下标
Here comes the chicken soup! Keep this quick guide for data analysts
将光盘中的cda保存到电脑中
How to modify data file path in DM database
Analyze the space occupied by the table according to segments, clusters and pages
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
6.30 year end summary, end of student age
Introduction to Luogu 3 [circular structure] problem list solution
Gin framework learning code
國產全中文-自動化測試軟件Apifox
How to configure PostgreSQL 12.9 to allow remote connections
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
VMware installation win10 reports an error: operating system not found
js面试收藏试题1
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?