领扣[LeetCode]从零开始[使用C++][1,10]

0.序

以后不做后端开发是不是就用不到C++了?真香。话不多说,我已经躺倒在第一题上了。不贴题目了,持续更新。

1.两数之和

原文:https://www.cnblogs.com/grandyang/p/4130379.html

解1:

搬过来是搬过来了,理解了才是自己的。但是理解也要好久啊o(╥﹏╥)o。

class Solution {
public: //公有,谁都能调用这里面的东西
    vector<int> twoSum(vector<int>& nums, int target) { //vector<int>是返回值,twoSum是函数名
        unordered_map<int, int> m; //哇,m是一个哈希表,元素无序但是搜索的时间复杂度是O(1),然后空间复杂度高了呗,但是LeetCode里谁快谁是大哥
        vector<int> res;
        for (int i = 0; i < nums.size(); ++i) { //由值得到下标
            m[nums[i]] = i;
}
for (int i = 0; i < nums.size(); ++i) { int t = target - nums[i]; if (m.count(t) && m[t] != i) { //t存在且不是它自己,count()统计了t出现的次数 res.push_back(i); //把i放进一个叫res的容器
res.push_back(m[t]); //知道了t的下标,把它放进容器
break; //跳出for循环 } } return res; } };

解2:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) { //这里加一个&到底是干嘛的呢
        unordered_map<int, int> m; //又是一个哈希表
        for (int i = 0; i < nums.size(); ++i) { //喵的,上面两个for循环这里直接就一个了,原理都是一样的
            if (m.count(target - nums[i])) {
                return {i, m[target - nums[i]]};
            }
            m[nums[i]] = i;
        }
        return {};
    }
};

解3:

好的,接下来让我自己来写一个时间复杂度超高的解。emmm,执行时间108ms,是上面两个解的13.5倍。要不要先判断非空呀?

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};
        return {};
    }
};

2018-12-6 提交


 2.两数相加

原文:https://blog.csdn.net/qq_32805671/article/details/79883391

解:

链表的知识,在数据结构里学过,看了下面的代码才想起来怎么用,哭。

/**
 * 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 *result = new ListNode(0); //给0分配内存,作为头结点
        ListNode *tmp = result; //temporal,临时指针tmp
        int sum = 0;
        while(l1 || l2){      
            if(l1){
                sum += l1->val;
                l1 = l1->next;
            }
            if(l2){
                sum += l2->val;
                l2 = l2->next;
            }
            tmp->next = new ListNode(sum%10);
            sum /= 10; //如果sum的值大于等于10的话是要进1位的
            tmp = tmp->next;
        } //while
        if(sum)
            tmp->next = new ListNode(1);
        return result->next;
    } //addTwoNumbers
}; //class Solution

2018-12-7 提交


 3. 无重复字符的最长子串

原文:https://www.cnblogs.com/ariel-dreamland/p/8668286.html

解1:

class Solution 
{
public:
    int lengthOfLongestSubstring(string s){
        int m[256] = {0}, res = 0, left = 0;//ASCII有256个字符
        for (int i = 0; i < s.size(); ++i){        
            if(m[s[i]]==0||m[s[i]]<left){//字符串中第i个元素以前没出现过,或出现在很左边的位置
                res = max(res, i-left+1);
            }else{
                left=m[s[i]];
            }
            m[s[i]]=i+1;//为了和缺省的0区分,所以要+1吧
        }
        return res;
    }
};

enmm,花了一个小时才大概搞懂一题的一个解哟,还没完呢。从sublime复制过来的注释是绿色的耶。

2018-12-8 提交

解2:

解1的简化。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int> m(256, -1);
        int res = 0, left = -1;
        for (int i = 0; i < s.size(); ++i) {
            left = max(left, m[s[i]]);
            m[s[i]] = i;
            res = max(res, i - left);
        }
        return res;
    }
};

解3:

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

解4:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int res = 0, left = 0, i = 0, n = s.size();
        unordered_map<char, int> m;
        for (int i = 0; i < n; ++i) {
            left = max(left, m[s[i]]);
            m[s[i]] = i + 1;
            res = max(res, i - left + 1);
        }
        return res;
    }
};

2018-12-9 提交


-

 

闭合括号

 1 import java.util.*;
 2 
 3 
 4 public class Solution {
 5     /**
 6      * 
 7      * @param s string字符串 
 8      * @return bool布尔型
 9      */
10     public boolean isValid (String s) {
11         // write code here
12         char[] chars = s.toCharArray();
13         LinkedList<Character> ll = new LinkedList();
14         for(char c: chars){
15             if(c=='('){
16                 ll.add(')');
17             }else if(c=='['){
18                 ll.add(']');
19             }else if(c=='{'){
20                 ll.add('}');
21             }else if(ll.isEmpty()||ll.removeLast()!=c) return false;
22         }
23         return ll.isEmpty();
24     }
25 }

 

posted @ 2018-12-06 20:21  wooooc  阅读(591)  评论(0)    收藏  举报