- 博客(33)
- 收藏
- 关注
原创 【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 300
原创 【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 235
原创 【LeetCode-169】Majority Element
今天整理了一下解这道题的方法,感觉大神就是大神,永远都那么神,写的代码帅呆了package leetcode;import java.util.*;public class MajorityElement { /** * 最好的一种方法 * 每找出两个不同的element,则成对删除。最终剩下的一定就是所求的。 * 可扩展到⌊n/k⌋的情况,每k个不同的element进行
2016-04-28 18:53:33 340
原创 【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 253
原创 【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 285
原创 【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 400
翻译 【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 638
原创 【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 427
原创 求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 2031
原创 【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 403
原创 【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 261
原创 【LeetCode-137】Single Number II
这道题首先理解题意正确,这个不同的数只会出现一次!碰见只有一个数不重复的都可以使用这种方法来解决,看代码吧,一切都在代码中。public int singleNumber(int[] A) { if (A == null || A.length == 0) { return -1; }
2016-04-20 14:08:35 227
转载 【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 280
原创 【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 209
原创 【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 194
原创 【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 196
原创 【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 198
原创 【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 289
原创 【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 234
转载 【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 231
原创 【LeetCode-45】Jump Game II
运用了动态规划的思想,希望不要被我写的代码搞糊涂public class JumpGameII { public int jump(int[] nums) { if(nums == null || nums.length == 0){ return 0; } int[] steps = new int[nums.length]; Arrays.fill(steps, I
2016-04-08 17:34:49 210
原创 【LeetCode-135】Candy
这道题真是让我有点陶醉,开始自己的思路空间复杂度最低,时间复杂度O(n),调了2小时也没ac!就看了别人的思路,别人思路最终自己实现了,但是我还是不服气,不信做不出来,最后废了九牛二虎之力终于ac了!!!public class ImportantCandy { //别人的思路 public int candy(int[] ratings) { if(ratings == null |
2016-04-08 15:45:37 202
转载 【LeetCode-4】Median of Two Sorted Arrays
这道题好难,一点没想到用求第K个最小值的办法来解决,共勉吧!public double findMedianSortedArrays(int[] nums1, int[] nums2) { int total = nums1.length + nums2.length; if (total % 2 == 1) return findKth
2016-04-08 09:39:26 197
转载 【LeetCode-73】Set Matrix Zeroes
这道题我完全理解错了,我开始以为有一个0,那不就整行整列都为0,接下来不就连锁反应,整个矩阵都为0了,费解了!后来才知道不是那么回事,看了别人了才明白,我这个语文能力真是有点差。/** * 这道题space复杂度降到了最低,通过第一行第一列来存储0的位置 * 1.先确定第一行和第一列是否需要清零,即,看看第一行中是否有0,记下来。也同时记下来第一列中有没有0。 * 2.扫描剩下的矩阵元素
2016-04-06 10:32:20 417
原创 【LeetCode-154】Find Minimum in Rotated Sorted Array II
这道题和Find Minimum in Rotated Sorted Array(LeetCode-153)简直一模一样额,没区别,重复元素根本不影响的!public class FindMinimuminRotatedSortedArray { int result = Integer.MAX_VALUE; public int findMin(int[] nums) { bin
2016-04-06 09:28:58 244
原创 【LeetCode-153】Find Minimum in Rotated Sorted Array
这道题的思想很简单,就是二分查找,只不过对二分查找稍微改变下,看了代码相信你就一定恍然大明白了public class FindMinimuminRotatedSortedArray { int result = Integer.MAX_VALUE; public int findMin(int[] nums) { binarySearch(nums, 0, nums.length
2016-04-06 09:26:42 191
转载 【LeetCode-236】Lowest Common Ancestor of a Binary Tree
这道题想用寻找路径的方法来解决,但是最后没能成功ac,看了一下别人的想法,自己又走弯路了!递归完全可以额!public class ImportantLowestCommonAncestorofaBinaryTree { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int
2016-04-06 08:41:30 193
原创 【LeetCode-209】Minimum Size Subarray Sum
咋一看挺麻烦,其实就是一层膜,捅破它你就恍然大明白了,开个玩笑,上代码吧,时间复杂度O(n)public class MinimumSizeSubarraySum { public int minSubArrayLen(int s, int[] nums) { if (nums.length == 0) { return 0; } int result = Integer.
2016-04-05 10:39:06 250
原创 【LeetCode-23】Merge k Sorted Lists
这道题把我搞得心情很郁闷了,不过最后还是征服了它,你就是是那脱缰的野马,我也要把你骑在胯下!归并排序的思想!public class MergekSortedLists { class ListNode{ int val; ListNode next; public ListNode(int val){ this.val = val; } } public L
2016-04-04 22:26:50 216
原创 【LeetCode-61】Rotate List
几点没刷题,刷起来就又慢了,一切都在代码中。/** * 首尾相连形成一个环,指针从最后一个节点开始后移,后移len - (k % len) - 1长度后的节点假设为temp,则temp.next为新链的起点 * @author MG * */public class RotateList { class ListNode{ int val; ListNode next;
2016-04-04 19:21:34 218
转载 【LeetCode-55】Jump Game
这道题自己实在没思路,然后看到别人写的,自己还是太弱了,fightingpublic class ImportantJumpGame { public boolean canJump(int[] nums) { if(nums.length == 0 || nums.length == 1){ return true; }
2016-04-02 10:20:22 249
原创 【LeetCode-59】Spiral Matrix II
这道题和Spiral Matrix思路一模一样,一读一写,come on!public class SpiralMatrixII { public int[][] generateMatrix(int n) { if(n < 0){ return null; } if(n == 0){ return new int[0][0]; } int[][] res
2016-04-01 17:24:26 220
原创 【LeetCode-54】Spiral Matrix
这道题思路很简单,从外层一直向内层循环管访问数组元素,上代码public class SpiralMatrix { public List spiralOrder(int[][] matrix) { List result = new ArrayList(); //(left,top)代表左上角,(right,down)代表左下角 int left = 0;
2016-04-01 16:34:17 187
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人