leetcode-x的平方根/删除排序链表中重复元素/合并2个有序数组/相同的树

这篇博客详细介绍了LeetCode上的四道经典算法题:使用二分法求解x的平方根问题;删除排序链表中的重复元素;合并两个有序数组的策略;以及通过中序遍历判断两棵树是否相同。每个题目都附有解题思路和关键点。
摘要由CSDN通过智能技术生成

x的平方根

题目链接:https://leetcode-cn.com/problems/sqrtx/

第一个想到的方法是二分法。

class Solution {
public:
    int mySqrt(int x) {
        double left=0;
        double right=x;
        while(int(left)<int(right))
        {
            double mid=left+(right-left)/2;  
            if(mid*mid==x)
                return int(mid);
            else if(mid*mid>x)
                right=mid;
            else
                left=mid;                
        }
        return int(left);
    }
};

看参考答案,提出了牛顿迭代法。。

image.png

class Solution {
public:
    int mySqrt(int x) {
        if(x==0)
            return 0;
        double pre=2;
        while(1)
        {
            double cur=(pre+x/pre)/2;
            if(int(pre)==int(cur))
                return int(pre);
            else
                pre=cur;
        }
    }
};

删除排序链表中重复元素

题目链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/

自立更生

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==NULL)
            return head;
        ListNode *pbefore=head;
        ListNode *pcur=pbefore->next;
        while(pcur)
        {
            if(pcur->val==pbefore->val)
            {
                ListNode *pnext=pcur->next;
                pbefore->next=pnext;
                delete pcur;
                pcur=pnext;                
            }
            else
            {
                pbefore=pcur;
                pcur=pcur->next;
            }
        }
        return head;
    }
};

简易版本(但会内存泄漏)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *cur=head;
        while(cur&&cur->next)
        {
            if(cur->val==cur->next->val)
                cur->next=cur->next->next;
            else
                cur=cur->next;
        }
        return head;
    }
};

合并2个有序数组

题目链接:https://leetcode-cn.com/problems/merge-sorted-array/

注意点:

(1)注意任一个数组都可以为空,即要考虑m或n为0的情况

(2)从后往前进行比较与覆盖

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        if(m==0)
        {
            for(int i=0;i<n;i++)
            {
                nums1[i]=nums2[i];
            }
            return;
        } 
        if(n==0)
            return;
        int len=m+n-1;
        m-=1;
        n-=1;
        while(m>=0&&n>=0)
        {
            if(nums1[m]<nums2[n])
                nums1[len--]=nums2[n--];
            else
                nums1[len--]=nums1[m--];
        }
        if(n>=0)
        {
            while(n>=0)
            {
                nums1[len--]=nums2[n--];
            }
        }
        return;
    }
};

相同的树

题目链接:https://leetcode-cn.com/problems/same-tree/

中序遍历的递归方法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p==NULL&&q==NULL)
            return true;
        else if(p==NULL||q==NULL)
            return false;
        else if(p->val!=q->val)
            return false;
        return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
    }
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值