2.4学习内容

news/2025/2/5 10:03:48/

922. 按奇偶排序数组 IIhttps://leetcode.cn/problems/sort-array-by-parity-ii/

922. 按奇偶排序数组 II

给定一个非负整数数组 nums,  nums 中一半整数是 奇数 ,一半整数是 偶数 。

对数组进行排序,以便当 nums[i] 为奇数时,i 也是 奇数 ;当 nums[i] 为偶数时, i 也是 偶数 。

你可以返回 任何满足上述条件的数组作为答案 。

示例 1:

输入:nums = [4,2,5,7]
输出:[4,5,2,7]
解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。

示例 2:

输入:nums = [2,3]
输出:[2,3]

提示:

  • 2 <= nums.length <= 2 * 104

  • nums.length 是偶数

  • nums 中一半是偶数

  • 0 <= nums[i] <= 1000

题解:

自己的代码:

int* sortArrayByParityII(int* nums, int numsSize, int* returnSize) {for (int i = 0, j = 0; i < numsSize;) {int temp=0;if (i % 2 == 0) {if (*(nums + j) % 2 == 0) {temp=*(nums+i),*(nums+i)=*(nums+j),*(nums+j)=temp;++i, j = i;} else j++;}else {if(*(nums+j)%2==1){temp=*(nums+i),*(nums+i)=*(nums+j),*(nums+j)=temp;++i, j = i;}else j++;}}*returnSize=numsSize;return nums;
}

其他人的快速解法1:

