C++ Py3 2pointer速解Leetcode2161 Partition Array According to Given Pivot
影片說明解法Py3用迴圈 list 相加。
----
The video explains the solution in Py3 using a loop to add lists. (C++ branchless, 0ms)
[codes on Leecode]https://leetcode.com/problems/partition-array-according-to-given-pivot/solutions/8320237/2-pointerbeats-100-by-anwendeng-wf4w/
---
Time stamps:
0:00 - Introduction and Problem Overview (LeetCode 2161)
0:45 - Explaining the Problem Requirements and Conditions
3:06 - Example 1 Walkthrough
3:56 - Example 2 Walkthrough
4:33 - Python 3 Solution Implementation and Explanation
6:12 - Python 3 Code Submission and Performance Result
6:32 - C++ Solution Implementation (Branchless Approach)
8:33 - C++ Code Submission and Performance Result (0ms)
#anwendeng
class Solution {
public:
static vector<int> pivotArray(vector<int>& nums, int pivot) {
int n=nums.size();
if (n==1) return nums;
int R[n];
int l=0, r=0;
for(int x: nums){
nums[l]=R[r]=x;
l+=(x<pivot);
r+=(x>pivot);
}
int m=n-r-l;
auto it_m=nums.begin()+l;
fill(it_m, it_m+m, pivot);
copy(R, R+r, it_m+m);
return nums;
}
};
沒有留言:
張貼留言