其他分享
首页 > 其他分享> > 刷题知识点记录

刷题知识点记录

作者:互联网

leetcode-刷题知识点记录

这里记录使用c++刷题过程中遇到的一些知识点

JZ开头表示《剑指offer(第2版)》系列

JZ37

queue的用法

queue name

函数 功能 时间复杂度
push(x) 将x进行入队 O(1)
front() 获得队首元素,使用前调用empty()函数 O(1)
back() 获得队尾元素,使用前调用empty()函数 O(1)
pop() 令队首元素出队 O(1)
empty() 检测queue是否为空 O(1)
size() 返回queue内元素的个数 O(1)

JZ37

string类型的变量可以用+连接,但是+两边不能都是字符串字面值(literal),比如 "hello"+"world" 就不行

字符串string转换为数值

image-20210804212721149

代码示例

#include <bits/stdc++.h>
#include <map>
using namespace std;
int main(){
	string s="123.45";
	cout<<"s="<<s<<endl;
	int ot = stoi(s,0,10);
	int ol = stol(s,0,10);
	float of = stof(s,0);
	double od = stod(s,0);
	cout<<"ot="<<ot<<endl;
	cout<<"ol="<<ol<<endl;
	cout<<"of="<<of<<endl;
	cout<<"od="<<od<<endl;
	return 0;
}
image-20210804213351394

[参考][https://blog.csdn.net/sinat_40872274/article/details/81367815]

字符串char[]转换为数值

C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字和字符串相互转换

image-20210804213020318

[参考][https://blog.csdn.net/sinat_40872274/article/details/81367815]

数值转换为字符串

函数 功能
to_string(val) 把val转换成string

c++中分割字符串

c++既有底层、又有抽象,两种看似矛盾的性质造就了缺失的split函数

按字符分割string类型变量的方法:

stringstream为字符串输入输出流,继承自iostream,灵活地使用stringstream流可以完成很多字符串处理功能,例如字符串和其他类型的转换,字符串分割等。下面使用其实现字符串分割功能,注意stingstream的使用需要包含sstream头文件:

vector<string> split3(const string &str, const char pattern)
{
    vector<string> res;
    stringstream input(str);   //读取str到字符串流中
    string temp;
    //使用getline函数从字符串流中读取,遇到分隔符时停止,和从cin中读取类似
    //注意,getline默认是可以读取空格的
    while(getline(input, temp, pattern))
    {
        res.push_back(temp);
    }
    return res;
}

关于stringstream的具体说明可以参见:
http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

[参考][https://www.jianshu.com/p/5876a9f49413]

JZ43

c++中开方、平方函数

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    //平方pow()
    int a = pow(4, 2);//a = 16
    //开方
    int b = pow(4, 0.5);//b = 2 
    int c = sqrt(4);//c = 2
    //整数绝对值
    int d = abs(b - c);//c2 = 0
    //浮点数绝对值
    double e = fabs(b - c);//d = 0
    cout << a << endl
         << b << endl
         << c << endl
         << d << endl
         << e << endl;
}

运行结果

image-20210820222038424

JZ56-I

C++中左移运算符,返回左移运算后的结果,但是被左移的那个数,值不变

#include <iostream>
using namespace std;
int main()
{
   int a = 1;
   // a <<= 1;
   a<<1;
   cout << a;
   return 0;
}

image-20211029223128978

#include <iostream>
using namespace std;
int main()
{
   int a = 1;
   a <<= 1;
   //a<<1;
   cout << a;
   return 0;
}

image-20211029223200356

JZ45

C++中if-else语句必须覆盖所有情况,最好对所有情况都进行判断

class Solution {
public:
    static bool cmp(int a, int b){
        string as = to_string(a);
        string bs = to_string(b);
		
        if( (as+bs) > (bs+as) || (as+bs) == (bs+as) ){
            return false;
        }
        //如果把if改成如下,就是错的
        //if( (as+bs) > (bs+as) ){
        //     return false;
        //}
        else{
            return true;
        }
    }
    string minNumber(vector<int>& nums) {
        int len = nums.size();
        string re = "";
        sort(nums.begin(),nums.end(),cmp);
        for(int tmp:nums){
            string nowa = to_string(tmp);
            re.append(nowa);
        }
        return re;
    }
};

写sort中的cmp函数的时候,最好把各种情况都考虑到,大于等于小于,否则会出现一些很难debug的问题:

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int a, int b)
{
    string as = to_string(a);
    string bs = to_string(b);

    if( (as+bs) < (bs+as) ){
        cout<<"less"<<endl;
        return true;
    }
    else{
        cout<<"large"<<endl;
        return false;
    }


    //如果把if-else换成下面的,就会死循环,直到溢出
    //写sort中的cmp函数的时候,最好把各种情况都考虑到,大于等于小于,否则会出现一些很难debug的问题
    // if( (as+bs) > (bs+as) ){
    //     cout<<"large"<<endl;
    //     return false;
    // }
    // else{
    //     cout<<"less"<<endl;
    //     return true;
    // }


    //如果把if-else换成下面的,就没有问题
    // if( (as+bs) > (bs+as) || (as+bs) == (bs+as) ){
    //     cout<<"large_equal"<<endl;
    //     return false;
    // }
    // else{
    //     cout<<"less"<<endl;
    //     return true;
    // }

    //如果把if-else换成下面的,就没有问题
    // if( (as+bs) < (bs+as) || (as+bs) == (bs+as) ){
    //     cout<<"less_equal"<<endl;
    //     return false;
    // }
    // else{
    //     cout<<"large"<<endl;
    //     return true;
    // }


}
int main()
{
    vector<int> nums;
    string re = "";
    for(int i=0; i<30; i++){
        nums.push_back(0);
    }
    sort(nums.begin(),nums.end(),cmp);
    for(int tmp:nums){
        string nowa = to_string(tmp);
        cout<<nowa<<endl;
        re.append(nowa);
    }
    cout<<re<<endl;
    return 0;
}

标签:知识点,string,记录,int,stringstream,bs,字符串,include,刷题
来源: https://www.cnblogs.com/alphaprime/p/15661345.html