头像

氘爸-wt




离线:12小时前



氘爸-wt
14小时前
class Solution {
public:
    int strToInt(string str) {
        bool flg = false;
        char sign;
        int len = 0;
        long long ret = 0;
        for(int i = 0; i < str.size(); i++){
            if(flg == false){
                if(str[i] != ' '){  //第一个非空字符
                    if(str[i] == '+'){
                        sign = '+';
                    }else if(str[i] == '-'){
                        sign = '-';
                    }else if(isdigit(str[i])){
                        ret = str[i] - '0';
                    }else{//第一个非空字符不是正负号及数字,则转换失败
                        return 0;
                    }
                    flg = true;     //之后就不是第一个非空字符了
                }
            }else if(isdigit(str[i])){
                len++;
                ret = ret * 10 + str[i] - '0';
                if(sign != '-' && ret > INT_MAX) return INT_MAX;
                if(sign == '-' && -ret < INT_MIN) return INT_MIN;
            }
        }
        if(len == 0) return 0;
        if(sign == '-') ret *= -1;
        return ret;
    }
};


活动打卡代码 AcWing 78. 左旋转字符串

氘爸-wt
15小时前
class Solution {
public:
    string leftRotateString(string str, int n) {
        return str.substr(n, str.size()-n) + str.substr(0, n);
    }
};



氘爸-wt
15小时前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* merge(ListNode* l1, ListNode* l2) {
        ListNode *node, *head = NULL;
        while(l1 != NULL && l2 != NULL){
            if(l1->val <= l2->val){
                if(head == NULL) node = head = l1;
                else node = node->next = l1;
                l1 = l1->next;
            }else{
                if(head == NULL) node = head = l2;
                else node = node->next = l2;
                l2 = l2->next;
            }
        }
        if(l1 != NULL){
            if(head == NULL) node = head = l1;
            else node = node->next = l1;
        }
        if(l2 != NULL){
            if(head == NULL) node = head = l2;
            else node = node->next = l2;
        }
        return head;
    }  
};



氘爸-wt
16小时前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode* nodetmp = node->next;
        node->val = nodetmp->val;
        node->next = nodetmp->next;
        delete nodetmp;
    }//把下一个结点nodetmp的val复制过来,然后架空并删除nodetmp
};


活动打卡代码 AcWing 84. 求1+2+…+n

氘爸-wt
17小时前
class Solution {
public:
    int getSum(int n) {
        int ret = n;
        n > 1 && (ret += getSum(n - 1));
        return ret;
    }
};


活动打卡代码 AcWing 16. 替换空格

氘爸-wt
17小时前
class Solution {
public:
    string replaceSpaces(string &str) {
        string st;
        for(int i = 0; i < str.size(); i++){
            if(str[i] == ' ') st = st + "%20";
            else st = st + str[i];
        }
        return st;
    }
};


活动打卡代码 AcWing 21. 斐波那契数列

氘爸-wt
17小时前
class Solution {
public:
    int Fibonacci(int n) {
        if(n == 0) return 0;
        if(n == 1) return 1;
        int a = 0, b = 1;
        for(int i = 2; i <= n; i++){
            int c = a + b;
            a = b;
            b = c;
        }
        return b;
    }
};

class Solution {
public:
    int Fibonacci(int n) {
        if(n <= 1) return n;
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
};


活动打卡代码 AcWing 823. 排列

氘爸-wt
21小时前
#include<bits/stdc++.h>
using namespace std;
int a[20];
bool h[20];
void Print(int a[], int size){
    for(int i = 0; i < size; i++){
        cout <<  a[i] << " ";
    }
    cout << endl;
}
void dictord(int dep, int size){
    if(dep == size){
        Print(a, size);
        return;
    }
    for(int i = 1; i <= size; i++){
        if(!h[i]){
            a[dep] = i;
            h[i] = true;
            dictord(dep + 1, size);
            h[i] = false;
        }
    }
}
int main(){
    int n;
    cin >> n;

    dictord(0, n);
    return 0;
}



活动打卡代码 AcWing 822. 走方格

氘爸-wt
21小时前
#include<bits/stdc++.h>
using namespace std;
int f(int x, int y){
    if(x < 0 || y < 0) return 0;
    if(x == 0 && y == 0) return 1;
    return f(x - 1, y) + f(x, y - 1);
}
int main(){
    int n, m;
    cin >> n >> m;

    cout << f(n, m) << endl;
    return 0;
}



活动打卡代码 AcWing 821. 跳台阶

氘爸-wt
21小时前
#include<bits/stdc++.h>
using namespace std;
int fab(int x){
    if(x <= 1) return 1;
    return fab(x - 1) + fab(x - 2);
}
int main(){
    int n;
    cin >> n;

    cout << fab(n) << endl;
    return 0;
}