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的更多相关文章

  1. Chap5: question: 29 - 31

    29. 数组中出现次数超过一半的数字. 方法a. 排序取中       O(nlogn). 方法b. partition 函数分割找中位数     >=O(n). 方法c. 设计数变量,扫描一遍 ...

  2. 地上有一个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< ...

  3. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. ethereum/EIPs-155 Simple replay attack protection 35,36

    EIP 155:重放攻击保护——防止了在一个以太坊链上的交易被重复广播到另外一条链. 在看椭圆曲线时有提到,与r.s.v中的v相关 不同的共有链定义不同的chainId, 防止同一笔交易在不同的共有链 ...

  5. 1Z0-050

    QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...

  6. OCJP(1Z0-851) 模拟题分析(二)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  7. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q35-Q39)

    Question 35You have a custom Web Part that is deployed as a sandboxed solution.You need to ensure th ...

  8. LoadRunner面试题

    在LoadRunner中为什么要设置思考时间和pacing 答: 录制时记录的是客户端和服务端的交互,如果要精确模拟 用户的行为,那么客户操作客户端时花费了很多时间要怎么模拟呢?录入 填写提交的内容, ...

  9. 雅虎公司C#笔试题及参考答案

    Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是——21. 传输率2. 误码率3. 信息容量4. 频带利用率Question 2. (单选) 以下关于链式存储结构的叙述中哪 ...

随机推荐

  1. [转]SecureCRT连接主机时,无法从键盘输入

    问题: SecureCRT连接主机时,无法从键盘输入 答案: 最近通过超级终端或者SecureCRT连接主机时,都只能读取设备信息,但是无法从键盘输入,进入不了配置状态,后来仔细检查了配置,居然是流控 ...

  2. JSP相关

    1.javax.servlet.jsp这个包两个接口,六个类 2.先说两个接口,分别是HttpJspPage,JspPage(JspPage是HttpJspPage的父类,JspPage 它自己继承至 ...

  3. 理解OAuth 2.0(转载)

    作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标 ...

  4. Different types of keystore in Java Overview

    Different types of keystore in Java JKS DKS JCEKS PKCS12 PKCS11 http://www.pixelstech.net/article/14 ...

  5. 2016 - 1 - 24 CSS初步

    1.The difference between CSS and HTML HTML document is that it specities the content of the page. An ...

  6. sphinx 增量索引 实现近实时更新

    一.sphinx增量索引的设置   数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少.例如.原来的数据有几百万条 ...

  7. public static void main(String[] args){}函数理解

    主函数的一般写法如下: public static void main(String[] args){…} 下面分别解释这些关键字的作用: (1)public关键字,这个好理解,声明主函数为publi ...

  8. augustus, gene prediction, trainning

    做基因组注释 先用augustus训练,然后再用maker做基因注释 augustus提供一些训练好的,如果有和你的物种非常接近的,直接用提供的,没有的话再自己训练. 网址: http://bioin ...

  9. Java笔记3-for,switch循环,格式化输出,随机数

    大纲:一.分支结构 if switch二.循环 for while do while break continue三.格式化输出 [printf] int score = 100; String na ...

  10. Linux简介

    一.Linux发展史 Linux出现之前,还有一个叫Minix的家伙(Andrew S. Tanenbaum教授为了教学买了Unix系统,参考其代码,但没有任何的代码抄袭,自己写了Minix系统,并开 ...