
LeetCode--Java版
LeetCode个人总结
James Shangguan
我是James Shangguan,目前我任职于京东,负责大型分布式系统的设计和开发工作。公众号「码上暴富」,我将与大家分享我的专业知识和经验,欢迎关注!
展开
-
LeetCode 209 长度最小的子数组
numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0。如果你已经实现 O(n) 时间复杂度的解法, 请尝试设计一个 O(n log(n)) 时间复杂度的解法。输入:target = 11, nums = [1,1,1,1,1,1,1,1]输入:target = 7, nums = [2,3,1,2,4,3]输入:target = 4, nums = [1,4,4]解释:子数组 [4,3] 是该条件下的长度最小的子数组。原创 2025-01-08 22:21:49 · 277 阅读 · 0 评论 -
LeetCode 977 有序数组的平方
给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。for循环时间复杂度O(n);解释:平方后,数组变为 [16,1,0,9,100]输入:nums = [-4,-1,0,3,10]输入:nums = [-7,-3,2,3,11]请你设计时间复杂度为 O(n) 的算法解决本问题。排序后,数组变为 [0,1,9,16,100]整体时间复杂度:O(n + nlogn)输出:[0,1,9,16,100]输出:[4,9,9,49,121]原创 2025-01-07 23:12:15 · 250 阅读 · 0 评论 -
LeetCode 704 如何正确书写一个二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。原创 2025-01-06 23:34:59 · 252 阅读 · 0 评论 -
贪心算法习题讲解
实验室的算法课程,今天轮到我给师弟师妹们讲贪心算法,顺便也复习一下。贪心算法这个名字听起来唬人,其实通常是比较简单的。虽然通常贪心算法的实现非常容易,但是,一个问题是否能够使用贪心算法,是一定要小心的。本文课通过LeetCode的一些习题,我们来回顾一下贪心算法。原创 2019-05-21 15:42:14 · 1770 阅读 · 1 评论 -
LeetCode重建二叉树系列问题总结
二叉树天然的递归特性,使得我们可以使用递归算法对二叉树进行遍历和重建。之前已经写过LeetCode二叉树的前序、中序、后序遍历(递归实现),那么本文将进行二叉树的重建,经过对比,会发现二者有着许多相似之处。原创 2019-02-27 11:30:58 · 786 阅读 · 0 评论 -
LeetCode栈的基础操作系列问题详细总结
本文总结LeetCode中考察栈的基础操作系列问题。原创 2018-11-13 14:36:42 · 690 阅读 · 3 评论 -
LeetCode 70. Climbing Stairs斐波那契系列问题解析
本文记录解决LeetCode 70. Climbing Stairs(爬台阶)斐波那契系列问题的方法,递归法和迭代法。原创 2018-11-01 15:16:30 · 601 阅读 · 2 评论 -
由LeetCode 581. Shortest Unsorted Continuous Subarray引发的对数组的深入思考(值&引用,复制数组)
本文是由LeetCode 581. Shortest Unsorted Continuous Subarray引发的对数组的一些深入思考,包括对对象、值、引用的理解和复制数组的基本操作原创 2018-10-25 14:49:59 · 574 阅读 · 1 评论 -
LeetCode 206. Reverse Linked List(反转链表)两种方法实现
本文用两种方法实现链表的反转(LeetCode 206. Reverse Linked List):①利用next指针穿针引线;②递归算法。原创 2018-10-18 15:14:23 · 723 阅读 · 0 评论 -
LeetCode 657. Robot Return to Origin(Java相同类型的包装类对象之间值的比较需要注意的地方)
本文旨在分享刷LeetCode 657. Robot Return to Origin时遇到的大坑:Java相同类型的包装类对象之间值的比较问题。原创 2018-10-17 14:48:33 · 819 阅读 · 0 评论 -
LeetCode 167. Two Sum II - Input array is sorted三种解法对比
本文对比LeetCode 167. Two Sum II - Input array is sorted三种不同的解法并进行优化,抛砖引玉,望各位大佬指教。原创 2018-10-11 19:51:13 · 435 阅读 · 1 评论 -
LeetCode删除数组元素系列问题总结
本文实现从数组中删除元素,提供时间复杂度为O(n),空间复杂度为O(1)的Java版本的基本模板,在模板上修改,可以解决LeetCode中数组删除元素相关的问题,如:27. Remove Element(删除元素),26. Remove Duplicates from Sorted Array(删除排序数组中的重复项),80. Remove Duplicates from Sorted Array...原创 2018-10-10 19:38:20 · 664 阅读 · 0 评论 -
LeetCode二叉树的层序遍历系列问题总结(队列实现)
本文用队列实现二叉树的层序遍历,提供Java版的基本模板,在模板上修改,即可解决LeetCode 102. Binary Tree Level Order Traversal,107. Binary Tree Level Order Traversal II,103. Binary Tree Zigzag Level Order Traversal,199. Binary Tree Right Side View。原创 2018-10-09 11:28:05 · 1102 阅读 · 0 评论 -
LeetCode二叉树的前序、中序、后序遍历(递归实现)
本文用递归算法实现二叉树的前序、中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍历),94. Binary Tree Inorder Traversal(二叉树中序遍历),145. Binary Tree Postorder Traversal(二叉树后序遍历)。原创 2018-10-08 16:58:27 · 786 阅读 · 0 评论 -
LeetCode刷题100. Same Tree
题目:Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Exa...原创 2018-10-07 18:26:56 · 281 阅读 · 0 评论 -
LeetCode刷题229. Majority Element II
题目描述:Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.Note: The algorithm should run in linear time and in O(1) space.Example 1:Input: [3,2,3]Output: [...原创 2018-10-07 14:59:02 · 318 阅读 · 0 评论 -
LeetCode刷题169. Majority Element
题目描述:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element...原创 2018-10-07 12:22:14 · 268 阅读 · 0 评论 -
LeetCode刷题283. Move Zeroes
题目描述:Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,3,12,0,0]解题思路1...原创 2018-10-06 23:46:17 · 267 阅读 · 0 评论 -
LeetCode刷题530. Minimum Absolute Difference in BST
题目描述:Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.Example:Input: 1 \ 3 / 2Output:1Explanati...原创 2018-10-06 21:41:23 · 253 阅读 · 0 评论 -
LeetCode刷题783. Minimum Distance Between BST Nodes
题目描述:Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.Example :Input: root = [4,2,6,1,3,null,nul...原创 2018-10-06 15:53:54 · 362 阅读 · 0 评论 -
LeetCode 338. Counting Bits的两种超简单方法解析
本文分享两种关于LeetCode 338. Counting Bits超级简单易懂的解法,第一种是使用已有的API,第二种是使用动态规划。原创 2018-10-06 12:28:09 · 475 阅读 · 0 评论 -
LeetCode刷题344. Reverse String
题目描述:Write a function that takes a string as input and returns the string reversed.Example 1:Input: "hello"Output: "olleh"Example 2:Input: "A man, a plan, a canal: Panama"Output: "amana...原创 2018-10-06 11:46:14 · 318 阅读 · 0 评论