当前位置: 首页 > article >正文

2.4学习内容

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.kler.cn/a/532587.html

相关文章:

  • frida 入门
  • LabVIEW微位移平台位移控制系统
  • 【基于SprintBoot+Mybatis+Mysql】电脑商城项目之用户登录
  • 交叉验证、精确率、召回率
  • Java中的泛型及其用途是什么?
  • 【LLM-agent】(task2)用llama-index搭建AI Agent
  • 代码随想录算法【Day36】
  • C++ 仿函数
  • babel-安装和使用
  • sort排序 计数排序 map set C++ 蓝桥杯
  • 实验13 JavaBean(二)
  • ROS-激光雷达-消息包格式-获取激光雷达数据-激光雷达避障
  • RabbitMQ深度探索:前置知识
  • 游戏引擎 Unity - Unity 打开项目、Unity Editor 添加简体中文语言包模块、Unity 项目设置为简体中文
  • 【435. 无重叠区间 中等】
  • K8s 分布式存储后端(K8s Distributed Storage Backend)
  • 基础IOIO
  • PHP 调用 DeepSeek API 完整指南
  • 【字节青训营-7】:初探 Kitex 字节微服务框架(使用ETCD进行服务注册与发现)
  • 机器学习day8
  • sql中奇数、偶数、正则
  • 【L2JMobius】ZGC requires Windows version 1803 or later
  • 宝塔面板端口转发其它端口至MySQL的3306
  • 关于大模型 AGI 应知应会_生在AI发展的时代
  • 51单片机入门_05_LED闪烁(常用的延时方法:软件延时、定时器延时;while循环;unsigned char 可以表示的数字是0~255)
  • 统计满足条件的4位数(信息学奥赛一本通-1077)