剑指offer 第九天
35.数组中的逆序对
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
输入描述:
题目保证输入的数组中没有的相同的数字
数据范围:
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5
示例1
输入1,2,3,4,5,6,7,0 输出7
public class Solution {
public int InversePairs(int [] array) {
if(array == null||array.length == 0) return 0;
int length = array.length;
int[] copy = new int[length];
for(int i = 0;i < length ;i++){
copy[i] = array[i];
}
int count = InversePairsCore(array,copy,0,array.length-1);
return count;
}
public int InversePairsCore(int[] array,int[] copy,int start,int end){
if(start == end){
return 0;
}
int count = 0;
int length = (end-start)/2;
//注意:这里是故意将copy和array调换位置的,因为每次执行InversePairCore之后copy在[start,end]部分都是排好序的
//随意使用data作为array输入,省去了来回复制的资源消耗。
int left = InversePairsCore(copy,array,start,start+length);
int right = InversePairsCore(copy,array,start+length+1,end);
int p1 = start+length;
int p2 = end;
int copyIdx = end;
while(p1 >= start && p2 >= start+length+1){
if(array[p1]>array[p2]){
count += p2-start-length;
//此处先判断一下,以免超出运算范围。
if(count > 1000000007)
count = count%1000000007;
copy[copyIdx--] = array[p1--];
}else{
copy[copyIdx--] = array[p2--];
}
}
while(p1 >= start){
copy[copyIdx--] = array[p1--];
}
while(p2 >= start+length+1){
copy[copyIdx--] = array[p2--];
}
return (count+left+right)%1000000007;
}
}
36.两个链表的第一个公共结点
输入两个链表,找出它们的第一个公共结点。技巧:不适用辅助Stack
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if(pHead1 == null || pHead2 == null) return null;
int L1 = getListLength(pHead1);
int L2 = getListLength(pHead2);
if(L1>L2)
for(int i = 0 ;i<(L1-L2);i++)
pHead1 = pHead1.next;
else if(L2>L1)
for(int i = 0 ;i<(L2-L1);i++)
pHead2 = pHead2.next;
while(pHead1!=null){
if(pHead1 == pHead2)
return pHead1;
pHead1 = pHead1.next;
pHead2 = pHead2.next;
}
return null;
}
public int getListLength(ListNode head){
ListNode temp = head;
int count = 1;
while(temp.next != null){
temp = temp.next;
count++;
}
return count;
}
}
剑指offer 第九天的更多相关文章
- 剑指Offer面试题:1.实现Singleton模式
说来惭愧,自己在毕业之前就该好好看看<剑指Offer>这本书的,但是各种原因就是没看,也因此错过了很多机会,后悔莫及.但是后悔是没用的,现在趁还有余力,把这本书好好看一遍,并通过C#通通实 ...
- 剑指Offer面试题:14.链表的倒数第k个节点
PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题 ...
- 《剑指offer》面试题12:打印1到最大的n位数
面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11< ...
- 《剑指offer》面试题11: 数值的整数次方
面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 面试题目——《剑指Offer》
1.把一个字符串转换成整数——<剑指Offer>P29 2.求链表中的倒数第k个结点——<剑指Offer>P30 3.实现Singleton模式——<剑指Offer> ...
- 剑指offer习题集2
1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...
- 剑指offer习题集1
1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...
- 剑指Offer:面试题20——顺时针打印矩阵(java实现)
题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数 字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...
随机推荐
- pat 1022 digital library
#include <iostream> #include <sstream> #include <string> #include <vector> # ...
- js结合方式:
1. 在页面中使用script标签,在标签中书写js代码.<head><script type="text/javascript"> alert(&q ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- Spring 当 @PathVariable 遇上 【. # /】等特殊字符
@PathVariable注解应该不是新鲜东西了Spring3.0就开始有了 URL中通过加占位符把参数传向后台 举个栗子,如下比较要说的内容比较简单就大概齐的写一下 画面侧 $.ajax({ typ ...
- c# Char && string
char 支持的方法 字符串 声明字符串 String str = [null]; 可以用此方法声明一个空字符串 连接字符串 str +"" + str1; 比较两个字符串 C ...
- javase学习小结二
三角函数方法 Math.sin(radians):Math.sin(Math.PI/6)=0.5 Math.cos(radians):Math.cos(Math.PI/3)=0.5 Math.tan( ...
- Apache自带的rotatelogs实现日志轮转
用Apache自带的rotatelogs程序处理apache生成的日志自动截断重新生成,rotatelogs是一个配合Apache管道日志功能使用的简单程序.设置方法如下: 编辑Apache的主配置文 ...
- Angular2学习笔记四(之Http通信)
前言: 在这里,我描述三个场景,即系统的注册与登录,及登录后的操作: 1.注册场景,前端页面传入用户名密码,通过一个api接口传到后台,在后台对这用户及密码进行保存: 2.登录场景,前端用户传入用户名 ...
- 使用TensorFlow Object Detection API+Google ML Engine训练自己的手掌识别器
上次使用Google ML Engine跑了一下TensorFlow Object Detection API中的Quick Start(http://www.cnblogs.com/take-fet ...
- MySQL计划任务(事件调度器)(Event Scheduler)[转]
原文链接: http://www.cnblogs.com/c840136/articles/2388512.html MySQL5.1.x版本中引入了一项新特性EVENT,顾名思义就是事件.定时任务机 ...