小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. Algorithm
  5. Leetcode
  6. 正文

[leetcode]题目解析(190513)

2019年5月13日 1297点热度 0人点赞 0条评论

Search for a range

题目描述

Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in the array, return[-1, -1].For example,
Given[5, 7, 7, 8, 8, 10]and target value 8,
return[3, 4].

题目解析

关键词:有序数组、复杂度O(logn)。这就很明显说明了一件事,题目想让我们使用二分查找的方法来实现。在C++的STL里面,已经由现成的函数帮我们实现返回第一个位置和最后一个位置的函数,也就是low_bound()和upper_bound()。

low_bound()和upper_bound()都是使用二分查找实现查找工作,两者都是返回一个迭代器(iterator),但是不同的是,low_bound()返回的是第一个大于或等于num的数字,找到返回该数字的地址,(若数组中没有该元素,则返回end),upper_bound()返回第一个大于num的数字的位置(如果数组没有该元素,同理)。因此,如果我们对low_bound()返回的地址减去起始地址,就得到了第一次出现目标数字的位置,对upper_bound()减去起始地址,就得到了目标数字下一个位置,再减1,得到最后一次出现目标数字的位置。代码实现见代码实现1。

这是一种取巧的方法,使用了STL中的Algorithm。但是,这样除了展示你会使用STL中的algorithm之外没有任何用处,所以还是需要去实现这个寻找过程。这个过程就是实现二分查找。

其实现具体过程很基础,不再重述。

代码实现1

#include<iostream>
#include<algorithm>
#include<vector>
//故意把所需要包含的头文件注明了
using namespace std;

vector<int> searchRange(int A[], int n, int target) {
    vector<int> result(2,-1);//默认2个值,均为-1
    //若传递过来的参数不合理,直接返回result
    if(A==nullptr || n<=0)
        return result;
    //数组不是STL标准的容器,其没有真正的iterator,所以退化为了指针,用指针来表示
    auto low=lower_bound(A,A+n,target)-A;//auto类型应该为int
    if(low==n||A[low]!=target)
        return result;
    //以上是说若在数组内没有找到target,那么high也不用找,直接返回result
    auto high=upper_bound(A,A+n,target)-A-1;
    result[0]=low;
    result[1]=high;
    return result;
}

代码实现2

vector<int> searchRange(int A[], int n, int target) {
    vector<int>result(2, -1);
    if (A == nullptr || n <= 0)
        return result;
    int low = 0, high = n - 1;
    while (low <= high)
    {
        int mid = (low + high) >>1;
        if (A[mid] < target)
            low = mid + 1;
        else
            high = mid - 1;
    }
    int low2 = 0; 
    int high2 = n - 1;
    while (low2 <= high2)
    {
        int mid2 = (low2 + high2) >> 1;
        if (A[mid2] <= target)
            low2 = mid2 + 1;
        else
            high2 = mid2 - 1;
    }
    if (low <= high2)
    {
        result[0] = low;
        result[1] = high2;
    }
    return result;
}

Validate Binary Search Tree

题目描述

Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.The right subtree of a node contains only nodes with keys greater than the node's key.Both the left and right subtrees must also be binary search trees.

题目解析

暂无

代码实现

    bool isValidBST(TreeNode *root) {
        if(root==nullptr)
            return true;
        if(root->left)
            if(toRight(root->left)->val>=root->val)
                return false;
        if(root->right)
            if(toLeft(root->right)->val<=root->val)
                return false;
        return isValidBST(root->left)&&isValidBST(root->right);
    }
    TreeNode* toLeft(TreeNode* root){
        while(root->left)
            root=root->left;
        return root;
    }
    TreeNode* toRight(TreeNode *root){
        while(root->right)
            root=root->right;
        return root;
    }
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: leetcode
最后更新:2019年5月13日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程小奥看房之鸿荣源珈誉府论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
这是一种成长吗? 吴恩达深度学习课程 DeepLearning.ai 编程作业(2-1)Part.2 leetcode题目解析(191101) 2010 S.V Beijing Travel 3~4:唉 2011年高考莱芜一中再创佳绩 本BLOG即将连载博主的作品——《ultraman china》
标签聚合
算法 生活 linux 高中 leetcode 学习 鸟哥的linux私房菜 Java python学习 Python
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 8 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号