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. (单选) 以下关于链式存储结构的叙述中哪 ...
随机推荐
- eclipse配置ros cakin编译环境
先安装eclipse,之前的博客:http://www.cnblogs.com/CZM-/p/5942435.html Catkin-y 方法使用catkin无法make eclipse工程,生成pr ...
- wpf中UserControl的几种绑定方式
我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...
- vs2008编译openssl问题
运行openssl demo 时,debug 版本正常,release 版本报异常:OPENSSL_Uplink(585E6000,08): no OPENSSL_Applink .demo 编译环境 ...
- HTTP Referer 防外链
HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理. if (Req ...
- powershell玩转sqlite数据库
脚本经常需要处理文本,有时候是行列整齐文本.那么powershell脚本处理行列文本有几种方法呢?一种是excel,另外的一些是?access?sqlite? sqlite是一个很小巧的,很方便嵌入到 ...
- 关于GSM基站定位
一、基站定位两个参数 1、什么是LAC:Location Area Code(LAC)地区区域码,用来划分区域 2、什么是CellID:Cell Tower ID(Cid)CellID代表一个移动基站 ...
- php函数的可变参数
<?php function add() { $arr = func_get_args(); //func_num_args() $sum =0; for($i=0;$i<count($a ...
- Python开发入门与实战11-单元测试
11. 单元测试 本章节我们来讲讲django工程中如何实现单元测试,单元测试如何编写以及在可持续项目中单元测试的重要性. 下面是单元测试的定义: 单元测试是开发者编写的一小段代码,用于检验被测代码的 ...
- Android常见控件— — —ProgressBar
ProgressBar用于在界面上显示一个进度条,表示我们的程序正在加载一些数据. <?xml version="1.0" encoding="utf-8" ...
- C#实现右下角弹出窗口效果
/// <summary> /// 窗体动画函数 注意:要引用System.Runtime.InteropServices; /// </summary> /// <pa ...