Lectcode手撕算法(C++)

Lectcode手撕算法

两数之和
删除数组中的重复项
移除元素
整数反转
回文数
最长公共前缀
合并两个有序链表
有效括号

  1. lectcode-1:两数之和:
    题目:https://leetcode-cn.com/problems/two-sum/
    解答:
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int length=nums.size();
        int i,j;
        for(int i=0;i<length-1;i++)
        {
            for(int j=i+1;j<length;j++)
            {
                if(target==nums[i]+nums[j])
                    return {i,j};
            }  
            
        }
        return {i,j};
    }
        
};

  1. lectcode-26:删除数组中的重复项

题目:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
解答:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int len=nums.size();
        if(len==0)
            return 0;
        int j=0;
        for(int i=1;i<len;i++)
        {
            if(nums[i]!=nums[j])
            {
                j++;
                nums[j]=nums[i];
            }
        }
        return j+1;
    }
};
  1. lectcode-27 移除元素

题目:https://leetcode-cn.com/problems/remove-element/
解答:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int len=nums.size();
        if(len==0)
            return 0;
        int j=0;
        for(int i=0;i<len;i++)
        {
            if(nums[i]!=val)
            {
                nums[j]=nums[i];
                j=j+1;
            }
        }
        return j;
    }
};
  1. lectcode-7:整数反转

题目:https://leetcode-cn.com/problems/reverse-integer/
解答:通过循环将数字x的每一位拆开,在计算新值时每一步都判断是否溢出。
溢出条件有两个,一个是大于整数最大值MAX_VALUE,另一个是小于整数最小值MIN_VALUE,设当前计算结果为ans,下一位为pop。
参考链接:https://leetcode-cn.com/problems/reverse-integer/solution/hua-jie-suan-fa-7-zheng-shu-fan-zhuan-by-guanpengc/

class Solution {
public:
    int reverse(int x)
    {
        long y=0;
        int a;
        while(x)
        {
            a=x%10;
            x=x/10;
            if(y>INT_MAX/10 || y==INT_MAX/10&&a>7) 
            	return 0;
            if(y<INT_MIN/10 || y==INT_MIN/10&&a<-8) 
            	return 0;
            y=y*10+a;
            
        }
        return y;
    }
};
  1. lectcode-9:回文数

题目:https://leetcode-cn.com/problems/palindrome-number/
解答:

class Solution {
public:
    bool isPalindrome(int x) {
        bool flag=false;
        long k=0;
        long a=0;
        int b=x;
        if(x<0)
            return flag;
        else if(x>=0)
        {
            while(x != 0)
            {
                k=x%10;
                a=k+a*10;
                x=x/10;
            }
            if(a==b)
                flag=true;
        }
        return flag;
    }
};
  1. lecrcode-14:最长公共前缀

题目:https://leetcode-cn.com/problems/longest-common-prefix/
解答:

class Solution {
public:
     string longestCommonPrefix(vector<string>& strs) {
        string ans="";
        if(strs.size()==0)
            return ans;
        if(strs.size()==1)
            return strs[0];
        string min=strs[0];
        for(int i=0; i<strs.size();i++)
        {
            if(strs[i].empty())
            {
                return ans;
                cout<<ans<<endl;
            }
        }
        for(int i=0;i<min.size();++i)
        {
            //if(min==strs[i])
               // return ans;
            //char temp=strs[0][i];
            for(int j=0;j<strs.size();j++)
           {
               if(min[i]!=strs[j][i])
                   return ans;
           }
            ans=ans+min[i];
        }
        return ans;
    }
};
  1. lectcode-21:合并两个有序链表

题目:https://leetcode-cn.com/problems/merge-two-sorted-lists/
解答:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* dummypHead=new ListNode(0);
        ListNode* k=dummypHead;
        while(l1!=NULL && l2!=NULL)
        {
            if(l1->val < l2->val)
            {
                k->next=l1;
                k=k->next;
                l1=l1->next;
            }
            else
            {
                k->next=l2;
                k=k->next;
                l2=l2->next;
            } 
        }
            if (l1!=NULL)
            {
                k->next=l1;
            }
            if(l2!=NULL)
            {
                k->next=l2;
            }
        return dummypHead->next;
    }
};
  1. lectcode-20:有效括号

题目:https://leetcode-cn.com/problems/valid-parentheses/
解答:hash表,建立map;

class Solution {
public:
    bool isValid(string s) {
        if(s.length()==0)
            return true;
        if(s.length()%2!=0)
            return false;
        map<char,char> map={{')','('},{'}','{'},{']','['}};
        stack<char> sp;
        for(char& c : s)
        {
            if(c=='('||c=='{'||c=='[')
            {
                sp.push(c);
            }
            if(c==')'||c=='}'||c==']')
                if(!sp.empty()&&sp.top()==map[c])
                    sp.pop();
            else
                return false;
        }
        return sp.empty();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值