数组去重
来源:互联网 发布:企业融资方式 知乎 编辑:程序博客网 时间:2024/05/01 19:38
有关数组的leetcode
2.1.1 Remove Duplicates from Sorted Array
描述
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
function f(&$arr){ if(false===is_array($arr)||empty($arr)) return 0; $prev=null; $num=0; foreach($arr as $key=>$value){ if($prev==$value){ unset($arr[$key]); }else{ $prev=$value; $num++; } } $arr=array_values($arr);//重建索引 return $num;}
2.1.2 Remove Duplicates from Sorted Array II
描述
Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?
For example, Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3]
function f2(&$arr,$DumTime){ if(empty($arr)) return 0; if(count($arr)<=$DumTime) return $DumTime; $dumNum=0; $num=0; $prev=null; foreach($arr as $key=>$value){ $num++; if($value==$prev){ $dumNum++; if($dumNum>=$DumTime){ unset($arr[$key]); $num--; } }else{ $dumNum=0; $prev=$value; } } $arr=array_values($arr); return $num;}
**这2个题,因为采用了php专有的函数,unset()销毁变量
在c/c++中不存在这个函数,题目要求不再开辟新的空间,
因该这样做,但是最后数组的长度变小了。之后的部分多余了。
主要点在于记录不同的个数num以及数组的不重复的值的下标
function f(&$arr){ if(empty($arr)) return 0; $index=0; $num=0; $couNum=count($arr); for($i=1;i<$couNum;$i++){ if($arr[$index]!=$arr[$i]){ $arr[++$index]=$arr[$i]; $num++; } } $num++; array_splice($arr,$num); //去掉$num后的一部分 return $num;}function f2(&$arr,$DumTime){ if(empty($arr)) return 0; if(count($arr)<=$DumTime) return $DumTime; $dumNum=0; $num=0; $prev=0; $countNum=count($arr); for($i=1;$i<$countNum;$i++){ if($arr[$i]==$arr[$prev]){ $dumNum++; if($dumNum<$DumTime){ $num++; $arr[++$prev]=$arr[$i]; } }else{ $num++; $dumNum=0; $arr[++$prev]=$arr[$i]; } } $num++;//这个$num的个数很重要 array_splice($arr,$num); return $num;}
**另外去除重复的
例如array(1,2,3,3)结果 array(1,2).length=2;**
function f(&$arr){ if(false===is_array($arr)||empty($arr)) return false; $prev=0; $isSame=false; $countNum=count($arr); for($i=1;$i<$countNum;$i++){ if($i!=$prev&&$arr[$i]==$arr[$prev]){ unset($arr[$i]); $isSame=true; }else if($isSame){ unset($arr[$prev]); $isSame=false; $prev=$i; } }//end for if($isSame) unset($arr[$prev]); return count($arr); }function f6(&$arr){ if(false===is_array($arr)||empty($arr)) return false; $prev=0; $index=0; $isSame=false; $countNum=count($arr); for($i=0;$i<$countNum;$i++){ if($prev==$i) continue; if($arr[$i]==$arr[$prev]){ $isSame=true; }else if($isSame){ $isSame=false; $prev=$i; }else if($arr[$i]!=$arr[$prev]&&false===$isSame){ $arr[$index]=$arr[$prev]; $index++; $prev=$i; } }//end for if(false===$isSame){ $arr[$index]=$arr[$countNum-1]; $index++; } array_splice($arr,$index); return $index;}
2.1.3 Search in Rotated Sorted Array
描述
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
2.1.4 Search in Rotated Sorted Array II
描述
Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
//判读一个taregt是否存在有序数组中
考察二分法
**/*分析:
123456789—》6789 1 2345
当
应该是6789 1 2345 后面肯定是升序 然后根据target目标来决定前面还是后面
当
应该是 3456 7 8912 前面肯定是升序 然后根据target目标来决定前面还是后
*/****
function f7(&$arr,$target,$begin,$end){ if(false===is_array($arr)||empty($arr)) return -1; if($end<$begin) return -1; $mid=$begin+intval(($end-$begin)/2); if($arr[$mid]==$target){ return $mid; } /*这是写错的 if($arr[$mid]>$target){ if($arr[$begin]>$target){ return f($arr,$target,$mid+1,$end); }else{ return f($arr,$target,$begin,$mid-1); } }else{ if($arr[$end]>$target){ return f($arr,$target,$mid+1,$end); }else{ return f($arr,$target,$begin,$mid-1); } }*/ if($arr[$begin]>$arr[$mid]){ if($target>$arr[$mid]&&$target<$arr[$end]){ return f7($arr,$target,$mid+1,$end); }else{ return f7($arr,$target,$begin,$mid-1); } }else if($arr[$begin]<$arr[$mid]){ if($target<$arr[$mid]&&$target>$arr[$begin]){ return f7($arr,$target,$begin,$mid-1); }else{ return f7($arr,$target,$mid+1,$end); } }else{ return f7($arr,$target,$begin+1,$end); }}
2.1.5 Median of Two Sorted Arrays
描述
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted
arrays. we overall run time complexity should be O(log(m + n)).
/*
查找第(N+M)/2个
*/
function f($arr,$brr){}
- 数组去数组重
- 数组去重,json数组去重
- 数组去重
- 数组去重
- JS 数组去重
- 数组去重算法
- 数组去重
- js数组去重
- php 数组去重
- 数组去重
- 数组排序去重
- javascript数组去重
- json数组去重
- 数组去重
- php数组去重
- 数组如何去重
- iOS 数组去重
- 动态数组去重
- MySQL中锁详解(行锁、表锁、页锁、悲观锁、乐观锁等)
- Python第三天
- 《C++ Primer Plus(第六版)》(29)(第十四章 C++中的代码重用 复习题答案)
- 博为峰Java技术文章 ——JavaEE Hibernate初始化类
- 循环队列及C语言实现<三>
- 数组去重
- 单字符I/O:getchar()和putchar()
- NYOJ27水池数目(dfs)
- chmod权限
- git备忘录
- JavaWeb日记——Springboot 热部署的两种方式
- 计算字符串t在字符串s中出现的次数(KMP)
- MySQL查询语句大全
- spring面试题