加载中...

daily leetcode - reverse-nodes-in-k-group - !

题目地址 https://leetcode.com/problems/reverse-nodes-in-k-group/ 题目描述 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. Example: Given this linked list: 1->2->3->4->5 For k = 2, you should return: 2->1->4->3->5 For k = 3, you should return: 3->....

daily leetcode - swap-nodes-in-pairs - !

题目地址 https://leetcode.com/problems/swap-nodes-in-pairs/ 题目描述 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. 思路 这道题不算难,是基本的链表操作题,我们可以分别用递归和迭代来实现。对于迭代实现,还是需要建立 dummy 节点,注意在连接节点的时候,最好画个图,以免把自己搞晕了。 思路 2 设置一个 dummy 节点简化操作,dummy next 指向 head。 初始化 first 为第一个节点 初始化 second 为第二个节点 初始化 current 为 dummy first.nex....

daily leetcode - merge-k-sorted-lists - !

题目地址 https://leetcode.com/problems/merge-k-sorted-lists/ 题目描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->3->4->4->5->6 思路 这道题让我们合并 k 个有序链表,最终合并出来的结果也必须是有序的,之前做过一道 Merge Two Sorted Lists,是混合插入两个有序链表。这道题增加了难度,变成合并 k 个有序链表了,但是不管合并几个,基本还是要两两合并。那么首先考虑的方法是能不能利用之前那道题的解法来解答此题。答案是肯定的,但是需要修改,怎么修改呢,最先想到的就是两两合并,就是前两个先合并,合并好了再跟第三个,然后第四个直到第 k 个。这样的思路是对的,但是效率不....

daily leetcode - generate-parentheses - !

题目地址 https://leetcode.com/problems/generate-parentheses/ 题目描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 思路 在 LeetCode 中有关括号的题共有七道,除了这一道的另外六道是 Score of Parentheses,Valid Parenthesis String, Remove Invalid Parentheses,Different Ways to Add Parentheses,Valid Parentheses 和 Longest Valid Parentheses。这道题给定一个数字 n,让生成共有 n 个括号的所有正确的形式,对于这种列出所有结果的....

daily leetcode - merge-two-sorted-lists - !

题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 题目描述 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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 思路 这道混合插入有序链表和我之前那篇混合插入有序数组非常的相似 Merge Sorted Array,仅仅是数据结构由数组换成了链表而已,代码写起来反而更简洁。具体思想就是新建一个链表,然后比较两个链表中的元素值,把较小的那个链到新链表中,由于两个输入链表的长度可能不同,所以最终会有一个链表先完成插入所有元素,则直接另一个未完成的链表直接链入新链表的末尾。 关键点解析 代码 C++ 解法一: class Sol....

daily leetcode - valid-parentheses - !

题目地址 https://leetcode.com/problems/valid-parentheses/ 题目描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1: Input: "()" Output: true Example 2: Input: "()[]{}" Output: true Example 3: Input: "(]" Output: false Example 4: In....

daily leetcode - remove-nth-node-from-end-of-list - !

题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 题目描述 Given a linked list, remove the n th 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. Follow up: Could you do this in one pass? 思路 这道题让我们移除链表倒数第 N 个节点,限定 n 一定是有效的,即 n 不会大于链表中的元素总数。还有题目要求....

daily leetcode - 4sum - !

题目地址 https://leetcode.com/problems/4sum/ 题目描述 Given an array S of n integers, are there elements a , b , c , and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet ( a , b , c , d ) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d ) The solution set must not contain duplicate quadruplets. For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2....

daily leetcode - letter-combinations-of-a-phone-number - !

题目地址 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题目描述 Given a string containing digits from 2-9inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example: Input: "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Note: Although the above answer is in lexicographical order, your answer could be in ....

daily leetcode - 3sum-closest - !

题目地址 https://leetcode.com/problems/3sum-closest/ 题目描述 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 思路 这道题让我们求最接近给定值的三数之和,是在之前那道 3Sum 的基础上又增加了些许难度,那么这道题让返回这个最接近于给定值的值,即要保证当前三数和跟给定值之间的差的绝对值最小,所以需要定....

avatar
Lonus Lan
It's better to burn out than to fade away!
公告
暂无更新通知!
最新文章
网站资讯
文章数目 :
156
已运行时间 :
0 天
本站在线访客数 :
0
本站总访问量 :
0