leetcode 存在重复元素/2的幂/二叉搜索树的最近公共祖先/删除链表节点

存在重复元素

题目链接 力扣

法一 排序不多说

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        for(int i=1;i<nums.size();i++)
        {
            if(nums[i]==nums[i-1])
                return true;
        }
        return false;
    }
};

法二 使用map(熟悉map的用法)

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        map<int,int> m;
        for(int i=0;i<nums.size();i++)
        {
            m[nums[i]]++;
            if(m[nums[i]]>1)
                return true;
        }
        return false;
    }
};

2的幂

题目链接:力扣

利用二进制来解答,如果一个数是2的幂次方,其二进制表示中除了第一位其余都是0(10  100 1000)

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return n>0 && (n&(n-1))==0;
    }
};

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

题目链接:力扣

法一 递归

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(p->val<root->val&&q->val<root->val)
            return lowestCommonAncestor(root->left,p,q);
        else if(p->val>root->val&&q->val>root->val)
            return lowestCommonAncestor(root->right,p,q);
        else
            return root;
    }
};

法二 递归改循环

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        int pval=p->val;
        int qval=q->val;
        
        int rval=root->val;
        
        while(root)
        {
            if(pval<rval&&qval<rval)
            {
                root=root->left;
                rval=root->val;
            }
            
            else if(pval>rval&&qval>rval)
            {
                root=root->right;
                rval=root->val;
            }
            else
                return root;
        }
        
        return NULL;
    }
};

删除链表节点

题目链接:力扣

与下一个节点交换即可,因为题目已经说明不是末尾节点

class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val=node->next->val;
        node->next=node->next->next;
    }
};

NIM游戏

题目链接 力扣

其实蛮好推的,,,,一开始以为很难,举个例子就好了

class Solution {
public:
    bool canWinNim(int n) {
        if(n%4==0)
            return false;
        return true;
    }
};

尼姆博弈扩展形式(一): 限定每次取物的上限。NYOJ-135,难度5~~~_NYIST_TC_LYQ的博客-CSDN博客

反转字符串

题目链接: 力扣

法一 直接reverse

class Solution {
public:
    void reverseString(vector<char>& s) {
        reverse(s.begin(),s.end());
    }
};

法二 双指针

class Solution {
public:
    void reverseString(vector<char>& s) {
        int i=0;
        int j=s.size()-1;
        while(i<j)
        {
            swap(s[i],s[j]);
            i++;
            j--;
        }
    }
};

反转字符串中的单词

题目链接: 力扣

class Solution {
public:
    string reverseWords(string s) {
        string ans="";   //最终结果
        string temp="";   //每个中间结果
        
        for(int i=0;i<=s.size();i++)
        {
            if(s[i]==' '||s[i]=='\0')
            {
                reverse(temp.begin(),temp.end());
                ans+=temp+s[i];
                temp="";
            }
            else
                temp+=s[i];
        }
        return ans;
    }
};

原地转换

class Solution {
public:
    string reverseWords(string s) {
        int len = s.size();
        if(len==0) return s;
        int i=0,start=0;
        while(i<len){
            while(i<len&&s[i]!=' ') ++i;
            int end = i-1;
            while(start<end){
                swap(s[start],s[end]);
                ++start;
                --end;
            }

            while(i<len&&s[i]==' ') ++i;
            start = i;
        }
        return s;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值