当前位置:网站首页>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]
}
}
边栏推荐
- 6.30 year end summary, end of student age
- Introduction to Luogu 3 [circular structure] problem list solution
- Video cover image setting, put cover images into multiple videos in the simplest way
- Steam教育的实际问题解决能力
- Comp 250 parsing
- Mysql重点难题(2)汇总
- [quick view opencv] familiar with CV matrix operation with image splicing examples (3)
- 数学问题(数论)试除法做质数的判断、分解质因数,筛质数
- Federal learning: dividing non IID samples according to Dirichlet distribution
- National all Chinese Automatic Test Software apifox
猜你喜欢

DMA Porter

Idea automatic package import and automatic package deletion settings

记录一次Unity 2020.3.31f1的bug

Mathematical knowledge -- understanding and examples of fast power

Win10 disk management compressed volume cannot be started

Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers

Mathematical knowledge (Euler function)

2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas

Rhcsa --- work on the third day

Getting started with pytest ----- confitest Application of PY
随机推荐
Beginner crawler - biqu Pavilion crawler
What data does the main account of Zhengda Meiou 4 pay attention to?
国产全中文-自动化测试软件Apifox
Analyze the space occupied by the table according to segments, clusters and pages
C# 图片显示占用问题
Super detailed pycharm tutorial
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
[bus interface] Axi interface
2022-003arts: recursive routine of binary tree
How to modify data file path in DM database
Record my pytorch installation process and errors
leetcode存在重复元素go实现
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
Video multiple effects production, fade in effect and border background are added at the same time
删除排序数组中的重复项go语言实现
DMA Porter
Learn BeanShell before you dare to say you know JMeter
初学爬虫-笔趣阁爬虫
ansible安装与使用
Basic differences between Oracle and MySQL (entry level)