zoukankan      html  css  js  c++  java
  • leetcode-剑指56-I-II-OK

    // language c
    // 剑指56-I
    // https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/
    // 通过了,但是肯定能优化,需要用到亦或,不太懂
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    
    // 排序是nlogn不太符合,但是好先试试吧
    int* singleNumbers(int* nums, int numsSize, int* returnSize){
        returnSize[0] = 2;
        int partition(int arr[],int low, int high){
            int pivot = arr[low];
            while (low <high){
                while(low <high && arr[high] >=pivot)   high--;
                arr[low] = arr[high];
                while(low <high && arr[low] <= pivot)   low++;
                arr[high] = arr[low];
            }
            arr[low] = pivot;
            return low;
        }
        void quick_sort_inside(int arr[], int start, int end){
            int pivot = partition(arr,start,end);
            if (start < pivot-1) quick_sort_inside(arr, start, pivot-1);
            if (end > pivot+1) quick_sort_inside(arr, pivot+1, end);
        }
        quick_sort_inside(nums,0,numsSize-1);
        int a;
        int b;
        bool a_found =false,done = false;
    
        void fill(int m){
            if(a_found){
                b = m;
                done = true;
            }else{
                a = m;
                a_found = true;
            }
    
        if(nums[0]<nums[1])
            fill(nums[0]);
        if(nums[numsSize-1]>nums[numsSize-2]){
            fill(nums[numsSize-1])
            if(done){
                nums[0] = a;
                nums[1] = b;
                return nums;
            }
        }
        for(int i=1; i<numsSize-1;i++){
            if((nums[i]>nums[i-1])&&(nums[i]<nums[i+1])){
                fill(nums[i])
                if(done){
                    nums[0] = a;
                    nums[1] = b;
                    return nums;
                }
            }
        }
        return nums;
    }
    
    // language c
    // 剑指56-II
    // https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/
    // 把I的代码改了改,大差不差
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    
    // 排序是nlogn不太符合,但是好先试试吧
    int singleNumber(int* nums, int numsSize){
        int partition(int arr[],int low, int high){
            int pivot = arr[low];
            while (low <high){
                while(low <high && arr[high] >=pivot)   high--;
                arr[low] = arr[high];
                while(low <high && arr[low] <= pivot)   low++;
                arr[high] = arr[low];
            }
            arr[low] = pivot;
            return low;
        }
        void quick_sort_inside(int arr[], int start, int end){
            int pivot = partition(arr,start,end);
            if (start < pivot-1) quick_sort_inside(arr, start, pivot-1);
            if (end > pivot+1) quick_sort_inside(arr, pivot+1, end);
        }
    
    
        quick_sort_inside(nums,0,numsSize-1);
    
    
    
        if(nums[0]<nums[1]){
            return nums[0];
        }
    
        if(nums[numsSize-1]>nums[numsSize-2]){
            return nums[numsSize-1];
        }
    
        for(int i=1; i<numsSize-1;i++){
            if((nums[i]>nums[i-1])&&(nums[i]<nums[i+1])){
                return nums[i];
            }
        }
        return 0;
    }
    
  • 相关阅读:
    网页素材收集
    【转】你离顶尖 Java 程序员,只差这11本书的距离
    Jetbrains 破解 2017
    WebStorm的常用操作
    浅谈MySQL主从复制
    Lombok注解指南
    【我的《冒号课堂》学习笔记】设计模式(3)行为模式
    【我的《冒号课堂》学习笔记】设计模式(2)结构模式
    【我的《冒号课堂》学习笔记】设计模式(1)创建模式
    【我的《冒号课堂》学习笔记】设计原则(4)保变原则
  • 原文地址:https://www.cnblogs.com/gallien/p/14337911.html
Copyright © 2011-2022 走看看