Leecode Day 1

目錄

1. Two 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].
思想:簡單查找的想法,還可以用<map>的思想,採用的是最笨的想法。
代碼如下:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i=0;i<nums.size();i++){
            for(int j=i+1;j<nums.size();j++){
                if(target-nums[i]==nums[j]){
                    return {i,j};
                }
            }
        }
    }
};

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) {
        if(l1==nullptr)return l1;
        if(l2==nullptr)return l2;
        ListNode *r=new ListNode(-1),*res=r;//*res=r等價於 ListNode *res;res=r;
        ListNode *q=l1,*p=l2;
        int carry=0;
        while(p||q||carry){
            int sum=carry;
            if(p){
                sum+=p->val;
                p=p->next;
            }
            if(q){
                sum+=q->val;
                q=q->next;
            }
            r->next=new ListNode(sum%10);
            carry=sum/10;
            r=r->next;
        }
        return res->next;
    }
};

3. Longest Substring Without Repeating Characters

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

Example 1:

Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:

Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Example 3:

Input: “pwwkew”
Output: 3
Explanation: 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.
思想:使用set容器,設置左右邊界,出現重複元素,擦除最左元素,並更新max值,未出現不斷加入倒容器中。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len=s.length(),max=0;
        set<char> sp;
        int left=0,right=0;
        while(right<len){
            if(sp.find(s[right])==sp.end()){
                sp.insert(s[right]);
                right++;
            }else{
                if(sp.size()>max){
                    max=sp.size();
                }
                sp.erase(s[left]);
                left++;
            }
        }
        return max>sp.size()?max:sp.size();
    }
};

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章