leetcode常用代码段

程序框架

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <algorithm>
using namespace std;
#define max 1000
const int max=100;
int main(int argc, char const *argv[])
{
    return 0;
}

输入相关代码块

牛客网平台的OJ模式,要自己实现主函数对样例循环
leetcode不用考虑这些

    int num=0;
    string str;
//方式一
    while(getline(cin,str))//不定量的输入需要使用getline(cin,str),否则会断流。
    { }
//方式二
    while(cin>>num)
    {
        cin>>str; 
    }
//方式三
    //scanf操作
    unsigned int IP_cell[4];
    while(scanf("%d.%d.%d.%d",&IP_cell[0],&IP_cell[1],&IP_cell[2],&IP_cell[3])  !=EOF)
    { }
    //符号说明:
        %d //输入十进制整数
        %c //输入单个字符
        %s //输入字符串

数学操作

//IP转数值
    //移位操作  和 与操作
    unsigned int ip_2_num = IP_cell[0]<<24 | IP_cell[1]<<16 | IP_cell[2]<<8 | IP_cell[3];
    
//取余
    int length = str.size();
    int a = length/8; //8位分割
    int b = length%8;

    int num;
    int a = num/2;  //num二进制表示,然后右移1位的数字
    int b = num%2;  //num二进制表示,最右边一位数值 0 or 1

    int num;
    int a = num/10; //num十进制右移
    int b = num%10; //num十进制最右边一位数值 0-9

//10转2进制
    int num;
    cin>>num;
    while(num)
    {
        int b = num%2;
        //b为最右边一位的数字,0 or 1
        num/=2;
        //相当于右移操作
    }

//--四舍五入
    float Num;
    while (cin >> Num)
    {
        Num = (int)Num+(int)((Num-(int)Num)/0.5);
        cout<<Num<<endl;
    }

//--计算一个数字各个位之和
int temp=0;
while(x){
    temp+=x%10;
    x/=10;
}

switch语句块

	switch (cell[0])
    {
        case 'W':
            cell.erase(0,1);
            y+=atoi(cell.c_str());  //注意:str2int语句
            break;
        case 'S':
            cell.erase(0,1);
            y-=atoi(cell.c_str());
            break;
        case 'A':
            cell.erase(0,1);
            x-=atoi(cell.c_str());
            break;
        case 'D':
            cell.erase(0,1);
            x+=atoi(cell.c_str());
            break;
        default:
            break;
    }
    case 'a': case 'b': case 'c':  decode+='2';  break;//a或b或c的情况,写在一起

动态数组操作相关代码块

  • 一维

    int num=0;
    while (cin >> num)
    {
        int *Arry;
        Arry = new int[num];
        for(int i=0; i<num; i++)
            cin >> Arry[i];
        delete []Arry;
        Arry = nullptr;
    }
  • 二维
//--二维动态数组
//--动态数组已经可以这样去定义了
int main(){
	int a;
	cin>>a;
	int b[a]={ 0,1,2,3 };
	for (size_t i = 0; i < 4; i++)
		cout<<b[i]<<endl;

	for (size_t i = 4; i < a; i++)
	{
		b[i]=2*i;
		cout<<b[i]<<endl;
	}
	
	return 0;
}

类框架

class solution
{
private:
    int a;
public:
    int function(vector<int> &nums)
    {

    }
};
int main(int argc, char const *argv[])
{
    solution S;
    vector<int> v ={1,2,3};
    int result = S.function(v);
    return 0;
}

struct框架

struct Astruct
{
private:
    int a;
public:
    int function(vector<int> &nums)
    {

    }
};
int main(int argc, char const *argv[])
{
    Astruct S;
    vector<int> v ={1,2,3};
    int result = S.function(v);
    return 0;
}
struct框架示例:自定义链表
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = (node->next)->val;
        node->next = node->next->next;
    }
};
struct框架示例:构造函数和重载操作符
struct point{
    int x;
    int y;
    point(int x, int y) : x(x), y(y) {}
    bool operator == (const point & obj) const {
        return x == obj.x && y == obj.y;
    }
};

引用传参

//字符串加密
void Encrypt (string& aucPassword, string& aucResult)
{
    for (size_t i = 0; i < aucPassword.size(); i++){
        if (aucPassword[i]<='9' && aucPassword[i]>='0')
            //aucResult[i]= (aucPassword[i]+1)%10;   //注意!!!aucResult地址,不可直接赋值元素i
            aucResult += ((aucPassword[i]-'0'+1)%10 + '0');
    }
}
//字符串解密
void unEncrypt (string& password, string& result)

int main()
{
    string text,code;
    while (cin >> text>> code)
    {
        string text2code, code2text;
        Encrypt(text,text2code);
        unEncrypt(code,code2text);
        cout<<text2code<<endl
            <<code2text<<endl;
    }
   
}

功能段

检测回文

//伪回文

//找出现奇数次的数字,这种数字比较危险,必须放在中间
//如果这种数字有两个以及以上,不能构成伪回文
bool Solution::check(vector<int> &path)
{
    bool flag[10]={0};
    int count=0;
    for(size_t i=0; i<path.size(); i++)
        if(!flag[path[i]]) flag[path[i]]=1;
        else flag[path[i]]=0;
    for(size_t i=0;i<10;i++)
        count+=flag[i];
    if(count<2)
        return 1;
    return 0;
}

//回文
bool Solution::check(vector<int> &path)
{
    for(size_t i=0;i<path.size()/2;i++)
        if(path[i]!=path[path.size()-1-i])
            return -1;
    return 0;
}

通信协议分析代码块

//–字符串单词截取,

    string str;
    vector<string> Box;

        while(getline(cin,str))
        {
            string words="";
            for (size_t i = 0; i < str.size(); i++)
                if ((str[i]>='a' && str[i]<='z') || (str[i]>='A'&&str[i]<='Z'))  //接收到合格字符
                    words+=str[i];
                else if(words.size()>0)
                {
                    Box.push_back(words);
                    words="";
                }
            if(words.size()>0)        
                Box.push_back(words);

            //for (size_t i = Box.size()-1; i>=0; i--)  //注意这种写法是错的!!!,最后i=0的时候还会再计算一步i--
            for (size_t i = Box.size(); i>1; i--)
                cout<<Box[i-1]<<' ';
            cout<<Box[0]<<endl;  //最后一个单词之后不能再输出" "

            Box.clear();
//还有其他更好的方法啊:最开始就在string后面加上一个" "
            int isPrefixOfWord(string sentence, string searchWord) {
                sentence+=" ";
                vector<string> Box;
                string words;
                for(char c:sentence){
                    if(c==' '){
                        Box.push_back(words);
                        words="";
                    }
                    else
                        words+=c;
                }
        
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值