【简单】922. 按奇偶排序数组 II

js代码-给定一个非负整数数组A,A中一半数是奇数,一半数是偶数,对数组进行排序,以便当A[i]为奇数时,i也是奇数;当A[i]为偶数时,i也是偶数。返回任何满足上述条件的数组作为答案。 立即下载

【题目】
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。你可以返回任何满足上述条件的数组作为答案。
来源:leetcode
链接:https://leetcode-cn.com/problems/sort-array-by-parity-ii/
【示例】
输入:[4,2,5,7]
输出:[4,5,2,7]
解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受
【提示】
2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
【方法一】

class Solution {
public:
    vector<int> sortArrayByParityII(vector<int>& A) {
        vector<int> jishu,oushu;
        for(auto x:A)
            if(x%2)//奇数
                jishu.push_back(x);
            else
                oushu.push_back(x);
        int len=A.size(),cnt=0;
        for(int i=0;i<len;i+=2)
            A[i]=oushu[cnt++];
        cnt=0;
        for(int i=1;i<len;i+=2)
            A[i]=jishu[cnt++];
        return A;
    }
};

【方法二】

class Solution {
public:
    vector<int> sortArrayByParityII(vector<int>& A) {
        int jishuindex=1,oushuindex=0;
        vector<int> rs(A.size());
        for(auto x:A)
            if(x%2){
                rs[jishuindex]=x;
                jishuindex+=2;
            }else{
                rs[oushuindex]=x;
                oushuindex+=2;
            }
        return rs;
    }
};
别再羡慕专业版!用这个批处理脚本,一键给Win11家庭版装上官方沙盒 本文详细介绍了如何通过批处理脚本在Windows 11家庭版上安装官方Windows沙盒(Windows Sandbox),无需复杂操作即可享受专业版功能。脚本自动激活系统内置组件,提供安全隔离的虚拟化环境,适用于软件测试、安全浏览等场景。 阅读详情

相关推荐

火车头采集器超级详细图文使用指导(面向新手)

火车采集器·超级详细使用指导

DE的个人原创笔记 1万+

按奇偶排序数组II(给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。)

给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 提示: 2 <= A.length ...

be_gin_ner的博客 2079

adb详细教程(四)-使用adb启动应用、关闭应用、清空应用数据、获取设备已安装应用列表

adb对于安卓移动端来说,是个非常重要的调试工具。通过ADB可以操作、管理设备上的应用。

公众号|软件测试老吴 3万+

按奇偶排序数据II

给定一个非负整数数组A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当A[i] 为奇数时,i也是奇数;当A[i]为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 class Solution:...

weixin_41076200的博客 543

UniApp 与 Taro 深度解析:如何根据项目需求选择最佳跨平台框架?

本文深度对比了UniApp和Taro两大主流跨平台开发框架。UniApp基于Vue生态,提供开箱即用的高效开发体验,尤其适合快速覆盖多端小程序;Taro则根植React范式,架构灵活,便于与现有React生态集成,更适合对性能和原生体验有高要求的复杂项目。文章结合团队技术栈、项目类型及性能需求,为开发者提供了清晰的选型指南。

snow3的博客 204

告别第三方工具!用Windows自带蓝牙玩转BLE设备调试(保姆级图文教程)

本文详细介绍了如何利用Windows自带的蓝牙功能进行BLE设备调试,无需第三方工具。从硬件准备、系统配置到实战操作,提供保姆级图文教程,帮助开发者高效完成设备发现、数据交互等全流程调试工作。特别适合物联网开发者和智能硬件爱好者。

o5p6q的博客 735

LeetCode922按奇偶排序数组II

给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 提示: 2 <= A.length &l...

max119xlx的博客 265

Ros入门 (十)----激光雷达避障 简易实现

本篇文章通过订阅/scan话题获取障碍物的距离信息,达到避障目的,提供部分代码,仅供参考。

weixin_51157015的博客 1万+

Leetcode 每日一题——922. 按奇偶排序数组 II

