自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

u013383813的博客

goodgoodstudy daydayup

  • 博客(319)
  • 问答 (1)
  • 收藏
  • 关注

原创 [LeetCode] 1109. Corporate Flight Bookings

题 : https://leetcode.com/problems/corporate-flight-bookings/题目大意n个航班,有1 到 n 标示。一种预定位置的格式是 bookings[p] = [i, j, k],意味着 第p个订单是预定 第i 至 j 航班中每个航班 k 个位置。输出各航班被预定位置的数量。思路观察限制条件,若暴力,每个航班去查所有订单 时间复杂度为 O...

2019-08-18 20:04:22 439

原创 [LeetCode] 1108. Defanging an IP Address

题:https://leetcode.com/problems/defanging-an-ip-address/题目大意给定一个 IP 地址,将IP地址中的所有 “.” 字符 变为 “[.]” 字符思路字符转换逐个扫描字符,若字符为 “.” 转化为 “[.]”,否则不处理。class Solution { public String defangIPaddr(String ad...

2019-08-18 19:41:57 303

原创 [LeetCode] 1047. Remove All Adjacent Duplicates In String

题:https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/题目大意一个字符串,若连续两个字符相同,那么就将这两个字符除去,生成新字符串。对新字符串继续进行上述处理,直到字符串没有连续两个字符相同。思路使用一个双向队列。在除去重复字符时,使用 stack的属性。遍历S,若当前字符与Deque 的尾部...

2019-05-19 21:07:36 256

原创 [LeetCode] 1046. Last Stone Weight

题:https://leetcode.com/problems/last-stone-weight/题目大意给定一组数,取出最大的两个数,然后计算绝对值的差,再将该值放入数组中。迭代上述操作,直到数组中只剩一个数。思路使用 大顶堆,每次取出两个数,计算绝对值差,然后再放入数组中。class Solution { public int lastStoneWeight(int[] ...

2019-05-19 19:17:17 483

原创 [LeetCode] 58. Length of Last Word

题:https://leetcode.com/problems/partition-array-for-maximum-sum/题目大意对于一字符串,找出最后一个单词的长度。单词使用空格来分割,字符串尾部的 ‘ ’ 忽略。思路分为两步:1.确定最后一个单词的尾部,2.找该单词的头部。找头部有两种:1.最后一个单词的前面为空格。2字符串只有一个单词。class Solution ...

2019-05-18 18:49:52 123

原创 [LeetCode] 1043. Partition Array for Maximum Sum

题:https://leetcode.com/problems/partition-array-for-maximum-sum/题目大意对于一个数组A,将A分割为多个连续的子数组,每个子数组长度不能超过K。将A中每个元素置换为 对应子数组中最大值 ,从而形成新数组,求新数组的和 最大能为多少。思路动态规划int dp[i] :数组 A[0:i] 形成新数组的最大和。初始化状态:dp...

2019-05-12 22:34:11 351

原创 [LeetCode] 1042. Flower Planting With No Adjacent

题:https://leetcode.com/problems/flower-planting-with-no-adjacent/题目大意存在N个花园,给定int[][] paths 为花园的连接关系。每个花园至多只能与其他3个花园连接。要求为每个花园指定1 - 4 种 颜色,使得相邻花园的颜色不同。思路将 paths,转化为 图 表示。查看每个节点,看起相邻的点已经使用了 4 种颜色...

2019-05-12 22:21:04 446

原创 [LeetCode] 1041. Robot Bounded In Circle

题:https://leetcode.com/problems/robot-bounded-in-circle/题目大意给定一个机器人,初始位置为(0,0) ,初始方向 为 北,机器人执行一段指令 S。S 由三种命令组成 ,“G” (直走)、“L”(左转 90度)、 “R”(右转 90度)。求若执行 S 若干次(1次或多次),机器人的轨迹是否为一个圆。思路若执行 S 若干次,机器人的轨...

2019-05-12 22:10:55 668

原创 [LeetCode] 44. Wildcard Matching

题:https://leetcode.com/problems/wildcard-matching/题目大意字符匹配,对于 字符串 s 给定 匹配字符串 p,返回 s 是否能被 p 通配。其中 p 有 两种 通配符:1.“?” 表示 通配 一个 s 中的字符。2.“*” 表示通配 任意个 s 中的字符。思路字符匹配 leetcode 10. Regular Expression Ma...

