- 博客(11)
- 收藏
- 关注
原创 [LeetCode]Linked List Cycle
此题一看就是two point的问题,这类问题肯定是一个先走一个后走,要么是按照倍速一快一慢,要么是一个先走多少步,再两者同时走(找公共子链的问题)。此题就是按照倍速,若有循环则快者必将追上慢者。代码如下:/** * Definition for singly-linked list. * class ListNode { * int val;
2015-08-29 18:45:55 269
原创 [LeetCode]Single Number
本系列一共有三个题目,第一题最为简单,只需要掌握异或运算满足交换律即可。第二题需要构造一个32位数组,根据二进制的原理,每当某一位上1的数量满足3的倍数时就清零,直到最后找出只出现一次的数字转化为二进制数后各个位置上的1,然后转化成十进制即可。public class Solution { public static int singleNumber(int[] A) {
2015-08-22 12:07:43 654
原创 [LeetCode]Search a 2D Matrix II
本题有三种做法,从最开始我想到的就是相对最简单的分治法。从右下角开始向左上角移动,当该处数值小于target时,递归调用查找左下和右上两个子问题。在原理正确的情况下如果超时,一定要删除其中的打印啊!!!System.out占用非常多的时间,坑爹。时间复杂度的计算为T(n)=2*T(n/2)+c,算去吧。代码如下:public class Solution
2015-08-20 19:12:02 399
原创 [LeetCode]Sliding Window Maximum
public class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if(nums.length==0)return new int[0]; int[] result = new int[nums.length-k+1]; int max = getMax(nums,0,k-1);
2015-08-17 11:02:27 323
原创 [LeetCode]Balanced Binary Tree
public boolean isBalanced(TreeNode root) { if(root==null)return true;// boolean result = false; if(Math.abs(depth(root.left)-depth(root.right))>1)return false; else return is
2015-08-10 13:19:50 303
原创 [LeetCode]Path Sum
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {
2015-08-10 12:21:11 331
原创 [LeetCode]Count Primes
题目链接https://leetcode.com/problems/count-primes/public class Solution { public int countPrimes(int n) { int count = 0; for(int i=2;i<=n;i++){ boolean isPrime = true; for(int j
2015-08-08 00:06:25 286
原创 [LeetCode]Reverse Linked List
public ListNode reverseList(ListNode head) { if(head==null||head.next==null)return head; if(head.next.next==null){ ListNode temp; temp = head.next; head.next.next = head;
2015-08-07 19:57:53 270
原创 [LeetCode]Contains Duplicate II
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Set set = new HashSet(); if(k==0)return false; for(int i=0;i<=nums.length-1;i++){ if(se
2015-08-07 19:45:33 273
原创 [LeetCode] Summary Ranges
public class Solution {public List summaryRanges(int[] nums) { int i=0; List result=new ArrayList(); while(i<nums.length){ String s=""; s+=nums[i]; if(i<nums.lengt
2015-08-06 01:11:22 354
原创 [LeetCode]Palindrome Linked List
class Solution { public boolean isPalindrome(ListNode head) { if(head==null||head.next==null)return true; ListNode thead = head; ListNode mid = findMid(head); mid = rever
2015-08-04 18:18:25 371
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人