随笔分类 -  leetcode

摘要:问题: 求构造类Calendar,能够记录用户录入自己的日程,日程起止时间不能重合,重合则返回false。 Example 1: MyCalendar(); MyCalendar.book(10, 20); // returns true MyCalendar.book(15, 25); // re 阅读全文
posted @ 2020-05-05 15:06 habibah_chang
摘要:问题: 求给定数组的对称轴index。(其左边元素之和=右边元素之和) 若不存在则返回-1,若存在多个,返回最左边的对称轴。 Example 1: Input: nums = [1, 7, 3, 6, 5, 6] Output: 3 Explanation: The sum of the numbe 阅读全文
posted @ 2020-05-05 13:00 habibah_chang
摘要:问题: 求给定数组中两两元素之差,从小到大第k个差是多少 Example 1: Input: nums = [1,3,1] k = 1 Output: 0 Explanation: Here are all the pairs: (1,3) -> 2 (1,1) -> 0 (3,1) -> 2 Th 阅读全文
posted @ 2020-05-04 14:31 habibah_chang
摘要:问题: 给定两个数组,求两个数组中最长公共子数组的长度。 Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2 阅读全文
posted @ 2020-05-04 12:43 habibah_chang
摘要:问题: 解码问题,给出一串0,1数列, 遇到1,则由10或11解码为一个字符。 否则遇到0,则单独0解码为一个字符。求解码到最后一个字符是否由0解码而来。 (给出数列,总是以0为结尾) Example 1: Input: bits = [1, 0, 0] Output: True Explanati 阅读全文
posted @ 2020-05-04 11:30 habibah_chang
摘要:问题: 橘子催熟问题,0:空地,1:新鲜橘子,2:成熟橘子 每一分钟成熟橘子会催熟四周的新鲜橘子,但如果新鲜橘子四周空着,则不会被催熟。 求给定数组情况,最多需要多久催熟所有的橘子,如果无法催熟所有则返回-1。 Example 1: Input: [[2,1,1],[1,1,0],[0,1,1]] 阅读全文
posted @ 2020-04-29 19:10 habibah_chang
摘要:问题: 给定数组0代表海水,1代表陆地,若陆地上下左右皆为海水,那么则称该陆地为一小岛。 求给定数组中有多少小岛。 Example 1: Input: 11110 11010 11000 00000 Output: 1 Example 2: Input: 11000 11000 00100 0001 阅读全文
posted @ 2020-04-29 15:53 habibah_chang
摘要:问题: 买股票问题,给出一个数组,表示某个股票在每一天的价格, 买家可以在一天买入,在之后的一天卖出,一次交易会产生交易费fee,求最大获利。 Example 1: Input: prices = [1, 3, 2, 8, 4, 9], fee = 2 Output: 8 Explanation: 阅读全文
posted @ 2020-04-26 15:31 habibah_chang
摘要:问题: 求给定数组的连续子数组个数,使得子数组之乘积,小于给定值 k Example 1: Input: nums = [10, 5, 2, 6], k = 100 Output: 8 Explanation: The 8 subarrays that have product less than 阅读全文
posted @ 2020-04-24 18:29 habibah_chang
摘要:问题: 给定一个非负整数,求只交换一次某两位的数字,使得值最大,求该最大值。 Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Input: 9973 Out 阅读全文
posted @ 2020-04-21 14:24 habibah_chang
摘要:问题: 给定StartWord和EndWord,在给定的dict中寻找每次值变换一个字母,最终从StartWord能够推移到EndWord的所有最短序列。 Example 1: Input: beginWord = "hit", endWord = "cog", wordList = ["hot", 阅读全文
posted @ 2020-04-21 13:11 habibah_chang
摘要:问题: 给定一个由0,1 组成的二维数组,0代表海水,1代表陆地, 求给定数组所形成小岛的最大面积。 Example 1: [[0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,1,1,0,1,0,0,0,0,0,0,0,0], 阅读全文
posted @ 2020-04-20 14:04 habibah_chang
摘要:问题: 给定一个数组,求从中取得3组连续长度为 k 的子数组,使得3组数组和为最大,且使得3组的index尽可能小(★)。 Example: Input: [1,2,1,2,6,7,5,1], 2 Output: [0, 3, 5] Explanation: Subarrays [1, 2], [ 阅读全文
posted @ 2020-04-20 12:51 habibah_chang
摘要:问题: 给定一个n,有数组1~n, 排列该数组,使得数组两两元素之间的差值有k种。 Example 1: Input: n = 3, k = 1 Output: [1, 2, 3] Explanation: The [1, 2, 3] has three different positive int 阅读全文
posted @ 2020-04-19 13:28 habibah_chang
摘要:问题: 给定一个攻击时间点数组,和每一次攻击所持续的时间长度。 求在攻击时间点数组的攻击下,一共能持续多久。 Example 1: Input: [1,4], 2 Output: 4 Explanation: At time point 1, Teemo starts attacking Ashe 阅读全文
posted @ 2020-04-19 11:26 habibah_chang
摘要:问题: 给定数组,nums[i]=k,k!=0 若k>0, 则下一个数的index是向右+|k| 若k<0,则下一个数的index是向左-|k| (数组首尾相连) 这样的移动规则,问是否存在形成一个循环圈的访问环。(该环长度>1,且单向移动) 解法: 快慢指针法 慢指针一次移动一个,快指针一次移动两 阅读全文
posted @ 2020-04-16 16:07 habibah_chang
摘要:问题: 给定一个数组,1 ≤ a[i] ≤ n (n = size of array),其中一些元素出现2次,其他出现1次, 求 出现两次的这些元素。 要求:不借助额外空间,时间复杂度为O(n) 解法: 抓住该题目给定数组的特性: ·所有元素 在1~n之间,可联系到 数值 和 偏移位置 的对应关系。 阅读全文
posted @ 2020-04-16 10:34 habibah_chang
摘要:问题: 设计数据结构,使得以下三个方法的时间复杂度都为O(1) 允许插入重复数字。 insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if 阅读全文
posted @ 2020-04-14 19:42 habibah_chang
摘要:问题: 设计数据结构,使得以下三个方法的时间复杂度都为O(1) insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if p 阅读全文
posted @ 2020-04-14 13:33 habibah_chang
摘要:问题: 给定数组(含有正数负数),求连续子集合和=k的子集合数。 Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of 阅读全文
posted @ 2020-04-12 19:30 habibah_chang