2019-05-10 22:22:51 80

原创 [LeetCode] 41. First Missing Positive

题:https://leetcode.com/problems/first-missing-positive/题目大意对于一个数组,找出数组中缺失的最小正整数,且 算法的时间复杂度为O(n),空间复杂度为O(1)。思路空间复杂度 O(n)对于这种问题,最简单的方法就是使用 set 记录 数组中已经出现的正整数,然后从1向上遍历找出最小没有出现的正整数。该方法的时间复杂度O(n),但空间...

2019-05-03 17:53:13 71

原创 [LeetCode] 37. Sudoku Solver

题:https://leetcode.com/problems/sudoku-solver/题目大意对于一个给定的 数独数组,求问题的一个解,答案填写在原数组上。数独 有三个要求:1.每行 1-9 个元素只能出现一次。2.每列 1-9 个元素只能出现一次。3.每个3x3的小区域 1-9个元素只能出现一次。思路bdfs 思路,使用boolean[][] rowVisited = n...

2019-05-01 17:17:11 123

原创 [LeetCode] 38. Count and Say

题:https://leetcode.com/problems/count-and-say/题目大意对于字符串 str = “1”,统计 str 中出现连续相同数字的个数,然后放入 新的str中,放入格式为str += cnt + str.charAt(i);思路对每个数统计,然后将统计数 与 元素数 放入str中,具体写法有两种,前一种更简单。class Solution { ...

2019-04-30 09:49:10 83

原创 [LeetCode] 80. Remove Duplicates from Sorted Array II

题:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/题目大意对于一个有序数组,若数组中若重复的元素不能大于2,多余的将会除去。在原数组上进行上述操作,最后返回数组的长度。思路双指针 & 计数在原数组上进行修改一般都是使用双指针,前一个作为新数组的修改指针,后一个作为原数组的遍历指针。使用...

2019-04-29 22:46:49 80

原创 [LeetCode] 82. Remove Duplicates from Sorted List II

题:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/题目大意对于一个有序链表,若链表中有重复元素,将该元素从链表中去除。思路计数使用sp记录head,然后head 往后 搜索,若 head.val 与 sp.val 相同,head指针向后移动,同时会使用cnt计数。直到 head.val 与 sp.v...

2019-04-29 15:19:27 74

原创 [LeetCode] 18. 4Sum

题:https://leetcode.com/problems/4sum/题目大意从 数组nums ,从中选取 4个数,使得 4 数之和为 target。思路O(n3) 类似 3 sum先取 2个数,然后 双指针方法对于 重复的数字,可以使用 两种措施改进。若 nums[i-1] == nums[i] 或 nums[j-1] == nums[j], 那么前面已经 遍历过 。若 ...

2019-04-18 23:42:50 99

原创 [LeetCode] 15. 3Sum

题:https://leetcode.com/problems/3sum/题目求一个数组中,取三个数,使得3个数的和为0。思路先排序,然后 i 从左到右 固定 一个target = -nums[i] ,若 nums[i] >0 时,i 就可以不用遍历,因为 这时后面的 两数之和 不可能为 负数。同时 若有相同的数,那么 若前面已经遍历了后面就不用遍历了。 i...

2019-04-05 18:53:15 73

原创 [LeetCode] 871. Minimum Number of Refueling Stops

题:https://leetcode.com/problems/minimum-number-of-refueling-stops/题目大意起点有油 startFuel 的车,想行驶到 终点 target位置。途中有多个加油站 station 可以加油,其中station[0] 表示与起点的距离,station[1]表示可以加的油,且 车在行驶过程中必须保持 油 >=0 。求车能到达t...

2019-04-05 10:48:34 121

原创 牛客《剑指Offer》和为S的连续正数序列

题:https://www.nowcoder.com/questionTerminal/c451a3fd84b64cb19485dad758a55ebe思路滑动窗口import java.util.ArrayList;public class Solution { public ArrayList<ArrayList<Integer> > FindConti...

2019-03-31 23:15:04 116

原创 牛客《剑指Offer》二叉搜索树与双向链表

题:https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tqId=11179&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking思路纯粹 递归的角度...

2019-03-22 16:05:35 122

原创 牛客《剑指Offer》按之字形顺序打印二叉树

