t;= times; i++) {
res += a;
}
if (res == s) {
cout << times << endl;
break;
}
}
}
return 0;
}
也可以通过字符串移位的贪心算法实现
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
string s;
while (cin >> s, s != ".") {
string res = s;
for (int i = 1; i <= res.size(); i++) {
res = res.substr(1) + res[0];
if (res == s) {
cout << res.size() / i << endl;
break;
}
}
}
return 0;
}
778
778. 字符串最大跨距 - AcWing题库
如何处理输入的问题
string s, s1, s2;
char c;
while (cin >> c, c != ',') s += c;
while (cin >> c, c != ',') s1 += c;
while (cin >> c) s2 += c;
779. 最长公共字符串后缀 - AcWing题库
这题没难度
函数默认参数
默认值需要是后面连续的几个参数,或全部都有默认值
void foo(int a,int b = 5,int c = 10)
sizeof 数组问题
main函数显示的是数组长度( 40字节),foo函数显示的是指针的长度(64位8字节)。
void foo(int b[]){
cout << sizeof b;
}
int main(){
int a[10];
cout << sizeof a << endl;
foo(a);
return 0;
}
/*
40
8
*/
inline
加在函数定义前,相当于函数展开,适用于函数调用次数小的情况。对递归函数无效
823 简单递归问题
821. 跳台阶 - AcWing题库
822. 走方格 - AcWing题库
823. 排列 - AcWing题库
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 10;
int n;
int pl(int index, int a[], bool h[]) {
if (index == n) {
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
} else
for (int i = 1; i <= n; i++) {
if (!h[i]) {
h[i] = 1;
a[index] = i;
pl(index + 1, a, h);
h[i] = 0;
}
}
}
int main() {
cin >> n;
int a[10] = {0};
bool h[10] = {0};
pl(0, a, h);
return 0;
}
指针、引用
int a = 3;
int* p = &a; // 指针
int& p2 = a; // 引用、别名
链表与指针 ?
#include <iostream>
using namespace std;
struct Node {
int val;
Node* next;
Node(int _val) : val(_val), next(NULL) {}
};
int main() {
Node* p = new Node(1); // ^ 加new返回地址,不加new返回变量
auto p2 = new Node(2);
auto p3 = new Node(3);
p->next = p2; // ^ 规定:如果p是指针用->、是变量用.
p2->next = p3;
// IMPORTANT 头结点:第一个结点的地址而不是值
Node* head = p;
for (Node* i = head; i; i = i->next) {
cout << i->val << endl;
}
// 添加节点
Node* u = new Node(4);
u->next = p;
head = u;
for (Node* i = head; i; i = i->next) {
cout << i->val << endl;
}
// ^ 链表删除是指在遍历时遍历不到这个点
head->next = head->next->next;
for (Node* i = head; i; i = i->next) {
cout << i->val << endl;
}
return 0;
}
84
84. 求1+2+…+n - AcWing题库
条件表达式脑筋急转弯
class Solution {
public:
int getSum(int n) {
int res = n;
n > 0 && (res += getSum(n - 1));
return res;
}
};
28
28. 在O(1)时间删除链表结点 - AcWing题库
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val; // 伪装成下一个点
node->next = node->next->next; // 将node删除(访问不到)
// *(node) = *(node->next);
}
};
36 ?
36. 合并两个排序的链表 - AcWing题库
二路归并,赋值从右往左
/**
* 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) {
auto dummy = new ListNode(-1), tail = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
tail = tail->next = l1;
l1 = l1->next;
} else {
tail = tail->next = l2;
l2 = l2->next;
}
}
if (l1)
tail->next = l1;
else
tail->next = l2;
return dummy->next;
}
};
87 ?
AcWing 87. 把字符串转换成整数 - AcWing
class Solution {
public:
int strToInt(string str) {
int i = 0, flag = 1;
while (i < str.size() && str[i] == ' ') i++;
if (i == str.size())