【刷题】Day3--错误的集合

embedded/2024/9/24 5:11:53/

hello!又见面啦~~~

一道习题,要长脑子了......

【. - 力扣(LeetCode)】

【思路】

/*** Note: The returned array must be malloced, assume caller calls free().*/void Bubble_sort(int arr[], int size) {int temp;for (int i = 0; i < size - 1; i++) {for (int j = 0; j < size - 1 - i; j++) {if (arr[j] > arr[j + 1]) {temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}
}
int* findErrorNums(int* nums, int numsSize, int* returnSize) {Bubble_sort(nums, numsSize);int* result = (int*)malloc(sizeof(int) * 2);int duplicate = 0, missing = 0;// 查找重复的数字for (int i = 0; i < numsSize - 1; i++) {if (nums[i] == nums[i + 1]) {duplicate = nums[i];break;}}// 计算总和int expectedSum = (numsSize * (numsSize + 1)) / 2;int actualSum = 0;for (int i = 0; i < numsSize; i++) {actualSum += nums[i];}// 通过公式计算丢失的数字missing = expectedSum - (actualSum - duplicate);result[0] = duplicate;  // 重复的数字result[1] = missing;    // 丢失的数字*returnSize = 2;return result;
}

【注意】:返回的 result 得是 malloc 开辟的一块空间,同时 *returnSize 等于 result 里面有效数据的个数。

【总结】:菜了,想不到公式法算缺失的数据。

继续干! 


分享一首歌~~~

我亲爱的_谭维维_高音质在线试听_我亲爱的歌词|歌曲下载_酷狗音乐

至此结束!

我是云边有个稻草人

期待与你的下一次相遇——


http://www.ppmy.cn/embedded/112780.html

相关文章

html限制仅有一个音/视频可播放

html限制仅有一个音/视频可播放 /** 多个音频仅能播放一个 */ function audiosPlay() {const audios document.getElementsByTagName(audio);const videos document.getElementsByTagName(video);function pauseAll() {var self this;[].forEach.call(audios, function (i) …

前端深拷贝

什么是 structuredClone()&#xff1f; structuredClone() 是 2022 年引入的全局函数&#xff0c;支持深度克隆 JavaScript 对象。与 JSON.stringify() 和 JSON.parse() 等传统方法不同&#xff0c;它们难以处理复杂的结构和循环引用&#xff0c;而 structuredClone() 可以毫不…

GVIM常用命令

Gvim常用命令 1.操作文件 1.1 gvim比较两个文件的不同 gvim -d file1 file2 2.操作文件内容 2.1全文替换 :1,$s/旧字符/新字符/g 2.2多行插入 步骤一&#xff1a;ctrlv进入visual模式 步骤二&#xff1a;选中要插入的行 步骤三&#xff1a;ctrlI进入insert模式 步骤…

Unity中InputField一些属性的理解

先看代码&#xff1a; using UnityEngine; using UnityEngine.UI;public class TestInput : MonoBehaviour {[SerializeField]InputField inputField;void Start(){Debug.Log(inputField.text);Debug.Log(inputField.text.Length);Debug.Log(inputField.preferredWidth);Debug…

HTML/CSS/JS学习笔记 Day4(CSS--C1 选择器声明)

跟着该视频学习&#xff0c;记录笔记&#xff1a;【黑马程序员pink老师前端入门教程&#xff0c;零基础必看的h5(html5)css3移动端前端视频教程】https://www.bilibili.com/video/BV14J4114768?p12&vd_source04ee94ad3f2168d7d5252c857a2bf358 Day4 内容梳理&#xff1a;…

Golang | Leetcode Golang题解之第400题第N位数字

题目&#xff1a; 题解&#xff1a; func findNthDigit(n int) int {d : 1for count : 9; n > d*count; count * 10 {n - d * countd}index : n - 1start : int(math.Pow10(d - 1))num : start index/ddigitIndex : index % dreturn num / int(math.Pow10(d-digitIndex-1)…

WPS如何删除表格下的空白页

WPS Office&#xff08;12.1.0.17827&#xff09; ① 鼠标右键&#xff0c;选择段落 ② 行距&#xff1a;固定值&#xff1b;设置值&#xff1a;1磅&#xff1b;取消勾选&#xff0c;确定即可~

c++题目_洛谷 / 题目详情 P1012 [NOIP1998 提高组] 拼数

# [NOIP1998 提高组] 拼数 ## 题目描述 设有 $n$ 个正整数 $a_1 \dots a_n$&#xff0c;将它们联接成一排&#xff0c;相邻数字首尾相接&#xff0c;组成一个最大的整数。 ## 输入格式 第一行有一个整数&#xff0c;表示数字个数 $n$。 第二行有 $n$ 个整数&#xff0c;表…