leetcode
cold2wind
目前还是一名就读研究生,希望可以和大家一起学习
展开
-
【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 · 122 阅读 · 0 评论 -
【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 · 115 阅读 · 0 评论 -
【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 · 80 阅读 · 0 评论 -
【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 · 184 阅读 · 0 评论 -
【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 · 182 阅读 · 0 评论 -
【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 · 198 阅读 · 0 评论 -
【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 · 179 阅读 · 0 评论 -
【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 · 254 阅读 · 0 评论 -
【LeetCode-142】Linked List Cycle II
好久没有刷题了,缺乏一种感觉,这道题还是蛮考验数学知识的,一切尽在代码中# -*- encoding = 'utf - 8' -*-# 自己的方法:# 从开始出发到相遇后,将slow = head,fast每次走一步,slow与fast再次相遇点,则为环的起点# 下面几个延伸的问题也可以解决# 1. 环的长度是多少?(第一次相遇后,继续走,再次相遇后,慢指针走的距离即为环的长度)#原创 2016-05-04 15:01:01 · 219 阅读 · 0 评论 -
【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 · 296 阅读 · 0 评论 -
【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 · 286 阅读 · 0 评论 -
【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 · 219 阅读 · 0 评论 -
【LeetCode-169】Majority Element
今天整理了一下解这道题的方法,感觉大神就是大神,永远都那么神,写的代码帅呆了package leetcode;import java.util.*;public class MajorityElement { /** * 最好的一种方法 * 每找出两个不同的element,则成对删除。最终剩下的一定就是所求的。 * 可扩展到⌊n/k⌋的情况,每k个不同的element进行原创 2016-04-28 18:53:33 · 321 阅读 · 0 评论 -
【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 · 217 阅读 · 0 评论 -
【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 · 271 阅读 · 0 评论 -
【LeetCode-17】Letter Combinations of a Phone Number
这道题运用的是dfs的思想,一个小小的错误害得我找了1个小时,以后++i 和 i++一定搞清楚,平时很清楚,用起来咋就不顶事了呢,上代码吧。递归的时候我把current++ 传进去了,结果总是报栈溢出错误,找了半天才发现!public class LetterCombinationsofaPhoneNumber { List result = new ArrayList(); pu原创 2016-03-28 17:14:14 · 221 阅读 · 0 评论 -
【LeetCode-27】Remove Element
用Python来解答,真的是很简单的题了class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ i = 0原创 2016-04-13 12:52:49 · 277 阅读 · 0 评论 -
【LeetCode-26】Remove Duplicates from Sorted Array
这是用Python写的第二道题,figthing,很简单的思想,不再解决了class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) <= 0原创 2016-04-13 12:44:13 · 222 阅读 · 0 评论 -
【LeetCode-1】Two Sum
第一次拿Python写,还不是很熟练,有点迷class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ di转载 2016-04-13 11:14:54 · 215 阅读 · 0 评论 -
【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 · 391 阅读 · 0 评论 -
【LeetCode-137】Single Number II
这道题首先理解题意正确,这个不同的数只会出现一次!碰见只有一个数不重复的都可以使用这种方法来解决,看代码吧,一切都在代码中。public int singleNumber(int[] A) { if (A == null || A.length == 0) { return -1; }原创 2016-04-20 14:08:35 · 212 阅读 · 0 评论 -
【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 · 246 阅读 · 0 评论 -
【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 · 330 阅读 · 0 评论 -
【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 · 496 阅读 · 0 评论 -
[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 · 305 阅读 · 0 评论 -
[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 · 334 阅读 · 0 评论 -
[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 · 208 阅读 · 0 评论 -
【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 · 388 阅读 · 0 评论 -
【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 · 261 阅读 · 0 评论 -
【LeetCode-421】Maximum XOR of Two Numbers in an Array
Maximum XOR of Two Numbers in an Array原创 2016-10-16 21:49:24 · 2138 阅读 · 0 评论 -
【LeetCode-201】Bitwise AND of Numbers Range
当看到这道题时,自己用了笨方法,每个相与,很显然最后没被accept,看到了别人的好的代码,记录一下吧。java代码如下:/** * 按位运算,所有数字自然要按二进制形式表达。一组数字按位与时,只有所有数字这一位上都为1时,结果才会为1。如果m=n,那自然结果就是m.下面讨论m!=n的情况。 * 让我们从最低位开始。 * 如果这一组数字的最低位不相同的话,那这一位就肯定会被消掉,变成转载 2016-06-23 09:22:30 · 255 阅读 · 0 评论 -
【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 · 368 阅读 · 0 评论 -
【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 · 195 阅读 · 0 评论 -
【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 · 413 阅读 · 0 评论 -
【LeetCode-47】Permutations II
这道题我是用递归来实现的,将数组的第1个元素和后面的所有元素看成是两部分,同理将第二部分的元素看成第一个元素和后面的所有元素两本.........很明显运用递归的思想!然后将第二部分的元素不断和第一个元素交换,知道index到达数组长度。public class PermutationsII { private List> res = new ArrayList>(); private原创 2016-06-01 16:42:59 · 235 阅读 · 0 评论 -
【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 · 249 阅读 · 0 评论 -
【LeetCode-334】Increasing Triplet Subsequence
把easy的刷完了之后,一步一步越来越难了public class IncreasingTripletSubsequence { /** * 遍历数组,维护一个最小值,和倒数第二小值,遍历原数组的时候, * 如果当前数字小于等于最小值,更新最小值,如果小于等于倒数第二小值,更新倒数第二小值, * 如果当前数字比最小值和倒数第二小值都大,说明此时有三个递增的子序列了,直接返回tru原创 2016-05-05 21:30:59 · 240 阅读 · 0 评论 -
【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 · 201 阅读 · 0 评论 -
【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 · 379 阅读 · 0 评论 -
【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 · 610 阅读 · 0 评论