2.4学习内容

devtools/2025/2/5 8:04: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/devtools/156214.html

相关文章

Multi-Scale Heterogeneous Text-Attributed Graph Datasets From Diverse Domains

Multi-Scale Heterogeneous Text-Attributed Graph Datasets From Diverse Domains WWW25 推荐指数&#xff1a;#paper/⭐⭐⭐#​ 代码地址&#xff1a;https://github.com/Cloudy1225/HTAG 作者主页&#xff1a;Yunhui Lius Homepage 一句话总结&#xff1a;提出了涵盖多…

工作总结:压测篇

前言 压测是测试需要会的一项技能&#xff0c;作为开发&#xff0c;有点时候也要会一点压测。也是被逼着现学现卖的。 一、压测是什么&#xff0c;以及压测工具的选择 压测&#xff0c;即压力测试&#xff0c;是一种性能测试手段&#xff0c;通过模拟大量用户同时访问系统&am…

PostgreSQL 数据库模式基础操作

查看数据库或者使用pgAdmin或者QGIS查看PG数据库时&#xff0c;可以看到数据库名下面有一个Public&#xff0c;然后才是具体的表&#xff0c;搜索了一下&#xff0c;按照PG官网&#xff1a;https://www.postgresql.org/docs/current/ddl-schemas.html 的说明&#xff0c;这个Pu…

php反序列化

php反序列化 声明&#xff1a;本人只是在学习反序列化 因此这篇文章大量参考了https://blog.csdn.net/Hardworking666/article/details/122373938 这位的博客 感谢他的详细文章让我可以详细学习反序列化 大家想看更详细的可以直接参考他的文章!!! 什么是序列化和反序列化 序…

swagger使用指引

1.swagger介绍 在前后端分离开发中通常由后端程序员设计接口&#xff0c;完成后需要编写接口文档&#xff0c;最后将文档交给前端工程师&#xff0c;前端工程师参考文档进行开发。 可以通过一些工具快速生成接口文档 &#xff0c;本项目通过Swagger生成接口在线文档 。 什么…

数组排序算法

数组排序算法 用C语言实现的数组排序算法。 排序算法平均时间复杂度最坏时间复杂度最好时间复杂度空间复杂度是否稳定适用场景QuickO(n log n)O(n)O(n log n)O(log n)不稳定大规模数据&#xff0c;通用排序BubbleO(n)O(n)O(n)O(1)稳定小规模数据&#xff0c;教学用途InsertO(n)…

实际操作 检测缺陷刀片

号he 找到目标图像的缺陷位置&#xff0c;首先思路为对图像进行预处理&#xff0c;灰度-二值化-针对图像进行轮廓分析 //定义结构元素 Mat se getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); morphologyEx(thre, tc, MORPH_OPEN, se, Point(-1, -1), 1); …

分布式微服务系统架构第91集:系统性能指标总结

加群联系作者vx&#xff1a;xiaoda0423 仓库地址&#xff1a;https://webvueblog.github.io/JavaPlusDoc/ 系统性能指标总结 系统性能指标包括哪些&#xff1f; 业务指标、资源指标、中间件指标、数据库指标、前端指标、稳定性指标、批量处理指标、可扩展性指标、可靠性指标。 …