Chap5: question 35 - 37
35. 第一个只出现一次的字符
char firtNotRepeat(char *s)
{
if(s == NULL) return 0;
int i = 0;
while(s[i] != '\0') record[s[i++]] ^= 1;
i = 0;
while(!record[s[i]]) ++i;
return s[i];
}
36.数组中的逆序对个数 (归并排序解法)
#include <iostream>
using namespace std;
void inversePairsCore(int data[], int copy[], int low, int high, int& count)
{
if(low == high) return; int mid = (low + high) / 2;
inversePairsCore(data, copy, low, mid, count);
inversePairsCore(data, copy, mid+1, high, count); int k = high, tag_mid = mid, tag_high = high;
while(low <= mid && tag_mid +1 <= high)
{
if(data[mid] > data[high])
{
copy[k--] = data[mid--];
count += high - tag_mid;
}
else if(data[mid] < data[high])
{
copy[k--] = data[high--];
}
else
copy[k--] = data[high--];
}
while(low <= mid) copy[k--] = data[mid--];
while(tag_mid+1 <= high) copy[k--] = data[high--]; for(k = low; k <= tag_high; ++k)
data[k] = copy[k];
}
int inversePairs(int data[], int length)
{
if(data == NULL && length < 1) return 0; int *copy = new int[length];
int count = 0;
inversePairsCore(data, copy, 0, length-1, count); delete[] copy;
return count;
} int main()
{
int data[] = {4, 4, 4, 3, 3};
printf("%d\n", inversePairs(data, sizeof(data)/4));
return 0;
}
37. 两个链表的第一个公共结点
int getLength(ListNode *pHead)
{
if(pHead == NULL) return 0;
int len = 0;
ListNode *p = pHead;
while(p != NULL)
{
p = p->next;
len ++;
}
return len;
}
ListNode* firstNode(ListNode *pHead1, ListNode* pHead2)
{
if(pHead1 == NULL || pHead2 == NULL) return NULL;
int len1 = getLength(pHead1);
int len2 = getLength(pHead2);
if(len1 < len2) return firstNode(pHead2, pHead1);
int k = len1 - len2;
ListNode *p1 = pHead1, *p2 = pHead2;
while(k > 0)
{
p1 = p1->next;
--k;
}
while(p1 != NULL && p2 != NULL && p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
}
if(p1 == NULL || p2 == NULL) return NULL;
return p1;
}
Chap5: question 35 - 37的更多相关文章
- Chap5: question: 29 - 31
29. 数组中出现次数超过一半的数字. 方法a. 排序取中 O(nlogn). 方法b. partition 函数分割找中位数 >=O(n). 方法c. 设计数变量,扫描一遍 ...
- 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 【35.37%】【codeforces 556C】Case of Matryoshkas
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- ethereum/EIPs-155 Simple replay attack protection 35,36
EIP 155:重放攻击保护——防止了在一个以太坊链上的交易被重复广播到另外一条链. 在看椭圆曲线时有提到,与r.s.v中的v相关 不同的共有链定义不同的chainId, 防止同一笔交易在不同的共有链 ...
- 1Z0-050
QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...
- OCJP(1Z0-851) 模拟题分析(二)over
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q35-Q39)
Question 35You have a custom Web Part that is deployed as a sandboxed solution.You need to ensure th ...
- LoadRunner面试题
在LoadRunner中为什么要设置思考时间和pacing 答: 录制时记录的是客户端和服务端的交互,如果要精确模拟 用户的行为,那么客户操作客户端时花费了很多时间要怎么模拟呢?录入 填写提交的内容, ...
- 雅虎公司C#笔试题及参考答案
Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是——21. 传输率2. 误码率3. 信息容量4. 频带利用率Question 2. (单选) 以下关于链式存储结构的叙述中哪 ...
随机推荐
- 快速排序C++
/* * quick_sort.cpp * * Created on: 2016-3-21 * Author: Lv_Lang */ //快速排序 #include <iostream> ...
- 期望DP
BZOJ 1415 #include <iostream> #include <cstring> #include <algorithm> #include < ...
- NSFetchedResultsControllerDelegate不执行
熬了2 ,3个小时 才解决这个问题 在进行IM 设置时候 NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@&q ...
- CCLabel在最大宽度已知的情况下如何获取实际宽高
当前环境在cocos2.2.6, 在UI摆图中,会遇到一种情况就是 设定了label的最大宽度MAX_WIDTH,但label的内容是动态的,如何在label输入了文字之后获取label的真实宽高? ...
- HDU 1796 容斥原理
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Flash Builder如何自定义工作目录
熟悉了myeclipse可以自定义目录的设置,今天在使用flash builder 时,当导入一个工程到现有项目空间 选择根目录,点击浏览的时候出现的目录是C:\Users\Administrator ...
- codeforces298c
link:http://codeforces.com/problemset/problem/298/C 这道题目可以看出来我智商确实拙计 #include <iostream> #incl ...
- 类似title的鼠标跟随事件
$(document).ready(function(){ // 创建一个div显示提示信息 var dropTitle = document.createElement("div" ...
- main函数的简介
//// main函数的简介.h// IOS笔记//// Created by .// Copyright © 2015年 All rights reserved.// //#import ...
- Sporadic IOException: Failed to persist config
问题 在调用Jenkins API来更新Job的时候报错‘Sporadic IOException: Failed to persist config’. 原因 https://issues.jenk ...