随笔分类 -  Leetcode

Leetcode题解
摘要:题目 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 输入: 2 输出: [0,1,1] 输入: 5 输出: [0,1,1,2,1,2] 解法 解法1 我们将数字按照位数进行分组 1位: 0(0) 1(1) 2位: 阅读全文
posted @ 2021-03-03 09:22 陌良 阅读(78) 评论(0) 推荐(0)
摘要:题目 编写一个算法来判断一个数 n 是不是快乐数。 「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为 1,那么这个数就是快乐数。 如果 n 是快乐数就返回 True ;不是,则返回 阅读全文
posted @ 2020-04-30 09:40 陌良 阅读(319) 评论(0) 推荐(0)
摘要:题目 (这是一个 交互式问题 ) 给你一个 山脉数组 mountainArr,请你返回能够使得 mountainArr.get(index) 等于 target 最小 的下标 index 值。 如果不存在这样的下标 index,就请返回 1。 何为山脉数组?如果数组 A 是一个山脉数组的话,那它满足 阅读全文
posted @ 2020-04-29 12:40 陌良 阅读(199) 评论(0) 推荐(0)
摘要:题目 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。 必须原地修改,只允许使用额外常数空间。 以下是一些例子,输入位于左侧列,其相应输出位于右侧列。 → `3,2,1 1,2,3` → 来 阅读全文
posted @ 2020-04-27 19:15 陌良 阅读(127) 评论(0) 推荐(0)
摘要:题目 实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 1。 示例 1: 示例 2: 说明: 当 needle 是空字符串时,我们应当返回什么值呢 阅读全文
posted @ 2020-04-27 17:33 陌良 阅读(114) 评论(0) 推荐(0)
摘要:题目 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 示例 1: 示例 2: 来源:力 阅读全文
posted @ 2020-04-27 17:22 陌良 阅读(164) 评论(0) 推荐(0)
摘要:题目 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 示例 1: 示例 2: 说明: 为什么返回数值是整数,但输出的答案是数组呢? 请注意,输入数 阅读全文
posted @ 2020-04-27 16:08 陌良 阅读(143) 评论(0) 推荐(0)
摘要:题目 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。 k 是一个正整数,它的值小于或等于链表的长度。 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。 示例: 说明: 你的算法只能使用常数的额外空间。 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。 阅读全文
posted @ 2020-04-27 15:47 陌良 阅读(183) 评论(0) 推荐(0)
摘要:题目 假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 1 。 你可以假设数组中不存在重复的元素。 你的算法时间复杂度必须是 阅读全文
posted @ 2020-04-27 12:14 陌良 阅读(155) 评论(0) 推荐(0)
摘要:题目 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示例: 来源:力扣(LeetCode) 链接:https://leetcode cn.com/problems/swap nodes in pairs 解法 解题思路 对 阅读全文
posted @ 2020-04-26 18:25 陌良 阅读(249) 评论(0) 推荐(0)
摘要:题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。 Example: 解法 阅读全文
posted @ 2020-04-26 16:42 陌良 阅读(134) 评论(0) 推荐(0)
摘要:题目 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. 将两个升序 阅读全文
posted @ 2020-04-23 23:28 陌良 阅读(140) 评论(0) 推荐(0)
摘要:题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 阅读全文
posted @ 2020-04-23 23:09 陌良 阅读(117) 评论(0) 推荐(0)
摘要:题目 Given a linked list, remove the n th node from the end of list and return its head. 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 Example: Note: Given n will a 阅读全文
posted @ 2020-04-23 19:51 陌良 阅读(117) 评论(0) 推荐(0)
摘要:题目 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all uniqu 阅读全文
posted @ 2020-04-23 19:09 陌良 阅读(120) 评论(0) 推荐(0)
摘要:题目 Given a string containing digits from 2 9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to 阅读全文
posted @ 2020-04-23 17:28 陌良 阅读(176) 评论(0) 推荐(0)
摘要:题目 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 阅读全文
posted @ 2020-04-23 16:31 陌良 阅读(98) 评论(0) 推荐(0)
摘要:题目 硬币。给定数量不限的硬币,币值为25分、10分、5分和1分,编写代码计算n分有几种表示法。(结果可能会很大,你需要将结果模上1000000007) 示例1: 示例2: 来源:力扣(LeetCode) 链接:https://leetcode cn.com/problems/coin lcci 解 阅读全文
posted @ 2020-04-23 13:30 陌良 阅读(233) 评论(0) 推荐(0)
摘要:题目 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives th 阅读全文
posted @ 2020-04-23 10:53 陌良 阅读(142) 评论(0) 推荐(0)
摘要:题目 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 编写一 阅读全文
posted @ 2020-04-22 11:43 陌良 阅读(262) 评论(0) 推荐(0)