数组去重

有关数组的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
arr[begin]>arr[mid]
应该是6789 1 2345 后面肯定是升序 然后根据target目标来决定前面还是后面
arr[begin]<arr[mid]
应该是 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)
{





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值