栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

leetcode刷题 ----- 数组部分

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode刷题 ----- 数组部分

Note:刷题顺序参考于代码随想录,写的很棒。 part 1. 二分查找

704 二分查找

int search(int* nums, int numsSize, int target){
    int low, high;
    low = 0;
    high = numsSize - 1;
    while(low <= high){
        int mid = (low + high) / 2;
        if(nums[mid] == target)
            return mid;
        if(nums[mid] < target)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return -1;
}

35 搜索插入位置

int searchInsert(int* nums, int numsSize, int target){
    int low, high;
    low = 0;
    high = numsSize - 1;

    while(low <= high){
        int mid = (low + high) / 2;
        if(nums[mid] == target)
            return mid;
        else if(nums[mid] > target)
            high = mid -1;
        else 
            low = mid + 1; 
    }
    return low;
}

34 在排序数组中查找元素的第一个和最后一个位置

int getRightBorder(int *nums, int numsSize, int target){
    int low, high, rBorder;
    low = rBorder = 0;
    high = numsSize - 1;
    
    while (low <= high){
        int mid = (low + high) / 2;
        if (nums[mid] <= target){
            low = mid + 1;
            rBorder = mid;
        }else
            high = mid - 1;
    }
    return nums[rBorder] == target ? rBorder : -1;
}

int getLeftBorder(int *nums, int numsSize, int target){
    int low, high, lBorder;
    low = lBorder = 0;
    high = numsSize - 1;

    while (low <= high){
        int mid = (low + high) / 2;
        if (nums[mid] >= target){
            high = mid - 1;
            lBorder = mid;
        }else
            low = mid + 1;
    }
    return nums[lBorder] == target ? lBorder : -1;
}

int* searchRange(int* nums, int numsSize, int target, int* returnSize){
    int *ret = (int*)malloc(sizeof(int) * 2);
    *returnSize = 2;
    if(numsSize == 0){
        ret[0] = ret[1] = -1;
        return ret;
    }
    ret[0] = getLeftBorder(nums, numsSize, target);
    ret[1] = getRightBorder(nums, numsSize, target);
    
    return ret;
}

69 X的平方根

int mySqrt(int x){
    long ret, low, high;
    low = 0;
    high = x;
    while (low <= high){
        long mid = ( low + high ) / 2;
        if( mid * mid <= x){
            low = mid + 1;
            ret = mid;
        }else
            high = mid - 1;
    }
    return ret;
}

307 有效的完全平方数

bool isPerfectSquare(int num){
    long x, low, high;
    low = 0;
    high = num;
    
    while (low <= high){
        long mid = (low + high) / 2;
        if(mid * mid <= num){
            x = mid;
            low = mid + 1;
        }else
            high = mid - 1;
    }
    return x * x == num ? true : false;
}
Part 2. 移除元素

27 移除元素

int removeElement(int* nums, int numsSize, int val){
    int low, high, ret;
    low = ret = 0;
    high = numsSize - 1;
    if(numsSize == 0)
        return ret;
    while (low < high){
        if(nums[low] != val){
            ret++;
            low++;
        }else{
            nums[low] = nums[high];
            high--;
        }
    }
    return nums[low] == val ? ret : ret + 1;
}

26 删除有序数组中的重复项

int removeDuplicates(int* nums, int numsSize){
    int slowIndex, fastIndex;
    slowIndex = 0;

    for(fastIndex = 1; fastIndex < numsSize; fastIndex++){
        if(nums[slowIndex] != nums[fastIndex])
            nums[++slowIndex] = nums[fastIndex];
    }
    return slowIndex+1;
}

283 移动零

void moveZeroes(int* nums, int numsSize){
    int slowIndex, fastIndex;
    slowIndex = 0;

    for (fastIndex = 0; fastIndex < numsSize; fastIndex++){
        if(nums[fastIndex] != 0){
            int temp = nums[slowIndex];
            nums[slowIndex++] = nums[fastIndex];
            nums[fastIndex] = temp;
        }
    }
}

944 比较含退格的字符串

void handle(char *s){
    int slowIndex, fastIndex;
    slowIndex = 0;

    for (fastIndex = 0; s[fastIndex] != ''; fastIndex++){
        if (s[fastIndex] != '#')
            s[slowIndex++] = s[fastIndex];
        else{
            if (slowIndex != 0)
                slowIndex--;
        }
    }
    s[slowIndex] = '';
}

bool backspaceCompare(char * s, char * t){
    handle(s);
    handle(t);
    return strcmp(s, t) == 0 ? true : false;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993573.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号