
leetcode
文章平均质量分 60
周英俊520
这个作者很懒,什么都没留下…
展开
-
Leetcode. 92
题目要求对链表的部分节点实现反转Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3->2->5->NULL首先找到反转的起始节点,然后做一个操作, 就是每次循环都让pre.next = then这样做(换一个思路, 每次把后面的节点插到pre和pre.next当中 像...原创 2019-04-22 23:18:36 · 134 阅读 · 0 评论 -
约瑟夫环问题数学推导(看不懂请举报作者)
题目:每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直...原创 2019-08-04 16:47:18 · 880 阅读 · 0 评论 -
Leetcode 73.Set Matrix Zeroes
看起来很容易,但是按照正常的思路来解却有很多坑, 如果一个值==0, 那么它所在的那行和列全部设为0, 好像表面看起来直接根据这个逻辑写个for循环就AC了, 但是实际上要考虑一个问题就是会出现递归的情况, 就是设为0以后 这些变成0的值又执行了上述的这个逻辑,这就比较麻烦了。 但是 实际上出现这种情况的原因自己跑一遍逻辑就会发现是[i][0] [0][j]这些元素搞的鬼。 所以做赋值操...原创 2019-07-01 11:07:17 · 165 阅读 · 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]]这道题现在的水平只考虑学会用回溯来解, 当时对于回溯...原创 2019-06-24 10:21:33 · 148 阅读 · 0 评论 -
Leetcode 45. Jump Game II
Given 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.Your goal is to...原创 2019-06-23 09:26:08 · 79 阅读 · 0 评论 -
01背包问题动态规划-atao
01背包类似问题一般是给3个参数1:sum 背包总重量2:w[]数组, 数组每个数为单个物品重量3:v[]数组,每个数为单个物品价值,一般就是要求在背包能装得下的情况下,保证价值最大。核心思路: 就是1个物品和背包总重量为1~总重量为sum的情况下获得的最大价值, 以此轮询, 在背包总重量为sum的最大价值求出来后再轮询2个物品 总重量为1~总重量为sum的情况下获得的最大价...原创 2018-04-05 14:17:57 · 335 阅读 · 0 评论 -
Leetcode 37. Sudoku Solver
数独题 是道比较费脑筋的题Write a program to solve a Sudoku puzzle by filling the empty cells.Asudoku solution must satisfyall ofthe following rules:Each of the digits1-9must occur exactlyonce in each ...原创 2019-06-21 14:02:23 · 111 阅读 · 0 评论 -
Leetcode 34. Find First and Last Position of Element in Sorted Array
Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue.Your algorithm's runtime complexity must be in the order ofO(logn).If the...原创 2019-06-21 11:30:16 · 132 阅读 · 0 评论 -
Leetcode 33. Search in Rotated Sorted Array
这道题要在一个已经被旋转后的数组当中找到目标数值target;Suppose 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 ...原创 2019-06-21 11:05:28 · 118 阅读 · 0 评论 -
Leetcode 32. Longest Valid Parentheses
Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid...原创 2019-06-21 10:54:40 · 111 阅读 · 0 评论 -
Leetcode 10. Regular Expression Matching
matching with support for'.'and'*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover theentireinput string (not partial).Note:...原创 2019-06-19 17:15:53 · 111 阅读 · 0 评论 -
牛客网(剑指offer)二叉树的下一个结点
这道题的思路是这样的, 首先根据节点是否有右子树, 有的话找到右子树的最左结点返回,没有的话 可以再进一步思考这个节点的3种情况1.是父节点的左孩子这种情况返回父节点2.是父节点的右孩子(1)如果父节点是爷爷节点的左孩子,那么返回爷爷节点,(2)如果父节点是爷爷节点的右孩子, 再看爷爷节点是不是祖父的左孩子,是就返回祖父, 不是的话再往上继续找3.没有父节点。...原创 2019-06-14 17:27:25 · 127 阅读 · 0 评论 -
Leetcode 146 LRU
实现LRU缓存, 学会两种实现方式, 原始一点的用HashMap,可以了解更多实现细节, 还有就是LinkedHashMap(比较简单 就省略了)( 可以先将代码放到Leetcode上跑通过后再来看代码), 看了半天别人的代码然后跑过去通不过是件很沮丧的事情。1. HashMap版本package com.azhou.xiaoming;import java.util.HashMap...原创 2019-05-23 22:10:28 · 181 阅读 · 0 评论 -
Leetcode 144~145
在这里会将树的前中后序遍历的非递归进行总结。先记住一个要点, 所有的非递归和递归操作其实思路是一样的, 只是形式不一样。 根据我的实习经验来看,实际上工作日常编码如果能用递归尽量用递归,方便代码维护。废话说完开始正篇:前中后序的非递归版本其实操作的流程是一样的 先附上高赞总结的代码//1. 前序public List<Integer> preorderTraversal...原创 2019-05-23 20:10:34 · 210 阅读 · 0 评论 -
Leetcode 143. Reorder List
Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…Example 1:Given 1->2->3->4, reorder it to 1->4->2->3.Example 2:Given 1->2->3->4-...原创 2019-05-23 11:29:12 · 139 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
这道题是找最长非重复字符串 , 思路的话对该字符串遍历判断,然后用一个Map来判断是否重复,在判断重复的时候应该还考虑个问题就是两个重复的字符之间的那个字符串是否还出现了重复的字符。 逻辑讲清楚了代码如下.public int lengthOfLongestSubstring(String s) { int max = 0; Map<Ch...原创 2019-05-09 16:21:58 · 130 阅读 · 0 评论 -
5. Longest Palindromic Substring
这道题是要找最大回文子串,回文子串判断的时候考虑两种类型一种是abba一种是aba,代码如下private String ss = ""; public String longestPalindrome(String s) { if(s.length()<2){ return s; } for(i...原创 2019-05-09 16:22:06 · 124 阅读 · 0 评论 -
Leetcode 152. Maximum Product Subarray
Given an integer arraynums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation:[2,3] h...原创 2019-08-28 10:33:08 · 215 阅读 · 0 评论