剑桥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 { ...
随机推荐
- MySQL☞abs函数
abs( )函数:求出绝对值 格式: select abs(数值) from 表名 如下图:
- 【WXS数据类型】Function
属性: 名称 值类型 说明 [Function].constructor [String] 返回值为“Function”,表示类型的结构字符串 [Function].length [Number] 返 ...
- informix如何查询第一条记录
1.select first 1 * from shop; 正序查询第一条数据 2.select first 1 * from shop order by create_time desc; 按创建时 ...
- 【转】从零开始学习Skynet_examples研究
转自 http://blog.csdn.net/mr_virus/article/details/52330193 一.编译Skynet: 1.用ubuntu15.10直接 make linux 编译 ...
- SIFT特征原理与理解
SIFT特征原理与理解 SIFT(Scale-invariant feature transform)尺度不变特征变换 SIFT是一种用来侦测和描述影像中局部性特征的算法,它在空间尺度中寻找极值点,并 ...
- ajax获取动态列表数据后的分页问题
ajax获取动态列表数据后的分页问题 这是我在写前台网站时遇到的一个分页问题,由于数据是通过ajax的方式来请求得到的,如果引入相应的js文件来做分页,假如只是静态的填放数据到列表各项内容中(列表条数 ...
- RDL/RDLC批量单据打印 [转]
RDL/RDLC批量单据打印 使用RDL或RDLC进行单据打印时,单张单据打印比较直观简单,无需说明.下面我们来谈一下批量单据打印的实现方法.以下以RDL的ReportBuilder设计环境为例进行讲 ...
- Python高级编程-多线程
(一)进程线程概述: 很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行 ...
- for和foreach的一点总结
两者都是数组的循环遍历,但是区别还是有点: for循环,如果块内有事件操作,那么i是不会等事件操作时候在依次增加,而是一次性走完,,也就是依靠下标定位,下标已经走完了,而foreah是依次增加,它是依 ...
- VUE中关于表单提交的简单实现
main.js import Vue from "../vue.js"; import App from "./App.js"; //启动 new Vue({ ...