- 博客(202)
- 收藏
- 关注
原创 Leetcode 922: Sort Array By Parity II
问题描述:一个数组的元素中奇数和偶数数量相等。我们要奇数位置是奇数,偶数位置为偶数思路:双指针,分别从index=0和index=1开始。如果此时偶数位为奇数且奇数位为偶数,我们只需交换它们。如果此时奇数位为奇数,奇数指针+2;若偶数位为偶数,偶数指针+2代码如下:class Solution { public int[] sortArrayByParityII(int[] nums) { int evenPtr=0; int oddPtr=1;
2021-08-20 15:26:26
269
原创 Leetcode 917: Reverse Only Letters
问题描述:将字符串逆转,但非英文字符位置不变思路:创建一个新字符型数组。第一遍固定非英文字符,第二遍逆序安置英文字符代码如下:class Solution { public String reverseOnlyLetters(String s) { int len=s.length(); char[] newString=new char[len]; for(int i=0; i<len; i++){ newStr
2021-08-20 15:11:46
267
原创 Leetcode 914: X of a Kind in a Deck of Cards
问题描述:将一堆牌等分为n堆(n>=1),每一堆至少2张牌且每张牌号码相同。判断给定的一堆牌能否这样分思路:(1)先自然想到用哈希表统计每张牌出现的次数(2)关键步:若可以上述等分,则这些“次数”必然有除1以外的公因数 。求公因数的方法有多样,可以每两两找,也可以从2开始试到这些数中最小的为止代码如下:class Solution { public boolean hasGroupsSizeX(int[] deck) { //step1: calculate th
2021-08-20 14:07:46
269
原创 Leetcode 908: Smallest Range
问题描述:You are given an integer array nums and an integer k.In one operation, you can choose any index i where 0 <= i < nums.length and change nums[i] to nums[i] + x where x is an integer from the range [-k, k]. You can apply this operation at most o
2021-08-20 13:25:27
233
原创 Leetcode 905: Sort Array By Parity
问题描述:把数组里的偶数移到左侧,奇数移到右侧,顺序不限思路:这道题虽然没说原位操作,但是应该尽量少用空间既然涉及两端,我们自然想到双指针,起始状态指向两端。如果出现左奇右偶的情况,我们需要给他们换位。指针移动基于以下条件:若左侧指针指向偶数,则左指针+1;若·右侧指针指向奇数,则右指针-1.代码如下:class Solution { public int[] sortArrayByParity(int[] nums) { int left=0; int
2021-08-20 11:22:21
167
原创 Leetcode 819: Most Common Word
问题描述:Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.The words in paragraph are case-i
2021-08-20 10:56:38
119
原创 Leetcode 762: Prime Number of Set Bits in Binary Representation
问题描述:找出一定范围内的数字数量,这些数字满足其二进制表示有质数个1思路:思路很直接,我们需要做三件事:(1)判断质数 (2)查看二进制有几个一 (3)遍历整个区间代码如下(低配版):class Solution { public int countPrimeSetBits(int left, int right) { int counter=0; for(int i=left; i<=right; i++){ if (isPr
2021-08-20 09:43:41
76
原创 Leetcode 760: Find Anagram Mappings
问题描述:You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1. Both arrays may contain duplicates.Return an index mapping array mapping from nums1 to nums2 where mapping[i] = j means the ith element in nums1 appears in nums2 at
2021-08-20 05:59:15
119
原创 Leetcode 748: Shortest Completing Word
问题描述:给定一个车牌号,找出words里面包含所有车牌号字母且最短的单词思路:(1)题目中提到不区分大小写,但车牌号里大小写均有,所以我们先转化成小写(2)找出车牌号里面所有字母,并记录在一个list里面(3)写一个function,判断某一个单词是否包含列表里面所有的字母3.1 用hashmap记录list里面所有的字母及其出现次数(可以一开始就用map)3.2 遍历这个字符串,如果当下字符是列表字符,则其对应数量-13.3 再次遍历hashmap,判断每个key对应的value是否都小
2021-08-20 04:56:47
75
原创 Leetcode 604: Design Compressed String Iterator
问题描述:用户提供一个字符串,该字符串是字母与数字的混合:L10e2t1C1o1d16e1字母代表循环单元,其后面紧跟的数字代表循环次数。我们需要这个压缩字符串的展开形式,以此实现逐个字母访问的功能(Iterator)思路:(1)拆分:将字符串拆成字母与数字一一对应地在两个list里面(注意:这里理论上可以进一步组合从而得到完整字符串,但空间占用太多)(2)设置一个ptr,从0开始,在两个list之间同步。(3)每一次next()访问后,减少当前字母所对应的数字。若该数字降到0,说明当前字母已
2021-08-19 16:13:21
85
原创 Leetocde 734: Sentence Similarity
问题描述:similarPairs相当于一个容器,包含一系列字符串与字符串之间的对应关系。如果两个字符串(string1, string2)对应关系存在, 则称string1与string2相似。如果sentence1和sentence2每一个位置的单词均相似或者相同,则称sentence1和sentence2相似思路:毫无疑问,这里需要hashmap,但用法稍有不同。鉴于a可以和b相似,同时a也可以和c相似,但hashmap不允许同时存在(a,b), (a,c)两个对。我们考虑用(string,
2021-08-19 15:39:34
95
原创 Leetcode 733: Flood Fill
问题描述:解释说明:这道题模拟画图软件中的填充工具。当你拿起填充工具并选择一种颜色后,单机图片中的一个点,这个点及其连带的四周均变成新的颜色。如果把一个图像想象为由一个个像素格子组成的,则这里的“四周”只针对上下左右,非对角线。当一个格子变色后,其“四周”相同原颜色的格子也将连带更新颜色,以此类推地向外扩展。思路:这种”一个元素连带一片元素“的问题是dfs算法。(1)首先我们要判断有没有必要变化颜色,如果原区域是黄色,新颜色也是黄色,则我们啥都不用干如果需要变色,我们要以下步骤:、(2)更
2021-08-19 12:14:10
152
原创 Leetcode 728: Self Diving Numbers
问题描述:Self Diving Numbers 被定义为:这个数可以被它的每一位整除。若某一位是0,则它不是Self Diving Numbers找出一定范围内所有的这种数思路:这种用求余法对一个整数逐位拆解的算法练习的比较多了,所以直接上代码class Solution { public List<Integer> selfDividingNumbers(int left, int right) { List<Integer> list=new
2021-08-19 07:38:24
118
原创 Leetcode 716: Max Stack
问题描述:设计一个堆栈,支持除普通堆栈功能之外,查找最大值和移除最大值的功能思路:双堆栈,之前题目中遇到过。一个堆栈负责正常来往顺序,另外一个堆栈保持与第一个堆栈等高,且对应高度的值代表在这个高度上的最大值代码如下:class MaxStack { Stack<Integer> normalStack; Stack<Integer> maxStack; /** initialize your data structure here. */
2021-08-19 07:21:22
222
原创 Leetcode 700: Search in a Binary Search Tree
经典题目:在二叉搜索树中找指定元素,如找到则返回该节点,如找不到则返回null思路:凡是二叉搜索树,要考虑左右孩子节点与父母节点的数值大小关系。若小于,则在左子树搜索(递归);若大于则在右子树搜索(递归);若等于,则立刻返回该节点。终止条件:当某一个需要考察的节点为null时,代表所所无法进行,则需要返回null代码如下:class Solution { public TreeNode searchBST(TreeNode root, int val) { if (root
2021-08-18 15:57:47
71
原创 Leetcode 594: Longest Harmonious Subsequence
问题描述:Harmonious 序列是一组数,且这一组数中最小的和最大的之差为1. 求给出的数组中最长的H序列思路:我一开始想用双指针法,但是没能解决。答案给出了hashmap方法。先用hashmap统计数组中每个元素出现的个数,再在hashmap中搜索一个值,如果存在比这个值大1的值,那么他们可以组成H序列,并求出该序列长度。在hashmap中对每个key做同样的操作,更新最大长度,最终得到最大长度。代码如下:class Solution { public int findLHS(in
2021-08-18 14:20:23
73
原创 Leetcode 589 590: N-nary Tree Preorder/postOrder Traversal
问题描述:多叉树的前序/后序遍历//preorderclass Solution { public List<Integer> preorder(Node root) { List<Integer> list= new ArrayList<>(); helper(root, list); return list; } private void helper(Node root, Lis
2021-08-18 10:29:24
95
原创 Leetcode 575: Distribute Candies
问题描述:有n块糖,每块糖的种类用一个整数表示。现在只允许吃n/2(一半量)的糖果,求最多能吃到多少种糖果?思路:用set统计糖果的种类。若糖果种类数k>n/2,则能吃到n/2种糖果(即每块糖都不一样),若糖果种类数k<n/2,则只能吃到k种糖果代码如下:class Solution { public int distributeCandies(int[] candyType) { Set set=new HashSet<>(); i
2021-08-18 10:15:51
101
原创 Leetcode 572: Subtree of Another Tree
问题描述:判断以subRoot为根节点的树是否为以root为根节点的树的子树subRoot树为root树的子树必须满足三个条件之一:(1)subRoot树是root树左子树的子树 (2)subRoot树是root树右子树的子树 (3)subRoot树与root树相同判断两棵树是否相同的方法在之前的题目中遇到过代码如下:class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { if
2021-08-18 09:56:38
82
原创 Leetcode: 566. Reshape the Matrix
问题描述:将mn矩阵转换成rc矩阵。若尺寸支持转换,则返回转换后矩阵;若不支持,则返回原矩阵思路:先计算矩阵的长宽,判断是否尺寸一致。若一致,则建立新矩阵。将原矩阵的数字填入一个辅助序列,再将辅助序列填回新矩阵代码如下:class Solution { public int[][] matrixReshape(int[][] mat, int r, int c) { int height=mat.length; int weight=mat[0].lengt
2021-08-18 09:08:33
119
原创 Leetcode 563. Binary Tree Tilt
问题描述:Given the root of a binary tree, return the sum of every tree node’s tilt.计算二叉树所有节点tilt值之和。tilt值是一个节点左子树之和与右子树之和的差的绝对值思路:用中序遍历访问二叉树的每个节点。在每个节点上计算tilt值 (计算tilt值需要再次用到递归subLeft-subRight)代码如下:class Solution { int total=0; public int findTi
2021-08-18 07:49:21
237
原创 Leetcode 559. Maximum Depth of N-ary Tree
问题描述:Given a n-ary tree, find its maximum depth.求多叉树的最大深度思路:求深度的问题一般用递归: 某一个节点处的深度=其所有子节点的最大深度+1代码如下:class Solution { public int maxDepth(Node root) { //base case: if the root is null, the tree doesn't exist -> its height is 0 if(r
2021-08-18 06:19:18
146
原创 Leetcode 506: Relative Ranks
问题描述:解释说明:按照奥运会颁奖规则,从第一名开始以此为:金牌,银牌,铜牌,第四名,第五名。。。。返回一个数组,对应原数组运动员分数所对应的名次涉及排序,考虑用优先队列。优先队列有不同的用法,这里我介绍一种新学到的方法–lambda 表达式法lambda表达式可以在优先队列的comparator中表示排序的依据例如: (o1,o2)->(o1,o2) 相当于没写,即默认的从小到大的排序, poll()每次弹出队列里面最小的元素(o1,o2)->(o2,o1)相当于逆序,即从大到小排
2021-07-14 12:23:23
100
原创 Leetcode 504: Base7
问题描述: 十进制化7进制(范围:-10000000-10000000)思路:这个在网上找的十进制化n进制的方法,例如n=4. 方法是原数字一直除以4,直到商小于4停止。把最后的商+倒序输出余数连起来念就是得到的4进制数. 7进制同理既然是倒叙输出,我们可以借助stack,也可以不用(随过程添加)代码如下:class Solution { public String convertToBase7(int num) { //keep original data
2021-07-14 11:16:32
76
原创 Leetcode 500: Keyboard Row
问题描述:判断在给定的数组中,有哪些字符串可以用标准键盘的同一行里面的字符打出来,比如说Sad可以,因为s,a,d都是在键盘的第二行思路:根据字符行数建立集合,并搜索。(这道题目实在太无聊 :O)代码如下:class Solution { public String[] findWords(String[] words) { List<String> list=new ArrayList<>(); for(int i=0; i<w
2021-07-14 10:50:14
67
原创 Leetcode 496: Next Greater Element I
问题描述:这道题目要求从nums1入手,找到nums2中nums1[i]出现的位置,并且找到从这个位置起右侧,第一个比它大的数,若不存在,返回-1. 返回值是这些值组成的数组思路:这道题的暴力解法显然是像刚刚描述的,从nums1入手,先定位,找到其元素在nums2中的位置,再搜索从这个位置起右侧第一个大于它的值。但是这道经典题目可以用stack解决,使得时间复杂度为线性代码如下:class Solution { public int[] nextGreaterElement(int[]
2021-07-13 13:01:49
75
原创 Leetcode: Teemo Attacking
问题描述:Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + dura
2021-07-13 11:41:46
100
原创 Leetcode 482: License Key Formatting
问题描述:You are given a license key represented as a string s that consists of only alphanumeric characters and dashes. The string is separated into n + 1 groups by n dashes. You are also given an integer k.We want to reformat the string s such that each gr
2021-07-13 09:21:40
65
原创 Leetcode 492: Construct the rectangle
问题描述:已知网页的面积(整数),求最接近的长宽组合(长大于等于宽,且均为整数)思路:从最接近的一种可能性(即平方根)开始往下试根。比如说面积等于37,他的平方根(取整)等于6。先试6,6不是因数;再试5,5也不是;一直到1,1终于是(也必然是)。再比如说18,先试4,4不是因数;再试3,3是因数,那么循环可以停止了,因为我们从最接近的情形开始试 。 我们在返回时要注意区分L,W大小(是题目要求)代码如下:class Solution { public int[] constructRect
2021-07-13 08:50:32
84
原创 Leetcode 476: Number Complement
问题描述:Given a positive integer num, output its complement number. The complement strategy is to flip the bits of its binary representation.将一个数字(的二进制表示)翻转,返回新的数限制:32位,且没有leading 0(比如,5就是101, 不是00000000101)方案一:通过字符串解决数字转换问题,比较直观,但不高效思路:将原数字(十进制)转化为二进制字符
2021-07-13 07:38:13
87
原创 Leetcode 463: Island Perimeter
问题描述:计算一片水域中一个岛的周长思路: 根据陆地的位置计算四周(四个方向)水域的数量,即该部分边长,注意矩阵边界的四周需要特别考量代码如下:class Solution { public int islandPerimeter(int[][] grid) { int lowerBound=grid.length-1; //index of the lowerBound of matrix int rightBound=grid[0].length-
2021-07-05 09:24:48
81
原创 Leetcode 461: Hamming Distance
问题描述:The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, return the Hamming distance between them.例如: 1: 00014: 0100以上两个数在两个位置上不同,故hamming distance=2思路:我考虑把十
2021-07-05 08:37:38
84
原创 Leetcode 453: Minimum Moves to Equal Array Elements
问题描述:Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.In one move, you can increment n - 1 elements of the array by 1.问题分析:数组中n个数,我们每一次操作把其中的n-1个增加1,问经过多少次操作可以拉平所有的数。其实就是一个简单的数学问题。直觉告诉
2021-07-05 06:18:21
79
原创 leetcode 455: Assign Cookies
问题描述:Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and eac
2021-07-05 06:07:46
75
原创 Leetcode 543: Diameter of Binary Tree
问题描述:Given the root of a binary tree, return the length of the diameter of the tree.The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.The length of a path betw
2021-07-04 17:07:14
80
原创 Leetcode 530: Minimum Absolute Difference in BST
问题描述:Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.思路:因为是BST,中序遍历获得有序序列,然后遍历序列获得最小差代码如下:class Solution { List<Integer> list=new ArrayList<>
2021-07-04 15:28:50
51
原创 Leetcode 501: Find Mode in Binary Search Tree
问题描述:Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.If the tree has more than one mode, return them in any order.找出二叉搜索树出现频率最高的值(若有多个,全部计入,顺序不限)思路:中序遍历,将结果直接加入哈希表
2021-07-04 15:19:33
56
原创 Leetcode 404: Sum of Left Leaves
问题描述:Given the root of a binary tree, return the sum of all left leaves.求二叉树所有左叶子之和思路:设立全局变量counter如果root的左孩子存在,则先判断它是否为叶子。如果是叶子,则累加。如果不是叶子,则将其视为子问题(以左孩子为根)。如果root的右孩子存在,(无需判断其是否为叶子),直接将其视为子问题(以右孩子为根)。代码如下:class Solution { int counter=0; pu
2021-07-04 13:39:28
80
原创 Leetcode 257: Binary Tree Paths
问题描述:Given the root of a binary tree, return all root-to-leaf paths in any order.A leaf is a node with no children.打印二叉树的路径(实际上不是打印,存在list里面)思路:我们需要调用一个方法,这个方法需要有以下参数:根节点,初始路径字符串,容器。根节点是所有路径的起始点,是root初始路径字符串是空字符串,因为一开始没有加入任何节点容器是我们要返回的list首先我们要加入根
2021-07-04 12:35:42
108
原创 Leetcode 744: Find Smallest Letter Greater Than Target
问题描述:Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target.Note that the letters wrap around.For example, if target == ‘z’ and letters == [
2021-07-01 15:08:03
81
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人