当前位置:网站首页>leetcode_ 1470_ 2021.10.12
leetcode_ 1470_ 2021.10.12
2022-06-24 21:53:00 【Programming rookie】
leetcode1470, Rearrange the array .
Law 1 : The most obvious way to solve this problem is to open up an array again , Then copy the data of the old array to the new array according to its own index . Spatial complexity O(N).
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> v;
v.resize(2 * n); // Open one 2n Array of sizes .
for(int i = 0; i < 2 * n; i += 2){
v[i] = nums[i/2];
v[i + 1] = nums[n + i/2];
}
return v;
}
};
Although this method is easy to think of , But need O(N) Spatial complexity of . There are two next O(1) Time complexity method .
- Law two :
In the title nums[i] The maximum value does not exceed 1000, and 1000 It only needs 10 individual bits I can represent ,int Yes 32 A bit , We can make use of the rest 22 A bit ( Exactly 10 individual ) To store an ordered array . Then move all the data in the array to the right 10 individual bits that will do .
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;
}
};
The details here are nums[i/2] & -x3ff, because nums[i/2] Possible 10-19 The bit may have been changed , If you don't shield it, moving it to the left will cause problems .
Law three :
Use each nums[i] > 0 Conditions , We use negative numbers to mark . What are the marks ? Mark nums[i] Whether it is in the sorted position . If so, it will nums[i] become - nums[i]; If not, then
adjustment nums[i];
Let's first calculate nums[i] It should be in the right place j, Then exchange nums[i] and nums[j]; here
nums[j] In the right place , We mark with negative numbers ; But at this time nums[i] Still may not be
The right place , At this point, however, we can take advantage of j(nums[i] Where I was originally ) Work out a new
nums[i] The right place , Then swap again , until nums[i] Is also replaced by a negative number . So cycle one
All over , Until all the numbers are in the right place .
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 yes nums[i] retrieval ,j adopt i Work out ;
while(nums[i] > 0){
j = j < n ? 2 * j : (j - n) * 2 + 1; // Each cycle passes j Calculate the new index position
swap(nums[i], nums[j]);
nums[j] = -nums[j]; //nums[j] Already in the right place , Mark as negative .
}
}
}
for(auto& e : nums){
e = -e;
}
return nums;
}
};
边栏推荐
- Datakit 代理实现局域网数据统一汇聚
- 多路转接select
- 双链表实现
- [theory] deep learning in the covid-19 epic: a deep model for urban traffic revitalization index
- Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
- 【无标题】
- socket(1)
- leetcode1720_2021-10-14
- Wireshark packet capturing skills summarized by myself
- 降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)
猜你喜欢
数据链路层 && 一些其他的协议or技术
最大流问题
LeetCode-513. Find the value in the lower left corner of the tree
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
(to be added) games101 job 7 improvement - knowledge you need to know to realize micro surface model
一文理解OpenStack网络
Implementing DNS requester with C language
网络层 && IP
手动事务的几个类
随机推荐
多路转接select
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
优雅的自定义 ThreadPoolExecutor 线程池
how to install clustershell
suspense组件和异步组件
Introduce the overall process of bootloader, PM, kernel and system startup
Suspend component and asynchronous component
应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
即构「畅直播」上线!提供全链路升级的一站式直播服务
[featured] how do you design unified login with multiple accounts?
[Web Security Basics] some details
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
Guava中这些Map的骚操作,让我的代码量减少了50%
Prompt that the device has no permission when using ADB to connect to the device
Based on asp Net development of fixed assets management system source code enterprise fixed assets management system source code
滤波数据分析
传输层 udp && tcp
C语言-关键字1
队列实现原理和应用