挑戰編程題(三)

接 挑戰編程題(二) http://www.cnblogs.com/qiange/p/5090588.html
 
題目:
  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 
相關文章
相關標籤/搜索