GithubHelp home page GithubHelp logo

fangchao1086 / leetcode_solutions Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 1.0 5.27 MB

剑指offer、Leetcode题解 c++版本

Home Page: https://github.com/FangChao1086/LeetCode_Solutions

leetcode-solutions cpp jianzhioffer

leetcode_solutions's Introduction

LeetCode Solutions

基础

输入

  • 数据个数未知
    1 3 5 10
    
    // C++
    vector<int> v;
    int tmp;
    while(cin >> tmp){
        v.push_back(tmp);
        if (getchar() == '\n')
            break;
    }

生成数据(结构)

// 二维数组
int dp[n][m];  // 方法1
memset(dp,0,sizeof(dp));  // 用0填充
vector<vector<int>> dp(n,vector<int>(m,0));  // 方法2;n*m填充0
vector<int> res(input.begin(), input.begin() + k);  // 方法3;input是已经存在的vector

// 优先队列  最大最小堆
priority_queue<pair<int,int>> pq;  // 最大堆,默认
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; // 最小堆

数据处理

// 将res(vector类型)中的元素反向
vector<int>(res.rbegin(), res.rend()); 

// 累加 vector 中的值
int sum = accumulate(A.begin(), A.end(), 0);  // 其中 A 是vector, 0是代表累加的初值为0;

// 排序(根据pair中的第二个关键字降序)
#include"algorithm"
bool cmp2(pair<int, int> a, pair<int, int> b) {
    return a.second > b.second; 
}
vector<pair<int, int>> vec;
sort(vec.begin(), vec.end(), cmp2);

数据类型转换

// char 转 string
// 假设mp[0]是char;
string str;
str.push_back(mp[0]);

// string 转 int
string str = "123";
int n = atoi(str.c_str());  // 其中 .c_str() 必须要有
cout << n;  // 123

leetcode_solutions's People

Forkers

zyz-notebooks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.