算法结构与设计基础作业第八周

本文介绍两种常见的算法问题解决方案:一是利用前缀和快速计算数组中指定区间的元素之和;二是通过一次遍历实现链表中倒数第n个节点的删除。

303.Range Sum Query-Immutabe

Description:

Given an integer array nums, find the sum of the elements between indices i and j (ij), inclusive

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

分析:

       题意很简单,求出一个数组第i位到第j位的和。


My C++ code:

class NumArray { 
    public:     
    NumArray(vector<int> nums) {         
        nSum.push_back(0);         
        for(auto e: nums)         
        {             
            nSum.push_back(e+nSum.back());         
            
        }     
    }      
    int sumRange(int i, int j) {         
        return nSum[j+1] - nSum[i];     
    } 
    private:     
    vector<int> nSum; 
};

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(i,j);
 */

19.Remove Nth Node From End Of List

Description:

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

分析:

      题目的意思是删除掉一个列表的倒数第n个数字。

My C++ code:

/** * Definition for singly-linked list.  
    * struct ListNode {  
    *     int val;  
    *     ListNode *next;  
    *     ListNode(int x) : val(x), next(NULL) {}  * };  
    */
    class Solution { 
        public:     
        ListNode* removeNthFromEnd(ListNode* head, int n) {         
            count = 0;         
            ListNode *dup_head = new ListNode(0);         
            dup_head->next = head;         
            recursive_helper(dup_head, n);         
            return dup_head->next;     }          
            void recursive_helper(ListNode* head, int n)     
            {         
                if(nullptr == head)         
                {             
                    //printf("from tail return\n");             
                    return;         
                }         
                else         
                {             
                    recursive_helper(head->next, n);             
                    if(count == n)             
                    {                 
                    //printf("deleteing, %d\n", head->next->val);                 
                    ListNode *toBeDel = head->next;                 
                    head->next = head->next->next;                 
                    delete toBeDel;             
                    }            
                    ++count;             
                    //printf("recursive return , head val %d, count %d\n", head->val, count);         
                }
            }     
        int count; 
    };


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值