922. 按奇偶排序数组 II 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 今天的题目很简单,看完题目后我反手就写出了答案(感觉自己又行了O(∩_∩)O哈哈~),下面是Python的代码: class Solution: def sortArrayByParityII(self, A: List[int]) -> List[i

qq_41978896的博客 431

一根网线,让整个公司断网,二层环路真实事故复盘

那天晚上 23:47,监控告警突然爆了。核心交换机 CPU 100%。ARP 表暴涨。广播流量飙升。业务系统集体超时。5 分钟后,全公司断网。运维群里一句话:“是不是黑客攻击?但老网工都知道,这种现象只有一种可能:二层环路。

网络技术联盟站 350

手把手教你完成基于matlab的LSTM时间序列分析

时间序列是按时间顺序组织的数字序列,是数据分析中重要的处理对象之一。时间序列的主要特点是数据获取方式一般具有客观性,能反映某种现象的变化趋势或统计指标,进而预测未来走向,这在本质上也是一个回归预测的问题。长短时记忆网络(Long Short-Term Memory,LSTM)是一种循环神经网络,适合处理有更长时间跨度的内部记忆,被广泛应用于时间序列分析,能够保持数据的内在持续性,反映数据的细粒度走势,具有良好的预测效果。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 897

【Leetcode】922. 按奇偶排序数组Ⅱ

题目描述: 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 提示: 2 &lt...

EasonWong的博客 736

【图像去噪】基于加权核范数最小化算法实现图像去噪附matlab代码

1 简介 WNNM是一种约束矩阵奇异值稀疏性的低秩算法,在NNM理论基础上增加了核范数权重,有效利用了自然图像的先验信息并提高了核范数的灵活性. 2 部分代码 ​clcclear allclose allnSig = 100;O_Img = double(imread('Monarch.png'));​randn('seed', 0);N_Img = O_Img + nSig* randn(size(O_Img)); ..

qq_59747472的博客 3118

922. 按奇偶排序数组 II(简单题)

题目描述: 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 提示: 2 <= A.length <= 20000 A.length % 2 == 0 0 <= A[i] <= 1000

闭关修炼 217

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Leetcode—922. 按奇偶排序数组 II【简单】

运行结果 之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

Atticus的博客 320

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

922. 按奇偶排序数组 II-简单-数组、排序

922. 按奇偶排序数组 II-简单-数组、排序 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i也是奇数;当 A[i] 为偶数时, i也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 提示: 2 <= A.length <= 20000 A.length % 2 == 0 0 &l

qq_38454093的博客 149

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

leetcode 922. 按奇偶排序数组 II 简单

4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。任何满足上述条件的数组作为答案。可以不使用额外空间解决问题吗?对数组进行排序,以便当。给定一个非负整数数组。

2401_88085478的博客 325

【LeetCode每日一题】[简单]922. 按奇偶排序数组 II

【LeetCode每日一题】[简单]922. 按奇偶排序数组 II 922. 按奇偶排序数组 II 922. 按奇偶排序数组 II 算法思想:数组 题目: java代码 两遍遍历: class Solution { public int[] sortArrayByParityII(int[] A) { int n = A.length;//长度 int[] a = new int[n / 2];//记录偶数 int[] b = new int[n / 2];//

qq_39457586的博客 197

922. 按奇偶排序数组 II 简单

4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。任何满足上述条件的数组作为答案。对数组进行排序,以便当。给定一个非负整数数组。

曾几何时的博客 196

【数组】922. 按奇偶排序数组 II(简单)

【题目】 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。你可以返回任何满足上述条件的数组作为答案。 【示例】 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 【提示】 2 <= A.length <= 20000 A.length % 2 == 0 0 <= A[i] <= 1000

... 178

leetcode 算法题922 (简单241) 按奇偶排序数组 II

leetcode 算法题922 (简单241) 按奇偶排序数组 II 题目介绍 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序, 以便当 A[i] 为奇数时,i 也是奇数; 当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例 输入:[4,2,5,7] 输出:[4...

FYuu95100的博客 211

leetcode 922. 按奇偶排序数组 II(简单)

leetcode 922. 按奇偶排序数组 II(简单) 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 步骤: 两次循环 class Solution: def sortArrayByParit

weixin_44456155的博客 107

922. 按奇偶排序数组 II(简单)

简单的快乐 加油 加油 加油

Enter 79

【Python】【难度:简单】Leetcode 922. 按奇偶排序数组 II【完】

给定一个非负整数数组A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当A[i] 为奇数时,i也是奇数;当A[i]为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 提示: 2 <= A.length <= 20000 A.length % 2 == 0 0 <= A[i] <=...

thomashhs12的博客 245

力扣刷题---922. 按奇偶排序数组 II【简单】

力扣刷题---922. 按奇偶排序数组 II【简单】

学无止境 349

922. 按奇偶排序数组 II(简单,数组)(12.20)

给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i也是偶数。 你可以返回任何满足上述条件的数组作为答案。 示例: 输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。 class Solution(ob...

花小七 188
上一篇: 【简单】面试题68 - II. 二叉树的最近公共祖先
下一篇: 【简单】面试题21. 调整数组顺序使奇数位于偶数前面
菜菜2022
博客等级 码龄9年 67粉丝 1033原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值