LeetCode笔记235-290中的简单题

235 二叉搜索树的最近公共祖先

题目
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

思路
自己想!!!都是二叉搜索树了,左边大往右跑;右边大,往左跑。。。

代码

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(p==nullptr || q==nullptr)
            return nullptr;
        TreeNode* temp = root;
        int mix = min(p->val,q->val);
        int man = max(p->val,q->val);
        while(temp!=nullptr){
            if(mix<=temp->val && man>=temp->val)
                break;
            if(man<temp->val)
                temp = temp->left;
            else
                temp = temp->right;
        }
        return temp;
 
    }
};

237 删除链表中的节点

题目
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定–要求被删除的节点。其他节点都不知道。

思路
很简单

代码

class Solution {
public:
    void deleteNode(ListNode* node) {
        while(node&&node->next)
        {
            node->val=node->next->val;
            if(node->next->next==nullptr)
                node->next = nullptr;
            else
                node = node->next;
        }
        
    }
};

242 有效的字母异位词

题目
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

思路:
整一个map,初始化为0,用a来++或者–;最后得到的map还是全为0就是的。

代码

class Solution {
public:
    bool isAnagram(string s, string t) {
        int map[26];
        for(int i=0;i<26;i++)
            map[i] = 0;
        for(auto c:s)
            map[c - 'a']++;
        for(auto c:t)
            map[c - 'a']--;
        for(int i=0;i<26;i++)
            if(map[i]!=0)
                return false;
        return true;
    }
};

257 二叉树路径

题目
返回所有从头到尾的路径

思路
自己想,我不会。。

代码

class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> ans;
        unit(root,"",ans);
        return ans;
    }
    
    void unit(TreeNode* root,string path,vector<string> &ans){
         if(!root) 
            return;
        if(!root->left&&!root->right)
        {
            ans.push_back(path+to_string(root->val));
            return;
        }
        
        unit(root->left,path+to_string(root->val)+"->",ans);
        unit(root->right,path+to_string(root->val)+"->",ans);
    }
};

258 各位相加

题目
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。

思路

代码

class Solution {
public:
    int addDigits(int num) {
        int sum = 0;
        while(num>9){
            while(num>0){
                sum += num%10;
                num = num/10;
            }
            num = sum;
            sum = 0;
        }
        return num;
    }
};

263 丑数

题目
编写一个程序判断给定的数是否为丑数。
丑数就是只包含质因数 2, 3, 5 的正整数。

代码

class Solution {
public:
    bool isUgly(int num) {
        if(!num)
            return 0;
        while(num%2==0)
            num /= 2;
        while(num%3==0)
            num /= 3;
        while(num%5==0)
            num /= 5;
        if(num==1)
            return true;
        else
            return false;
    }
};

268 确实数字

题目
给定一个包含 0, 1, 2, …, n 中 n 个数的序列,找出 0 … n 中没有出现在序列中的那个数。

思路
1.序号相加-数字相加
2.异或运算:X ^ Y ^ Y=X

代码:

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int n = nums.size();
        int result = 0,temp=0;
        for(int i=1;i<=n;i++)    
        {
            temp += nums[i-1];
            result+=i;
        }
        return result - temp;
    }
};

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int n = nums.size();
        int result = n;
        for(int i=0;i<n;i++)
        {
            result ^=i;
            result ^=nums[i];
        }
        return result;
    }
};

278 第一个错误的版本

题目
你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。

假设你有 n 个版本 [1, 2, …, n],你想找出导致之后所有版本出错的第一个错误的版本。

你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。

思路
二分查找法

代码

bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        int low = 1;
        int high =n;
        while(low<high){
             int middle=low + (high - low)/2;
            if(isBadVersion(middle))
            {
                if(isBadVersion(middle-1))
                    high=middle;
                else
                    return middle;
            }else{
                low = middle + 1;
            }
        }
        return n;
    }
};

283 移动零

题目
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

思路
一次遍历,不占用多余内存。

代码

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int n=0;
        for(int i=0;i<nums.size();i++){
            if(nums[i]!=0)
                nums[n++]=nums[i];  
        }

        for(;n<nums.size();n++){
            nums[n]=0;
        }
    }
};

290 单词规律

题目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值