- 博客(38)
- 资源 (3)
- 收藏
- 关注
原创 LeetCode 378. Kth Smallest Element in a Sorted Matrix
public class Solution { public int kthSmallest(int[][] matrix, int k) { int n = matrix.length; int low = matrix[0][0]; int high = matrix[n - 1][n - 1]; while (low <
2016-12-28 20:33:49 194
原创 LeetCode 268. Missing Number
public class Solution { public int missingNumber(int[] nums) { int xor = 0; int l = nums.length; for (int i = 0; i < l; i++) xor ^= i ^ nums[i]; return xor ^ l; }}
2016-12-28 19:27:53 183
原创 LeetCode 94. Binary Tree Inorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {
2016-12-28 19:09:55 187
原创 LeetCode 392. Is Subsequence
public class Solution { public boolean isSubsequence(String s, String t) { int ls = s.length(); int lt = t.length(); if (ls > lt) return false; int iS = 0;
2016-12-25 20:33:57 261
原创 LeetCode 343. Integer Break
public class Solution { public int integerBreak(int n) { if (n == 2) return 1; else if (n == 3) return 2; int power = n / 3; int reg = n % 3; int i = (int)Math
2016-12-25 20:08:39 221
原创 LeetCode 384. Shuffle an Array
public class Solution { int[] nums; Random random; public Solution(int[] nums) { this.nums = nums; random = new Random(); } /** Resets the array to its original
2016-12-25 19:44:33 261
原创 LeetCode 357. Count Numbers with Unique Digits
public class Solution { public int countNumbersWithUniqueDigits(int n) { if (n > 10) n = 10; if (n == 0) return 1; else if (n == 1) return 10; int nums = 10;
2016-12-22 21:19:26 201
原创 LeetCode 445. Add Two Numbers II
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { int len1 = 0; int len2 = 0; int max1 = 0; int max2 = 0; List
2016-12-22 20:57:11 517
原创 LeetCode 122. Best Time to Buy and Sell Stock II
public class Solution { public int maxProfit(int[] prices) { int sum = 0; for (int i = 0; i < prices.length - 1; i++) { if (prices[i + 1] > prices[i]) sum += prices[i + 1] - pri
2016-12-20 21:19:32 197
原创 LeetCode 238. Product of Array Except Self
public class Solution { public int[] productExceptSelf(int[] nums) { int l = nums.length; int[] r = new int[l]; r[0] = 1; for (int i = 1; i < l; i++) r[i] = r[i - 1
2016-12-19 21:09:02 190
原创 LeetCode 167. Two Sum II - Input array is sorted
public class Solution { public int[] twoSum(int[] numbers, int target) { int l = numbers.length; int pre = 0; boolean isNotFirst = false; for (int i = 0; i < l; i++
2016-12-18 20:54:52 220
原创 LeetCode 260. Single Number III
public class Solution { public int[] singleNumber(int[] nums) { int l = nums.length; int xor = 0; for (int i = 0; i < l; i++) xor ^= nums[i]; for (int i = 0; i < 32; i++) {
2016-12-18 20:20:09 218
原创 LeetCode 462. Minimum Moves to Equal Array Elements II
public class Solution { public int minMoves2(int[] nums) { Arrays.sort(nums); int l = nums.length; int median = nums[l / 2]; int moves = 0; for (int i = 0; i <
2016-12-18 19:16:59 308
原创 LeetCode 406. Queue Reconstruction by Height
public class Solution { public int[][] reconstructQueue(int[][] people) { int l = people.length; if (l == 1) return people; for (int i = l - 1; i > 0; i--) { for (int j = 0
2016-12-17 21:20:18 245
原创 LeetCode 413. Arithmetic Slices
public class Solution { public int numberOfArithmeticSlices(int[] A) { int l = A.length; if (l < 3) return 0; int num = 0; int count = 0; int interval = 0;
2016-12-17 20:17:09 217
原创 LeetCode 338. Counting Bits
public class Solution { public int[] countBits(int num) { int[] bits = new int[num + 1]; int level = 0; bits[0] = 0; for (int i = 1; i <= num; i++) { int n
2016-12-14 21:55:00 219
原创 LeetCode 419. Battleships in a Board
public class Solution { public int countBattleships(char[][] board) { int rows = board.length; int cols = board[0].length; int num = 0; for (int i = 0; i < rows; i+
2016-12-14 21:11:40 286
原创 LeetCode 8. String to Integer (atoi)
public class Solution { public int myAtoi(String str) { if (str.length() == 0) return 0; StringBuilder sb = new StringBuilder(); char sign = ' '; for (int i = 0; i
2016-12-13 23:31:51 183
原创 Java使用split()按"."切分出错解决方法
因为"."是正则表达式里的一个关键字,如果没有经过转义split()会把它当成正则表达式处理,所以需要使用下面的方法:public class SplitDot { public static void main(String[] args) { String test = "www.helloworld.com"; String[] t = test.split("\\.");
2016-12-13 22:16:01 1981
原创 LeetCode 165. Compare Version Numbers
public class Solution { public int compareVersion(String version1, String version2) { String[] v1 = version1.split("\\."); String[] v2 = version2.split("\\."); int l1 = v1.
2016-12-13 22:02:50 202
原创 LeetCode 189. Rotate Array
public class Solution { public void rotate(int[] nums, int k) { int l = nums.length; k %= l; int[] newNums = new int[l]; for (int i = 0; i < k; i++) newNums[i] = nums[
2016-12-09 21:13:46 163
原创 LeetCode 7. Reverse Integer
public class Solution { public int reverse(int x) { if (x == 0) return 0; boolean isNeg = x < 0 ? true : false; String s = new StringBuilder(String.valueOf(Math.abs(x))).re
2016-12-09 20:44:11 174
原创 LeetCode 278. First Bad Version
/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */public class Solution extends VersionControl { public int firstBadVersion(int n) { int h = n; int l = 0; int r = h + (
2016-12-08 21:25:30 188
原创 LeetCode 168. Excel Sheet Column Title
public class Solution { public String convertToTitle(int n) { StringBuilder sb = new StringBuilder(); do { n -= 1; sb.append((char)(n % 26 + 'A')); n /=
2016-12-08 21:00:34 155
原创 LeetCode 125. Valid Palindrome
public class Solution { public boolean isPalindrome(String s) { if (s.length() == 0) return true; s = s.toLowerCase(); int ifront = 0; int iback = s.length() - 1; while (ifront < iback) { char cfront = s.charAt(i
2016-12-08 20:48:54 197
原创 LeetCode 6. ZigZag Conversion
public class Solution { public String convert(String s, int numRows) { if (s.length() == 0) return ""; int count = 0; boolean reverse = false; List> rows = new Arra
2016-12-07 22:04:46 198
原创 LeetCode 155. Min Stack
public class MinStack { int size; List list; int min; /** initialize your data structure here. */ public MinStack() { size = 0; list = new ArrayList(); min = Inte
2016-12-07 21:12:39 161
原创 LeetCode 204. Count Primes
public class Solution { public int countPrimes(int n) { if (n <= 1) return 0; int[] nums = new int[n]; int count = 0; for (int i = 2; i < n; i++) { if (nums[i] == 0
2016-12-07 20:46:23 163
原创 LeetCode 303. Range Sum Query - Immutable
public class NumArray { int[] sums; public NumArray(int[] nums) { sums = new int[nums.length + 1]; for (int i = 1; i <= nums.length; i++) sums[i] += sums[i - 1] + nums[i - 1];
2016-12-06 20:23:15 212
原创 LeetCode 414. Third Maximum Number
public class Solution { public int thirdMax(int[] nums) { int[] max = new int[3]; max[0] = nums[0]; int count = 1; for (int i = 1; i < nums.length; i++) { boolean exist = false; for (int j = 0; j < c
2016-12-06 19:55:09 243
原创 LeetCode 28. Implement strStr()
public class Solution { public int strStr(String haystack, String needle) { int lh = haystack.length(); int ln = needle.length(); if (lh < ln) return -1; for (int i = 0;
2016-12-06 19:21:16 152
原创 LeetCode 1. Two Sum
public class Solution { public int[] twoSum(int[] nums, int target) { int[] indices = new int[2]; Map map = new HashMap(); for (int i = 0; i < nums.length; i++) map.put(num
2016-12-05 21:48:09 164
原创 LeetCode 190. Reverse Bits
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int r = 0; for (int i = 0; i < 32; i++) { r += n & 1; n >>>=
2016-12-05 21:01:57 163
原创 LeetCode 396. Rotate Function
public class Solution { public int maxRotateFunction(int[] A) { int l = A.length; if (l == 0) return 0; int first = 0; int offset = 0; int sup = 0; for (i
2016-12-04 15:39:47 176
原创 LeetCode 67. Add Binary
public class Solution { public String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); char c = '0'; int la = a.length() - 1; int lb = b.leng
2016-12-03 21:28:39 181
原创 LeetCode 14. Longest Common Prefix
public class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) {
2016-12-03 21:05:46 169
原创 LeetCode 400. Nth Digit
public class Solution { public int findNthDigit(int n) { int level = 1; long count = level * 9 * (long)Math.pow(10, level - 1); while (n > count) { n -= count;
2016-12-01 21:50:21 226
原创 LeetCode 203. Remove Linked List Elements
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode remo
2016-12-01 19:30:18 169
Deep Learning中文版
2017-05-02
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人