当前位置:网站首页>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]
}
}
边栏推荐
- A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
- Super detailed pycharm tutorial
- Use of Baidu map
- Summary of main account information of zhengdaliu 4
- Domestic all Chinese automatic test software apifox
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
- 从数组中找出和为目标的下标
- 6.30年终小结,学生时代结束
- 初学爬虫-笔趣阁爬虫
- 农业生态领域智能机器人的应用
猜你喜欢
idea自动导包和自动删包设置
Virtual machine installation deepin system
How to write a client-side technical solution
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Save the CDA from the disc to the computer
Mapping location after kotlin confusion
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Here comes the chicken soup! Keep this quick guide for data analysts
Hcip day 17
Analyzing the hands-on building tutorial in children's programming
随机推荐
Gin framework learning code
Learn AI safety monitoring project from zero [attach detailed code]
[bus interface] Axi interface
js面试收藏试题1
[high speed bus] Introduction to jesd204b
[common error] the DDR type of FPGA device is selected incorrectly
Mathematical knowledge (Euler function)
Pit encountered in win11 pytorch GPU installation
Mysql重点难题(2)汇总
Lm09 Fisher inverse transform inversion mesh strategy
国产全中文-自动化测试软件Apifox
Lay the foundation for children's programming to become a basic discipline
正大美欧4的主账户关注什么数据?
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
Leetcode- insert and sort the linked list
Pytest learning ----- pytest assertion of interface automation testing
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
洛谷入门3【循环结构】题单题解
js中的Map(含leetcode例题)
win10 磁盘管理 压缩卷 无法启动问题