剑指offer66题(Python)——第七天

37、数组中的逆序对

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007。

【思路】根绝归并排序的原理,可得到时间复杂度为O(n*logn)的方法。

先将原序列排序,然后从排完序的数组中取出最小的,它在原数组中的位置表示有多少比它大的数在它前面,每取出一个在原数组中删除该元素,保证后面取出的元素在原数组中是最小的,这样其位置才能表示有多少比它大的数在它前面,即逆序对数。

此题用python无法AC,给出C++的方法。

# -*- coding:utf-8 -*-
class Solution:
    def InversePairs(self, data):
        # write code here
        count = 0
        copy = []
        for i in data:
            copy.append(i)
        copy.sort()
        for i in range(len(copy)):
            count += data.index(copy[i])
            data.remove(copy[i])
        return count%1000000007
class Solution {
public:
    int InversePairs(vector<int> data) {
       int length=data.size();
        if(length<=0)
            return 0;
       //vector<int> copy=new vector<int>[length];
       vector<int> copy;
       for(int i=0;i<length;i++)
           copy.push_back(data[i]);
       long long count=InversePairsCore(data,copy,0,length-1);
       //delete[]copy;
       return count%1000000007;
    }
    long long InversePairsCore(vector<int> &data,vector<int> &copy,int start,int end)
    {
       if(start==end)
          {
            copy[start]=data[start];
            return 0;
          }
       int length=(end-start)/2;
       long long left=InversePairsCore(copy,data,start,start+length);
       long long right=InversePairsCore(copy,data,start+length+1,end); 
        
       int i=start+length;
       int j=end;
       int indexcopy=end;
       long long count=0;
       while(i>=start&&j>=start+length+1)
          {
             if(data[i]>data[j])
                {
                  copy[indexcopy--]=data[i--];
                  count=count+j-start-length;          //count=count+j-(start+length+1)+1;
                }
             else
                {
                  copy[indexcopy--]=data[j--];
                }          
          }
       for(;i>=start;i--)
           copy[indexcopy--]=data[i];
       for(;j>=start+length+1;j--)
           copy[indexcopy--]=data[j];       
       return left+right+count;
    }
};

38、两个链表的第一个公共结点

输入两个链表,找出它们的第一个公共结点。
【思路】 两个单向链表若有公共结点,则从第一个公共结点开始,之后的节点都是重合的。先得到两个链表的长度,让长的先走两个链表的长度差,然后在一起走。
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        list1 = []
        list2 = []
        while pHead1:
            list1.append(pHead1.val)
            pHead1 = pHead1.next
        while pHead2:
            if pHead2.val in list1:
                return pHead2
            else:
                pHead2 = pHead2.next


39、数字在排序数组中出现的次数

统计一个数字在排序数组中出现的次数。
【思路】二分查找,找到第一个K 和 最后一个K,二者位置相减。然而我第一个想到的是用字典。
# -*- coding:utf-8 -*-
class Solution:
    def GetNumberOfK(self, data, k):
        # write code here
        if k not in data :
            return 0
        if data is None:
            return 0
        dic={}
        for i in data:
            if i in dic:    
                dic[i]+=1
            else:
                dic[i]=1
        for key,v in dic.items():
            if int(key)==k:
                return v
            


40、二叉树的深度

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
【思路】不要忘记加上根结点(+1)
# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        if pRoot is None:
            return False
        nleft = self.TreeDepth(pRoot.left)
        nright = self.TreeDepth(pRoot.right)
        if nleft > nright:
            return nleft+1
        else:
            return nright+1


41、数组中只出现一次的数次

输入一棵二叉树,判断该二叉树是否是平衡二叉树。
【思路】 下面第一种方法遍历了两次结点,第二种方法采用后序遍历,后序遍历二叉树,遍历过程中求子树高度,判断是否平衡。
# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def IsBalanced_Solution(self, pRoot):
        # write code here
        if pRoot is None:
            return True
        nleft = self.TreeDepth(pRoot.left)
        nright = self.TreeDepth(pRoot.right)
        dif=nleft-nright
        if dif<-1 or dif >1:
            return False
        else:
            return True
        
    def TreeDepth(self, pRoot):
        # write code here
        if pRoot is None:
            return False
        nleft = self.TreeDepth(pRoot.left)
        nright = self.TreeDepth(pRoot.right)
        if nleft > nright:
            return nleft+1
        else:
            return nright+1
# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def IsBalanced_Solution(self, pRoot):
        # write code here
        return self.IsBalanced(pRoot) != -1   
    
    def IsBalanced(self, root):
        if not root:
            return 0
        left = self.IsBalanced(root.left)
        right = self.IsBalanced(root.right)
        if left == -1 or right == -1 or abs(left-right) > 1:
            return -1
        return max(left, right)+1

42、数组中只出现一次的数次

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
【思路】可以用位运算实现,如果将所有所有数字相异或,则最后的结果肯定是那两个只出现一次的数字异或
 的结果,所以根据异或的结果1所在的最低位,把数字分成两半,每一半里都还有只出现一次的数据和成对出现的数据
 这样继续对每一半相异或则可以分别求出两个只出现一次的数字。
嗯,但是我没这么写。
# -*- coding:utf-8 -*-
class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        # write code here
            if array is None:
                return 0
            dic = {}
            for i in array:
                if i in dic:
                    dic[i] += 1
                else:
                    dic[i] = 1
            vec = []
            for key, v in dic.items():

                if v == 1:
                    vec.append(int(key))
            return vec


猜你喜欢

转载自blog.csdn.net/u012114090/article/details/80463307
今日推荐