剑桥offer(41~50)
41.题目描述
class Solution {
public:
int Sum_Solution(int n) {
char a[n][n+];
return sizeof(a)>>;
}
};
42.题目描述
class Solution {
public:
int Add(int num1, int num2)
{
if(num1 == )
return num2;
if(num2 == ){
return num1;
}
return Add(num1^num2,(num1&num2)<<);
}
};
43.题目描述
class Solution {
public:
int StrToInt(string str) {
if(str.length()==){
return ;
}
int symbol = ;
int val = ;
if(str[]=='+'){
symbol=;
}else if(str[]=='-'){
symbol=-;
}else if(str[]-''>= && str[]-''<){
val = str[]-'';
symbol=;
}
for(int i=;i<str.length();i++){
if(str[i]-''> || str[i]-''<){
return ;
}
val = val* + str[i]-'';
}
return val*symbol;
}
};
44.题目描述
class Solution {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
map<int,int>a;
for(int i=;i<length;i++){
a[numbers[i]]++;
}
for(auto it=a.begin();it!=a.end();it++){
if(it->second>){
*duplication = it->first;
return true;
}
}
return false;
}
};
45.题目描述
利用两个辅助数组,第一个数组L依次保存A数组从0-length-1的乘积,第二个数组h依次保存从length-1到0的乘积,然后每一个要求的B[i]=L[i-1]*H[i+1].//B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]
//从左到右算 B[i]=A[0]*A[1]*...*A[i-1]
//从右到左算B[i]*=A[i+1]*...*A[n-1]
class Solution {
public:
vector<int> multiply(const vector<int>& A) { int n=A.size();
vector<int> b(n);
int ret=;
for(int i=;i<n;ret*=A[i++]){
b[i]=ret;
}
ret=;
for(int i=n-;i>=;ret*=A[i--]){
b[i]*=ret;
}
return b;
}
};
46.题目描述
class Solution {
public:
bool match(char* str, char* pattern)
{
if (*str == '\0' && *pattern == '\0')
return true;
if (*(pattern + ) == '*')
{
// 如果已经到结尾了,把后面的*都匹配掉
if (*str == '\0')
return match(str, pattern + );
if ((*pattern == *str || *pattern == '.'))
{
// 尝试匹配一个,模式串往后移动
// 尝试匹配一个,模式串不往后移
// 一个都不匹配,模式串往后移动
// 3种当中有一种成功就可以了
return match(str + , pattern + ) || match(str + , pattern) || match(str, pattern + );
}
else
{
// 因为是*所以不匹配也没事,直接跳到下一个
return match(str, pattern + );
}
}
else if (*str == '\0') // 如果没有*了,但是模式串还没匹配完,那么失败
return false;
else if (*pattern == '.' || *pattern == *str) // .或者模式串字符本来就匹配
{
return match(str + , pattern + );
}
return false;
}
};
47.题目描述
class Solution {
public:
bool isNumeric(char* str) {
// 标记符号、小数点、e是否出现过
bool sign = false, decimal = false, hasE = false;
for (int i = ; i < strlen(str); i++) {
if (str[i] == 'e' || str[i] == 'E') {
if (i == strlen(str)-) return false; // e后面一定要接数字
if (hasE) return false; // 不能同时存在两个e
hasE = true;
} else if (str[i] == '+' || str[i] == '-') {
// 第二次出现+-符号,则必须紧接在e之后
if (sign && str[i-] != 'e' && str[i-] != 'E') return false;
// 第一次出现+-符号,且不是在字符串开头,则也必须紧接在e之后
if (!sign && i > && str[i-] != 'e' && str[i-] != 'E') return false;
sign = true;
} else if (str[i] == '.') {
// e后面不能接小数点,小数点不能出现两次
if (hasE || decimal) return false;
decimal = true;
} else if (str[i] < '' || str[i] > '') // 不合法字符
return false;
}
return true;
}
};
48.题目描述
class Solution
{
public:
//Insert one char from stringstream
string str;
void Insert(char ch)
{
str += ch;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
vector<char> a;
map<char, int>b;
for (int i = ; i<str.size() ; i++){
a.push_back(str[i]);
b[str[i]]++;
}
for (int i = ; i<a.size();i++){
if (b[a[i]]>){
continue;
}
else if (b[a[i]] == ){
return a[i];
}
}
return '#';
} };
49.题目描述
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
//if(pHead == NULL)return NULL;
ListNode* p = pHead;
map<ListNode*,int>a; while(p){
if(++a[p] == )
{
break;
}
p = p->next;
} return p;
}
};
50.题目描述
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL || pHead->next == NULL)
return pHead; /*---------先为链表创建一个头结点---------*/ int firstNumber = pHead->val; //假设我的头结点数值为-1
int myFirst = -; //万一链表的头结点也为-1,那么我就改成-2
if (myFirst == firstNumber)
{ myFirst = -;
}
ListNode *head = new ListNode(myFirst);
head->next = NULL;
head->next = pHead; ListNode *p = head;
ListNode *q = head->next; while (q)
{
while (q->next && (q->next->val == q->val))
{
q = q->next;
}
if (p->next != q)
{ q = q->next;
p->next = q;
}
else
{
p = q;
q = q->next;
}
} //返回的时候,注意去掉头结点(自己创建的辅助节点)
return head->next; } };
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL || pHead->next == NULL)
return pHead;
int val;
if (pHead->val != -){
val = pHead->val-;
}
else{
val = -;
}
ListNode* node = pHead;
ListNode* fp = node->next;
ListNode* pre = NULL;
ListNode* head = NULL;
while (fp)
{
if (node->val != fp->val&&val != node->val){ //如果相邻两个值不等,且也不是前一个相等的和当前的值相等。
if (pre == NULL){
pre = node;
head = pre;
}
else{
pre->next = node;
pre = pre->next;
}
}
else{
val = node->val;
}
node = node->next;
fp = node->next;
}//end while
//处理最后一个节点
if (node -> val == val){
if (pre)
pre->next = NULL;
}
else{
if (pre)
pre->next = node;
else{
pre = node;
head = pre;
}
}
return head;
}
};
剑桥offer(41~50)的更多相关文章
- [剑指Offer]41.和为S的两个数字 VS 和为S的连续正数序列
[剑指Offer]41 和为S的两个数字 VS 和为S的连续正数序列 Leetcode T1 Two Sum Given an array of integers, return indices of ...
- 剑指 Offer 41. 数据流中的中位数 + 堆 + 优先队列
剑指 Offer 41. 数据流中的中位数 Offer_41 题目详情 题解分析 本题使用大根堆和小根堆来解决这个寻找中位数和插入中位数的问题. 其实本题最直接的方法是先对数组进行排序,然后取中位数. ...
- 【Java】 剑指offer(41) 数据流中的中位数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中 ...
- 剑指offer 面试50题
面试50题: 题目:第一个只出现一次的字符 题:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 解题思路一:利用Python特 ...
- 【剑指Offer】50、数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果 ...
- 【剑指offer】50.数组中重复出现的数字
50.数组中重复出现的数字 知识点:数组:Set的不可重复性 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重 ...
- 剑指offer(50)数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 剑指Offer 41. 和为S的连续正数序列 (其他)
题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- 剑桥offer(51~60)
51.题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. /* struct TreeLinkNode { ...
随机推荐
- Linux命令应用大词典-第46章 其他命令
46.1 mkfontdir:创建X字体文件的索引 46.2 dumpiso:转储IEEE 1394同步信道的数据包 46.3 iconv:转换文件编码 46.4 hash:显示和删除哈希表 46.5 ...
- 平衡的括号 (Parentheses Balance UVA - 673)
题目描述: 原题:https://vjudge.net/problem/UVA-673 题目思路: 1.水题 2.栈+模拟 3.坑在有空串 AC代码 #include <iostream> ...
- opencv-学习笔记(5)形态学转变
opencv-学习笔记(4)形态学转变 本章讲了几种形态学操作 腐蚀erode 膨胀dilate 开运算MORPH_OPEN 闭运算MORPH_CLOSE 形态学梯度MORPH_GRADIENT 礼帽 ...
- ViewPager的简单使用说明
前提:工程中使用ViewPager,需要导入google提供的jar包(android-support-v4.jar). 要学习ViewPager的使用,建议直接看官方文档 Creating Swip ...
- C中的除法,商和余数的大小、符号如何确定
对于C中的除法,商和余数的大小.符号是如何确定的呢?在C89中,只规定了如果两个数为正整数,那么余数的符号为正,并且商的值是接近真实值的最大整数.比如5 / 2,那么商就是2,余数就是1.但是,C89 ...
- 视频播放截图及简要文字介绍——Thunder团队
视频播放截图及简要文字介绍 图一:团队Logo ——从此我们有了自己的标志 图二:扫描本地书籍 ——可阅读本地的喜爱书籍 图三:在本地添加自己喜爱的图书 ——将自己喜爱的书籍加入书架,方便阅读 图四: ...
- DAY3敏捷冲刺
站立式会议 工作安排 (1)服务器配置 (2)数据库配置 燃尽图 燃尽图有误,已重新修改,先贴卡片的界面,后面补修改后燃尽图 代码提交记录
- lintcode-42-最大子数组 II
42-最大子数组 II 给定一个整数数组,找出两个 不重叠 子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [1 ...
- TCP系列30—窗口管理&流控—4、Cork算法
一.Cork算法概述 Cork算法与Nagle算法类似,也有人把Cork算法称呼为super-Nagle.Nagle算法提出的背景是网络因为大量小包小包而导致利用率低下产生网络拥塞,网络发生拥塞的时候 ...
- 3dContactPointAnnotationTool开发日志(二一)
今天完成了修改按钮颜色,添加smpl模型到工具,以及可以显示物体子物体对应选项卡的功能.把之前的meshRenderer+meshFilter都改成了skinnedMeshRenderer,因为s ...