
LeetCode
Line_Walker
微信公众号:芥子观须弥
展开
-
面试题06. 从尾到头打印链表
1.题目输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。示例 1:输入:head = [1,3,2]输出:[2,3,1]2. 思路2.1递归递推阶段: 每次传入 head.next ,以 head == null(即走过链表尾部节点)为递归终止条件,此时直接返回。回溯阶段:层层回溯时,将当前节点值加入列表,即tmp.add(head.val)。最终,将列表 ...原创 2020-02-15 21:46:12 · 257 阅读 · 0 评论 -
面试题05. 替换空格
一、题目请实现一个函数,把字符串 s 中的每个空格替换成"%20"。示例 1:输入:s = “We are happy.”输出:“We%20are%20happy.”二、思路2.1 遍历用另外一个初始为空的字符串来存储结果,遍历一遍字符串,每遇到非空格就直接在末尾加上该字符,如果为空则替换加到末尾。2.2 StringBuilder的使用StringBuilder是可变类,线性...原创 2020-02-15 21:39:14 · 185 阅读 · 0 评论 -
面试题04. 二维数组中的查找
1. 题目在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。2. 示例示例:现有矩阵 matrix 如下:[[1, 4, 7, 11, 15],[2, 5, 8, 12, 19],[3, 6, 9, 16, 22],[10, 13,...原创 2020-02-14 21:31:05 · 206 阅读 · 0 评论 -
面试题03. 数组中重复的数字
1. 题目在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。2.示例示例 1:输入:[2, 3, 1, 0, 2, 5, 3]输出:2 或 3限制:2 <= n <= 1000003. 思路遍历该数组,每遇到一个数字查询是否在哈希表...原创 2020-02-14 21:11:42 · 228 阅读 · 0 评论 -
LeetCode2 Add Two Numbers 两数相加
1. problem descriptionYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the t...原创 2019-10-07 16:13:43 · 131 阅读 · 0 评论 -
LeetCode4 Median of Two Sorted Arrays 寻找两个有序数组的中位数
1. problem descriptionThere are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).You m...原创 2019-09-15 20:26:02 · 184 阅读 · 0 评论 -
LeetCode11 Container With Most Water 盛最多水的容器(最大水容量) 一句话解释原理
1. problem descriptionGiven n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai)...原创 2019-09-15 20:38:46 · 242 阅读 · 0 评论 -
LeetCode15 3Sum 三数之和
1. problem descriptionGiven an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solut...原创 2019-09-15 20:45:17 · 100 阅读 · 0 评论 -
LeetCode16 3Sum Closest最接近的三数之和
1. problem descriptionGiven an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume...原创 2019-09-15 20:48:21 · 109 阅读 · 0 评论 -
LeetCode20 Valid Parentheses 有效的字符串
problem descriptionGiven a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must be closed by...原创 2019-08-04 22:06:10 · 120 阅读 · 0 评论 -
LeetCode 21. Merge Two Sorted Lists 合并两个有序链表
1. problem descriptionMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个...原创 2019-10-07 16:16:58 · 135 阅读 · 0 评论 -
LeetCode23 Merge k Sorted Lists(合并K个排序链表)
1. problem descriptionMerge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。Input:[1->4->5,1->3->4,2-...原创 2019-08-08 21:09:12 · 181 阅读 · 0 评论 -
LeetCode26 Remove Duplicates from Sorted Array删除排序数组中的重复项
1. problem descriptionGiven a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you m...原创 2019-09-15 20:30:36 · 143 阅读 · 0 评论 -
LeetCode33 Search in Rotated Sorted Array 搜索旋转排序数组
1. problem descriptionSuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).You are given a target value...原创 2019-09-15 20:52:08 · 124 阅读 · 0 评论 -
LeetCode53 最大子序和
1. problem descriptionGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4]...原创 2019-09-15 20:33:35 · 114 阅读 · 0 评论 -
LeetCode54 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Examp...原创 2019-09-15 20:56:24 · 122 阅读 · 0 评论 -
LeetCode59 Spiral Matrix II 螺旋矩阵II
1. problem descriptionGiven a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.Example:Input: 3Output:[[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]]2. s...原创 2019-09-15 20:59:25 · 160 阅读 · 0 评论 -
LeetCode 61. Rotate List 旋转链表
1. problem descriptionGiven a linked list, rotate the list to the right by k places, where k is non-negative.给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。Example 1:Input: 1->2->3->4->5->...原创 2019-10-07 16:20:23 · 148 阅读 · 0 评论 -
LeetCode62 Unique Paths 不同路径
1. problem descriptionA robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is ...原创 2019-09-15 21:09:03 · 129 阅读 · 0 评论 -
LeetCode78 Subsets子集
1. problem descriptionGiven a set of distinct integers, nums, return all possible subsets (the power set).给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。Note: The solution set must not contain duplicate sub...原创 2019-08-11 21:08:55 · 163 阅读 · 0 评论 -
LeetCode88 合并两个有序数组
1. problem descriptionGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectivel...原创 2019-09-15 21:01:55 · 109 阅读 · 0 评论 -
LeetCode104. Maximum Depth of Binary Tree 二叉树的最大深度
1. problem descriptionGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf is a...原创 2019-08-16 23:18:06 · 162 阅读 · 0 评论 -
LeetCode122 Best Time to Buy and Sell Stock II 买卖股票最佳时机
1. problem descriptionSay you have an array for which the i^th element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as...原创 2019-08-09 19:41:10 · 266 阅读 · 0 评论 -
LeetCode124 Binary Tree Maximum Path Sum 二叉树中的最大路径和
1. problem descriptionGiven a non-empty binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the ...原创 2019-08-19 22:57:02 · 154 阅读 · 0 评论 -
LeetCode136 Single Number 只出现一次的数字
1. problem descriptionGiven a non-empty array of integers, every element appears twice except for one. Find that single one.给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。Note:Your algorithm should have a l...原创 2019-08-12 21:21:28 · 172 阅读 · 0 评论 -
LeetCode146 LRU Cache (LRU缓存机制)
1. problem descriptionDesign and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) :Get the value (will always be posi...原创 2019-09-15 20:21:03 · 129 阅读 · 0 评论 -
LeetCode148 Sort List 排序链表
1. problem descriptionSort a linked list in O(n log n) time using constant space complexity(在 O(n log n) 时间复杂度和常数级空间复杂度下).eg1:Input: 4->2->1->3Output: 1->2->3->4eg2:Input: -...原创 2019-08-10 21:18:36 · 126 阅读 · 0 评论 -
LeetCode 155 Min Stack最小栈
1. problem descriptionDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time.设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。push(x) – Push element x onto stack....原创 2019-08-04 16:42:47 · 175 阅读 · 0 评论 -
LeetCode169 Majority Element 求众数
1. problem descriptionGiven 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...原创 2019-08-13 22:53:51 · 135 阅读 · 0 评论 -
LeetCode215 Kth Largest Element in an Array 数组中的第K个最大元素
problem descriptionFind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:Input: [3,2,1,5,6,4] and k...原创 2019-08-06 12:47:18 · 123 阅读 · 0 评论 -
LeetCode217 Contains Duplicate 存在重复元素
1. problem descriptionGiven an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return fa...原创 2019-09-15 21:12:40 · 118 阅读 · 0 评论 -
LeetCode230. Kth Smallest Element in a BST 二叉搜索树中第K小的元素
1. problem descriptionGiven a binary search tree, write a function kthSmallestto find the kth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.(你可以假设 k 总是有...原创 2019-08-19 23:08:44 · 163 阅读 · 0 评论 -
LeetCode231 Power of Two 2的幂
1. problem descriptionGiven an integer, write a function to determine if it is a power of two.Example 1:Input: 1Output: trueExplanation: 2^0 = 1Example 2:Input: 16Output: trueExplanation: ...原创 2019-08-16 00:08:22 · 131 阅读 · 0 评论 -
LeetCode235 Lowest Common Ancestor of a Binary Search Tree二叉搜索树的最近公共祖先
1. problem descriptionGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancest...原创 2019-08-21 22:12:01 · 144 阅读 · 0 评论 -
LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
1. problem descriptionGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is define...原创 2019-09-02 00:17:41 · 153 阅读 · 0 评论 -
LeetCode238 Product of Array Except Self 除自身以外数组的乘积
目录1. problem description2. solution1. problem descriptionGiven an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of ...原创 2019-10-07 16:04:42 · 136 阅读 · 0 评论 -
LeetCode292 Nim Game 拈游戏
1. problem descriptionYou are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the...原创 2019-09-14 23:00:37 · 240 阅读 · 0 评论