leetcode 448. Find All Numbers Disappeared in an Array
来源:互联网 发布:iphone6突然没有4g网络 编辑:程序博客网 时间:2024/05/02 16:32
原题:
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:
Input:[4,3,2,7,8,2,3,1]Output:[5,6]思路:
循环一个做标记
第二次查找哪个数没有被标记就好,(我第一次竟然只是想着做排序然后再遍历。。。 我真是用qsort着魔了。。。)
代码如下:
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize) { int* flag; flag=(int*)malloc(sizeof(int)*(numsSize+1)); memset(flag,0,sizeof(int)); for(int n=0;n<numsSize;n++) { *(flag+*(nums+n))=1; } int* result; result=(int*)malloc(sizeof(int)*numsSize); for(int n=1;n<=numsSize;n++) { if(*(flag+n)!=1) { *(result+*returnSize)=n; *returnSize+=1; } } return result;}
慎用排序函数。。。
慎用排序。
慎用
阅读全文
0 0
- LeetCode-Array-448. Find All Numbers Disappeared in an Array
- leetcode:448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array
- <leetcode>448.Find All Numbers Disappeared in an Array
- [LeetCode] 448. Find All Numbers Disappeared in an Array
- [LeetCode]448. Find All Numbers Disappeared in an Array
- leetcode 448. Find All Numbers Disappeared in an Array
- Leetcode 448. Find All Numbers Disappeared in an Array
- 【LeetCode】 448. Find All Numbers Disappeared in an Array
- 448. [LeetCode]Find All Numbers Disappeared in an Array
- [leetcode] 448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array
- Leetcode-448. Find All Numbers Disappeared in an Array
- 【Leetcode】448. Find All Numbers Disappeared in an Array
- LeetCode - 448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array
- LeetCode--448. Find All Numbers Disappeared in an Array
- LeetCode 448. Find All Numbers Disappeared in an Array(Java)
- 如何让self.navigationController退出到指定页面,或者让它一次性pop出两个页面popViewControllerAnimated没有
- Python3之shutil模块
- Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求
- 计算机组成原理知识点梳理(一)
- mybatis缓存
- leetcode 448. Find All Numbers Disappeared in an Array
- Kotlin之let,apply,with,run函数区别
- C#excel操作辅助类
- 集合
- 计算机网络-常识1
- Solr6.6.1 在 Tomcat7.0.56 部署,页面提示SolrCore Initialization Failures
- C/C++ 语言中函数参数传递的三种方式
- 【编程题】Java编程题二(10道)
- windows环境下C++实现的多种目录监控和优劣解析