头像

要进亚研NLP和狗家AI组的男人


访客:5187

离线:3天前


活动打卡代码 LeetCode 6. Z 字形变换

class Solution {
public:
    string convert(string s, int numRows) {
      if (numRows == 1) return s;

        int step = numRows * 2 - 2; 
        int index = 0;
        int len = s.length();
        int add = 0; 
        string ret;
        for (int i = 0; i < numRows; i++) 
        {
            index = i;
            add = i * 2;
            while (index < len)
            {
                ret += s[index]; 
                add = step - add;
                index += (i == 0 || i == numRows-1) ? step : add; 
            }
        }
        return ret;  
    }
};




class Solution {
public:
    int lengthOfLongestSubstring(string s) {
                unordered_set<char> occ;
        int n = s.size();

        int rk = -1, ans = 0;

        for (int i = 0; i < n; ++i) {
            if (i != 0) {

                occ.erase(s[i - 1]);
            }
            while (rk + 1 < n && !occ.count(s[rk + 1])) {

                occ.insert(s[rk + 1]);
                ++rk;
            }

            ans = max(ans, rk - i + 1);
        }
        return ans;
    }
};



class Solution {
public:
    int lengthOfLongestSubstring(string s) {

        unordered_set<char> occ;
        int n = s.size();

        int rk = -1, ans = 0;

        for (int i = 0; i < n; ++i) {
            if (i != 0) {

                occ.erase(s[i - 1]);
            }
            while (rk + 1 < n && !occ.count(s[rk + 1])) {

                occ.insert(s[rk + 1]);
                ++rk;
            }

            ans = max(ans, rk - i + 1);
        }
        return ans;
    }
};


活动打卡代码 LeetCode 5. 最长回文子串

class Solution {
public:
    string longestPalindrome(string s) {
        int len=s.size();
        if(len==0||len==1)
            return s;
        int start=0;
        int max=1;
        vector<vector<int>>  dp(len,vector<int>(len));
        for(int i=0;i<len;i++)
        {
            dp[i][i]=1;
            if(i<len-1&&s[i]==s[i+1])
            {
                dp[i][i+1]=1;
                max=2;
                start=i;
            }
        }
        for(int l=3;l<=len;l++)
        {
            for(int i=0;i+l-1<len;i++)
            {
                int j=l+i-1;
                if(s[i]==s[j]&&dp[i+1][j-1]==1)
                {
                    dp[i][j]=1;
                    start=i;
                    max=l;
                }
            }
        }
        return s.substr(start,max);
    }
};


活动打卡代码 LeetCode 7. 整数反转

class Solution {
public:
    int reverse(int x) {
        int temp=0;
        while(x!=0){
            if( temp> INT_MAX /10 || temp<INT_MIN /10)
                return 0;
            temp = temp*10+x%10;
            x = x/10;
        }
        return temp;
    }
};


活动打卡代码 LeetCode 9. 回文数

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)
            return false;
        if(x>=0 && x<10)
            return true;

        long long temp = 0;
        int xx = x;
        while(x!=0){
            int n = x % 10;
            temp = temp * 10 + n;
            x /=10;
        }
        if(temp == xx)
            return true;
        else
        {
            return false;
        }

    }
};


活动打卡代码 LeetCode 2. 两数相加

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head=new ListNode(-1);
        ListNode* h=head;
        int sum=0;
        bool carry=false;
        while(l1!=NULL||l2!=NULL)
        {
            sum=0;
            if(l1!=NULL)
            {
                sum+=l1->val;
                l1=l1->next;
            }
            if(l2!=NULL)
            {
                sum+=l2->val;
                l2=l2->next;
            }
            if(carry)
                sum++;
            h->next=new ListNode(sum%10);
            h=h->next;
            carry=sum>=10?true:false;
        }
        if(carry)
        {
            h->next=new ListNode(1);
        }
        return head->next;
    }
};


活动打卡代码 LeetCode 1. 两数之和

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        map<int,int> m;
        vector<int> res;
        for(int i=0;i<nums.size();i++)
            m[nums[i]] = i; //用原数组的值作为map的key,下标作为value
        //target 4 ,第一个元素2,4-2 = 2
        for(int i=0;i<nums.size();i++){
            int temp = target - nums[i];
            if(m.count(temp) && m[temp]!=i){
                res.push_back(i);
                res.push_back(m[temp]);
                break;
            }
        }
        return res;
    }
};



题目描述

将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

样例

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

C++ 代码

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1 == NULL){
            return l2;
        }else if(l2 == NULL){
            return l1;
        }else{
            if(l1->val <= l2->val){
                l1->next = mergeTwoLists(l1->next,l2);
                return l1;
            }
            else{
                l2->next = mergeTwoLists(l1,l2->next);
                return l2;
            }
        }
    }
};




C++ 代码

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int result = nums.size();
        int i=0;

        for(int num:nums){
            result ^= num;
            result ^= i;
            i++;
        }

        return result;
    }
};