41.题目描述

求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
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.题目描述

将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0 
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.题目描述

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
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.题目描述

给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
思路:

利用两个辅助数组,
第一个数组L依次保存A数组从0-length-1的乘积,
第二个数组h依次保存从length-10的乘积,
然后每一个要求的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.题目描述

请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

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.题目描述

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
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.题目描述

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。 

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.题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
/*
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)的更多相关文章

  1. [剑指Offer]41.和为S的两个数字 VS 和为S的连续正数序列

    [剑指Offer]41 和为S的两个数字 VS 和为S的连续正数序列 Leetcode T1 Two Sum Given an array of integers, return indices of ...

  2. 剑指 Offer 41. 数据流中的中位数 + 堆 + 优先队列

    剑指 Offer 41. 数据流中的中位数 Offer_41 题目详情 题解分析 本题使用大根堆和小根堆来解决这个寻找中位数和插入中位数的问题. 其实本题最直接的方法是先对数组进行排序,然后取中位数. ...

  3. 【Java】 剑指offer(41) 数据流中的中位数

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中 ...

  4. 剑指offer 面试50题

    面试50题: 题目:第一个只出现一次的字符 题:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置. 解题思路一:利用Python特 ...

  5. 【剑指Offer】50、数组中重复的数字

      题目描述:   在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果 ...

  6. 【剑指offer】50.数组中重复出现的数字

    50.数组中重复出现的数字 知识点:数组:Set的不可重复性 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重 ...

  7. 剑指offer(50)数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  8. 剑指Offer 41. 和为S的连续正数序列 (其他)

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  9. 剑桥offer(51~60)

    51.题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. /* struct TreeLinkNode { ...

随机推荐

  1. CentOS 7.2 安装zabbix 3.4

    一.zabbix版本选择及部署环境说明 1.zabbix版本选择 zabbix官网地址:www.zabbix.com zabbix每半年发布一个长期支持版,目前长期支持版有2.0.3.0等,所以选择z ...

  2. Unity编辑器 - 自动排版

    Unity编辑器 - 自动排版 使用花括号提高可读性 //一组横向排列的控件 GUILayout.BeginHorizontal(); { GUILayout.BeginVertical(); { / ...

  3. Angular6项目搭建

    参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/b076702elvw.html 安装工具: Nodejs, npm     最新版, h ...

  4. spring boot 报错 Error creating bean with name

    Application 启动类 要和父目录平级

  5. 【转】从零开始学习Skynet_examples研究

    转自 http://blog.csdn.net/mr_virus/article/details/52330193 一.编译Skynet: 1.用ubuntu15.10直接 make linux 编译 ...

  6. [2017 - 2018 ACL] 对话系统论文研究点整理

    (论文编号及摘要见 [2017 ACL] 对话系统. [2018 ACL Long] 对话系统. 论文标题[]中最后的数字表示截止2019.1.21 google被引次数) 1. Domain Ada ...

  7. return语句的用法

    1.return语句的作用:a.返回一个值,这个值可以是任意类型.b.使程序返回到操作系统(即终止程序)2.java中对于一个函数,不论有没有返回值类型,都可以带有return 语句.但是区别在于,r ...

  8. lintcode-138-子数组之和

    138-子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 注意事项 There is at least one subarray that it' ...

  9. JDK源码分析 – LinkedList

    LinkedList类的申明 public class LinkedList<E> extends AbstractSequentialList<E> implements L ...

  10. BAT批处理(六)

    字符串处理 批处理有着具有非常强大的字符串处理能力,其功能绝不低于C语言里面的字符串函数集.批处理中可实现的字符串处理功能有:截取字符串内容.替换字符串特定字段.合并字符串.扩充字符串等功能.下面对这 ...