Leetcode- 1-5题 题解

最近开始刷Leetcode了,大三狗还没有找到实习,很菜啊,希望明年三月份能找份实习工作吧!2017年最后一篇博客,明天都元旦了,提前对大家说元旦快乐哈!

下面所有代码在我GitHub上也有,地址:https://github.com/zzuliLL/Leetcode-


1Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
题意:给你一个数组,求数组两个数和为target的下标

代码:

//O(n^2)
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> s;
        for(int i = 0; i < nums.size(); i++)
        {
            for(int j = i + 1; j < nums.size(); j++)
                if(nums[i] + nums[j] == target)
                {
                    s.push_back(i);
                    s.push_back(j);
                    break;
                }
            if(s.size()) break;
        }
        return s;
    }
};

//O(n*log(n))
//通过map进行二分
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> s;
        map<int, int> mp;
        for(int i = 0; i < nums.size(); i++)
        {
            if(mp.find(target - nums[i]) != mp.end())
            {
                s.push_back(mp[target - nums[i]]);
                s.push_back(i);
                break;
            }
            mp[nums[i]] = i;
        }
        return s;
    }
};
2 Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

题意:给你两个链表,链表代表两个数,求他们的和

解析:大数模拟下就可以了

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *l = new ListNode(0), *o;
        o = l;
        int p = 0;
        while(l1&&l2)
        {
            int cur = l1->val + l2->val + p;
            ListNode *now = new ListNode(cur%10);
            o->next = now;
            o = now;
            p = cur / 10;
            l1 = l1->next;
            l2 = l2->next;
        }
        while(l1)
        {
            int cur = l1->val + p;
            ListNode *now = new ListNode(cur%10);
            o->next = now;
            o = now;
            p = cur / 10;
            l1 = l1->next;
        }
        while(l2)
        {
            int cur = l2->val + p;
            ListNode *now = new ListNode(cur%10);
            o->next = now;
            o = now;
            p = cur / 10;
            l2 = l2->next;
        }
        if(p)
        {
            ListNode *now = new ListNode(p);
            o->next = now;
        }
        return l->next;
    }
};
3 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.

题意:求不连续不同字符的最大个数,注意不是光字母,还有其他字符比如{}等

代码:

//复杂度O(n),简单尺取法

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int l, r, ans = 0;
        l = r = 0;
        int len = s.length();
        map<char, bool> mp;
        while(r < len)
        {
            if(mp[s[r]])
            {
                ans = max(ans, r - l);
                while(l < r && s[l] != s[r])
                {
                    mp[s[l]] = false;
                    l++;
                }
                l++;
            }
            mp[s[r]] = true;
            r++;
        }
        ans = max(ans, r - l);
        return ans;
    }
};
4 Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

题意:给你两个有序数组,求中位数,要求最大复杂度为O(log(m+n)),显然要二分哈~,我是看dml学长的题解才会的

代码:

//*********二分*********
//
//要找中位数就是找中间的那个数,如果是m+n是奇数,就是找(m+n)/2,如果是偶数,就是找(m+n)/2和(m+n)/2+1两个数的平均数
//换句话说就是在两个有序数组中找第k大的数,在第一个数组中找k/2的数a,在第二个数组中找k-k/2的数b,
//如果a>b,第二个数组中前k-k/2个数中必然不包括第k个数,将第二个数组前k-k/2个数舍弃。反之亦然


class Solution {
public:
    int find_k(vector<int>& a, int l, int r, vector<int>& b, int ll, int rr, int k)
    {
        if(r - l < rr - ll) return find_k(b, ll, rr, a, l, r, k);
        if(rr == ll) return a[l+k-1];
        if(k == 1) return min(a[l], b[ll]);

        int q = min(k/2, rr-ll);
        int p = k - q;
        if(a[l+p-1] > b[ll+q-1]) return find_k(a, l, r, b, ll+q, rr, k - q);
        return find_k(a, l+p, r, b, ll, rr, k - p);
    }

    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
         int len1 = nums1.size();
         int len2 = nums2.size();
         int k = (len1 + len2) / 2;
         if((len1+len2)&1)
            return find_k(nums1, 0, len1, nums2, 0, len2, k+1);
         else
            return (find_k(nums1, 0, len1, nums2, 0, len2, k+1) + find_k(nums1, 0, len1, nums2, 0, len2, k)) / 2.0;;
    }
};
5 Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

题意:求最长回文串,数据范围才1000,暴力也可以,这里给的是O(n)的manacher算法

代码:

//暴力O(n^2)也能过,数据才1000,所以很简单,下面是manacher算法,复杂度O(n)

class Solution
{
public:
    string longestPalindrome(string s)
    {
        int len = s.length();
        char ss[(len+6)<<1];
        int dp[(len+6)<<1];
        for(int i = len; i >= 0; i--)
        {
            ss[i * 2 + 1] = s[i];
            ss[i * 2] = '#';
        }

        int mx, id, ans;
        ans = mx = id = 0;
        for(int i = 0; ss[i]; i++)
        {
            dp[i] = mx > i ? min(dp[2*id-i], mx - i) : 1;
            while(i - dp[i] >= 0 && ss[i - dp[i]] == ss[i + dp[i]]) dp[i]++;
            if(mx - i < dp[i])
            {
                mx = i + dp[i];
                id = i;
            }
            if(dp[ans] < dp[i]) ans = i;
        }

        string ans_s = "";
        for(int j = ans - dp[ans] + 1; j < ans + dp[ans]; j++)
            if(ss[j] != '#') ans_s += ss[j];

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值