题目1

题目描述

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数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 {
int cnt=0; public int InversePairs(int[] array) {
cnt = 0;
if (array != null)
mergeSortUp2Down(array, 0, array.length - 1);
return cnt;
} public void mergeSortUp2Down(int[] array,int start,int end){
if(start>=end)
return; int mid = (start+end)/2;
mergeSortUp2Down(array,start,mid);
mergeSortUp2Down(array,mid+1,end); sort(array,start,mid,end);
} public void sort(int[] array,int start,int mid,int end){
int p1=start,p2=mid+1,k=0;
int tmp[] = new int[end-start+1];
while(p1<=mid&&p2<=end){
if(array[p1]>array[p2]){
cnt+=mid-p1+1;
cnt%=1000000007;
tmp[k++]=array[p2++];
}else{
tmp[k++]=array[p1++];
}
}
while(p1<=mid)
tmp[k++]=array[p1++];
while(p2<=end)
tmp[k++]=array[p2++]; for(int i=0;i<tmp.length;i++)
array[start++]=tmp[i];
}
}

题目2

题目描述

输入两个链表,找出它们的第一个公共结点。
一种自己的解法(如下,较长)+一种巧妙的解法(第二种,同时出发,走完不同路程的两个人 再走一遍对方走过的路 那么他们就会相遇)
/*
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 length1=0,length2=0;
ListNode node = pHead1;
while(node!=null){
length1++;
node=node.next;
}
node = pHead2;
while(node!=null){
length2++;
node=node.next;
}
ListNode firstGoNode;
int chaValue=length2-length1;
if(chaValue>0){
firstGoNode=pHead2;node=pHead1;
}else{
firstGoNode=pHead1;node=pHead2;
}
if(chaValue<0)///之前少了这个地方!!!因为可能是负数!
chaValue=-chaValue;
while(chaValue>0){
chaValue--;
firstGoNode=firstGoNode.next;
}
while(node!=null&&firstGoNode!=null){
if(node.val==firstGoNode.val)
return node;
firstGoNode=firstGoNode.next;
node=node.next;
}
return null;
}
}
/*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode p1 = pHead1;
ListNode p2 = pHead2;
while(p1!=p2){
p1 = (p1==null ? pHead2 : p1.next);
p2 = (p2==null ? pHead1 : p2.next);
}
return p1;
}
}

题目3

题目描述

统计一个数字在排序数组中出现的次数。
public class Solution {
public int GetNumberOfK(int [] array , int k) {
int minIndex = getMinIndex(array,k);
int maxIndex = getMaxIndex(array,k); return maxIndex-minIndex+1;
}
public int getMinIndex(int [] array , int k) {
int start=0,end=array.length-1;
int mid=(start+end)/2;
while(start<=end){
if(array[mid]<k){
start=mid+1;
}else{
end=mid-1;
}
mid=(start+end)/2;
}
return start;
} public int getMaxIndex(int [] array , int k) {
int start=0,end=array.length-1;
int mid=(start+end)/2;
while(start<=end){
if(array[mid]>k){
end=mid-1;
}else{
start=mid+1;
}
mid=(start+end)/2;
}
return end;
}
}

题目4

题目描述

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null)
return 0; int left = TreeDepth(root.left);
int right = TreeDepth(root.right); return left>right?left+1:right+1; }
}

题目5

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {

    boolean isBalanced = true;

    public boolean IsBalanced_Solution(TreeNode root) {
getDepth(root);
return isBalanced;
} public int getDepth(TreeNode root) {
if(root==null)
return 0;
int left = getDepth(root.left);
int right = getDepth(root.right);
if(Math.abs(left-right)>1)
{
isBalanced = false;
return 0;//已经不是平衡二叉树没必要继续遍历了
}
return left>right?left+1:right+1;
}
}

题目6

题目描述

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
public class Solution { public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
       if(array==null ||array.length<2)
           return ;
       int temp = 0;
       for(int i=0;i<array.length;i++)
           temp ^= array[i];
        
       int indexOf1 = findFirstBitIs(temp);
       for(int i=0;i<array.length;i++){
           if(isBit(array[i], indexOf1))
               num1[0]^=array[i];
           else
               num2[0]^=array[i];
       }
   }
   public int findFirstBitIs(int num){
       int indexBit = 0;
       while(((num & 1)==0) && (indexBit)<8*4){
           num = num >> 1;
           ++indexBit;
       }
       return indexBit;
   }
   public boolean isBit(int num,int indexBit){
       num = num >> indexBit;
       return (num & 1) == 1;
   } }

算法学习之剑指offer(七)的更多相关文章

  1. 算法学习之剑指offer(十一)

    一 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. import java.util.*; ...

  2. 算法学习之剑指offer(九)

    一 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). public class Solution ...

  3. 算法学习之剑指offer(六)

    题目1 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. import java.util.*; public cl ...

  4. 算法学习之剑指offer(五)

    题目1 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. public class Solution ...

  5. 算法学习之剑指offer(四)

    题目1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) /** public class TreeNode { int val = 0; Tree ...

  6. 算法学习之剑指offer(十二)

    一 题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩 ...

  7. 算法学习之剑指offer(十)

    一 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3 ...

  8. 算法学习之剑指offer(八)

    题目一 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没 ...

  9. 算法学习之剑指offer(三)

    题目1 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在 ...

随机推荐

  1. Salesforce LWC学习(七) Navigation & Toast

    上一篇我们介绍了针对LWC中常用的LDS的适配的wire service以及@salesforce模块提供的相关的service,其实LWC中还提供其他的好用的service,比如针对导航相关的lig ...

  2. Istio 1.3 发布,HTTP 遥测不再需要 Mixer

    原文链接:Istio 1.3 发布,HTTP 遥测不再需要 Mixer Istio 是 Google.IBM 和 Lyft 联合开源的服务网格(Service Mesh)框架,旨在解决大量微服务的发现 ...

  3. 【转载】pandas中的循环

    原始文章链接: https://towardsdatascience.com/how-to-make-your-pandas-loop-71-803-times-faster-805030df4f06 ...

  4. 磁盘告警之---神奇的魔法(Sparse file)

      一.问题来源 半夜钉钉接到告警,某台机器的磁盘使用率少于20%,于是迷糊中爬起来,咔咔咔 find / -size +1G,咔咔咔,把几个只有4-5G的日志文件echo空值了一下,然后吓蒙了,刚刚 ...

  5. Cabloy-CMS:动静结合,解决Hexo痛点问题(进阶篇)

    前言 前一篇文章 介绍了如何通过Cabloy-CMS快速搭建一个博客站点. 这里简单介绍Cabloy-CMS静态站点的渲染机制,更多详细的内容请参见https://cms.cabloy.com 渲染规 ...

  6. 小白专场-是否同一颗二叉搜索树-python语言实现

    目录 一.二叉搜索树的相同判断 二.问题引入 三.举例分析 四.方法探讨 4.1 中序遍历 4.2 层序遍历 4.3 先序遍历 4.4 后序遍历 五.总结 六.代码实现 一.二叉搜索树的相同判断 二叉 ...

  7. 关于git的认知

    Git,官方的解释为一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.是一个开放源码的版本控制软件. 就个人而言,这是一种不同开发者之间的代码交流.合并的途径,进而完成目 ...

  8. [工具][vim] vim设置显示行号

    转载自:electrocrazy的博客 在linux环境下,vim是常用的代码查看和编辑工具.在程序编译出错时,一般会提示出错的行号,但是用vim打开的代码确不显示行号,错误语句的定位非常不便.那么怎 ...

  9. 生产环境轻量级dns服务器dnsmasq搭建文档

    dnsmasq搭建文档 一.生产环境域名解析问题 之前生产环境设备较少,是通过维护master(192.168.1.1)设备的hosts文件实现的.每次新增设备后,需要在master的hosts文件中 ...

  10. centos文件解压缩7z

    1.7z 安装 yum install p7zip 压缩test文件夹生成test.7z 7za a -t7z -r test.7z test #a 代表添加文件/文件夹到压缩包 -t 是指定压缩类型 ...