- 博客(87)
- 收藏
- 关注
原创 【LeetCode-3】Longest Substring Without Repeating Characters
时隔4年,继续记录一下自己的成长历程吧。题目:mediumGiven a string s, find the length of the longest substring without repeating characters.Example 1:Input: s = “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Example 2:Input: s = “bbbbb”Output:
2020-12-22 20:31:28
240
原创 【LeetCode-2】Add Two Numbers
时隔4年,继续记录一下自己的成长历程吧。题目:mediumYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list
2020-12-22 20:01:58
184
原创 【LeetCode-1】Two Sum
时隔4年,继续记录一下自己的成长历程吧。题目:easyGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twic
2020-12-21 20:27:03
156
原创 【LeetCode-451】Sort Characters By Frequency
题目: Given a string, sort it in decreasing order based on the frequency of characters.Example 1:Input: “tree”Output: “eert”Explanation: ‘e’ appears twice while ‘r’ and ‘t’ both appear once. So ‘e’
2016-12-21 15:16:02
427
原创 【LeetCode-462】Minimum Moves to Equal Array Elements II
题目: Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element
2016-12-20 21:03:32
369
原创 【LeetCode-477】Total Hamming Distance
题目: The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the given
2016-12-20 16:46:51
524
原创 [LeetCode-455]Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cook
2016-12-19 21:55:23
334
原创 [LeetCode-461]Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note: 0 ≤ x, y < 2^31题目这么简
2016-12-19 21:06:53
366
原创 [LeetCode-463]Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely s
2016-12-19 19:05:16
238
原创 质数筛选法
近期刷题遇到质数的问题比较多,于是就去搜索了一些关于质数的问题,下面是我的一点总结,就作为学习知识的记录吧。下面是质数筛选的两种方法,学习算法的关键并不仅仅是知道这种方法,更多的是学习算法的思想,学习算法的策略,一个好的策略是非常关键的,它直接影响你算法的效率。/** * 1.质数筛选法 : * (1)埃拉托色尼筛法(时间复杂度O(nlogn)) * (2)欧拉筛法 (时间复杂度O(n))
2016-12-14 10:02:06
1586
原创 【LeetCode-146】 LRU
LeetCode-146 LRU 在java中实现LRU的模拟其实很简单,因为提供了现成的数据结构,如果你还不了解,那就一起来学习吧。先把这道题解决了。。。public class LRUCode { MyMap<Integer, Integer> map; public LRUCode(int capacity) { map = new MyMap<Integer,
2016-12-09 10:48:03
294
原创 【LeetCode-421】Maximum XOR of Two Numbers in an Array
Maximum XOR of Two Numbers in an Array
2016-10-16 21:49:24
2210
转载 【LeetCode-337】House Robber III
看到了别人的好的方法,记录一下,注释什么的都在代码里//dfs解决public class HouseRobberIII { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int rob(TreeN
2016-06-24 09:21:29
401
转载 【LeetCode-201】Bitwise AND of Numbers Range
当看到这道题时,自己用了笨方法,每个相与,很显然最后没被accept,看到了别人的好的代码,记录一下吧。java代码如下:/** * 按位运算,所有数字自然要按二进制形式表达。一组数字按位与时,只有所有数字这一位上都为1时,结果才会为1。如果m=n,那自然结果就是m.下面讨论m!=n的情况。 * 让我们从最低位开始。 * 如果这一组数字的最低位不相同的话,那这一位就肯定会被消掉,变成
2016-06-23 09:22:30
295
原创 JosephRing约瑟夫环
今天再次遇到了这个问题,记得当初考研上机考试时也做了这道题,现在想想当时用的方法,真的是挺low的,今天看到了一个好的解决方法,记录一下这个奇妙的方法!public class JosephRing { //约瑟夫环问题 //一种方法是模拟一个环状数据结构,每次删除第m个元素,这样的方法每次删除需要m步,一共需要删除n - 1个数字,所以时间复杂度是O(mn),并且空间复杂度是O(n)
2016-06-08 17:11:56
373
原创 【LeetCode-47】Permutations II
这道题我是用递归来实现的,将数组的第1个元素和后面的所有元素看成是两部分,同理将第二部分的元素看成第一个元素和后面的所有元素两本.........很明显运用递归的思想!然后将第二部分的元素不断和第一个元素交换,知道index到达数组长度。public class PermutationsII { private List> res = new ArrayList>(); private
2016-06-01 16:42:59
282
原创 【LeetCode-106】Construct Binary Tree from Inorder and Postorder Traversal
和LeetCode-105是相同的原理,这次只需要将后续遍历序列从后向前遍历,这道题的关键是递归时参数的设定要想清楚public class ConstructBinaryTreefromInorderandPostorderTraversa { public class TreeNode { int val; TreeNode left; TreeNode right;
2016-05-20 09:16:48
221
原创 【LeetCode-105】Construct Binary Tree from Preorder and Inorder Traversal
好久没写题了,写起来就有点不适应,上大学时知道怎么根据前序遍历和中序遍历来恢复一棵树,但是从来没有实现过,今天碰到了,终于把曾经留下的遗憾弥补了public class ConstructBinaryTreefromPreorderandInorderTraversa { public class TreeNode { int val; TreeNode left; Tree
2016-05-17 20:59:54
278
原创 【LeetCode-334】Increasing Triplet Subsequence
把easy的刷完了之后,一步一步越来越难了public class IncreasingTripletSubsequence { /** * 遍历数组,维护一个最小值,和倒数第二小值,遍历原数组的时候, * 如果当前数字小于等于最小值,更新最小值,如果小于等于倒数第二小值,更新倒数第二小值, * 如果当前数字比最小值和倒数第二小值都大,说明此时有三个递增的子序列了,直接返回tru
2016-05-05 21:30:59
267
转载 【LeetCode-131】Palindrome Partitioning
这道题有些难度,一步一步调试了半天才看明白递归的过程public class PalindromePartitioning { //生成标志回文字符串的数组,partitioning_map[i][j] = 1的话,表明:string[i..j]是一个回文字符串 //如果s.charAt(i) == s.charAt(j),partitioning_map[i + 1][j - 1] =
2016-05-05 20:28:13
237
原创 判断一个字符串中所有子串是否为回文子串
今天刷题,碰到了一个类似的题,总结一下这个方法,我开始也没想到这种方法,竟然用的是dp的思想,我真是有点笨了,下面贴上这种方法的代码!//生成标志回文字符串的数组,partitioning_map[i][j] = 1的话,表明:string[i..j]是一个回文字符串//如果s.charAt(i) == s.charAt(j),partitioning_map[i + 1][j - 1] =
2016-05-05 19:51:27
1873
转载 【LeetCode-233】Number of Digit One
一看到这道题就想到了动态规划,但是tle了,先看看我的python代码吧!class Solution(object): # 最容易想到的方法了(动态规划),但是tle了 def countDigitOne(self, n): """ :type n: int :rtype: int """ if
2016-05-04 16:39:37
336
原创 【LeetCode-142】Linked List Cycle II
好久没有刷题了,缺乏一种感觉,这道题还是蛮考验数学知识的,一切尽在代码中# -*- encoding = 'utf - 8' -*-# 自己的方法:# 从开始出发到相遇后,将slow = head,fast每次走一步,slow与fast再次相遇点,则为环的起点# 下面几个延伸的问题也可以解决# 1. 环的长度是多少?(第一次相遇后,继续走,再次相遇后,慢指针走的距离即为环的长度)#
2016-05-04 15:01:01
251
原创 【LeetCode-109】Convert Sorted List to Binary Search Tree
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class ListNode { int val; ListNode next; Lis
2016-04-29 15:17:18
320
原创 【LeetCode-238】Product of Array Except Self
很久之前做的这道题了,昨天被朋友问起来,竟然不会了,于是乎又开始了思索。最终还是翻开了曾经做的答案,怎么越来越迷糊了!public class ProductofArrayExceptSelf { public int[] productExceptSelf(int[] nums) { int len = nums.length; if(len < 2)
2016-04-29 10:46:02
252
原创 【LeetCode-169】Majority Element
今天整理了一下解这道题的方法,感觉大神就是大神,永远都那么神,写的代码帅呆了package leetcode;import java.util.*;public class MajorityElement { /** * 最好的一种方法 * 每找出两个不同的element,则成对删除。最终剩下的一定就是所求的。 * 可扩展到⌊n/k⌋的情况,每k个不同的element进行
2016-04-28 18:53:33
364
原创 【LeetCode-242】Valid Anagram
迷迷糊糊的就做出来了!思想简直简单的不行!,竟然还击败了94%的人!上代码了!public boolean isAnagram(String s, String t) { char[] sChar = s.trim().toCharArray(); char[] tChar = t.trim().toCharArray(); int[] countS = new int
2016-04-28 17:32:27
284
原创 【LeetCode-215】Kth Largest Element in an Array
方法一:冒泡方法,循环K次,找到第K大的值,鉴于这种方法比较low,就不介绍了方法二:Java实现,用优先队列(堆)实现,这种确实比较简单,感觉应用了较复杂的数据结构不太爽public class KthLargestElementinanArray { public int findKthLargest1(int[] nums, int k) { // 记住这种数据结构
2016-04-26 22:13:42
304
原创 【LeetCode-343】Integer Break
这道题观察出规律是关键!上来一看并没有什么头绪,多列一些数你就会发现出现了规律# encoding = 'utf - 8'# 动态规划# 2 = 1 + 1 -> 1 * 1 = 1# 3 = 1 + 2 -> 1 * 2 = 2# 4 = 2 + 2 -> 2 * 2 = 4# 5 = 2 + 3 -> 2 * 3 = 6# 6 = 3 + 3 -> 3 * 3 = 9# 7
2016-04-25 21:48:54
418
翻译 【LeetCode-345】Reverse Vowels of a String
java的思想就是设置前后指针,碰到都是原因字母则交换。本来想在python里面也这样实现,发现python的字符串不能赋值,如果采用合并的方法则空间复杂度太高了,后来发现了这种方法!不过有个地方也没弄懂class Solution(object): def reverseVowels(self, s): """ :type s: str
2016-04-25 11:21:30
673
原创 【LeetCode-334】Reverse String
这个题用python,简单的不行不行的,多说无益,上代码class Solution(object): def reverseString(self, s): """ :type s: str :rtype: str """ return s[::-1]
2016-04-25 09:30:34
453
原创 求N以内所有质数的和
今天同学给我出了一道题,求N以内的所有的质数的和,要求在2000000以内进行测试,然后我就苦逼的做起来了,来分享一下我的成果吧,你们肯定一看就懂的# -*- encoding = 'utf-8' -*-import time# n以下的质数的和def primeNumberSum(n): if n < 2: return 0 if n == 3:
2016-04-24 15:33:52
2082
原创 【LeetCode-342】Power of Four
这是上一篇文章的升级版本,这是一解题的思想# -*- encoding = 'utf-8' -*-__author__ = 'MG'import math as mclass Solution(object): # 最low的一种解法了 def isPowerOfFour1(self, num): """ :type num: int
2016-04-20 15:34:57
424
原创 【LeetCode-231】Power of Two
闲暇之余,总结了一下解决这个题目的几种方法,是解决问题的一种思想# -*- encoding = 'utf-8' -*-__author__ = 'MG'class Solution(object): # 第一种方法(整除法 时间 O(1) 空间 O(1)) def isPowerOfTwo1(self, n): """ :type n:
2016-04-20 15:03:55
284
原创 【LeetCode-137】Single Number II
这道题首先理解题意正确,这个不同的数只会出现一次!碰见只有一个数不重复的都可以使用这种方法来解决,看代码吧,一切都在代码中。public int singleNumber(int[] A) { if (A == null || A.length == 0) { return -1; }
2016-04-20 14:08:35
254
转载 【LeetCode-260】Single Number III
这道题自己使用set实现的,也被ac了,发现太low了,后来发现别人写的就是棒# -*- encoding = 'utf-8'-*-__author__ = 'Administrator'class Solution(object): def singleNumber(self, nums): """ :type nums: List[int]
2016-04-20 09:57:32
305
原创 【LeetCode-103】Binary Tree Zigzag Level Order Traversal
这道题只需在102的基础上稍微变化下就行,很简单了,fightingclass Solution(object): def zigzagLevelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ # 最终结果
2016-04-17 21:05:22
231
原创 【LeetCode-107】Binary Tree Level Order Traversal II
这道题和102题相同的,只是最后将结果逆序class Solution(object): def levelOrderBottom(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ # 最终结果 res = [
2016-04-17 21:02:48
210
原创 【LeetCode-102】Binary Tree Level Order Traversal
今天又长姿势了,哈哈,fighting,一切都在代码中class Solution(object): def levelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ # 最终结果 res = [
2016-04-17 21:01:03
209
原创 【LeetCode-94】Binary Tree Inorder Traversal
这道题可以运用一个栈来实现,注意安排栈中放的元素就ok了,python继续。。。class Solution(object): def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ res = []
2016-04-17 20:57:58
214
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人