![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
Ding_xiaofei
程序员
展开
-
二分搜索的题目
二分搜索针对有序的数组,主要是一些边界的问题leetcode_35题解public class Leetcode_35 { public int searchInsert(int[] nums, int target) { int l = 0; int r = nums.length-1; while(l<=r...原创 2018-09-17 16:55:47 · 451 阅读 · 0 评论 -
Leetcode_343
题目描述给一个n(n>=2),求相加等于n且乘积最大的一组整数的积。题解这其实就是一道高中数学题,但是leetcode上的数据比较水,导致完全不用数学推导的O(n2)的dp也可以过。解法一(纯dp):令dp[n]为n对应的最大积。那么递推方程就是:dp[n]=max(i*dp[n-i],i*(n-i))(其中i从1到n-1)。边界:dp[2]=1;时间复杂度:...原创 2018-07-24 09:19:10 · 197 阅读 · 0 评论 -
Leetcode 322 零钱兑换
题目描述给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。示例1:输入: coins = [1, 2, 5], amount = 11输出: 3 解释: 11 = 5 + 5 + 1示例2:输入: coins = [2], amount = 3输出: ...原创 2018-07-28 10:42:08 · 470 阅读 · 0 评论 -
Leetcode_524 通过删除字母匹配到字典里最长单词
题目描述题解被这个字典序坑死了class Solution { public String findLongestWord(String s, List<String> d) { Collections.sort(d); //先做个字典序的排序吧 int maxIndex = 0; int ma...原创 2018-08-16 09:54:20 · 589 阅读 · 0 评论 -
Leetcode_343 整数拆分
#include &amp;amp;lt;iostream&amp;amp;gt;#include &amp;amp;lt;vector&amp;amp;gt;using namespace std;class Solution {private: vector&amp;amp;lt;int&amp;amp;gt; memo; int max3(int a, int b, int c)原创 2018-05-19 10:39:00 · 184 阅读 · 0 评论 -
Leetcode_298
记忆化搜索#include &lt;iostream&gt;#include &lt;vector&gt;using namespace std;class Solution {private: vector&lt;int&gt; memo; int tryRob(vector&lt;int&gt;&amp; nums, int inde原创 2018-05-22 16:26:02 · 295 阅读 · 0 评论 -
Leetcode_141 环形链表
题目描述判断一个链表是否有环题解public class Solution { public boolean hasCycle(ListNode head) { if(head==null) return false; ListNode slow = head; ListNode fast = he...原创 2018-08-15 18:11:58 · 143 阅读 · 0 评论 -
Leetcode_88 合并两个有序数组
题解题解class Solution: def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m: int :type nums2: List[int] :type n: int :rtype:...原创 2018-08-15 17:14:05 · 177 阅读 · 0 评论 -
Leetcode_213
题目描述你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。示例1:输入: [2,3,2]输出:...原创 2018-07-25 15:19:49 · 314 阅读 · 0 评论 -
Leetcode_680 验证回文字符串 Ⅱ
题目描述题解又被细节坑死的题目class Solution { public boolean validPalindrome(String s) { int count = 1; int l = 0; int r = s.length()-1; while(l<r) { if(s....原创 2018-08-15 16:24:00 · 210 阅读 · 0 评论 -
Leetcode_202 快乐数
题目描述(查找类题目)题解pythonclass Solution: def isHappy(self, n): """ :type n: int :rtype: bool """ ss = set(); while True: if n == ...原创 2018-08-16 17:33:14 · 272 阅读 · 0 评论 -
Leetcode_93 复原IP地址 (递归与回溯)
题目描述题解和17的思路基本是一样的,主要多了几个判定条件 除了单个要小于256,还有就是不能出现01,010这种import java.util.*;public class Leetcode_93 { public List<String> restoreIpAddresses(String s) { List<String&g...原创 2018-08-22 14:43:48 · 992 阅读 · 0 评论 -
Leetcode_131 分割回文串
题目描述题解class Solution { public List<List<String>> partition(String s) { List<List<String>> ret = new ArrayList<>(); if(s.length()==0) ...原创 2018-08-22 16:34:23 · 400 阅读 · 0 评论 -
Leetcode_79 单词搜索
题目描述这题目代码稍微多了一些,不过思路也很清晰题解javaimport java.util.*;public class Leetcode_79 { private int m; private int n; private boolean[][] visited; int[][] d = {{-1,0},{0,1},{1,0},{0,...原创 2018-09-06 16:22:46 · 192 阅读 · 0 评论 -
Leetcode_401 二进制手表
题目描述题解用递归回溯有点麻烦了,这是简单题,换个思路就成了,输出所有的时间,再转换成二进制,计算1的数量class Solution { public List<String> readBinaryWatch(int num) { List<String> times = new ArrayList<>(); ...原创 2018-09-05 14:46:01 · 268 阅读 · 0 评论 -
Leetcode_90 subset ll
题解题解import java.util.*;public class Leetcode_90 { public List<List<Integer>> subsetsWithDup(int[] nums) { List<Integer> templist = new ArrayList<>(); ...原创 2018-09-05 11:21:01 · 165 阅读 · 0 评论 -
Leetcode_77 组合问题
题目描述题解javaimport java.util.*;public class Leetcode_77 { public List&lt;List&lt;Integer&gt;&gt; combine(int n, int k) { List&lt;List&lt;Integer&gt;&gt; ret = new ArrayLis原创 2018-08-30 10:38:40 · 407 阅读 · 0 评论 -
Leetcode39&&Leetcode40 组合总和
题目描述题解很经典的题目import java.util.*;public class Leetcode_39 { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<Integer> templist =...原创 2018-09-04 16:21:46 · 756 阅读 · 1 评论 -
Leetcode_216 组合总和 III
题目描述题解基本就和77是一样的组合问题import java.util.*;public class Leetcode_216 { public List<List<Integer>> combinationSum3(int k, int n) { List<Integer> templist = new Arr...原创 2018-09-01 20:43:12 · 449 阅读 · 0 评论 -
Leetcode_78 subset
题目描述我对递归和回溯的理解还是有问题,这是一道很简单的题目这边先贴一个博客参考一下 参考博客题解import java.util.*;public class Leetcode_78 { public List<List<Integer>> subsets(int[] nums) { List<Integer>...原创 2018-09-01 20:11:20 · 160 阅读 · 0 评论 -
Leetcode_47 全排列II
题目描述题解我用了46递归回溯的方法,然后用set去除了重复的元素 没想到也能直接提交通过class Solution { boolean[] isUsed = new boolean[10000]; public List<List<Integer>> permuteUnique(int[] nums) { L...原创 2018-08-23 15:09:10 · 273 阅读 · 0 评论 -
Leetcode_46 全排列
题目描述题解javajava里面不可以在方法里面定义全局变量import java.util.*;public class Leetcode_46 { boolean[] isUsed = new boolean[10000]; public List<List<Integer>> permute(int[] nums) {...原创 2018-08-23 11:48:14 · 200 阅读 · 0 评论 -
Leetcode 300 最长上升子序列
题目描述给定一个无序的整数数组,找到其中最长上升子序列的长度。示例输入: [10,9,2,5,3,7,101,18]输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。题解public int lengthOfLIS(int[] nums) { int n = nums.length; int[] ...原创 2018-07-30 08:54:28 · 332 阅读 · 0 评论 -
leetcode_283 移动零
class Solution: def moveZeroes(self, nums): &quot;&quot;&quot; :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. &quot;&quot;&quot;原创 2018-08-01 21:34:31 · 161 阅读 · 0 评论 -
leetcode_222 完全二叉树的节点个数
题目描述给出一个完全二叉树,求出该树的节点个数。说明:完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。示例:输入: 1 / \ 2 3 / \ /4 5 6输出: 6题解使用层次遍历,出...原创 2018-08-04 15:56:00 · 260 阅读 · 0 评论 -
leetcode_404 左叶子之和
题目描述计算给定二叉树的所有左叶子之和。 示例: 3 / \ 9 20 / \ 15 7在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24题解class Solution { public int sumOfLeftLeaves(TreeNode root) { if(root==null)...原创 2018-08-04 16:30:10 · 239 阅读 · 0 评论 -
Leetcode_257 二叉树的所有路径
题目描述给定一个二叉树,返回所有从根节点到叶子节点的路径。 说明: 叶子节点是指没有子节点的节点。示例:输入: 1 / \2 3 \ 5输出: ["1-&gt;2-&gt;5", "1-&gt;3"]解释: 所有根节点到叶子节点的路径为: 1-&gt;2-&gt;5, 1-&gt;3原创 2018-08-05 17:13:10 · 180 阅读 · 0 评论 -
Leetcode_129 求根到叶子节点数字之和
题目描述给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字。例如,从根到叶子节点路径 1-&gt;2-&gt;3 代表数字 123。计算从根到叶子节点生成的所有数字之和。说明: 叶子节点是指没有子节点的节点。示例 1:输入: [1,2,3] 1 / \ 2 3输出: 25解释:从根到叶子节点路径 1-&...原创 2018-08-10 16:50:56 · 423 阅读 · 0 评论 -
Leetcode_437 路径总和 III
题目描述root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \3 -2 1Return 3. The paths that sum to 8 are:1. 5 -&gt; 32. 5 -&gt; ...原创 2018-08-10 19:08:46 · 354 阅读 · 0 评论 -
Leetcode 143 重新排列链表
题目描述题解这里涉及到三个链表的基本操作,这是一道很经典的链表操作题目首先是寻找链表的中点,设置两个指针,一快 一慢 其次是反转链表,这是非常常见的一个链表操作,很容易出错 最后是两个链表的交叉连接java代码public void reorderList(ListNode head) { if(head == null) ...原创 2018-08-13 09:27:21 · 267 阅读 · 0 评论 -
Leetcode_648 词根替换单词
题目描述题解javapublic String replaceWords(List&lt;String&gt; dict, String sentence) { Set&lt;String&gt; set = new HashSet&lt;&gt;(dict); StringBuilder sb = new StringBuilder(); ...原创 2018-08-14 08:50:36 · 618 阅读 · 0 评论 -
Leetcode_34 寻找目标数字在数组里面的范围
题目描述java我写的垃圾代码,中间还出错了好几次。class Solution { public int[] searchRange(int[] nums, int target) { int[] ret = {-1,-1}; int l = 0; int r = nums.length-1; while...原创 2018-08-14 14:54:15 · 294 阅读 · 0 评论 -
Leetcode_6 Z字形变换
题目描述题解class Solution { public String convert(String s, int numRows) { if(numRows&lt;=1) return s; StringBuilder[] sb = new StringBuilder[numRows]; for(...原创 2018-08-15 09:01:31 · 177 阅读 · 0 评论 -
leetcode_101 对称二叉树
题解给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3题解:class Solut...原创 2018-08-04 14:16:09 · 394 阅读 · 0 评论 -
leetcode_112 路径总和
题目描述给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。说明: 叶子节点是指没有子节点的节点。示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 / / \ 11 13...原创 2018-08-04 11:47:24 · 283 阅读 · 0 评论 -
leetcode_226 翻转二叉树
题目描述翻转一棵二叉树。输入: 4 / \ 2 7 / \ / \1 3 6 9输出: 4 / \ 7 2 / \ / \9 6 3 1java里面没有直接可以用的swap函数,所以这边采用c++的代码/** * Definition for a binary tree n...原创 2018-08-04 10:43:53 · 175 阅读 · 0 评论 -
leetcode_75 分类颜色
题目描述给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。注意: 不能使用代码库中的排序函数来解决这道题。示例:输入: [2,0,2,1,1,0]输出: [0,0,1,1,2,2]题解计数排序from collec...原创 2018-08-02 09:01:04 · 237 阅读 · 0 评论 -
Leetcode_167 两数之和
题目描述给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。说明:返回的下标值(index1 和 index2)不是从零开始的。 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。 示例:输入: numbers = [2, 7, ...原创 2018-08-02 10:27:05 · 146 阅读 · 0 评论 -
leetcode_125 验证回文
题目描述给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。说明:本题中,我们将空字符串定义为有效的回文串。示例 1:输入: "A man, a plan, a canal: Panama"输出: true示例 2:输入: "race a car"输出: false题解class Solution: def isP...原创 2018-08-02 10:42:10 · 179 阅读 · 0 评论 -
leetcode_345 反转字符串中的元音字母
leetcode题解class Solution: def reverseVowels(self, s): s, vowels = list(s), {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} indices = [i for i, c in enumerate(s) if c in vo...原创 2018-08-02 10:56:47 · 190 阅读 · 0 评论