zoukankan      html  css  js  c++  java
  • 剑指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: ma6174@163.com 
        > 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;
    }

      

  • 相关阅读:
    (SenchaTouch+PhoneGap)开发笔记(2)开发环境搭建二
    Sql语句复习
    冒泡排序
    微信开发订阅号(ASP.NET MVC4+jquery mobile+AppHarbor发布)
    Ext4 ComboBox组件使用
    ExtJs 进度条(轮询)
    如何替换掉.net toolStrip控件溢出按钮背景图
    easyui-menu 宽度自适应
    Python之入门学习
    servlet和filter的区别
  • 原文地址:https://www.cnblogs.com/liudw-0215/p/9476797.html
Copyright © 2011-2022 走看看