- 博客(148)
- 收藏
- 关注
原创 2021-10-20
字符串是不可变对象,当用操作符+连接字符串的时候,每执行一次+都会申请一块新的内存,然后复制上一个+操作的结果和本次操作的右操作符到这块内存空间,因此用+连接字符串的时候会涉及好几次内存申请和复制。而join在连接字符串的时候,会先计算需要多大的内存存放结果,然后一次性申请所需内存并将字符串复制过去,这是为什么join的性能优于+的原因。所以在连接字符串数组的时候,我们应考虑优先使用join。 ...
2021-10-20 20:17:46 115
原创 剑指 Offer 03. 数组中重复的数字
class Solution { //空间为O(1)的原地置换 public: int findRepeatNumber(vector<int>& nums) { for(int i=0;i<nums.size();++i){ while(i!=nums[i]){ if(nums[i]==nums[nums[i]]) return nums[i]; int tmp=num
2021-03-18 15:32:25 122
原创 B1011 A+B 和 C (15 分)
#include<cstdio> int main() { int t,turn=0; scanf("%d", &t); while (t--) { long long a, b, c; turn++; scanf("%lld%lld%lld", &a, &b, &c); if (a + b > c) printf("Case...
2019-04-09 18:10:07 195
原创 B1001 害死人不偿命的(3n+1)猜想 (15 分)
#include<cstdio> int main() { int step = 0; int n; scanf("%d", &n); while (n != 1) { if (n % 2 == 0) { step++; n /= 2; continue; } else { step++; n = (3*n + 1) / 2; ...
2019-04-09 17:58:20 193
原创 从尾到头打印链表
class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { ListNode* p=head; vector<int> arraylist; while(p!=NULL){ arraylist.pus...
2019-04-08 18:16:20 175
原创 替换空格
class Solution { public: void replaceSpace(char *str,int length) { int i=0; while(str[i]!='\0'){ if(str[i]==' '){ for(int j=length-1;j>i;j--){ ...
2019-04-08 17:49:03 191
原创 二维数组中的查找
class Solution { public: bool Find(int target, vector<vector<int> > array) { int row=array.size(); int col=array[0].size(); for(int i=0;i<row;i++) { ...
2019-04-08 17:28:47 147
转载 A1128 N Queens Puzzle (20 分)
#include <cstdio> #include <iostream> #include <vector> #include<cmath> using namespace std; bool isvalid(vector<int> v) { for (int i = 0; i < v.size(); i++) { f...
2019-02-13 22:10:12 176
原创 A1127 ZigZagging on a Tree (30 分)
#include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn = 35; vector<int> level[maxn]; int in[maxn], post[maxn]; struct node { int data,level; n...
2019-02-13 13:55:44 254
原创 A1126 Eulerian Path (25 分)
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 510; int n, m, a, b; int degree[maxn] = { 0 }; int G[maxn][maxn]; bool vis[maxn] = { 0 }; int sum = 0; void dfs(i...
2019-02-13 10:10:29 162
原创 1125 Chain the Ropes (25 分)
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 10010; double a[maxn]; int main() { int n; double result; scanf("%d", &n); for (int i = 0; i < n; i++)...
2019-02-13 00:42:42 173
原创 A1124 Raffle for Weibo Followers (20 分)
#include&lt;cstdio&gt; #include&lt;set&gt; #include&lt;string&gt; using namespace std; set&lt;string&gt; sq; int main() { int m, n, s; char str[25]; scanf("%d %d %d", &
2019-02-13 00:19:44 185
原创 A1123 Is It a Complete AVL Tree (30 分)
#include<cstdio> #include<algorithm> #include<queue> using namespace std; int n; struct node { int v, height; node *lchild, *rchild; }; node* newNode(int v) { node* Node = new nod...
2019-02-12 15:37:20 160
原创 A1122 Hamiltonian Cycle (25 分)
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 210; int G[maxn][maxn]; int vis[maxn],q[maxn]; int main() { fill(G[0], G[0] + maxn * maxn, 0); int n, m; scanf...
2019-02-11 23:03:13 219
原创 A1121 Damn Single (25 分)
#include<cstdio> #include<set> #include<algorithm> using namespace std; int n,m; const int maxn = 100010; int ans[maxn]; int notsingle[maxn] = { 0 }; int hashtable[maxn] = { 0 }; set...
2019-02-11 22:21:48 158
原创 A1120 Friend Numbers (20 分)
#include<cstdio> #include<set> using namespace std; int n,temp; set<int> s; int change(int x) { int result = 0; while (x != 0) { result += x % 10; x /= 10; } return result; ...
2019-02-11 21:57:06 192
原创 A1119 Pre- and Post-order Traversals (30 分)前序和后序确定树
#include<cstdio> #include<iostream> #include<vector> using namespace std; vector<int> pre, post, in; bool unique = 1; void inorder(int preleft, int preright, int postleft, int ...
2019-02-11 21:43:15 238
原创 A1118 Birds in Forest (25 分)并查集
#include<cstdio> const int maxn = 10010; int n; int father[maxn]; void init(){for (int i = 0; i < maxn; i++) father[i] = i;} int findfather(int x) { int a = x; while (x != father[x]) { x = ...
2019-02-11 00:25:31 150
原创 A1117 Eddington Number (25 分)
#include&lt;cstdio&gt; const int maxn = 1e5 + 10; int a[maxn]; int main() { int n; scanf("%d", &amp;n); for (int i = 0; i &lt; n; i++) scanf("%d", &amp;a[i]); int E; for (int
2019-02-10 18:23:23 250
原创 A1116 Come on! Let's C (20 分)
#include<cstdio> #include<cmath> using namespace std; const int maxn = 10010; bool isprime(int n) { if (n <= 1) return false; else { int sqr = (int)sqrt(1.0*n); for (int i = 2; i...
2019-02-10 17:45:42 242
原创 A1115 Counting Nodes in a BST (30 分)二叉排序树建立
二叉排序树的建立,加上树的遍历。 #include<cstdio> using namespace std; struct node { int data; node *left, *right; }; void insert(node *&root, int data) { if (root == NULL) { root = new node; root-&...
2019-02-10 16:58:18 158
原创 A1114 Family Property (25 分)
#include&amp;lt;cstdio&amp;gt; #include&amp;lt;algorithm&amp;gt; #include&amp;lt;vector&amp;gt; #include&amp;lt;set&amp;gt; using namespace std; const int maxn = 10010; set&amp;lt;int&
2019-02-10 16:12:43 282
原创 A1113 Integer Set Partition (25 分)
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 1e5 + 10; int a[maxn],n; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i...
2019-02-10 12:42:04 211
原创 A1112 Stucked Keyboard (20 分)
AC代码,先把没坏的键记录下来,然后剩下的就是可能是坏的键,应当输出。 #include<cstdio> #include<cstring> using namespace std; int k; char s[1010]; int notbroken[128],hashtable[128]; int p=0; int main() { scanf("%d", &...
2019-02-09 23:45:11 229
原创 A1111 Online Map (30 分)
直接用两个dijkstra和dfs,思路挺简单的,就是写起来很长。 #include<cstdio> #include<vector> #include<algorithm> using namespace std; const int maxv = 510; const int INF = 1e8; int source, destination; int n...
2019-02-09 17:23:48 154
原创 A1110 Complete Binary Tree (25 分)atoi函数
atoi函数与之前的atof函数一样的函数头,atof是转换为double,而atoi转换为整数。 这道题目由于序号可能为两位数,所有要输入字符串,而不是字符。 #include<cstdio> #include<stdlib.h> using namespace std; const int maxn = 25; int n; struct node { int lef...
2019-02-09 00:51:26 147
原创 A1109 Group Photo (25 分)
#include<cstdio> #include<iostream> #include<cmath> #include<vector> #include<string> #include<algorithm> using namespace std; struct person { string name; int he...
2019-02-08 17:00:45 196
原创 A1108 Finding Average (20 分)atof()函数
#include&lt;cstdio&gt; #include&lt;cstring&gt; int main() { int n, sum = 0, count = 0; double total = 0;//count记录有效数字数 bool negative = 0,flag=0,point=0; char str[10000]; scanf("%d", &amp;n); f
2019-02-08 13:08:06 226
原创 A1026 Table Tennis (30 分)
#include<cstdio> #include<cmath> #include<vector> #include<algorithm> using namespace std; const int K = 110; const int INF = 1e9; struct player { int arrival, start, train; ...
2019-02-08 00:39:16 258
原创 A1017 Queueing at Bank (25 分)
#include<cstdio> #include<algorithm> using namespace std; int N, K; int wintime[110]; struct person { int hh, mm, ss; int time; }c[10010]; bool cmp(person a, person b) { if (a.hh != b.h...
2019-02-06 22:23:47 142
原创 A1014 Waiting in Line (30 分)
开始对于题意的理解有问题,最后一句说不能在17点之前包括17点接受服务的才输出sorry,而不是在17点之前不能结束服务才输出sorry #include<cstdio> #include<vector> #include<algorithm> using namespace std; vector<int>wintoid[25]; int N, M...
2019-02-05 23:50:21 144
原创 A1105 Spiral Matrix (25 分)
这个题目自己的想法是螺旋输入mat数组,举例来说,在输入完第一趟外层,停在第二层的起点,然后再循环,虽然结果正确,但是时间长的吓人。。。对算法理解还是不深,不明白为什么和这种停在内层第一个位置的时间复杂度相差的时间这么久 #include<cstdio> #include<cmath> #include<algorithm> using namespace st...
2019-02-05 01:30:15 173
原创 A1057 Stack (30 分)分块和树状数组
#include&lt;cstdio&gt; #include&lt;vector&gt; #include&lt;cstring&gt; #include&lt;algorithm&gt; using namespace std; int n; vector&lt;int&gt; v,mid; int main() { char str[15];
2019-02-04 00:16:36 238
原创 A1068 Find More Coins (30 分)01背包问题
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 10010,maxv=110; int w[maxn], dp[maxv] = { 0 }; bool choice[maxn][maxv] = { 0 }, flag[maxn] = { 0 }; bool cmp(int ...
2019-02-03 01:12:35 173
原创 A1040 Longest Symmetric String (25 分)注意fgets
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; char str[1010]; int dp[1010][1010] = { 0 }; int main() { fgets(str, 1010, stdin); int len=strlen(str); ...
2019-02-02 23:05:41 128
原创 A1045 Favorite Color Stripe (30 分)
LIS(最长不下降子序列)解法: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, m, l; int fc[210],tc[10010],chash[210],dp[10010],s[10010];//写完发现,tc完全不用定义 int main...
2019-02-02 22:28:01 152
原创 A1007 Maximum Subsequence Sum (25 分)
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 10010; int n,A[maxn],dp[maxn],b[maxn]; bool flag = false; int main() { scanf("%d", &n); for (int i = 0; i &...
2019-02-02 20:06:26 152
原创 A1087 All Roads Lead to Rome (30 分)
#include<iostream> #include<string> #include<map> #include<vector> #include<algorithm> using namespace std; const int INF = 1e9; const int maxv = 210; vector<int> p...
2019-02-01 01:16:16 166
原创 A1072 Gas Station (30 分)
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxv = 1020; const int INF = 1e9; int n, m, k, ds; int G[maxv][maxv]; int vis[maxv], d[maxv]; in...
2019-01-31 17:11:45 170
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人