剑桥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 { ...
随机推荐
- * 197. Permutation Index【LintCode by java】
Description Given a permutation which contains no repeated number, find its index in all the permuta ...
- 基础的表ADT -数据结构(C语言实现)
读数据结构与算法分析 表的概述 形如A1,A2,A3... 操作合集 PrintList MakeEmpty Find Insert Delete 表的简单数组实现 分析: PrintList和Fin ...
- django request bug
bug描述:django请求request接收数据时,如果参数中包含分号时,会导致分号后面的消息丢失. 比如前台js调用代码 $.post('/get_params', { "A" ...
- Ubuntu 16.04 安装显卡驱动后循环登录和无法设置分辨率的一种解决方案
1. 安装环境 电脑:MSI GP63 显卡:GeForce GTX 1070 系统:Ubuntu 16.04 驱动版本:NVIDIA 384.130 2. 循环登录 如果按照这篇文章 Ubuntu ...
- 十三:Transparent Encryption in HDFS(转)
透明加密:http://blog.csdn.net/linlinv3/article/details/44963429 hadoop透明加密 kms 简介 Hadoop Key Manag ...
- 自测之Lesson16:并发通信
知识点:三个多路并发模型(select .poll .epoll) 题目:以epoll模型,编写一个可供多个客户端访问的服务器程序. 实现代码: #include <netinet/in.h&g ...
- win7 个人电脑 IIS7服务器(web服务器) 同一局域网下均可访问本机网页
建立web服务器: 1.控制面板-->程序-->打开或关闭windows功能-->internet信息服务全部打钩,确定即可. 访问网页: 1.C:\inetpub\wwwroot\ ...
- Huffuman树
问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的过程如下: 1. ...
- 软工冲刺-Alpha 冲刺 (3/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 很胖,刚学,照猫画虎做了登录与注册界面. 展示GitHub ...
- css那些事儿2 盒子模型
盒子模型是网页元素所占据页面窗口的矩形范围,是网页布局的核心基础之一,这里的盒子模型与我们平常收到的包裹类似. 一个包裹从内到外,分为真实物品部分,物品与外壳之间的填充区,外壳的厚度,当多个包裹放置在 ...