当前位置:网站首页>go实现leetcode旋转数组
go实现leetcode旋转数组
2022-07-02 04:58:00 【Jimmy_jimi】
目标:旋转数组
给你一个数组,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
结果产出:
这里先使用最简单的循环改变,执行结果显示超时,经行优化,k取模以后仍旧超时,考虑应该是操作数组浪费了太多时间。
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
}
使用临时数组,计算出每个元素的位置,然后重新添加到新的数组中去。
这里遇到问题,leetcode一直检测失败,查看逻辑应该没有问题,然后在自己电脑上运行显示没有问题。这是因为他直接检测了nums数组,而不是对返回值经行检测。
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
}
修改以后
执行用时:28 ms, 在所有 Go 提交中击败了26.28%的用户
内存消耗:7.9 MB, 在所有 Go 提交中击败了45.76%的用户
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]
}
}
边栏推荐
- CY7C68013A之keil编译代码
- Markdown edit syntax
- 正大美欧4的主账户关注什么数据?
- Summary of database problems
- Use of Baidu map
- 6.30 year end summary, end of student age
- Save the CDA from the disc to the computer
- Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
- Getting started with pytest ----- confitest Application of PY
- [Yu Yue education] autumn 2021 reference materials of Tongji University
猜你喜欢
How to configure PostgreSQL 12.9 to allow remote connections
cs架构下抓包的几种方法
Knowledge arrangement about steam Education
Cubemx DMA notes
Let genuine SMS pressure measurement open source code
Federal learning: dividing non IID samples according to Dirichlet distribution
Future trend of automated testing ----- self healing technology
Mathematical knowledge (Euler function)
Orthogonal test method and function diagram method for test case design
Several methods of capturing packets under CS framework
随机推荐
Idea automatic package import and automatic package deletion settings
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Change deepin to Alibaba image source
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
Summary of main account information of zhengdaliu 4
CY7C68013A之keil编译代码
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
解决:代理抛出异常错误
Leetcode merge sort linked list
Steam教育的实际问题解决能力
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
Promise all()
oracle 存储过程与job任务设置
Learn BeanShell before you dare to say you know JMeter
Learn what definitelytyped is through the typescript development environment of SAP ui5
Win10 disk management compressed volume cannot be started
10 minute quick start UI automation ----- puppeter
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
Virtual machine installation deepin system