题:https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0?tpId=13&tqId=11212&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking思路使用两个栈,同时...

2019-03-21 14:53:36 111

原创 牛客《剑指Offer》按之字形顺序打印二叉树

题:https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0?tpId=13&tqId=11212&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking思路使用两个栈,同时...

2019-03-21 14:53:36 80

原创 [LeetCode] 25. Reverse Nodes in k-Group

题:https://leetcode.com/problems/reverse-nodes-in-k-group/思路主要 用到的方法是 如何 在 一个 链表中 对 结点进行翻转。https://zxi.mytechroad.com/blog/?s=25/** * Definition for singly-linked list. * public class ListNode {...

2019-03-16 22:45:59 87

原创 [LeetCode] 473. Matchsticks to Square

题:https://leetcode.com/problems/matchsticks-to-square/题目大意数组nums 中只有 正整数,能否将 nums 中的元素 放入 4 个集合中,使得 每个集合中的元素和相同。思路和 416. Partition Equal Subset Sum非常相似,可以说是 前者的进阶版。先看上一道再做下一道会好很多。使用 dfs,同样 对nums ...

2019-03-10 16:26:02 99

原创 [LeetCode] 416. Partition Equal Subset Sum

题:https://leetcode.com/problems/partition-equal-subset-sum/题目大意思路方法一 dfs遍历,对于使用 dfs 分为 两个 集合,若其中有一个 成功返回 true但是 这个方法 在 java 中 会超时class Solution { public boolean dfs(int[]nums,int index, int[...

2019-03-10 15:56:51 85

原创 [LeetCode] 713. Subarray Product Less Than K

题:https://leetcode.com/problems/subarray-product-less-than-k/思路这个的 滑动窗口 很有意思,while 循环中 不是为了 获得 结果,而是 对 窗口 进行修正。因为 本身 窗口 是 答案的解。这里 有 看见 了 一种 新思路。从 统计答案的角度来说,是从 窗口 的右端来统计。以窗口 左端为 子串尾部 的 数量。clas...

2019-03-09 13:09:46 85

原创 [LeetCode] 991. Broken Calculator

题:https://leetcode.com/problems/broken-calculator/题目大意只能对 x 进行 两种操作:1.乘2;2.减1;求 最小 操作 数 使 x 变为 y。思路x —&amp;gt; y 比较难尝试 y -&amp;gt;x 。存在 两种 操作1.除以2.2.加1.对于 y &amp;lt;= x ,那么 操作数 定为 x - y若 y &amp;gt; x,那...

2019-03-05 15:11:32 182

原创 [LeetCode] 295. Find Median from Data Stream

题:https://leetcode.com/problems/find-median-from-data-stream/思路双 heep ,大小,分别 存入 两个 priorityQueue 中。

2019-02-28 19:00:23 149

原创 [LeetCode] 448. Find All Numbers Disappeared in an Array

题:https://leetcode.com/problems/find-all-anagrams-in-a-string/题目大意对于数组int[] nums,其nums.length = n;找出 i &lt;=x &lt;=n ,未出现在nums 中的元素。要求 空间复杂度为 O(1);思路方法一 数组中 正负 为 出现标志由于 数组中 均为 正数 且 只需要标识数的范围为 ...

2019-02-19 10:33:44 74

原创 [LeetCode] 977. Squares of a Sorted Array

题:https://leetcode.com/problems/squares-of-a-sorted-array/题目大意对于非递减数组A,对所有元素的平方进行排序。思路元素的平方 较大的元素 只可能是 A中 很小的(负数)或 A中较大的元素。于是使用双 指针,指向A 的 首元素 与 末尾元素。比较两者的绝对值。讲较大值放入 res 的末尾。class Solution { ...

2019-01-20 17:55:28 370

原创 [LeetCode] 976. Largest Perimeter Triangle

题:https://leetcode.com/problems/largest-perimeter-triangle/题目大意从数组A中找出三个元素,这三个元素能围成三角形且周长最长。思路组成三角形的条件 a&lt;=b&lt;=c ,a &gt; c - b;class Solution { public int largestPerimeter(int[] A) { ...

2019-01-13 21:43:22 299 1

原创 [LeetCode] 973. K Closest Points to Origin

