剑指offer例题分享--7

Posted liudw-0215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指offer例题分享--7相关的知识,希望对你有一定的参考价值。

  前言:继续前面的分享...

  面试题31:

  技术分享图片 

  代码如下:

  

#include<iostream>
#include<limits.h>
using namespace std;

bool g_InvalidInput = false;

int FindGreatestSumOfSubArray(int *data,int len)
{
    if(data==NULL || len<=0)
    {
        g_InvalidInput = true;
        return 0;
    }
    
    int curnum = 0;
    //整型最小值
    //int greatestnum = 0x80000000;
    int greatestnum = INT_MIN;
    for(int i=0;i<len;++i)
    {
        if(curnum <= 0)
            curnum = data[i];
        else
            curnum += data[i];

        if(curnum > greatestnum)
            greatestnum = curnum;
    }
    return greatestnum;
}

int main()
{
    int data[]={1,-2,3,10,-4,7,2,-5};
    cout << "max: " << FindGreatestSumOfSubArray(data,8) << endl;
    
    return 0;
}

  面试题37:

  技术分享图片

  代码如下:

  

/*************************************************************************
    > File Name: 37.cpp
    > Author: ma6174
    > Mail: [email protected] 
    > Created Time: Tue 14 Aug 2018 04:41:52 PM CST
 ************************************************************************/

#include<iostream>
#include<algorithm>
#include<iterator>
#include<string>
#include<list>
#include<stdio.h>
using namespace std;

struct  ListNode
{
    int data;
    struct ListNode *next;
} ;

typedef struct ListNode  linknode_t; 
typedef struct ListNode* linklist_t;
linknode_t *CreateLink()         // 创建空的链表  返回值 链表的首地址
{    
    linknode_t *H;
    H = (linklist_t)malloc(sizeof(linknode_t)); 
    
    H->next = NULL;
    return H;
}   
void InitLink(linknode_t *H,int n)        // 初始化一个空的链表  
{
    linknode_t *r, *p;
    
    int i;
    r = H;                     //  r 指向 队尾位置 
    for(i = 0; i < n; i++)
    {
        p = (linknode_t *)malloc(sizeof(linknode_t));
        p->data = i+1;
        p->next = NULL;         // 没有下一个节点
        r->next = p;            // 将p 放在 r 的后面
        r = p;                  //  r 指向新的队尾
    }
    
}
void ShowLink(linknode_t* H)           // 从队首->队尾 打印所有节点
{
    struct ListNode *p;
    p = H->next;
        while(p != NULL){
        printf("%d --> ", p->data);
        p = p->next;   
    }
    printf("
");
}

unsigned int GetListLength(ListNode *pHead)
{
    unsigned int len = 0;
    ListNode *pNode = pHead;
    //pNode = pNode->next;
    while(pNode->next != NULL)
    {
        ++len;
        pNode = pNode->next;
    }
    return len;
}

ListNode *FindFirstCommonNode(ListNode *pHead1,ListNode *pHead2)
{
    //得到链表长度
    unsigned int len1 = GetListLength(pHead1);
    cout << "len1: " << len1 << endl;
    unsigned int len2 = GetListLength(pHead2);
    cout << "len2: " << len2 << endl;
    
    int nLengthDif = len1 - len2;
    ListNode *pListHeadLong = pHead1;
    ListNode *pListHeadShort = pHead2;
    if(len2 > len1)
    {
        pListHeadLong = pHead1;
        pListHeadShort = pHead2;
        nLengthDif = len2 - len1;
    }

    //现在长链表走几步,再同时在两个链表上同时遍历
    for(int i=0;i<nLengthDif;++i)
        pListHeadLong = pListHeadLong->next;

    while((pListHeadLong!=NULL)&&(pListHeadShort!=NULL)&&(pListHeadLong!=pListHeadShort))
    {
        pListHeadLong = pListHeadLong->next;
        pListHeadShort = pListHeadShort->next;
    }

    //得到第一个公共节点
    ListNode *CommonNode = pListHeadLong;
    //cout << "data: " << CommonNode->data << endl;

    return CommonNode;
}

int main()
{
    linknode_t *H = CreateLink();
    InitLink(H,5);
    ShowLink(H);
    linknode_t *H1 = CreateLink();
    InitLink(H1,3);
    ShowLink(H1);
    ListNode *node = FindFirstCommonNode(H,H1);
    if(node != NULL)
        cout << "data: " << node->data << endl;
    else 
        cout << "no CommonNode!" << endl;
       
       return 0;
}

  

以上是关于剑指offer例题分享--7的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode810. 黑板异或游戏/455. 分发饼干/剑指Offer 53 - I. 在排序数组中查找数字 I/53 - II. 0~n-1中缺失的数字/54. 二叉搜索树的第k大节点(代码片段

《剑指offer》之7-9题

完整可运行代码剑指 Offer 29. 顺时针打印矩阵

完整可运行代码剑指 Offer 29. 顺时针打印矩阵

[剑指offer]面试题7:用两个栈实现队列

剑指Offer面试题27. 二叉树的镜像