![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
IsaacLin_code
这个作者很懒,什么都没留下…
展开
-
Longest Palindrome
https://leetcode.com/problems/longest-palindrome/#/description题目思想: 题目给我们一个字符串,由大写小写字母构成,让我们把这些字母拼凑成回文串,并返回最长回文串的长度。我的思路是这样的,这道题的关键在于如何统计出该字符串中有多少个成对的字母。具体实现是,创建一个集合set,用来存放不属于最长回文串的字母。遍历一次字符串,如果set中原创 2017-06-05 11:12:59 · 200 阅读 · 0 评论 -
Prime Number of Set Bits in Binary Representation
https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/description/题解:给出两个数L,R,要我们在【L,R】范围内找出其二进制数字1的个数是素数的数,并返回有多少个这样的数。class Solution { public int countPrimeSetBits原创 2018-02-01 14:36:01 · 189 阅读 · 0 评论 -
Merge Two Binary Trees
https://leetcode.com/problems/merge-two-binary-trees/description/题解:递归遍历结点/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNod原创 2018-01-26 01:33:57 · 135 阅读 · 0 评论 -
Self Dividing Numbers
https://leetcode.com/problems/self-dividing-numbers/description/题解:用变量i遍历left到right的所有数,利用变量t进行除和取余的操作来遍历每个数的每位数,从而进行验证是否为self-dividing number。class Solution { public List selfDividingNumbers原创 2018-01-26 15:07:57 · 143 阅读 · 0 评论 -
Binary Number with Alternating Bits
https://leetcode.com/problems/binary-number-with-alternating-bits/description/题解:给出一个整数,判断其二进制是否会出现连续的0或者1,如果出现,返回true,如果没有,返回false。以下是三种解法。第一种是我自己写的,第二第三种是借鉴的。第三个不理解的话可以用一个数带进入算一下就会懂的。第一种:clas原创 2018-02-03 14:43:08 · 157 阅读 · 0 评论 -
Partition Labels
https://leetcode.com/problems/partition-labels/description/题解:题目的意思是输入一个字符串,将其尽可能地连续地分为多个字符串,且每个字符串彼此之间没有相同的字母。最后返回每个字符串的长度。由题可得,对字符串进行切分之后,相同的字母一定是属于同一字符串的。我们可以利用这个特性,先来确定并保存每个字符串的最后一个字母在原始字符串中的原创 2018-01-27 18:06:54 · 203 阅读 · 0 评论 -
Find Anagram Mappings
https://leetcode.com/problems/find-anagram-mappings/description/题目思路:找出数组A在数组B中的位置,如果有相同的,就返回任意一个结果就行。我们只要遍历一下B数组,然后和A数组依次进行比较即可,最后把结果存储在A数组返回。过程注意相同元素不要存入同一个位置,如果已经存入某个位置,不要存,继续找。class Solution原创 2018-01-19 22:15:10 · 128 阅读 · 0 评论 -
Average of Levels in Binary Tree
https://leetcode.com/problems/average-of-levels-in-binary-tree/description/题解:给出一颗二叉树,要求返回一个集合List,包含每一层结点值的平均值。写一个search方法来遍历二叉树。用list参数保存每一层的结点总值,count参数保存每一层的结点个数,i参数表示当前层数,从0开始,root表示当前结点。/**原创 2018-02-04 13:49:48 · 108 阅读 · 0 评论 -
Complex Number Multiplication
https://leetcode.com/problems/complex-number-multiplication/description/题解:这道题只要了解复数是怎么运算就好了class Solution { public String complexNumberMultiply(String a, String b) { //a1,a2是a的实部和虚部,原创 2018-01-28 15:15:33 · 129 阅读 · 0 评论 -
Battleships in a Board
https://leetcode.com/problems/battleships-in-a-board/description/题解:题目的意思是给定一个矩阵,其中只有’.’和’X’两种符号。X只会形成1×N矩阵或者N×1矩阵,且互相不相邻。要求返回有多少个这样的矩阵。我们只要录入每个X矩阵的第一个X就可以得出有多少个矩阵。class Solution { public int原创 2018-01-29 12:50:30 · 141 阅读 · 0 评论 -
Jewels and Stones
https://leetcode.com/problems/jewels-and-stones/description/题解:给出两个字符串,返回在字符串S中包含有字符串中的J的字母的个数是多少。class Solution { public int numJewelsInStones(String J, String S) { int count = 0;原创 2018-02-05 20:40:54 · 492 阅读 · 0 评论 -
Baseball Game
https://leetcode.com/problems/baseball-game/description/题解:略class Solution { public int calPoints(String[] ops) { int sum = 0; List list = new ArrayList<>(); for(Str原创 2018-01-31 16:21:04 · 105 阅读 · 0 评论 -
Toeplitz Matrix
https://leetcode.com/problems/toeplitz-matrix/description/题解:题目主要是比较每个左上至右下对角线的元素是否相等,都相等的话返回true,有不相等的返回false。观察可得,同一对角线上的元素,横纵坐标都是差相等的常数。遍历数组中除第一行和第一列的元素,然后依次比较对角线前一个元素即可。class Solution { p原创 2018-01-23 15:40:37 · 205 阅读 · 0 评论 -
Minimum Moves to Equal Array Elements
https://leetcode.com/problems/minimum-moves-to-equal-array-elements/#/description 题目思路: /* * 一开始的目地是要想让他们全部相等,可以等同于如何让最小的数和全部比它大的数变得相等。 * 每次都是全部数加1,一个数停止增大。 * 假设这组数为1,2,3,4,5。 * 最小的数为1,想要让1和5原创 2017-06-01 11:03:59 · 132 阅读 · 0 评论 -
First Unique Character in a String
https://leetcode.com/problems/first-unique-character-in-a-string/#/description题目思路: 题目的要求是让我们在一个字符串中找到第一个没有重复的字符并返回它的索引。首先这个字符串不重复的字符有多个,且重复的字符重复次数不是偶数,所以不能通过异或全部字符来求解。但是题目的提示中说可以假设所有的字符都是小写字母,所以很容易想原创 2017-06-02 14:38:56 · 258 阅读 · 0 评论 -
Number of Boomerangs
https://leetcode.com/problems/number-of-boomerangs/#/description题目思想: 题目输入n个平面的点,要我们输出有多少个“boomerang”。一个boomerang是指平面上的一组点(i, j, k),i到j的距离等于i到k的距离,且j,k是有顺序的,即(i, j, k)和(i, k, j)不是同一个。 枚举每一个点原创 2017-06-07 23:45:17 · 166 阅读 · 0 评论 -
Add Strings
https://leetcode.com/problems/add-strings/#/description题目思路: 题目是给予两个以字符串为形式的非负整数,然后让我们输出两个数的和的字符串形式。第一想法就是通过对字符串的每一个字母进行操作,来模仿加法操作。程序解说: sb变量是用来存储两个非负整数的和的,i和j分别用来操作num1和num2的伪指针。val是用来存放num1和num2相应原创 2017-07-11 23:42:14 · 151 阅读 · 0 评论 -
Convert BST to Greater Tree
https://leetcode.com/problems/convert-bst-to-greater-tree/#/description题目思路: 题目给了一个二叉搜索树,需要我们进行转换,改变二叉树每个值。值等于其本身再加上原二叉树中比所有它大的节点值。由于BST二叉树root.left.val < root.val < root.left.val,所有我们可以从最右边的值开始遍历,用s原创 2017-07-10 00:24:07 · 164 阅读 · 0 评论 -
Construct String from Binary Tree
https://leetcode.com/problems/construct-string-from-binary-tree/#/description题目思路:本题目主要是给予一个二叉树,让我们返回一个字符串,这个字符串是由二叉树先序遍历,除了根节点,都要对把其左子树和右子树用括号括起来。如果右子树不为空,左子树为空,则要空括号。其它情况下省略空括号。因此程序先序遍历了二叉树,然后当出现左子树原创 2017-07-19 00:25:50 · 191 阅读 · 0 评论 -
Student Attendance Record I
https://leetcode.com/problems/student-attendance-record-i/#/description题目思路:本题将输入一只含有P,A,L 三个字母的字符串,如果该字符串超过一个A则返回false,或者该字符串有超过两个L连续出现在一起,则返回falsepublic class Solution { public boolean checkReco原创 2017-07-20 01:05:13 · 171 阅读 · 0 评论 -
Encode and Decode TinyURL
https://leetcode.com/problems/encode-and-decode-tinyurl/description/题解:本题主要是实现URL的编码和解密。public class Codec { ArrayList arr = new ArrayList(); // Encodes a URL to a shortened URL. publ原创 2018-01-21 12:04:59 · 144 阅读 · 0 评论 -
Maximum Binary Tree
https://leetcode.com/problems/maximum-binary-tree/description/题解:运用递归的思想,找到最大的元素,然后构建二叉树。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;原创 2018-01-21 13:06:26 · 93 阅读 · 0 评论 -
Judge Route Circle
https://leetcode.com/problems/judge-route-circle/description/题解:分解移动为水平移动和竖直移动,只要水平移动和竖直移动最后都没变,则最后的移动形成一个回路。用变量horizontal来记录竖直移动,vertical来记录水平移动。class Solution { public boolean judgeCircle(S原创 2018-01-23 14:55:25 · 116 阅读 · 0 评论 -
Trim a Binary Search Tree
https://leetcode.com/problems/trim-a-binary-search-tree/description/题解:给出一颗二叉查找树,一个范围[L,R]。要把这颗树中值不在此范围中的结点删去。根据二叉查找树的特点可知,当此结点的值落在小于 L的范围内,其左子树的所有结点的值都小于L;当此结点的值落在大于L的范围内,其右子树的所有结点的值都大于R。所以分别递归返回其原创 2018-01-30 15:58:19 · 154 阅读 · 0 评论