
LeetCode编程题
此专栏不再更新,请移至【LeetCode讲解】专栏
Teacher_HENG
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode211.添加与搜索单词-数据结构设计(Java实现)
链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/class WordDictionary { private Node root; public WordDictionary() { root=new Node(); } ...原创 2019-04-26 20:06:02 · 448 阅读 · 0 评论 -
LeetCode347.前K个高频元素(Java实现)
链接:https://leetcode-cn.com/problems/top-k-frequent-elements/class Solution { private class Freq implements Comparable<Freq>{ int num,freq; public Freq(int num,int freq){ this.num...原创 2019-04-26 20:04:45 · 713 阅读 · 1 评论 -
LeetCode677.键值映射(Java实现)
链接:https://leetcode-cn.com/problems/map-sum-pairs/ class MapSum { private Node root; public MapSum() { root=new Node(); } public void insert(String word, int val) {...原创 2019-04-26 20:03:23 · 472 阅读 · 0 评论 -
LeetCode766.托普利茨矩阵(Java实现)
链接:https://leetcode-cn.com/problems/toeplitz-matrix/class Solution { public boolean isToeplitzMatrix(int[][] matrix) { if(matrix==null){ return false; } for(int i=0;i<matrix.l...原创 2019-04-26 20:02:22 · 479 阅读 · 0 评论 -
LeetCode349.两个数组的交集(Java实现)
链接:https://leetcode-cn.com/problems/intersection-of-two-arrays/class Solution { public int[] intersection(int[] nums1, int[] nums2) { TreeSet<Integer> set=new TreeSet<Integer&...原创 2019-04-26 20:01:22 · 802 阅读 · 0 评论 -
LeetCode804.唯一摩尔斯密码词(Java实现)
链接:https://leetcode-cn.com/problems/unique-morse-code-words/class Solution { public int uniqueMorseRepresentations(String[] words) {String[] codes={".-","-...","-.-.","-..",".","..-.","--.","....原创 2019-04-26 20:00:09 · 689 阅读 · 0 评论 -
LeetCode88.合并两个有序数组(Java实现)
链接:https://leetcode-cn.com/problems/merge-sorted-array/class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int index1=m-1; int index2=n-1; int i...原创 2019-04-26 19:54:10 · 884 阅读 · 0 评论 -
LeetCode485.最大连续1的个数(Java实现)
链接:https://leetcode-cn.com/problems/max-consecutive-ones/class Solution { public int findMaxConsecutiveOnes(int[] nums) { if (nums == null) { return 0; } int maxCount = 0; for (int i =...原创 2019-04-25 10:28:52 · 380 阅读 · 0 评论 -
LeetCode169.求众数(Java实现)
链接:https://leetcode-cn.com/problems/majority-element/class Solution { public int majorityElement(int[] nums) { int max=nums[0]; int count=1; for(int i=1;i<nums.length...原创 2019-04-25 10:27:57 · 420 阅读 · 0 评论 -
LeetCode905.按奇偶排序数组(Java实现)
链接:https://leetcode-cn.com/problems/sort-array-by-parity/class Solution { public int[] sortArrayByParity(int[] A) { if(A==null){ return null; } for(int i=1;i<A.length;i++){ ...原创 2019-04-25 10:27:09 · 182 阅读 · 0 评论 -
LeetCode118.杨辉三角
链接:https://leetcode-cn.com/problems/pascals-triangle/class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> lists=new ArrayList&...原创 2019-04-25 10:24:40 · 149 阅读 · 0 评论 -
LeetCode54.螺旋矩阵(Java实现)
链接:https://leetcode-cn.com/problems/spiral-matrix/class Solution { public List<Integer> spiralOrder(int[][] matrix) { ArrayList<Integer> list=new ArrayList<Integer>();...原创 2019-04-25 10:23:49 · 1557 阅读 · 0 评论 -
LeetCode498.对角线遍历(Java实现)
链接:https://leetcode-cn.com/problems/diagonal-traverse/class Solution { public int[] findDiagonalOrder(int[][] matrix) { if(matrix==null||matrix.length==0){ return new int[0]...原创 2019-04-25 10:22:48 · 342 阅读 · 0 评论 -
LeetCode66.加一(Java实现)
链接:https://leetcode-cn.com/problems/plus-one/class Solution { public int[] plusOne(int[] digits) { int carry=1; for(int i=digits.length-1;i>=0;i--){ int sum=digit...原创 2019-04-25 10:21:07 · 447 阅读 · 0 评论 -
LeetCode747.至少是其他数字两倍的最大数(Java实现)
链接:https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/class Solution { public int dominantIndex(int[] nums) { int len = nums.length; if (len == 1) return 0; ...原创 2019-04-25 10:12:50 · 313 阅读 · 0 评论 -
LeetCode724.寻找数组的中心索引(Java实现)
链接:https://leetcode-cn.com/problems/find-pivot-index/class Solution { public int pivotIndex(int[] nums) { if(nums.length<2){ return -1; } int sum=0; ...原创 2019-04-25 10:05:27 · 207 阅读 · 0 评论 -
LeetCode232.用栈实现队列(Java实现)
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks/class MyQueue { private Stack<Integer> stackA; private Stack<Integer> stackB; /** Initialize your data structure...原创 2019-04-25 10:03:03 · 314 阅读 · 0 评论 -
LeetCode225.用队列实现栈(Java实现)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues/class MyStack { private LinkedList<Integer> queue1; private LinkedList<Integer> queue2; public MyStack() { ...原创 2019-04-24 18:53:11 · 628 阅读 · 0 评论 -
LeetCode150.逆波兰表达式求值(Java实现)
链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/class Solution { public int evalRPN(String[] tokens) { Stack<Integer> stack=new Stack<Integer>(...原创 2019-04-24 18:51:50 · 1128 阅读 · 0 评论 -
LeetCode20.有效的括号(Java实现)
链接:https://leetcode-cn.com/problems/valid-parentheses/class Solution { public boolean isValid(String s) { Stack<Character> stack=new Stack<Character>(); for(int i=0;...原创 2019-04-24 18:50:19 · 608 阅读 · 0 评论 -
LeetCode450.删除二叉搜索树中的节点(Java实现)
链接:https://leetcode-cn.com/problems/delete-node-in-a-bst/class Solution { public TreeNode deleteNode(TreeNode root, int key) { if(root==null){ return null; } ...原创 2019-04-24 18:48:17 · 411 阅读 · 0 评论 -
LeetCode701.二叉搜索树中的插入操作(Java实现)
链接:https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if(root==null){ return new Tree...原创 2019-04-24 18:46:57 · 305 阅读 · 0 评论 -
LeetCode700.二叉搜索树中的搜索(Java实现)
链接:https://leetcode-cn.com/problems/search-in-a-binary-search-tree/class Solution { public TreeNode searchBST(TreeNode root, int val) { if(root==null){ return null; ...原创 2019-04-24 18:45:37 · 173 阅读 · 0 评论 -
LeetCode173.二叉搜索树迭代器(Java实现)
链接:https://leetcode-cn.com/problems/binary-search-tree-iterator/class BSTIterator { LinkedList<TreeNode> stack=new LinkedList<>(); public BSTIterator(TreeNode root) { ...原创 2019-04-24 18:43:54 · 296 阅读 · 0 评论 -
LeetCode98.验证二叉搜索树(Java实现)
链接:https://leetcode-cn.com/problems/validate-binary-search-tree/class Solution { public boolean isValidBST(TreeNode root) { BSTIterator it=new BSTIterator(root); ArrayList<I...原创 2019-04-24 18:42:29 · 540 阅读 · 0 评论 -
LeetCode2.两数相加(Java实现)
链接:https://leetcode-cn.com/problems/add-two-numbers/class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode root=new ListNode(0); ListNode p=root; ...原创 2019-04-24 18:40:37 · 368 阅读 · 0 评论 -
LeetCode21.合并两个有序链表(Java实现)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode p1=l1; ListNode p2=l2; Lis...原创 2019-04-24 18:39:07 · 1189 阅读 · 0 评论 -
LeetCode234.回文链表(Java实现)
链接:https://leetcode-cn.com/problems/palindrome-linked-list/思路:先寻找中间节点,快慢指针寻找然后将中间节点后续链表进行反转最后前半段和后半段挨个比较即可1.判空2.设定中间节点,并寻找到中间节点3.将中间节点下一跳作为头节点的链表进行反转(参考206题)4.设定指针p1从head开始,p2从中间指针下一...原创 2019-04-02 13:46:14 · 1401 阅读 · 0 评论 -
LeetCode328.奇偶链表(Java实现)
链接:https://leetcode-cn.com/problems/odd-even-linked-list/思路穿针引线的赶脚1.定义odd指针从head开始,用于穿奇数位节点2.定义even指针从head.next开始,用于穿偶数为节点3.在此注意一点,先穿奇,后穿偶,但是穿完之后,偶数位链表要在奇数位链表之后,所以事先存好偶数位链表的开始节点4.开穿,直到ev...原创 2019-04-01 23:32:40 · 664 阅读 · 0 评论 -
LeetCode203.移除链表元素(Java实现)
链接:https://leetcode-cn.com/problems/remove-linked-list-elements/思路:此处要将给定元素val全部删除1.链表判空2.定义虚拟头结点root,用于存储删除元素后的链表,最终返回root.next即可3.定义指针cur,从root开始用于遍历root所存储的链表,cur其实也算是root的尾指针4.head后移,直...原创 2019-04-01 23:21:12 · 670 阅读 · 0 评论 -
LeetCode206.反转链表(Java实现)
链接:https://leetcode-cn.com/problems/reverse-linked-list/思路一:递归实现请读者深入理解该函数的意义 0_0public ListNode reverseList(ListNode head)返回以当前节点下一跳为头节点的链表经过反转后的头节点例如:ListNode h=reverseList(head.next)解...原创 2019-04-01 15:27:57 · 396 阅读 · 1 评论 -
LeetCode19.删除链表的倒数第N个节点(Java实现)
链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/思路:重点是找到要删除元素的前驱1.设置left和right指针,均从head开始2.right向后移动N位3.如果right为空,说明删除的是第一位元素(0_0)自己画一画吧4.否则,right和left同时后移,直到right下一...原创 2019-04-01 15:16:25 · 465 阅读 · 0 评论 -
LeetCode160.相交链表(Java实现)
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/思路:1.链表判空2.设置pa从headA开始,设置pb从headB开始3.pa,pb同时向后移动,直到两者有一个为空则结束,表示无交点4.在3循环中,如果出现pa相遇pb,则此处为交点5.pa和pb后移一位6.如果pa和pb均换...原创 2019-04-01 14:42:43 · 573 阅读 · 0 评论 -
LeetCode142.环形链表 II(Java实现)
LeetCode链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/submissions/思路一:同141题,快慢指针1.链表判空2.设置两个指针q和s,均从head开始3.只要q不为空或者q下一跳不为空,q向后移两位,s向后移移位4.移动过程中,如果q和s相遇,则退出3循环5.此时q如果为空或者q的下...原创 2019-04-01 00:20:10 · 674 阅读 · 0 评论 -
LeetCode141.环形链表(Java实现)
LeetCode链接:https://leetcode-cn.com/problems/linked-list-cycle/设想一个环形跑道,两个人都向同一个方向跑,一个跑得慢,一个跑得快跑上一段时间,跑得快的肯定会追上跑得慢的快慢指针问题,可自行百度思路:1.链表判空2.设置两个指针slow和fast,均从head开始3.只要fast本身不是空,或者fast下一跳...原创 2019-03-31 23:58:19 · 671 阅读 · 0 评论