Re: [閒聊] 每日LeetCode

作者: oin1104 (是oin的說)   2024-02-14 14:01:55
※ 引述 《Rushia (みけねこ的鼻屎)》 之銘言:
:  
: https://leetcode.com/problems/rearrange-array-elements-by-sign/description
: 2149. Rearrange Array Elements by Sign
: 給你一個有相同數量正負數的陣列nums,重新排序他的元素並滿足:
: 1.整個陣列的元素是:正負正負正負...正負
: 2.正數和負數的順序和原陣列保持一致。
:  
: 思路:
: 1.宣告一個大小相同的list,用一個指標指向正數要插入的地方一個指向負數要插入的
: 地方,遇到正數就往正數指標位置插入並右移兩格負數同理。
哭啊 我的也一樣
為什麼過年比較忙的那幾天都出比較難
然後這幾天比較閒反而比較簡單
在哭喔
https://i.imgur.com/jYFn5PX.jpg
class Solution {
public:
vector<int> rearrangeArray(vector<int>& nums)
{
int len = nums.size();
vector<int> all(len,0);
int a = 0;
int b = 1;
for(int i = 0 ; i < len ; i ++)
{
if(nums[i] > 0)
{
all[a] = nums[i];
a+=2;
}
else
{
all[b] = nums[i];
b+=2;
}
}
return all;
}
};

Links booklink

Contact Us: admin [ a t ] ucptt.com