• 挑战编程题(三)


     
    题目:
    1. 有两个非负数的链表,每个链表都反序的存储一个多位数字的每一位,(例如:链表2->4->3  数字342),现在求两个链表数字的相加,求和的链表。
          例如: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
                      Output: 7 -> 0 -> 8
     
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
           //函数
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
     
        }
     
     
    解答:这道题不难主要是考察“单链表“的相关操作。
     解题思想,就是单链表每个位置相加,如果每位和大约等于10,向前一位进一,依次类推;
    如下:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
                    ListNode *result=NULL;
                    ListNode *l=result;
                     int carry =0;   //进位值
     
                     while (l1 && l2){
                                     int val=0;
                                     int tempVal= l1->val + l2->val + carry;
                                     if (tempVal>=10){
                                                    val=tempVal-10;
                                                    carry=1;
                                    }
                                     else{
                                                    val=tempVal;
                                                    carry=0;
                                    }
     
                                    ListNode *l3=(ListNode *)malloc(
                                                     sizeof(ListNode));
                                    l3->val=val;
                                    l3->next = NULL;
                                     if(!l){
                                                    l=l3;
                                                    result=l;
                                    } else{
                                                    l->next=l3;
                                                    l=l->next;
                                    }
     
                                    l1=l1->next;
                                    l2=l2->next;
                    }
                     //对l1链表剩余项处理
                     while(l1){
                                     int val=0;
                                     int tempVal= l1->val + carry;
                                     if (tempVal>=10){
                                                    val=tempVal-10;
                                                    carry=1;
                                    }
                                     else{
                                                    val=tempVal;
                                                    carry=0;
                                    }
     
                                    ListNode *l3=(ListNode *)malloc(
                                                     sizeof(ListNode));
                                    l3->val=val;
                                    l3->next = NULL;
                                     if(!l){
                                                    l=l3;
                                    } else{
                                                    l->next=l3;
                                                    l=l->next;
                                    }
     
                                    l1=l1->next;
                                    
                    }
     
                     //对l2链表剩余项处理
                     while(l2){
                                     int val=0;
                                     int tempVal= l2->val + carry;
                                     if (tempVal>=10){
                                                    val=tempVal-10;
                                                    carry=1;
                                    }
                                     else{
                                                    val=tempVal;
                                                    carry=0;
                                    }
     
                                    ListNode *l3=(ListNode *)malloc(
                                                     sizeof(ListNode));
                                    l3->val=val;
                                    l3->next = NULL;
                                     if(!l){
                                                    l=l3;
                                    } else{
                                                    l->next=l3;
                                                    l=l->next;
                                    }
                                    l2=l2->next;
                    }
     
                     //对还存在的进位处理
                     if (carry)
                    {
                                    ListNode *l3=(ListNode *)malloc(
                                                     sizeof(ListNode));
                                    l3->val=carry;
                                    l3->next = NULL;
     
                                    l->next=l3;
                                    l=l->next;
                    }
                     return result;
     
    }
     
    运行结果如下图所示:(完整源码下载地址:http://download.csdn.net/detail/cq20151207/9389996
     
    很久没有用C++的单链表了,平时用php数组实现的很多东西,这次通过这道题,让我有重新回顾了一下单链表的使用;
    关于单链表的使用,我整理了一下,大家可以参考下:http://www.cnblogs.com/qiange/p/5100441.html
     
    今天的题目
     
    3.给定一个字符串,获得最长且不重复的字符的子串的长度。例如,“abcabcbb” 的最长子串而不必重复字母是“abc”,它的长度是3为“bbbbb”最长子串而不必重复字母是 “b”,它的长度是1。
     
     int lengthOfLongestSubstring(string s) {
     
        }
     
    如有问题,欢迎和我联系。 我的邮箱 cq20151207@163.com 
  • 相关阅读:
    Java内存模型
    mysql通过“延迟关联”进行limit分页查询优化的一个实例
    RabbitMQ(二):相应机制、持久化、负载均衡分发
    RabbitMQ(一):安装配置、用户权限
    S7.Net与西门子PLC通讯——纯新手必看
    [.net core] 创建和发布NuGet包 (dotnet CLI)
    [收藏]2018年10月最新全网可用接码打码平台 [若快已挂]
    如何写出无法维护的代码
    C# ——Parallel类
    .net语音播放,自定义播报文字
  • 原文地址:https://www.cnblogs.com/qiange/p/5100576.html
Copyright © 2020-2023  润新知