题:题目大意对于 数组int[][] points,找出 前 K个 离原点最近的 点。思路大意大堆求前k个小的点可以使用 小堆,然后 取 先 k 个数。但这个 O(nlogn)。使用 大 堆,只用 O(nlogk),堆变小些。维护一个大堆,当堆容量 小于 k,将 point 直接加入。若大于 k,将堆顶 与 新point 比较,若新point 较小,将堆顶删除,将新point加入。...

2019-01-13 21:34:27 796

原创 [LeetCode] 752. Open the Lock

题:https://leetcode.com/problems/open-the-lock/题目大意有一个锁,上的有四个数,每个数可以循环转动从0-9,同时若锁上的数转为 deadends 中的元素时,锁会坏掉。每转一下,视为 一个 move,求将锁从 0000,转为 target的最小move。思路无向图的 搜索,使用 bfs,这里需要注意的是,对string的 任意位 改变 时,将 ...

2019-01-11 15:22:30 171

原创 [LeetCode] 611. Valid Triangle Number

题:https://leetcode.com/problems/valid-triangle-number/题目大意从数组中取3个数,使得 这 3 个元素 可以组成 三角形。思路由于 抽取的方式 是 C方法一 a + b &gt; c &amp;&amp; abs(a - b) &lt; c上面 形成 三角形的公式。对于确定的 a、b,我们只需要确定 c的位置。对数组排序,按顺序...

2019-01-10 15:36:53 125

原创 [LeetCode] 692. Top K Frequent Words

题:https://leetcode.com/problems/top-k-frequent-words/题目大意对于 string[] words,输出 出现频率前k高的 word,顺序 为 word 出现的频率 由高到低 ,频率相同的 word 按 字符排序。思路其实是对words中的所有word进行一个排序。排序有两个规则:1.word 在 words中出现的次数。2.在相同 ...

2019-01-09 15:16:31 209

原创 [LeetCode] 969. Pancake Sorting

题:https://leetcode.com/problems/pancake-sorting题目大意对于数组A,可进行 k-reverse操作,即将 前k个元素翻转,求能使A为升序的 k-reverse 操作顺序。解题思路分析k操作,k-reverse操作只会影响 前面的数,不会影响 数组中后面的数,所以思路是逐步将最大元素 放置在合适的位置。先拷贝 A 为 Abackup,然后 A...

2019-01-06 21:53:18 401

原创 [LeetCode] 970. Powerful Integers

题:https://leetcode.com/problems/powerful-integers/题目大意对于 x,y ,求所有 xi + yj &amp;lt;= bound 的数值。解题思路brute forcei循环,j循环。这里使用a来代替 pow 函数。当 x ,y 为1时,只需要循环一次。class Solution { public List&amp;lt;Integer&amp;g...

2019-01-06 21:41:09 349

原创 [LeetCode] 740. Delete and Earn

题:https://leetcode.com/problems/delete-and-earn/题目大意对于数组nums ,选取一个数 num,那么nums数组中 num - 1 与 num + 1 都会被删除,重复多次直到 nums 数组为空。求选取 num 的最大和。解题思路方法一 treeMap将nums 中所有元素进行reduce操作,得到 TreeMap,其中 key = ...

2019-01-05 19:57:48 111

原创 [LeetCode] 524. Longest Word in Dictionary through Deleting

题:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/题目大意对s删除某些元素,使得删除后的s 为 d中的某个元素。思路trie首先将 建立tire,然后是用dfs搜索,遍历s。boolean dfs(TrieNode node,String s,StringBuilder curSb,i...

2019-01-05 19:33:24 170

原创 [LeetCode] 190. Reverse Bits

题:https://leetcode.com/problems/reverse-bits/题目大意将32位的数,二进制翻转。解题思路解法和 将int a =123,翻转的思路 相同。int b= 0;while(a&gt;0){ b = b*10 + a %10; a /=10;}将新数整体左移一位,然后 取每个数的第i位置,将该位放到 新数的最低位。循环32次遍可以得到翻...

2019-01-04 15:05:51 79

原创 [LeetCode] 137. Single Number II

题:https://leetcode.com/problems/single-number-ii/题目大意给定array,其中有一个元素只出现了1次,其他元素都出现了3次。思路求和 减去(set(array)*3 - array)/2 作为答案。class Solution { public int singleNumber(int[] nums) { Set...

2019-01-04 12:12:02 76

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除