leetcode题解日练--2016.6.19

2024-03-06 10:48
文章标签 leetcode 题解 19 日练 2016.6

本文主要是介绍leetcode题解日练--2016.6.19,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

编程新手,尽量保证每天至少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;elsepNode->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.

这篇关于leetcode题解日练--2016.6.19的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779811

相关文章

详解Spring Boot接收参数的19种方式

《详解SpringBoot接收参数的19种方式》SpringBoot提供了多种注解来接收不同类型的参数,本文给大家介绍SpringBoot接收参数的19种方式,感兴趣的朋友跟随小编一起看看吧... 目录SpringBoot接受参数相关@PathVariable注解@RequestHeader注解@Reque

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

LeetCode:64. 最大正方形 动态规划 时间复杂度O(nm)

64. 最大正方形 题目链接 题目描述 给定一个由 0 和 1 组成的二维矩阵,找出只包含 1 的最大正方形,并返回其面积。 示例1: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4 示例2: 输入: 0 1 1 0 01 1 1 1 11 1 1 1 11 1 1 1 1输出: 9 解题思路 这道题的思路是使用动态规划