1.0万

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;
}
};


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


15小时前
/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* merge(ListNode* l1, ListNode* l2) {
while(l1 != NULL && l2 != NULL){
if(l1->val <= l2->val){
else node = node->next = l1;
l1 = l1->next;
}else{
else node = node->next = l2;
l2 = l2->next;
}
}
if(l1 != NULL){
else node = node->next = l1;
}
if(l2 != NULL){
else node = node->next = l2;
}
}
};


16小时前
/**
* 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
};


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


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;
}
};


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);
}
};


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;
}



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;
}



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;
}