刷题之LeetCode
sun10081
这个作者很懒,什么都没留下…
展开
-
leetcode刷题之旅(94)二叉树的中序遍历
题目描述给定一个二叉树,返回它的中序 遍历 样例输入: [1,null,2,3] 1 \ 2 / 3输出: [1,3,2] 思路分析方法一:递归,按照中序遍历“左根右”的顺序,依次遍历,递归即可方法二:和先序类似,非递归实现,由根节点向左遍历(过程中并不添加),直到叶子节点,随后pop操作相当于返回上一节点,再遍历右子树部...原创 2018-09-21 01:00:58 · 188 阅读 · 0 评论 -
leetcode刷题之旅(27)Remove Element
题目描述Given an array nums and a value val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input ar...原创 2018-06-01 00:22:49 · 143 阅读 · 0 评论 -
leetcode刷题之旅(28)Implement strStr()
题目描述Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Clarification:What should we return when needle is an empty string? This ...原创 2018-05-31 12:26:52 · 93 阅读 · 0 评论 -
leetcode刷题之旅(25)Reverse Nodes in k-Group
题目描述Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number o...原创 2018-05-31 11:39:36 · 109 阅读 · 0 评论 -
leetcode刷题之旅(26)Remove Duplicates from Sorted Array
题目描述Given 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 must do this by modif...原创 2018-05-31 00:33:13 · 74 阅读 · 0 评论 -
leetcode刷题之旅(47)Permutations II
题目描述Given a collection of numbers that might contain duplicates, return all possible unique permutations.样例Example:Input: [1,1,2]Output:[ [1,1,2], [1,2,1], [2,1,1]]思路分析思路和上题基本一致,不过此题增加了重复元素,需...原创 2018-06-13 08:41:09 · 120 阅读 · 0 评论 -
leetcode刷题之旅(46)Permutations
题目描述Given a collection of distinct integers, return all possible permutations.样例Example:Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]思路分析假设我们有了当前前 i 个元素的组...原创 2018-06-13 00:55:27 · 139 阅读 · 0 评论 -
leetcode刷题之旅(24) Swap Nodes in Pairs
题目描述Given a linked list, swap every two adjacent nodes and return its head.Note:Your algorithm should use only constant extra space.You may not modify the values in the list's nodes, only nodes itself...原创 2018-05-30 08:36:32 · 104 阅读 · 0 评论 -
leetcode刷题之旅(23) Merge k Sorted Lists
题目描述Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.样例Example:Input:[ 1->4->5, 1->3->4, 2->6]Output: 1->1->2->3->4...原创 2018-05-29 12:18:55 · 123 阅读 · 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.样例Exampl...原创 2018-06-23 00:21:06 · 89 阅读 · 0 评论 -
leetcode刷题之旅(101)Symmetric Tree
题目描述Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).判断是否为对称二叉树样例For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3...原创 2018-06-23 01:00:03 · 105 阅读 · 0 评论 -
leetcode刷题之旅(43)字符串相乘
题目描述给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。 样例示例 1:输入: num1 = "2", num2 = "3"输出: "6"示例 2:输入: num1 = "123", num2 = "456"输出: "56088"说明:num1 和 num2 的长度小原创 2018-09-10 16:48:17 · 139 阅读 · 0 评论 -
leetcode刷题之旅(144)二叉树的前序遍历
题目描述给定一个二叉树,返回它的 前序 遍历 样例输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 思路分析方法一:递归,由先序“根左右”的顺序,依次遍历,进行递归即可方法二:非递归实现,由根节点向左遍历,直到叶子节点,随后pop操作相当于返回上一节点,再遍历右子树部分即可方法三:双指针...原创 2018-09-21 00:25:21 · 181 阅读 · 0 评论 -
leetcode刷题之旅(47)全排列2
题目描述给定一个可包含重复数字的序列,返回所有不重复的全排列。 样例输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]]思路分析依旧是回溯法,不过要注意重复元素,加入flag标志数组判断 代码及结果public List<List<Integer>> permuteUnique(int...原创 2018-10-01 11:36:41 · 200 阅读 · 0 评论 -
leetcode刷题之旅(46)全排列
题目描述给定一个没有重复数字的序列,返回其所有可能的全排列。 样例输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] 思路分析典型的回溯法,注意剪枝 代码及结果public List<List<Integer>> ...原创 2018-10-01 11:19:13 · 184 阅读 · 0 评论 -
leetcode刷题之旅(40)组合总和2
题目描述给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只能使用一次。说明:所有数字(包括目标数)都是正整数。 解集不能包含重复的组合。 样例示例 1:输入: candidates = [10,1,2,7,6,1,5], tar...原创 2018-10-01 11:06:34 · 188 阅读 · 0 评论 -
leetcode刷题之旅(39)组合总和
题目描述给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。说明:所有数字(包括 target)都是正整数。 解集不能包含重复的组合。 样例输入: candidates = [2,3,6,7], target = 7,...原创 2018-10-01 10:48:46 · 130 阅读 · 0 评论 -
leetcode刷题之旅(94)Binary Tree Inorder Traversal
题目描述Given a binary tree, return the inorder traversal of its nodes' values. 样例 Input: [1,null,2,3] 1 \ 2 / 3Output: [1,3,2] 思路分析 递归法:按照中序遍历的定义,即“左 根 右”的进行递归即可...原创 2018-06-30 00:05:16 · 158 阅读 · 0 评论 -
leetcode刷题之旅(145)二叉树的后序遍历
题目描述给定一个二叉树,返回它的 后序 遍历。 样例输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路分析方法一:递归,按照后序遍历“左右根”的顺序,依次遍历,递归即可方法二:循环写法,写法类似先序和中序,但添加的list时用了头插,实际遍历顺序为右根左方法三:利用双栈,一个栈进行右...原创 2018-09-23 12:17:13 · 316 阅读 · 0 评论 -
leetcode刷题之旅(63)不同路径II
题目描述一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?网格中的障碍物和空位置分别用 1 和 0 来表示。说明:m 和 n 的值均不超过 100。 样例输入...原创 2018-09-18 12:22:24 · 152 阅读 · 0 评论 -
leetcode刷题之旅(62)不同路径
题目描述一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。问总共有多少条不同的路径?例如,上图是一个7 x 3 的网格。有多少可能的路径?说明:m 和 n 的值均不超过 100。 样例输入: m = 3, n = 2输出: ...原创 2018-09-18 11:47:23 · 181 阅读 · 0 评论 -
leetcode刷题之旅(22)Generate Parentheses
题目描述Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.样例For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())...原创 2018-05-29 11:34:31 · 105 阅读 · 0 评论 -
leetcode刷题之旅(18) 4Sum
题目描述Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of...原创 2018-05-24 15:51:24 · 92 阅读 · 0 评论 -
leetcode刷题之旅(17) Letter Combinations of a Phone Number
题目描述Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is g...原创 2018-05-25 15:44:12 · 187 阅读 · 0 评论 -
leetcode刷题之旅(10) Regular Expression Matching
题目描述Given an input string (s) and a pattern (p), implement regular expression matching with support for '.'and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element....原创 2018-05-25 15:07:03 · 119 阅读 · 0 评论 -
leetcode刷题之旅(8) String to Integer (atoi)
public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. Empty string if(str.length() == 0) return 0; //2. Remove Spaces while(str.charAt(index) == ' ' && ...原创 2018-05-15 00:52:48 · 87 阅读 · 0 评论 -
leetcode刷题之旅(7)Reverse Integer
题目描述Given a 32-bit signed integer, reverse digits of an integer.样例Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dealing with an ...原创 2018-05-07 21:08:13 · 78 阅读 · 0 评论 -
leetcode刷题之旅(9)Palindrome Number
题目描述Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.样例Example 1:Input: 121Output: trueExample 2:Input: -121Output: falseExplanat...原创 2018-05-07 20:25:00 · 67 阅读 · 0 评论 -
leetcode刷题之旅(5) Longest Palindromic Substring
public String longestPalindrome(String s) { int n = s.length(); String res = null; boolean[][] dp = new boolean[n][n]; for (int i = n - 1; i >= 0; i--) { for (int j = i; j < ...原创 2018-05-11 08:56:31 · 114 阅读 · 0 评论 -
leetcode刷题之旅(4)Median of Two Sorted Arrays
题目描述There 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)).样例Example 1:nums1 = [1, 3]...原创 2018-05-04 08:44:27 · 70 阅读 · 0 评论 -
leetcode刷题之旅(3)无重复字符的最长子串
题目描述给定一个字符串,找出不含有重复字符的最长子串的长度。 样例输入: "abcabcbb"输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 3。输入: "bbbbb"输出: 1解释: 无重复字符的最长子串是 "b",其长度为 1。输入: "pwwkew"输出: 3解释: 无重复字符的最长子串是 "wke",其长度为 3。 请原创 2018-05-03 11:52:36 · 120 阅读 · 0 评论 -
leetcode刷题之旅(2)Add Two Numbers
题目描述给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。 样例Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8Explanation: 342 + 46...原创 2018-05-03 11:22:26 · 92 阅读 · 0 评论 -
leetcode刷题之旅(13) Roman to Integer
题目描述Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2018-05-26 18:44:00 · 424 阅读 · 0 评论 -
leetcode刷题之旅(12) Integer to Roman
题目描述Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2018-05-26 18:58:08 · 101 阅读 · 0 评论 -
leetcode刷题之旅(35)Search Insert Position
题目描述Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array....原创 2018-06-03 00:09:34 · 125 阅读 · 0 评论 -
leetcode刷题之旅(58)Length of Last Word
题目描述Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defin...原创 2018-06-12 00:03:18 · 158 阅读 · 0 评论 -
leetcode刷题之旅(16) 3Sum Closest
题目描述Given 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 that each input wo...原创 2018-05-24 12:39:44 · 85 阅读 · 0 评论 -
leetcode刷题之旅(53)Maximum Subarray
题目描述Given 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],Output: 6Explanat...原创 2018-06-11 23:11:39 · 113 阅读 · 0 评论 -
leetcode刷题之旅(14)Longest Common Prefix
题目描述Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".Note:All given inputs are in lowercase letters a-z.样例E...原创 2018-05-24 10:15:51 · 78 阅读 · 0 评论 -
leetcode刷题之旅(15) 3Sum
题目描述Given 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 solution set must not cont...原创 2018-05-23 22:33:31 · 144 阅读 · 0 评论