LeetCode题解
EE转CS自学之路
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode 590. N叉树的后序遍历
基本思路 建立两个Stack,一个用于把每层放入,再每层弹出给下一个stack和把root交给子节点。下一个stack用于把值传递给list。 /* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} p...原创 2018-12-27 16:40:54 · 192 阅读 · 0 评论 -
LeetCode 496. 下一个更大元素I
496. 下一个更大元素I 基本思路 class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] res = new int[nums1.length]; int index = 0; for(int i=0; i<nums1.le...原创 2019-01-09 14:27:36 · 567 阅读 · 0 评论 -
LeetCode 463. 岛屿的周长
463. 岛屿的周长 设定几个规则: 读到1的数就记4 读到与右或下都是1就减2 为了避免最后和最下的数组的越界需要停止扫描 为了避免1行或1列的越界需要设置扫描行列的最小行列数 class Solution { public int islandPerimeter(int[][] grid) { int res = 0; int sum = 0; ...原创 2019-01-06 21:42:46 · 211 阅读 · 0 评论 -
LeetCode 349. 两个数组的交集
349. 两个数组的交集 基本思路 set实现单个数组无重复数,set.contains()选取重复数,iterator().hasNext()迭代器遍历set读取结果。 class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new H...原创 2019-01-05 14:30:01 · 159 阅读 · 0 评论 -
LeetCode 237. 删除链表中的节点
由于不删除结尾节点,所以只需要把删除节点的值和指向用下一个值替代即可 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solutio...原创 2018-12-25 15:14:37 · 153 阅读 · 0 评论 -
LeetCode 226. 翻转二叉树
左右置换,递归左右节点 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solutio...原创 2018-12-25 15:02:40 · 160 阅读 · 0 评论 -
LeetCode 108.将有序数组转化为二叉搜索树
108.将有序数组转化为二叉搜索树 因为数组有序,取数组中间值,然后递归树的左值和右值即可。 class Solution { public TreeNode sortedArrayToBST(int[] nums) { int n = nums.length; if(nums ==null || n == 0){ return nu...原创 2019-01-04 11:27:52 · 156 阅读 · 0 评论 -
LeetCode 171.Excel表列序号
171. Excel表列序号 题目其实就是转换26进制,26n∗mn+26n−1∗mn−1+...+26∗m126^n*m_n+26^{n-1} * m_ {n-1} +...+26*m_126n∗mn+26n−1∗mn−1+...+26∗m1, n为字母位数,m为每一位字母与A字母-1的差值。 class Solution { public int titleToNumber(St...原创 2018-12-29 16:26:42 · 279 阅读 · 0 评论 -
LeetCode 476. 数字的补救
476. 数字的补救 主要注意取反头有补码表示正负 highestOneBit(num)最高为置1,-1以后首位为0其它为1。 &保证补码为0不干扰数字 class Solution { public int findComplement(int num) { return ~num&(Integer.highestOneBit(num)-1); ...原创 2018-12-24 16:59:00 · 210 阅读 · 0 评论 -
LeetCode 617. 合并二叉树
基本思路 判断t1和t2是否为空,给val赋值并赋值 分别递归t1,t2左节点和t1,t2右节点 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int...原创 2018-12-24 16:58:09 · 237 阅读 · 0 评论 -
LeetCode 961. 重复N次元素
基本思路 对A进行排序,遍历A 记录重复次数,满足一半就返回;不满足就重置计数 class Solution { public int repeatedNTimes(int[] A) { Arrays.sort(A); int ret = A[0]; int num = 0; for(int i=0; i...原创 2018-12-23 21:36:19 · 342 阅读 · 0 评论 -
LeetCode 500. 键盘行
500. 键盘行 基本思路 创建三个字符串用于存储三行字符 遍历输入的字符 以头字符作为依据,遍历每个字符的字母判断是否包含在该行,不再直接跳出 最后把结果list转换成string class Solution { public String[] findWords(String[] words) { String s1 = "qwertyuiop"; ...原创 2018-12-28 15:42:18 · 259 阅读 · 2 评论 -
LeetCode 589. N叉树的前序遍历
与后序方法类似,只需要更改list添加数的位置。 class Solution { List<Integer> list = new ArrayList(); public List<Integer> preorder(Node root) { if (root != null) { list.add(root.v...原创 2018-12-28 13:37:13 · 195 阅读 · 0 评论 -
LeetCode 389.找不同
389.找不同 思路一(较快) 将所给的字符串转换为字符数组,求字符数组的int和,作差,再转回char,返回 class Solution { public char findTheDifference(String s, String t) { char[] ss = s.toCharArray(); char[] tt = t.toCharArray(...原创 2019-01-17 08:40:13 · 349 阅读 · 0 评论
分享