leetcode题解日练--2016.6.19

来源:互联网 发布:搬瓦工vps搭建ss优化 编辑:程序博客网 时间:2024/05/21 14:49

编程新手,尽量保证每天至少3道leetcode题,仅此记录学习的一些题目答案与思路,尽量用多种思路来分析解决问题,不足之处还望指出。

今日题目:1、爬台阶;2、有环链表;3、买卖股票(最大子序列和);4、合并两个有序链表

70. Climbing Stairs | Difficulty: Easy

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
题意:爬台阶,每次可以爬1-2阶,问有多少中爬法可以到顶?
思路:
1、斐波那契数的应用,当台阶只有1阶的时候,只有一种爬法,当台阶有两阶的时候,可以分两次1步,也可以一次走两步,因此有2种爬法,当台阶有三阶的时候,可以逆推,又分两种情况,第一种情况是最后一步走了1阶,那么这种情况可能性就是(3-1)阶台阶对应的可能性,即2,第二种情况是最后一步走了2阶,这种情况下对应的可能性是(3-2)阶对应的可能性,即1,那么总计就有3种可能性;依次类推4阶对应3阶(最后一步走1阶)加上2阶(最后一步走2阶)的可能性之和。

//总之就是用迭代不用递归就好class Solution {public:    int climbStairs(int number) {        if(number==1||number==2)        {return number;}        int jumpFib=0;        int NumberMinusOne=2;        int NumberMinusTwo=1;        for(int i=3;i<=number;i++){            jumpFib = NumberMinusOne+NumberMinusTwo;            NumberMinusTwo = NumberMinusOne;            NumberMinusOne = jumpFib;        }            return jumpFib;           }};

结果:0ms,Your runtime beats 11.92% of cppsubmissions.

141. Linked List Cycle | Difficulty: Easy

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:给定一个链表,判断是否有环。
思路:
1、之前遇到过,从链表的头开始出发,设置两个指针,一个指针一次走一步,另外一个指针一次走两步,如果最终能够相遇说明有环,否则无环路。

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    bool hasCycle(ListNode *head) {        ListNode* fast = head;        ListNode* slow = head;        if (head==NULL||head->next==NULL) return false;        while(fast->next!=NULL && fast->next->next!=NULL)        {            fast = fast->next->next;            slow = slow->next;            if (fast==slow)            return true;        }        return false;    }};

结果:12ms Your runtime beats 27.03% of cppsubmissions.

121. Best Time to Buy and Sell Stock | Difficulty: Easy

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
题意:假定有一个数组给定第i天的股票的价格,设计一个最大利润的算法。
思路:
1、因为价格高的可能在价格低的前面,所以不能仅仅只是选取全局最高的和全局最低的。那么我们可以存下每一天所对应的之前的所有天数中价格最低的价格然后用该天的价格-之前最低得到利润存储下来,如果大于最大值就更新一次。

class Solution {public:    int maxProfit(vector<int>& prices) {        int maxProfit = 0;        int minPrices = INT_MAX;        for(int i=0;i<prices.size();i++)        {            minPrices = min(minPrices,prices[i]);            maxProfit = max(maxProfit,(prices[i]-minPrices));        }        return maxProfit;    }};

结果:8ms Your runtime beats 34.27% of cppsubmissions.

2、借鉴了https://leetcode.com/discuss/48378/kadanes-algorithm-since-mentioned-about-interviewer-twists的思路,是否可以将这道题转换成另外一个问题呢?例如prices是{1, 7, 4, 11}, 我们列出后一个元素与钱一个元素的差{0, 6, -3, 7}(第一个元素是0,因为没有前一个元素)。这个时候我们就相当于求最大子数列的问题了,可以用动态规划的思想来解决,用一个sum来记录累加和,并用另一个值来记录sum到达的最大值。

class Solution {public:    int maxProfit(vector<int>& prices) {        int maxProfit = 0;        int sum = 0;        for(int i=1;i<prices.size();i++)        {            sum = max(0,sum+=prices[i]-prices[i-1]);            maxProfit = max(maxProfit,sum);        }        return maxProfit;    }};

结果:8ms Your runtime beats 34.27% of cppsubmissions.

21. Merge Two Sorted Lists | Difficulty: Easy

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
题意:拼接两个排序链表
思路:
逐个比较两个链表中的元素即可

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(l1==NULL)        return l2;        if (l2==NULL)        return l1;        ListNode* head=NULL;        if(l1->val>=l2->val)        {head = l2;l2 = l2->next;}        else        {head = l1;l1 = l1->next;}        ListNode* pNode = head;        while(l1 && l2)        {            if(l1->val>=l2->val)                {pNode->next=l2;l2 =l2->next;}            else                {pNode->next= l1;l1 = l1->next;}            pNode = pNode->next;        }        if(l1==NULL)            pNode->next = l2;        else            pNode->next = l1;        return head;    }};

结果:8ms Your runtime beats 72.76% of cppsubmissions.

递归版本

class Solution {public:    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {        if(l1 == NULL) return l2;        if(l2 == NULL) return l1;        if(l1->val < l2->val) {            l1->next = mergeTwoLists(l1->next, l2);            return l1;        } else {            l2->next = mergeTwoLists(l2->next, l1);            return l2;        }    }};

结果:8ms Your runtime beats 72.76% of cppsubmissions.

0 0
原创粉丝点击