首页 > 其他分享 >leetcode-1470. 重新排列数组

leetcode-1470. 重新排列数组

时间:2022-08-29 09:57:50浏览次数:80  
标签:重新排列 int 复杂度 1470 数组 leetcode

1470. 重新排列数组

图床:blogimg/刷题记录/leetcode/1470/

刷题代码汇总:https://www.cnblogs.com/geaming/p/16428234.html

题目

image-20220829094008947

思路

开辟新的空间,装入元素。

解法

class Solution {
public:
    vector<int> shuffle(vector<int>& nums, int n) {
        vector<int>ans;
        for(int i =0;i<n;i++){
            ans.push_back(nums[i]);
            ans.push_back(nums[i+n]);
        }
        return ans;
    }
};
  • 时间复杂度:\(O(n)\),for循环一遍
  • 空间复杂度:\(O(n)\),若返回值不计入则为\(O(1)\)

补充

补充一种空间复杂度为\(O(1)\)的方法

image-20220829094244503

标签:重新排列,int,复杂度,1470,数组,leetcode
From: https://www.cnblogs.com/geaming/p/16634856.html

相关文章

  • LeetCode 1779. Find Nearest Point That Has the Same X or Y Coordinate
    原题链接在这里:https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/题目:Youaregiventwointegers, x and y,whichrepresen......
  • LeetCode — 最小路径和
    LeetCode—最小路径和问题陈述给定一个mxn网格用非负数填充,找到一条从左上角到右下角的路径,该路径最小化沿其路径的所有数字的总和。笔记:您只能在任何时间点向下或......
  • LeetCode 854. K-Similar Strings
    原题链接在这里:https://leetcode.com/problems/k-similar-strings/题目:Strings s1 and s2 are k-similar (forsomenon-negativeinteger k)ifwecanswapthe......
  • [Google] LeetCode 2128 Remove All Ones With Row and Column Flips
    Youaregivenanmxnbinarymatrixgrid.Inoneoperation,youcanchooseanyroworcolumnandflipeachvalueinthatroworcolumn(i.e.,changingall0's......
  • leetcode刷题记录之25(集合实现)
    题目描述:给你链表的头节点head,每 k 个节点一组进行翻转,请你返回修改后的链表。k是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将......
  • leetcode 斐波那契数列 javascript实现
    写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项(即F(N))。斐波那契数列的定义如下:F(0)=0,  F(1) =1F(N)=F(N-1)+F(N-2),其中N>1.斐波那契数列由0......
  • leetcode 696. Count Binary Substrings 计数二进制子串(简单)
    一、题目大意给定一个字符串s,统计并返回具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是成组连续的。重复出现(不同位置)的子串......
  • leetcode191:位1的个数
    packagecom.mxnet;publicclassSolution191{publicstaticvoidmain(String[]args){System.out.println(1<<5);}/***编写一......
  • leetcode198:打家劫舍
    packagecom.mxnet;publicclassSolution198{publicstaticvoidmain(String[]args){}/***你是一个专业的小偷,计划偷窃沿街的房屋。每间......
  • 反转二叉树演练 leetcode |第 6 部分
    反转二叉树演练leetcode|第6部分上一个问题[有效回文演练Leetcode|第5部分上一个问题媒体网](/@nerdhide/valid-palindrome-walkthrough-leetcode-part-5-8......