当前位置:网站首页>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;
}
};
边栏推荐
- 02---纵波不可能产生的现象
- 66 pitfalls in go programming language: pitfalls and common errors of golang developers
- Structured interview of state-owned enterprises and central enterprises employment of state-owned enterprises Modou Interactive Employment Service steward
- Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
- Prompt that the device has no permission when using ADB to connect to the device
- 优雅的自定义 ThreadPoolExecutor 线程池
- LeetCode-513. 找树左下角的值
- Elegant custom ThreadPoolExecutor thread pool
- Graduation design of phase 6 of the construction practice camp
- Based on asp Net development of fixed assets management system source code enterprise fixed assets management system source code
猜你喜欢

Bld3 getting started UI

cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)

XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
![在每个树行中找最大值[分层遍历之一的扩展]](/img/5b/81ff20b61c0719ceb6873e44878859.png)
在每个树行中找最大值[分层遍历之一的扩展]

最大流问题

一文理解OpenStack网络
Visit Amazon memorydb and build your own redis memory database

滤波数据分析

(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识

Multi task model of recommended model: esmm, MMOE
随机推荐
123. the best time to buy and sell shares III
Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme
EasyBypass
SAP接口debug设置外部断点
socket done
leetcode1720_2021-10-14
Remove the screen recording reminder (seven cattle cloud demo)
Ebpf XDP mount point analysis
How to achieve energy conservation and environmental protection of full-color outdoor LED display
WMI and PowerShell get TCP connection list
在每个树行中找最大值[分层遍历之一的扩展]
堆排序和快速排序原理实现
Several classes of manual transactions
Object.defineProperty和Reflect.defineProperty的容错问题
Pattern recognition - 1 Bayesian decision theory_ P1
Shengzhe technology AI intelligent drowning prevention service launched
Why are life science enterprises on the cloud in succession?
栈的两种实现方式
VSCode无网环境快速迁移开发环境(VIP典藏版)
字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?