int* sortArrayByParityII(int* nums, int numsSize, int* returnSize) {int i = 0, j = 1;while (i < numsSize) {if (nums[i] % 2 == 0) {i += 2; // 寻找偶数下标中最左边的奇数} else if (nums[j] % 2 == 1) {j += 2; // 寻找奇数下标中最左边的偶数} else {int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;i += 2;j += 2;}}*returnSize = numsSize;return nums;
}

 其他人的快速解法2:

int* sortArrayByParityII(int* A, int ASize, int* returnSize)
{   for (int even = 0, odd = 1; even < ASize; even += 2) {if (A[even] & 1) {while (A[odd] & 1) {odd += 2;} A[odd] ^= A[even];A[even] ^= A[odd];A[odd] ^= A[even];}   }*returnSize = ASize;return A;
}

P4738 [CERC2017] Cumulative Codehttps://www.luogu.com.cn/problem/P4738

P4738 [CERC2017] Cumulative Code

题目描述

As you probably know, a tree is a graph consisting of n nodes and n−1 undirected edges in which any two nodes are connected by exactly one path. In a labeled tree each node is labeled with a different integer between 1 and n. The Prüfer code of a labeled tree is a unique sequence associated with the tree, generated by repeatedly removing nodes from the tree until only two nodes remain. More precisely, in each step we remove the leaf with the smallest label and append the label of its neighbour to the end of the code. Recall, a leaf is a node with exactly one neighbour. Therefore, the Prüfer code of a labeled tree is an integer sequence of length n−2. It can be shown that the original tree can be easily reconstructed from its Prüfer code. The complete binary tree of depth k, denoted with C_k , is a labeled tree with 2^k−1 nodes where node j is connected to nodes 2j and 2j+1 for all j<2^k−1. Denote the Prüfer code of Ck with p1,p2,...,p2^k−3. Since the Prüfer code of C_k can be quite long, you do not have to print it out. Instead, you need to answer n questions about the sums of certain elements on the code. Each question consists of three integers: a,d and m. The answer is the sum of the of the C′_k s Prüfer code elements p_a,p_a+d,p_a+2d,...,p_a+(m−1)d.

输入格式

The first line contains two integers k and q(2≤k≤30,1≤q≤300) — the depth of the complete binary tree and the number of questions. The j−th of the following q lines contains the j−th question:three positive integers a_j,d_j and mj  such that a_j,d_j  and a_j+(m_j−1)d_jare all at most 2^k−3.

输出格式

Output 1 lines. The j−th line should contain a single integer — the answer to the j−th question.

 

输入输出样例

输入 #1

3 5
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1

输出 #1

2
2
1
3
3

输入 #2

4 4
2 1 5
4 4 3
4 8 1
10 3 2

输出 #2

18
15
5
13

输入 #3

7 1
1 1 125

输出 #3

4031

说明/提示

In the first example above, when constructing the Prüfer code for C_3​ the nodes are removed in the following order: 4,5,2,1,6. Therefore, the Prüfer code of C_3​ is 2,2,1,3,3.
 

声明:所用题皆来自刷题网,作者仅用来向组织反馈学习情况,无任何盈利行为。

http://www.ppmy.cn/news/1569479.html

相关文章

机器学习10

自定义数据集 使用scikit-learn中svm的包实现svm分类 代码 import numpy as np import matplotlib.pyplot as pltclass1_points np.array([[1.9, 1.2],[1.5, 2.1],[1.9, 0.5],[1.5, 0.9],[0.9, 1.2],[1.1, 1.7],[1.4, 1.1]])class2_points np.array([[3.2, 3.2],[3.7, 2.9],…

从理论到实践:Linux 进程替换与 exec 系列函数

个人主页&#xff1a;chian-ocean 文章专栏-Linux 前言&#xff1a; 在Linux中&#xff0c;进程替换&#xff08;Process Substitution&#xff09;是一个非常强大的特性&#xff0c;它允许将一个进程的输出直接当作一个文件来处理。这种技术通常用于Shell脚本和命令行操作中…

Linux网络 | 理解TCP面向字节流、打通socket与文件的关系

前言&#xff1a;我们经常说TCP是面向字节流的&#xff0c; TCP是面向字节流的。 但是&#xff0c; 到底是什么事面向字节流呢&#xff1f; 另外&#xff0c; 我们知道sockfd其实就是文件fd。 但是&#xff0c;为什么sockfd是文件fd呢&#xff1f; 这些问题都在本节内容中的到回…

DeepSeek本地部署的一些使用体会

春节期间我也尝试了一下Deepseek的本地部署&#xff0c;方案选用了Ollama Chatbox或AnythingLLM。Chatbox里有很多有意思的“助手”&#xff0c;而AnythingLLM支持本地知识库。 网上教程很多&#xff0c;总的来说还是很方便的&#xff0c;不需要费太多脑子。甚至可以这么说&a…

基于机器学习的布伦特原油价格的分析与研究

项目&#xff1a;基于机器学习的布伦特原油价格的分析与研究 摘 要 布伦特原油期货及现货市场所构成的布伦特原油定价体系&#xff0c;最多时竟涵盖了世界原油交易量的80%&#xff0c;即使在纽约原油价格日益重要的今天&#xff0c;全球仍有约65%的原油交易量&#xff0c;是以…

基于 STM32 的智能电梯控制系统

1. 引言 随着城市化进程的加速&#xff0c;高层建筑日益增多&#xff0c;电梯作为垂直交通工具的重要性愈发凸显。传统电梯控制系统在运行效率、安全性和智能化程度上已难以满足现代需求。智能电梯控制系统能够实时监测电梯的运行状态、乘客需求&#xff0c;并根据这些信息优化…

使用VCS对Verilog/System Verilog进行单步调试的步骤

Verilog单步调试&#xff1a; System Verilog进行单步调试的步骤如下&#xff1a; 1. 编译设计 使用-debug_all或-debug_pp选项编译设计&#xff0c;生成调试信息。 我的4个文件&#xff1a; 1.led.v module led(input clk,input rst_n,output reg led );reg [7:0] cnt;alwa…

【ChatGPT:开启人工智能新纪元】

一、ChatGPT 是什么 最近,ChatGPT 可是火得一塌糊涂,不管是在科技圈、媒体界,还是咱们普通人的日常聊天里,都能听到它的大名。好多人都在讨论,这 ChatGPT 到底是个啥 “神器”,能让大家这么着迷?今天咱就好好唠唠。 ChatGPT,全称是 Chat Generative Pre-trained Trans…