![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
再练习
白帽子hhh
这个作者很懒,什么都没留下…
展开
-
二叉树的层次遍历
题目:给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。例如:给定二叉树:[3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回其层次遍历结果:[ [3], [9,20], [15,7]]分析:采用队列实现,队列中元素是当前结点和当前结点下标所...原创 2019-06-18 17:12:58 · 360 阅读 · 0 评论 -
找出字符串中第一个只出现一次的字符(题目已经更新)
题目:分析:映射法,最后出去记得判断#include <iostream>#include <string>using namespace std;int main(){ string s; while(getline(cin, s)) { int count[256]={0}; ...原创 2019-06-13 19:55:32 · 1987 阅读 · 0 评论 -
小易的升级之路
题目:分析:#include <iostream>#include <vector>using namespace std;//求两个数的最大公约数int max_com(int x, int y){ while(x%y!=0) { int left=x%y;//余数 x=y; ...原创 2019-06-13 19:51:01 · 220 阅读 · 0 评论 -
超长整数相加
题目:分析:从两个int main(){ string s1; string s2; while(cin>>s1>>s2) { int end1=s1.size()-1; int end2=s2.size()-1; int carray=0;//保存进位 ...原创 2019-06-07 09:42:42 · 371 阅读 · 0 评论 -
杨辉三角变形
题目:分析:先构造该变形的杨辉三角,再求值#include <iostream>#include <vector>using namespace std;int main(){ int n;//n行 while(cin>>n) { vector<vector<int>>...原创 2019-06-07 09:41:12 · 147 阅读 · 0 评论 -
查找组成一个偶数的最接近的两个素数
题目:分析:从i从1开始到n/2,判断i和n-i同时是素数,一直到最后,最后一对数就是满足题意的。需要注意的是输出的时候要先输出小的,而且n/2要取到#include <iostream>#include <vector>#include <cmath>using namespace std;//判断一个数是否为素数bool is...原创 2019-06-01 16:29:22 · 792 阅读 · 0 评论 -
跳石板
题目:小易来到了一条石板路前,每块石板上从1挨着编号为:1、2、3....... 这条石板路要根据特殊的规则才能前进:对于小易当前所在的编号为K的 石板,小易单次只能往前跳K的一个约数(不含1和K)步,即跳到K+X(X为K的一个非1和本身 的约数)的位置。 小易当前处在编号为N的石板,他想跳到编号恰好为M的石板去,小易想知道最少需要跳跃几次可以到达。 例如: N = 4,...原创 2019-06-06 11:09:45 · 140 阅读 · 0 评论 -
参数解析
题目:在命令行输入如下命令: xcopy /s c:\ d:\, 各个参数如下: 参数1:命令字xcopy 参数2:字符串/s 参数3:字符串c:\ 参数4: 字符串d:\ 请编写一个参数解析程序,实现将命令行各个参数解析出来。 解析规则: 1.参数分隔符为空格 2.对于用“”包含起来的参数,如果中间有空格,不能解析为多个参数。比如在命令行输入xc...原创 2019-06-06 11:06:55 · 1395 阅读 · 1 评论 -
扑克牌大小
题目:分析:#include <iostream>#include <string>#include <algorithm>using namespace std;int main(){ string s; while(getline(cin, s)) { //看有没有王 i...原创 2019-06-06 10:51:34 · 390 阅读 · 0 评论 -
完全数计算
题目:分析:#include <iostream>#include <math.h>using namespace std;bool IsCom(int num){ int sum = 0; /*for(int i = 1; i<num; i++) { if(num % i == 0) ...原创 2019-06-06 10:50:32 · 684 阅读 · 0 评论 -
统计每个月的兔子数
题目:分析:斐波那契数列问题#include <iostream>#include <vector>using namespace std;int main(){ int month; while(cin>>month) { //这个月的兔子数=(上个月的老兔子+上个月的新兔子)所有兔子生...原创 2019-06-08 08:26:27 · 148 阅读 · 0 评论 -
字符串通配符
题目:分析:当前对应字符相同,则进行下一组匹配;当前字符不同,如果模式串是?,则匹配一个,如果当前模式串是*,则匹配-个、1个、多个。#include <iostream>#include <string>using namespace std;bool match(const char* pattern, const char* str...原创 2019-06-08 08:28:44 · 2364 阅读 · 0 评论 -
二叉树的最近公共祖先
题目:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”例如,给定如下二叉树: root =[3,5,1,6,2,0,8,null,null,7,4]示例 1:输入: root...原创 2019-06-18 14:43:11 · 430 阅读 · 0 评论 -
用队列实现栈
题目:使用队列实现栈的下列操作:push(x) -- 元素 x 入栈pop() -- 移除栈顶元素top() -- 获取栈顶元素empty() -- 返回栈是否为空注意:你只能使用队列的基本操作-- 也就是push to back, peek/pop from front, size, 和is empty这些操作是合法的。你所使用的语言也许不支持队列。你可以使用...原创 2019-06-18 16:03:25 · 103 阅读 · 0 评论 -
用栈实现队列
题目:使用栈实现队列的下列操作:push(x) -- 将一个元素放入队列的尾部。pop() -- 从队列首部移除元素。peek() -- 返回队列首部的元素。empty() -- 返回队列是否为空。示例:MyQueue queue = new MyQueue();queue.push(1);queue.push(2); queue.peek(); // 返回 ...原创 2019-06-18 14:59:57 · 217 阅读 · 0 评论 -
公共子串计算
题目:分析:#include <string>#include <iostream>using namespace std;int main(){ string s1; string s2; while(cin>>s1>>s2) { int max = 0; ...原创 2019-06-11 21:54:35 · 437 阅读 · 0 评论 -
手套
题目:分析:需要注意:1.如果一只手某个颜色的手套是0,那么另一只对应这个位置的数量要加上(最坏覆盖)2.经过1处理之后,左手有的颜色右手必然有,反之也是(左右手颜色匹配),那么需要分别计算左右手要覆盖每一种颜色的最小取值,然后取左右手覆盖最小的数量然后再在另一只随便取一只即可最终=左手是0对应右手的+右手是0对应左手的+min{覆盖左手所有颜色最小值,覆盖右手颜色最小...原创 2019-06-05 09:01:00 · 132 阅读 · 0 评论 -
查找输入整数二进制中1的个数
题目:分析:注意每执行一次n=n&(n-1)会将n的二进制中的1消掉一位#include <iostream>using namespace std;int main(){ int n; while(cin>>n) { int count=0; while(n) {...原创 2019-06-05 08:58:39 · 382 阅读 · 0 评论 -
汽水瓶
#include <iostream>#include <vector>using namespace std;int GetRet(int num){ int blank_bot = num; int drink = 0; while(blank_bot >= 3) { drink ...原创 2019-06-10 22:08:12 · 2324 阅读 · 0 评论 -
查找两个字符串a,b中的最长公共子串
方法一:直接遍历。#include <iostream>#include <string>#include <vector>using namespace std;int main(){ string s1; string s2; while(cin>>s1>>s2) { int len1 ...原创 2019-06-10 22:07:02 · 1351 阅读 · 0 评论 -
计算日期到天数转换
题目:根据输入的日期,计算是这一年的第几天。。详细描述:输入某年某月某日,判断这一天是这一年的第几天?。接口设计及说明:/*****************************************************************************Description:数据转换InputParam:year输入年...原创 2019-06-04 13:16:58 · 848 阅读 · 0 评论 -
幸运的袋子
题目:一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的)。如果一个袋子是幸运的当且仅当所有球的号码的和大于所有球的号码的积。例如:如果袋子里面的球的号码是{1, 1, 2, 3},这个袋子就是幸运的,因为1 + 1 + 2 + 3 > 1 * 1 * 2 * 3你可以适当从袋子里移除一些球(可以移除0个,但是别移除完),要使移除后的袋子是幸运的。现在让你...原创 2019-06-04 13:14:38 · 198 阅读 · 0 评论 -
n*m的棋盘
题目:分析:递归法每次向右、向下走(行规模减1此时列规模不变,加上列规模减1此时行规模不变),递归出口是行列规模都是0就返回0,只要有一个是0,就说只剩一条路了返回1即可。#include <iostream>using namespace std;int process(int n, int m){ if (m == 0 && n ...原创 2019-05-29 10:01:17 · 1039 阅读 · 0 评论 -
另类加法
题目:分析:亦或表示不算进位之和,与右移一位表示进位值class UnusualAdd {public: int addAB(int A, int B) { // write code here //位运算符 while(B!=0) { int sum=A^B;/...原创 2019-05-29 09:51:35 · 214 阅读 · 0 评论 -
n个数里出现次数大于等于n/2的数
题目:分析:一个记录当值,一个记录次数#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){ vector<int> v; //输入数字 int num = 0; w...原创 2019-05-22 11:26:47 · 423 阅读 · 0 评论 -
不要二
题目:分析:先将二维数组全都初始化为1,然后从第一个位置开始放蛋糕,然后计数器加加,并且每在一个位置放了蛋糕后,里这个位置距离为2的行、列都不能放,则将其置为0,那么计数器每次就加位置上是1的数据,并且将距离为2的置掉就行。(即一边走一边抛弃不能放置的位置,并且一边统计)int main(){ int W;//长(列) int H;//宽(行) cin...原创 2019-05-25 11:55:45 · 428 阅读 · 0 评论 -
把字符串转化成整数
题目:class Solution {public: int StrToInt(string str) { //字符串->整数,串不合法返回0 int flag=1; int i=0; if(str[i]=='+') { i++...原创 2019-05-25 11:21:47 · 2105 阅读 · 0 评论 -
连续最大和
题目:分析:用临时变量保存连续和,当小于0时重置为0,继续累加,每次当临时和大于max时,更新max#include <iostream>#include <vector>using namespace std;int main(){ int n; cin>>n; vector<int>...原创 2019-05-24 16:27:51 · 126 阅读 · 0 评论 -
统计回文
题目:分析:遍历将s1分成所有可能的左右两部分,将s2插进去,判断插入后是否回文串,注意i的最大取值是s1.size(),因此循环取等号#include <iostream>#include <string>using namespace std;//判断一个字符串是不是回文串bool is_reverse(const string& ...原创 2019-05-24 15:40:22 · 280 阅读 · 0 评论 -
组队竞赛
题目:链接:https://www.nowcoder.com/questionTerminal/6736cc3ffd1444a4a0057dee89be789b?orderByHotValue=1&page=1&onlyReference=false来源:牛客网牛牛举办了一次编程比赛,参加比赛的有3*n个选手,每个选手都有一个水平值a_i.现在要将这些选手进行组队,...原创 2019-05-20 22:00:42 · 147 阅读 · 0 评论 -
关于子网掩码
子网掩码顾名思义是用于划分子网的。子网掩码由连续的1和0组成,连续的1表示网络地址,连续的0表示主机地址,通过0的个数可以计算出子网的容量(子网中主机的IP地址范围)。首先来看看默认的子网掩码255.255.255.0是怎么划分子网的,将该子网掩码的二进制由24个1和8个0组成,8个0表示该子网掩码划分出的子网容量为256(2的8次方),也就是说192.168.1.0-255都在同一个子网中...原创 2019-03-21 16:03:20 · 2150 阅读 · 0 评论 -
赋值运算符函数
题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数。class CMyString{private: char* m_pData;public: CMyString(char* pData = nullptr) {} CMyString(const CMyString& str) {} ~CMyString() {} CMyStrin...原创 2019-03-14 22:18:50 · 85 阅读 · 0 评论 -
【笔记】获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
2.获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。方法一:注意:(1)在第二个for循环的时候,记得置j=0,因为在上面已经将它加到15达到最大,若不管则发生越界(2)这种方法也属于按位与,只是如果不用数组,那么输出的是二进制的逆序(3)但是这种方法创建了数组,占用空间,理论上不够优化#include int main(){ int i=0,j=原创 2016-11-09 12:03:57 · 1468 阅读 · 0 评论 -
字符串中找出连续最长的数字串
题目:分析:方法(1):动态规划。只考虑对角线上的数据,因为对角线构成整个字符串#include <iostream>#include <vector>using namespace std;int main(){ string s; while (getline(cin,s)) { int len = s.size(); ...原创 2019-05-22 11:30:31 · 1545 阅读 · 0 评论 -
Fibonacci数列
题目:分析:找离N最近的左右两个fib值,并且比较找出距离最小的那一个#include <iostream>using namespace std;int main(){ int N; while(cin>>N) { int first=0; int second=1; i...原创 2019-05-27 10:32:16 · 537 阅读 · 0 评论 -
合法括号序列判断
题目:分析:借助“实现”,遇到左括号将其压入栈,遇到右括号,从栈中取左括号,一直这样到字符串完了栈也空了,则说明匹配成功,否则匹配失败class Parenthesis {public: stack<char> s;//放字符的栈 bool chkParenthesis(string A, int n) { // write cod...原创 2019-05-27 10:34:48 · 1322 阅读 · 0 评论 -
洗牌算法
题目:分析:123456洗一次牌,相当于将123456分成两部分:123 和 456 ,然后分别从两部分的起始端取数字,用一个空的vector尾插接收,如1 4,2 5,3 6这就是一次洗牌结果,然后如果还要继续洗的话,一定继续在这个洗好的基础上进行洗。#include <iostream>#include <vector>using name...原创 2019-06-12 16:21:59 · 148 阅读 · 0 评论 -
MP3光标位置
题目:分析:用三个变量保存当前列表的顶部、底部、当前光标位置。n小于4时只考虑当前光标位置在顶部并U,当前光标位置在底部并D;而当n大于等于4时,除了以上情况,还要考虑当前光标等于顶部时并U,或者当前光标在底部时并D,这两种情况要考虑翻页。#include <iostream>using namespace std;int main(){ int ...原创 2019-06-12 15:54:16 · 467 阅读 · 0 评论 -
求最大连续bit数
题目:、分析:挨着统计,遇到更长的替换Maxint main(){ int n; while(cin>>n) { int max=0; for(int i=0;i<8;i++) { int count=0; while(((n>>...原创 2019-05-31 12:48:28 · 156 阅读 · 0 评论 -
最近公共祖先
题目:分析:哪个大哪个先向上走,走到相遇为止int getLCA(int a, int b) { // write code here while(a!=b) { if(a>b)//a先向上走 { a=a/2; } ...原创 2019-05-31 12:46:50 · 111 阅读 · 0 评论