丢人代码大全

记录一些思路一样,可是性能和简洁性被吊打的我的代码,希望可以慢慢从中学到一些写代码的思考方法和思想

leetcode 55.跳跃游戏

在这里插入图片描述

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int max = 0, sign = 0;
        int flag = 1;
        while(flag != max)
        {
            flag = max;
            max = max>(nums[flag]) ? max : (nums[flag]);
            for(int i=1; i<=nums[sign] && (i+sign) < nums.size(); i++)
                if(max < (sign+i+nums[i+sign]))
                    {
                        max = sign+i+nums[i+sign];
                        sign = i+sign;
                    }
            if(max >= nums.size() - 1)
                return true;
        }
        return false;
    }
};

大佬
在这里插入图片描述

bool canJump(vector<int>& nums) 
{
	int k = 0;
	for (int i = 0; i < nums.size(); i++)
	{
		if (i > k) return false;
		k = max(k, i + nums[i]);
	}
	return true;
}

思路相同,我却浪费了那么多空间和运算…


leetcode 136. 只出现一次的数字

题目
在这里插入图片描述

利用set不会重复存储元素的特点

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        set<int> ms;
        for(auto c:nums)
        {
            if(ms.count(c) == 0)
                ms.insert(c);
            else
                ms.erase(c);
        }
        auto c = *ms.begin();
        return c;
    }
};

大佬
在这里插入图片描述
利用了异或位运算的魔力

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int ret = 0;
        for (auto e: nums) ret ^= e;
        return ret;
    }
};

我是fw

leetcode 141. 环形链表

题目
在这里插入图片描述

class Solution {
public:
    bool hasCycle(ListNode *head) {
        set<ListNode*> ms;
        while(head != nullptr)
        {
            ms.insert(head);
            head = head->next;
            if(ms.find(head) != ms.end())
                return true;
        }
        return false;
    }
};

大佬

利用了快慢指针

class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode* slow = head;
        while(head != nullptr)
        {
            if(head->next !=nullptr)
                head = head->next->next;
            else
                head = nullptr;
            slow = slow->next;
            if(slow == head && slow != nullptr)
                return true;
        }
        return false;
    }
};

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

题目
在这里插入图片描述

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        string now;
        int max = 0, point = 0, i = 0;
        while(i < s.length()){
            if(now.find(s[i]) != string::npos){
                point = now.find(s[i]);
                string temp(now);
                now.clear();
                for(int j = 0; j < temp.length()-point-1; j++)
                    now += temp[j+point+1];
            }
            else{
                now += s[i];
                i++;
            }
            max = max > now.length() ? max: now.length();
        }
        return max;
    }
};

大佬
在这里插入图片描述

    int lengthOfLongestSubstring(string s) {
        vector<int> m(128, 0);
        int ans = 0;
        int i = 0;
        for (int j = 0; j < s.size(); j++) {
            i = max(i, m[s[j]]);
            m[s[j]] = j + 1;
            ans = max(ans, j - i + 1);
        }
        return ans;
    }

leetcode 36. 有效的数独

题目
在这里插入图片描述

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        //检查行
        for(int i=0; i<9; i++){ //i表示行数
            for(int j=1; j<10; j++){
                if(count(board[i].begin(), board[i].end(), (j+'0')) > 1){
                    return false;
                }
            }    
        }
        //检查列
        vector<char> temp(9,'.');
        for(int i=0; i<9; i++){ //i表示列数
            for(int j=0; j<9; j++){
                temp[j] = board[j][i];
            }
            for(int j=1; j<10; j++){
                if(count(temp.begin(), temp.end(), (j+'0')) > 1){
                    return false;
                }
            }    
        }
        //检查格
        for(int i=0; i<3; i++){ //i表示行数
            for(int j=0; j<3; j++){
                temp = {board[i*3][j*3],board[i*3][j*3+1],board[i*3][j*3+2],board[i*3+1][j*3],board[i*3+1][j*3+1],board[i*3+1][j*3+2],board[i*3+2][j*3],board[i*3+2][j*3+1],board[i*3+2][j*3+2]};
                for(int k=1; k<10; k++){
                    if(count(temp.begin(), temp.end(), (k+'0')) > 1){
                        return false;
                    }
                }
            }    
        }
        return true;                
    }
};

大佬
在这里插入图片描述

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int row[9][10] = {0};// 哈希表存储每一行的每个数是否出现过,默认初始情况下,每一行每一个数都没有出现过
        // 整个board有9行,第二维的维数10是为了让下标有9,和数独中的数字9对应。
        int col[9][10] = {0};// 存储每一列的每个数是否出现过,默认初始情况下,每一列的每一个数都没有出现过
        int box[9][10] = {0};// 存储每一个box的每个数是否出现过,默认初始情况下,在每个box中,每个数都没有出现过。整个board有9个box。
        for(int i=0; i<9; i++){
            for(int j = 0; j<9; j++){
                // 遍历到第i行第j列的那个数,我们要判断这个数在其所在的行有没有出现过,
                // 同时判断这个数在其所在的列有没有出现过
                // 同时判断这个数在其所在的box中有没有出现过
                if(board[i][j] == '.') continue;
                int curNumber = board[i][j]-'0';
                if(row[i][curNumber]) return false; 
                if(col[j][curNumber]) return false;
                if(box[j/3 + (i/3)*3][curNumber]) return false;

                row[i][curNumber] = 1;// 之前都没出现过,现在出现了,就给它置为1,下次再遇见就能够直接返回false了。
                col[j][curNumber] = 1;
                box[j/3 + (i/3)*3][curNumber] = 1;
            }
        }
        return true;
    }
};

作者:liujin-4
链接:https://leetcode-cn.com/problems/valid-sudoku/solution/36-jiu-an-zhao-cong-zuo-wang-you-cong-shang-wang-x/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值