头像

JeffCheng




离线:1天前



class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        int n = nums.size(); 

        if(!n) return 1;  

        for(auto &e : nums) 
            if(e != INT_MIN)
                e--;

        for(int i=0; i<n; i++){
            while( (nums[i] >=0 && nums[i] < n) && nums[i] != i && nums[i] != nums[nums[i]]) {
                swap(nums[i], nums[nums[i]]); 
            }
        } 


        for(int i=0; i<n; i++) {
            if(nums[i] != i) return i+1;
        }

        return n+1;
    }
};


活动打卡代码 LeetCode 221. 最大正方形

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if(matrix.empty()  || matrix[0].empty() ) return 0;
        int n = matrix.size(), m = matrix[0].size();
        vector<vector<int> > dp(n+1, vector<int>(m+1));

        int res = 0;
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=m; j++) {
                if(matrix[i-1][j-1] == '1'){
                    dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) + 1;
                    res = max(res, dp[i][j]);
                }
            }
        }

        return res*res;


    }
};


活动打卡代码 AcWing 608. 差

#include <iostream> 
using namespace std; 

int main() {
  int a,b,c,d;

  scanf("%d%d%d%d", &a,&b,&c,&d); 
  printf("%d ", (a*b) - (c*d) );
}


活动打卡代码 AcWing 1. A + B

#include <iostream>

using namespace std;

int main(){
  int a, b;
  cin >> a >> b;
  cout << a+b; 
}


活动打卡代码 AcWing 1. A + B

#include <iostream>

using namespace std;

int main(){
  cin >> a >> b;
  cout << a+b; 
}



#include <iostream>
#include <vector> 
#include <utility> // pair


using namespace std;

int n;

bool cmp(pair<int,int> a, pair<int, int> b){
    return a.second < b.second;
}
int main(int argc, char *argv[]) {
    cout << "input range number : ";
    scanf("%d", &n);


    vector<pair<int, int>> range;

    int l,r;
    for(int i=0; i<n; i++){
        scanf("%d%d", &l, &r);
        range.push_back({l,r});
    }

    sort(range.begin(), range.end(), cmp);

    r = -2e9;
    int res = 0;
    for(int i=0; i<n ;i++){
        if(range[i].first > r)  {
            res ++;
            r = range[i].second;
        }
    }

    cout << res;
}




活动打卡代码 AcWing 905. 区间选点

#include <iostream>
#include <vector> 
#include <utility> // pair


using namespace std;

int n;

bool cmp(pair<int,int> a, pair<int, int> b){
    return a.second < b.second;
}
int main(int argc, char *argv[]) {
    cout << "input range number : ";
    scanf("%d", &n);


    vector<pair<int, int>> range;

    int l,r;
    for(int i=0; i<n; i++){
        scanf("%d%d", &l, &r);
        range.push_back({l,r});
    }

    sort(range.begin(), range.end(), cmp);

    r = -2e9;
    int res = 0;
    for(int i=0; i<n ;i++){
        if(range[i].first > r)  {
            res ++;
            r = range[i].second;
        }
    }

    cout << res;
}





JeffCheng
11天前

给定一个字符串, 求同时包含大小写的最短子串
aBa返回空 / aAa 返回aA或者Aa

“azABaabza” => “ABaab”
“TacoCat” => “”
“AcZCbaBz” => “AcZCbaBz”


双指针写完一看好家伙直接成暴力解了…




JeffCheng
14天前
/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */

class NestedIterator {
public:
    vector<int> res;
    int index; 
    NestedIterator(vector<NestedInteger> &nestedList) {
        index = 0;
        for(auto e : nestedList) 
            parse(e);
    }

    void parse(NestedInteger &list){
        if(list.isInteger()) 
            res.push_back(list.getInteger());
        else 
            for(auto e : list.getList()) 
                parse(e);
    }

    int next() {
        return res[index++];
    }

    bool hasNext() {
        return index<res.size();
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */


活动打卡代码 LeetCode 216. 组合总和 III

JeffCheng
14天前
class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    vector<vector<int>> combinationSum3(int k, int n) {
        backtrack(k,n,1);
        return res;
    }

    void backtrack(int number, int sum, int index){
        if(number == 0) {
            if(sum==0) res.push_back(path);
                return;
        }

        for(int i=index; i<=9; i++){
            if(sum < i) continue;
            path.push_back(i); 
            backtrack(number-1, sum-i, i+1);
            path.pop_back();
        }
    }
};