当前位置:网站首页>leetcode_1470_2021.10.12
leetcode_1470_2021.10.12
2022-06-24 19:25:00 【programing菜鸟】
法一:这道题目最明显的做法就是重新开辟一个数组,然后将旧数组的数据按照自己的索引拷贝到新数组中去。空间复杂度O(N)。
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> v;
v.resize(2 * n); //开一个2n大小的数组.
for(int i = 0; i < 2 * n; i += 2){
v[i] = nums[i/2];
v[i + 1] = nums[n + i/2];
}
return v;
}
};
这种方法虽然很容易想到,但是需要O(N)的空间复杂度。接下来有两种O(1)时间复杂度的方法。
- 法二:
题目中说nums[i]最大值不超过1000,而1000只需要10个bits就可以表示,int有32个比特位,我们可以利用剩下的22个比特位(准确的说是10个)来存储排好序的数组。然后将数组中的所有数据右移10个bits即可。
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
for(int i = 0; i < 2 * n; i += 2){
nums[i] |= (nums[i/2] & 0x3ff) << 10;
nums[i + 1] |= nums[n + i/2] << 10;
}
for(int& e : nums){
e >>= 10;
}
return nums;
}
};
这里的细节就是nums[i/2] & -x3ff,因为nums[i/2]可能的10-19位可能已经被改变了,如果不屏蔽掉直接左移就会出问题。
法三:
利用每个nums[i] > 0的条件,我们使用负数来标记。标记什么呢?标记nums[i]是否在排序后的位置上。如果在那么将nums[i]变成 - nums[i]; 如果不在那么就
调整nums[i];
我们先计算出nums[i]应该在的位置j,然后交换nums[i]和nums[j]; 此时
nums[j]在正确的位置上,我们用负数标记; 但是此时nums[i]仍然可能不在
正确的位置,然而此时我们可以利用j(nums[i]本来处在的位置)计算出新的
nums[i]正确的位置,然后再次交换,直到nums[i]也被换成负数。这样循环一
遍,直到所有数字都在正确的位置。
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
for(int i = 0; i < 2 * n; ++i){
if(nums[i] > 0){
int j = i; //j是nums[i]检索,j通过i算出;
while(nums[i] > 0){
j = j < n ? 2 * j : (j - n) * 2 + 1; //每次循环就通过j算出新的索引位置
swap(nums[i], nums[j]);
nums[j] = -nums[j]; //nums[j]已经在正确的位置上,标记为负数。
}
}
}
for(auto& e : nums){
e = -e;
}
return nums;
}
};
边栏推荐
- Call process of package receiving function
- Volcano成Spark默认batch调度器
- [product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial
- (to be optimized and modified) vivado DDR4 SDRAM (MIG) (2.2) IP core learning record
- OSI and tcp/ip model
- Auto. JS to realize automatic unlocking screen
- Handwritten RPC the next day -- review of some knowledge
- Oauth2.0 introduction
- Failed to open after installing Charles without any prompt
- 福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
猜你喜欢

Dynamic routing protocol rip, OSPF

Memcached comprehensive analysis – 2 Understand memcached memory storage

Memcached full profiling – 1 Fundamentals of memcached

使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北

Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached

123. the best time to buy and sell shares III

福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
Visit Amazon memorydb and build your own redis memory database

Alibaba cloud schedules tasks and automatically releases them

Page replacement of virtual memory paging mechanism
随机推荐
Volcano becomes spark default batch scheduler
Tso hardware sharding is a header copy problem
Functional analysis of ebpf tracepoint
how to install clustershell
[cloud native learning notes] kubernetes Foundation
基于STM32的物联网下智能化养鱼鱼缸控制控制系统
memcached全面剖析–3. memcached的删除机制和发展方向
PKI notes
VirtualBox virtual machine installation win10 Enterprise Edition
Remove the screen recording reminder (seven cattle cloud demo)
【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程
RFC 793 why to send reset and when to send reset
CondaValueError: The target prefix is the base prefix. Aborting.
TCP Jprobe utilization problem location
【Camera基础(一)】Camera摄像头工作原理及整机架构
关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
Blender's simple skills - array, rotation, array and curve
EditText 控制软键盘出现 搜索
[cloud native learning notes] learn about kubernetes' pod
推荐模型之多任务模型:ESMM、MMOE