
LeetCode
Cloud陈
这个作者很懒,什么都没留下…
展开
-
LeetCode 412. Fizz Buzz
public class Solution { public List fizzBuzz(int n) { List numList = new ArrayList(); for (int i = 1; i <= n; i++) { if ((i % 15) == 0) numList.add("FizzBuzz"); else if ((i % 3) ==原创 2016-11-03 17:00:25 · 440 阅读 · 0 评论 -
LeetCode 344. Reverse String
public class Solution { public String reverseString(String s) { char tmp[] = s.toCharArray(); int l = tmp.length; for(int i = 0; i < l / 2; i++) { char t = tmp[l - i - 1]; tmp[l原创 2016-11-03 17:01:17 · 186 阅读 · 0 评论 -
LeetCode 292. Nim Game
public class Solution { public boolean canWinNim(int n) { return ((n % 4) != 0); }}原创 2016-11-03 17:02:18 · 206 阅读 · 0 评论 -
LeetCode 136. Single Number
public class Solution { public int singleNumber(int[] nums) { int singleNum = 0; for (int i = 0; i < nums.length; i++) singleNum ^= nums[i]; return singleNum; }}原创 2016-11-03 17:02:50 · 285 阅读 · 0 评论 -
LeetCode 371. Sum of Two Integers
public class Solution { public int getSum(int a, int b) { int c = (a & b) << 1; int d = a ^ b; while (c != 0) { int tmp = d; d = d ^ c; c = (c & tmp) <原创 2016-11-03 17:03:17 · 218 阅读 · 0 评论 -
LeetCode 104. Maximum Depth of Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-11-03 17:03:44 · 206 阅读 · 0 评论 -
LeetCode 258. Add Digits
public class Solution { public int addDigits(int num) { return (num % 9 == 0) && num != 0 ? 9 : num % 9; }}原创 2016-11-03 17:04:07 · 178 阅读 · 0 评论 -
LeetCode 389. Find the Difference
public class Solution { public char findTheDifference(String s, String t) { char[] sToChars = s.toCharArray(); char[] tToChars = t.toCharArray(); char r = '\0'; for原创 2016-11-03 17:04:34 · 212 阅读 · 0 评论 -
LeetCode 226. Invert Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-11-03 17:05:24 · 179 阅读 · 0 评论 -
LeetCode 283. Move Zeroes
public class Solution { public void moveZeroes(int[] nums) { if (nums.length <= 1) return; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == 0) count++;原创 2016-11-03 17:06:00 · 192 阅读 · 0 评论 -
LeetCode 404. Sum of Left Leaves
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-11-03 17:06:35 · 182 阅读 · 0 评论 -
LeetCode 383. Ransom Note
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { char[] r = ransomNote.toCharArray(); char[] m = magazine.toCharArray(); int rl = r原创 2016-11-03 17:07:23 · 177 阅读 · 0 评论 -
LeetCode 237. Delete Node in a Linked List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public void deleteNo原创 2016-11-03 17:08:04 · 226 阅读 · 0 评论 -
LeetCode 349. Intersection of Two Arrays
public class Solution { public int[] intersection(int[] nums1, int[] nums2) { if (nums1 == null || nums2 == null) return null; Set set1 = new HashSet(); Set set2 = new Hash原创 2016-11-03 17:08:29 · 176 阅读 · 0 评论 -
LeetCode 100. Same Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-11-03 19:48:19 · 166 阅读 · 0 评论 -
LeetCode 171. Excel Sheet Column Number
public class Solution { public int titleToNumber(String s) { if (s == "") return 0; char[] c = s.toCharArray(); int r = 0; for (int i = 0; i < c.length; i++) { r += (c原创 2016-11-03 19:48:52 · 193 阅读 · 0 评论 -
LeetCode 387. First Unique Character in a String
public class Solution { public int firstUniqChar(String s) { char[] c = s.toCharArray(); Map map = new HashMap(); for (int i =0; i < c.length; i++) { if (!map.cont原创 2016-11-03 19:49:16 · 183 阅读 · 0 评论 -
LeetCode 242. Valid Anagram
public class Solution { public boolean isAnagram(String s, String t) { if (s == "" && t == "") return true; else if (s.length() != t.length()) return false; Map map = new H原创 2016-11-03 19:50:06 · 164 阅读 · 0 评论 -
LeetCode 169. Majority Element
public class Solution { public int majorityElement(int[] nums) { Map map = new HashMap(); for (int i = 0; i < nums.length; i++) { if (!map.containsKey(nums[i])) map.put(nu原创 2016-11-03 19:50:41 · 184 阅读 · 0 评论 -
LeetCode 409. Longest Palindrome
public class Solution { public int longestPalindrome(String s) { int[] c = new int[52]; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) <= 'Z') c[s.charAt(i) - 'A']原创 2016-11-03 19:51:33 · 218 阅读 · 0 评论 -
LeetCode 401. Binary Watch
public class Solution { public List readBinaryWatch(int num) { List r = new ArrayList(); if (num == 0) { r.add("0:00"); return r; } else if (num > 10) ret原创 2016-11-03 19:51:58 · 188 阅读 · 0 评论 -
LeetCode 217. Contains Duplicate
public class Solution { public boolean containsDuplicate(int[] nums) { Arrays.sort(nums); for (int i = 1; i < nums.length; i++) { if(nums[i] == nums[i - 1]) {原创 2016-11-03 19:52:26 · 232 阅读 · 0 评论 -
LeetCode 350. Intersection of Two Arrays II
public class Solution { public int[] intersect(int[] nums1, int[] nums2) { if (nums1.length == 0 || nums2.length == 0) return new int[0]; Arrays.sort(nums1); Arrays.sort(nu原创 2016-11-03 19:52:59 · 179 阅读 · 0 评论 -
LeetCode 13. Roman to Integer
public class Solution { public int romanToInt(String s) { int r = 0; int tmp = 0; for (int i = s.length() - 1; i >= 0; i--) { int n = 0; char c = s.charAt(i);原创 2016-11-03 19:53:48 · 181 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reve原创 2016-11-03 19:54:24 · 192 阅读 · 0 评论 -
LeetCode 415. Add Strings
public class Solution { public String addStrings(String num1, String num2) { int l1 = num1.length(); int l2 = num2.length(); int c = 0; String sum = ""; for原创 2016-11-03 19:55:11 · 196 阅读 · 0 评论 -
LeetCode 405. Convert a Number to Hexadecimal
public class Solution { public String toHex(int num) { if (num == 0) return "0"; StringBuilder hex = new StringBuilder(); boolean isNegative = false; if (num < 0) { is原创 2016-11-03 21:30:14 · 222 阅读 · 0 评论 -
LeetCode 326. Power of Three
public class Solution { public boolean isPowerOfThree(int n) { return n 0 ? false : true); }}原创 2016-11-03 22:06:18 · 253 阅读 · 0 评论 -
LeetCode 231. Power of Two
public class Solution { public boolean isPowerOfTwo(int n) { return n <= 0 ? false : (Integer.bitCount(n) == 1 ? true : false); }}原创 2016-11-05 19:50:06 · 180 阅读 · 0 评论 -
LeetCode 202. Happy Number
public class Solution { public boolean isHappy(int n) { List<Integer> list = new ArrayList<Integer>(); while (!list.contains(n)) { list.add(n); int tmp = 0; int l = String.valueOf(n).length(); for (in原创 2016-11-05 20:20:06 · 285 阅读 · 0 评论 -
LeetCode 83. Remove Duplicates from Sorted List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode dele原创 2016-11-06 21:16:24 · 200 阅读 · 0 评论 -
LeetCode 70. Climbing Stairs
public class Solution { public int climbStairs(int n) { if (n <= 3) return n; int[] stairs = new int[n]; stairs[0] = 1; stairs[1] = 2; stairs[2] = 3; for (int i = 3原创 2016-11-07 20:49:31 · 211 阅读 · 0 评论 -
LeetCode 191. Number of 1 Bits
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { return Integer.bitCount(n); }}原创 2016-11-07 21:00:07 · 241 阅读 · 0 评论 -
LeetCode 437. Path Sum III
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-11-07 22:14:12 · 243 阅读 · 0 评论 -
LeetCode 263. Ugly Number
public class Solution { public boolean isUgly(int num) { if (num == 1) return true; else if (num < 1) return false; while (num != 1) { if (num % 2 == 0) {原创 2016-11-08 19:58:17 · 207 阅读 · 0 评论 -
LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-11-08 21:17:04 · 235 阅读 · 0 评论 -
LeetCode 21. Merge Two Sorted Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode merg原创 2016-11-09 20:50:09 · 228 阅读 · 0 评论 -
LeetCode 441. Arranging Coins
public class Solution { public int arrangeCoins(int n) { int count = 0; while (n >= (count + 1)) { count++; n -= count; } return count; }}原创 2016-11-09 21:14:45 · 249 阅读 · 0 评论 -
LeetCode 107. Binary Tree Level Order Traversal II
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<List<Integer>> levelOrderBottom(TreeNode原创 2016-11-10 20:41:12 · 204 阅读 · 0 评论 -
LeetCode 290. Word Pattern
public class Solution { public boolean wordPattern(String pattern, String str) { String[] p = new String[26]; String[] s = str.split(" "); if (pattern.length() != s.length)原创 2016-11-28 20:05:54 · 198 阅读 · 0 评论