挑战编程题(三)

 
题目:
  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 

posted on 2016-01-04 23:02  钱胖子  阅读(785)  评论(1编辑  收藏  举报

导航