
Leetcode
每天刷题积累,刷着刷着,你就上瘾了
erzhuerzhu
这个作者很懒,什么都没留下…
展开
-
打印错排
关于错排的定义,可以自行百度其中也有错排种数的计算这个算法是基于全排列,在全排列添加条件。采用回溯,过程有点类似于剪支(那个条件的判断是否就不用继续执行了)import java.util.*;public class Derangements { public List<List<Integer>> derangements(int[] nums) { int len = nums.length; // 使用一个动态数组保存所有可能原创 2021-05-22 16:13:41 · 225 阅读 · 0 评论 -
LeetCode 27. 移除元素
problemLeetCode 27. 移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。solution思路过程一想到的就是把数组中的数往前覆盖,不行,这个想法没有想通先遍历一次,把等于val的值置为0,再遍历一次,把0的位置填满假如有k个0,那么数组后面肯定向前遍历k个位置就可以了代码class Solution {原创 2021-04-19 14:56:19 · 98 阅读 · 0 评论 -
LeetCode 95. 不同的二叉搜索树 II
problemLeetCode 95. 不同的二叉搜索树 II 给定一个整数 n,生成所有由 1 … n 为节点所组成的 二叉搜索树 。solution思路过程在96题的基础上,需要把生成的树的结果表示出来表示的顺序是怎么回事呢?前序遍历表示一直没有绕清楚怎么才能把每个结果都展示出来知道有要不断递归,可是顺序整不出来看了大佬的解答,顿时觉得清晰多了,果然我太菜了/(ㄒoㄒ)/~~代码class Solution { public List<TreeNode>原创 2021-04-18 15:10:42 · 85 阅读 · 0 评论 -
LeetCode 96. 不同的二叉搜索树
class Solution { public int numTrees(int n) { // n>=1; int [] dp=new int[n+1]; dp[1]=1; dp[0]=1;// 边界,没有左子树和右子树的时候 for(int i=2;i<n+1;++i){ for(int j=1;j<=i;++j){ dp[i]+=dp[j-1原创 2021-04-16 18:22:03 · 87 阅读 · 0 评论 -
线性时间选择
problem元素选择问题的一般提法是:给定线性序集中n个元素和一个整数k,1<=k<=n,要求找到这n个元素中第k小的元素。solution随机时间线性选择教材《算法设计与分析(第三版)》(王晓东)中这部分的算法解释怎么都看不懂,只能自己再琢磨一次。自己带入具体的例子,一步一步按照代码流程走下来,算法明白是明白了,但是我还是不太明白背后的原理。首先是用的随机划分算法,这个题目类似一个LeetCode的题目,印象中做过,找到了就来补上是什么题目。流程:终止条件是递归到当前数组只原创 2021-03-26 16:28:57 · 541 阅读 · 0 评论 -
LeetCode 50. Pow(x, n)
problemLeetCode 50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn)。solution思路过程直接看解答 ↓,弄懂了就写了50. Pow(x, n) (快速幂,清晰图解)代码class Solution { public double myPow(double x, int n) { if(x==0.0f){ return 0.0d; } lon原创 2021-03-10 09:44:12 · 74 阅读 · 0 评论 -
LeetCode39. 组合总和
problemLeetCode39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。solution思路过程回溯用Set来存储进行快速查找结束终点是sum 累加到了target代码class Solution { Set<Integer> set=new HashSet<>();原创 2021-03-10 09:26:35 · 82 阅读 · 0 评论 -
LeetCode 1047. 删除字符串中的所有相邻重复项
problemLeetCode 1047. 删除字符串中的所有相邻重复项 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在 S 上反复执行重复项删除操作,直到无法继续删除。在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。solution方法一思路过程就用循环,用一个标志位表明遍历所有S,是否存在邻近的相同的字母存在,就替换掉(用字符串替换函数,可能把没有找到的相同的也替换掉,不过没关系)不存在,结束,返回另外一个结束就是空字符串原创 2021-03-09 17:12:35 · 84 阅读 · 0 评论 -
LeetCode 面试题 05.06. 整数转换
problemLeetCode 面试题 05.06. 整数转换 整数转换。编写一个函数,确定需要改变几个位才能将整数A转成整数B。solution思路过程先将两个数进行异或^得到的不同数字与1相遇,得到1次数就加1,0就跳过,右移,然后循环32次代码class Solution { public int convertInteger(int A, int B) { int C=A^B; int res=0; for(int i原创 2021-03-08 09:30:59 · 119 阅读 · 0 评论 -
LeetCode 1669. 合并两个链表
problemLeetCode 1669. 合并两个链表 给你两个链表 list1 和 list2 ,它们包含的元素分别为 n 个和 m 个。请你将 list1 中第 a 个节点到第 b 个节点删除,并将list2 接在被删除节点的位置。solution思路过程p标记要被删除的节点的的后续节点找到要被删除的开始节点的前续节点,然后指向List2走到list2的尾节点,并指向p返回 list1a的计数是从零开始的吗?还是说节点中数值于a和b相等做个实验,是从零开始计数删除的节点原创 2021-03-07 09:59:07 · 89 阅读 · 0 评论 -
LeetCode 剑指 Offer 25. 合并两个排序的链表
problemLeetCode 剑指 Offer 25. 合并两个排序的链表同 LeetCode 21. 合并两个有序链表 输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的solution思路过程设立一个头节点head,直接可以防止了l1,l2是null,分别为l1,l2设立两个指针,在设立一个追踪的节点初始时p=head比较指针两个值,小的就加到p的后面,直到其中一个结束把没有遍历完的直接继续添加代码/** * Definition for sing原创 2021-03-07 09:13:00 · 66 阅读 · 0 评论 -
LeetCode 415. 字符串相加
problemLeetCode 415. 字符串相加给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。solution思路过程java字符串相减可以变为数字的特性每个字符从地位到高位相加相加计算出本位和进位最后结果要反转代码class Solution { public String addStrings(String num1, String num2) { StringBuffer res=new StringBuffer("");原创 2021-03-06 15:02:46 · 79 阅读 · 0 评论 -
M个苹果放在N个盘子里,有多少种不同的放法
problem这个题目前我再LeetCode 没找到相同或是相似的题目,有同学知道的话,可以留言一下M个苹果放在N个盘子里,有多少种不同的放法?solution这个题是我在面试的时候,面试官出的一个题目,当时用的回溯,但是思路不对,面试官还提醒分情况和动态规划,可是着实没有做出来,面试之后就去百度了解答练习了一次。方法一思路过程提前是苹果数M>=0,盘子数N>=0对每次分发,都可以如下考虑:如果苹果数小于盘子数(M<=N),一定会存在空盘,那么其实只能在M个原创 2021-03-06 14:30:47 · 1158 阅读 · 2 评论 -
LeetCode剑指 Offer 61. 扑克牌中的顺子
https://blog.youkuaiyun.com/erzhushashade/article/details/113495769problemLeetCode 剑指 Offer 64. 求1+2+…+n求 1+2+…+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。solution方法一(寻常思路,错误)思路过程看到这个题,正在啃着肉包子的我,觉得包子真香,这题真不香这题的方向大概是往位运算方向可以建立在 LeetC原创 2021-03-04 09:33:56 · 145 阅读 · 1 评论 -
LeetCode 剑指 Offer 64. 求1+2+…+n
https://blog.youkuaiyun.com/erzhushashade/article/details/113495769problemLeetCode 剑指 Offer 64. 求1+2+…+n求 1+2+…+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。solution方法一(寻常思路,错误)思路过程看到这个题,正在啃着肉包子的我,觉得包子真香,这题真不香这题的方向大概是往位运算方向可以建立在 LeetC原创 2021-02-19 09:47:45 · 129 阅读 · 0 评论 -
LeetCode 1423. 可获得的最大点数
problemLeetCode 1423. 可获得的最大点数几张卡牌 排成一行,每张卡牌都有一个对应的点数。点数由整数数组 cardPoints 给出。每次行动,你可以从行的开头或者末尾拿一张卡牌,最终你必须正好拿 k 张卡牌。你的点数就是你拿到手中的所有卡牌的点数之和。给你一个整数数组 cardPoints 和整数 k,请你返回可以获得的最大点数。solution思路过程这个就很有意思对于每次选择来说,你都有头和尾两张牌的选择递归返回每次最大的结果结束的位置是递归k次用左和右原创 2021-02-06 09:55:09 · 184 阅读 · 0 评论 -
LeetCode 剑指 Offer 65. 不用加减乘除做加法
problemLeetCode 剑指 Offer 65. 不用加减乘除做加法写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号solution思路过程位运算本位结果加就是可以表示为本位异或,进位结果表示为每位的与在左移一位最终直到进位为零,加法结束代码class Solution { public int add(int a, int b) { while(b!=0){ int c=(a原创 2021-02-01 09:33:09 · 74 阅读 · 0 评论 -
LeetCode 剑指 Offer 66. 构建乘积数组
problemLeetCode 剑指 Offer 66. 构建乘积数组给定一个数组 A[0,1,…,n-1],请构建一个数组 B[0,1,…,n-1],其中 B[i] 的值是数组 A 中除了下标 i 以外的元素的积, 即 B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。solution思路过程第一:可以暴力,对每一个元素完成除了这个元素以外的乘法,时间复杂度就是O(n^2),也许过不了, 没试第二,可能来三次,只要从头到尾,从尾到头两次就好了原创 2021-01-31 10:14:47 · 119 阅读 · 0 评论 -
LeetCode 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
problemLeetCode 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。solution思路过程先做了68-II,觉得这个题相对而言确实简单了上句话我收回,突然想到没这么简单呀,我再想想利用搜索树的性质,先用 p 和 q 与 root 的值进行大小比较如果 q 和 p 中的值其中一个小于等于 root 的值而另外一个大于等于 root 的值,就直接返回 root;如果 q 和 p 同时大于或者小于,向下找,原创 2021-01-30 12:47:14 · 79 阅读 · 0 评论 -
LeetCode 剑指 Offer 67. 把字符串转换成整数
problemLeetCode 剑指 Offer 67. 把字符串转换成整数写一个函数 StrToInt,实现把字符串转换成整数这个功能。不能使用 atoi 或者其他类似的库函数。尝试一思路过程如果时-23+45,是输出-23呢?还是22呢?需要我计算吗?不需要如果遇到负号,后面都没有数字,那不就记录错误了,例如-a32,测试返回零,说明符号后一定会跟着数字如果要完成转换,开头一定是要正负号或是数字,是字母就不可以了不用计算,直到一开始到最后截取到有效的数字就行把字符串挨个遍历没有原创 2021-01-30 12:38:29 · 144 阅读 · 0 评论 -
LeetCode:剑指 Offer 68 - II. 二叉树的最近公共祖先
problemLeetCode 剑指 Offer 68 - II. 二叉树的最近公共祖先给定一个二叉树, 找到该树中两个指定节点的最近公共祖先solution方法一(失败)思路过程分为两种情况给定的两个结点中,其中一个就是他们的祖先其他结点是他们的祖先肯定是要遍历,但是用什么方式遍历?如果先找到其中一个结点,剩下的就是找另外一个结点需要把寻找的路径先保存下来,尤其是根节点所以采用后序遍历就很好,使用迭代的方式,用栈来保存不考虑root 为空或是找不到 p,q 结点的情况似乎原创 2021-01-26 21:00:40 · 162 阅读 · 0 评论 -
面试高频算法题
文章目录前言一、二叉树二、使用步骤1.引入库2.读入数据总结前言恍恍惚惚的过了秋招,但是没有拿到理想的offer,这篇便是记边复习遍记录面试中比较高频的算法题,为接下来的春招做准备,冲鸭<( ̄︶ ̄)↗[GO!]提示:以下是本篇文章正文内容,下面案例可供参考一、二叉树示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。二、使用步骤1.引入库代码如下(示例):import numpy as npimport pandas as pdimport原创 2021-01-21 20:59:24 · 218 阅读 · 1 评论 -
LeetCode:378 Kth Smallest Element in a Sorted Matrix
problemLeetCode 378. Kth Smallest Element in a Sorted MatrixGiven a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order,原创 2020-07-05 11:23:35 · 175 阅读 · 0 评论 -
LeetCode:23 Merge k Sorted Lists
problemLeetCode 23. Merge k Sorted ListsMerge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.solution方法一思路过程代码python在这里插入代码片Java在这里插入代码片原创 2020-07-05 11:22:13 · 212 阅读 · 0 评论 -
LeetCode:剑指offer 09:用两个栈实现队列
problemLeetCode 剑指offer 09:用两个栈实现队列用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )solution方法一思路过程明白栈和队列两种数据结构,栈先进后出,队列先进先出每一次添加元素,直接放在一个栈的后面每一次删除元素,把栈的元素pop放到另外一个栈中append,删除中转的栈的栈顶元原创 2020-07-01 16:30:21 · 269 阅读 · 0 评论 -
LeetCode:297 Serialize and Deseialize Binary Tree
problemLeetCode 297Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same o原创 2020-06-18 07:53:34 · 124 阅读 · 0 评论 -
LeetCode:126Word Ladder II
problemLeetCode 126Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a timeEach transformed word must exist in th原创 2020-06-14 16:44:32 · 185 阅读 · 0 评论 -
LeetCode:128 Longest Consecutive Sequence
problemLeetCode 128. Longest Consecutive SequenceGiven an unsorted array of integers, find the length of the longest consecutive elements sequence.Your algorithm should run in O(n) complexity.solution思路过程首先没有排序,还不知道最小的值是多少最暴力的方法就是,找到最小值和最大值,然后就使维原创 2020-06-06 16:53:41 · 135 阅读 · 0 评论 -
LeetCode:771Jewels and Stones
problemYou’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many o...原创 2020-05-02 20:14:30 · 138 阅读 · 0 评论 -
LeetCode:278 bad version
problem这里是引用solution方法一思路过程代码python在这里插入代码片Java在这里插入代码片方法二思路代码python在这里插入代码片Java在这里插入代码片方法三思路代码python在这里插入代码片Java在这里插入代码片总结...原创 2020-05-01 20:26:34 · 138 阅读 · 0 评论 -
LeetCode:175 Combine two tables
problemsolution思路过程当用一个人插入两条地址,address是一样的吗?当你的地址中的personid不在person表中?,以person表为基准自然连接行不行呢?发现自己完全想错了,不熟悉数据库内部操作的原理,尤其是各种joinquery statementselect FirstName,LastName,city,State from Person ...原创 2020-05-01 03:26:04 · 129 阅读 · 0 评论 -
LeetCode:check if a String is a vaild swquence from root to leaves path in binary tree
problemGiven a binary tree where each path going from the root to any leaf form a valid sequence, check if a given string is a valid sequence in such binary tree.We get the given string from the co...原创 2020-04-30 16:16:59 · 146 阅读 · 0 评论 -
Leetcode:124.binary Tree Maxiumum Path Sum
problemGiven 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 parent-child c...原创 2020-04-29 20:09:19 · 157 阅读 · 0 评论 -
Leetcode:144,94,145,102,104,105,106,98树的题目总结
如果对于二叉树的各种结构和遍历方式不了解,可以通过以下连接学习LeetCode二叉树探索当你把里面的题目都基本刷完,估计对树这种结构,至少是二叉树的了解和掌握会有很大的提升当然,有时间刷刷相应的晋升题目会更好下面也是自己刷题的记录problem 144 Binary Tree Preorder TraversalLeetCode144 Binary Tree Preorder Tr...原创 2020-04-29 19:52:36 · 156 阅读 · 0 评论 -
LeetCode:First unique number
problemYou have a queue of integers, you need to retrieve the first unique integer in the queue.Implement the FirstUnique class:FirstUnique(int[] nums) Initializes the object with the numbers in t...原创 2020-04-28 23:47:24 · 239 阅读 · 0 评论 -
leetCode:146 和 面试题 16.25. LRU Cache LCCI
problemDesign 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 positive) of the k...原创 2020-04-28 23:24:13 · 149 阅读 · 0 评论 -
LeetCode:221 Maximal square
problemGiven a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its areasolution思路:一个方正全是1,就是12and 22 and 32,边长相等全是1,怎么搜索才能是最优化的先从最简单的暴力搜索开始分析,...原创 2020-04-27 18:44:08 · 123 阅读 · 0 评论 -
LeetCode:55jump game
problemGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determin...原创 2020-04-25 16:38:11 · 138 阅读 · 0 评论 -
LeetCode:201.bitwise AND of Number
problemBitwise AND of Numbers RangeGiven a range [m, n] where 0 <= m <= n <= 2147483647, return thebitwise AND of all numbers in this range, inclusive.Solution方法1思路第一个想法是,brute force...原创 2020-04-23 21:35:51 · 111 阅读 · 0 评论 -
LeetCode:Leftmost Column with at Least a One
Problem A binary matrix means that all elements are 0 or 1. For each individual row of the matrix, this row is sorted in non-decreasing order. Given a row-sorted binary matrix binaryMatrix, r...原创 2020-04-21 16:22:03 · 436 阅读 · 0 评论