
LeetCode
xiueer
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
202 Happy Number
public static boolean isHappy(int n) { HashSet<Integer> al = new HashSet<Integer>(); while (n != 1) { n = func(n); if (!al.add(n)) return false;原创 2015-08-22 00:39:41 · 274 阅读 · 0 评论 -
260 Single Number III
public class Solution { public int[] singleNumber(int[] nums) { int []res=new int[2]; Arrays.sort(nums); int i=0,j=0; while(i<nums.length){ if(i<(nums.leng原创 2015-08-21 16:11:19 · 284 阅读 · 0 评论 -
136 Single Number
N^N=0public class Solution { public int singleNumber(int[] nums) { int result = nums[0]; for (int i = 1; i < nums.length; i++) { result = result ^ nums[i]; }转载 2015-08-21 10:13:38 · 335 阅读 · 0 评论 -
264 Ugly Number II
除第一个数 1 外,所有的Ugly Number都是有较小的Ugly Number乘2,3,5得到的,所以本题的关键就是在 1*2,2*2,3*2,4*2,5*2,6*2,8*2… 1*3,2*3,3*3,4*3,5*3,6*3,8*3… 1*5,2*5,3*5,4*5,5*5,6*5,8*5… 之中找到第n个数。public static int nthUglyNumber(int n)转载 2015-08-20 19:25:23 · 409 阅读 · 0 评论 -
263 Ugly Number
public boolean isUgly(int num) { if (num == 0)//不要忘记num=0的情况 return false; while (num % 2 == 0 || num % 3 == 0 || num % 5 == 0) { if (num % 2 == 0)原创 2015-08-20 10:40:50 · 259 阅读 · 0 评论 -
257 Binary Tree Paths
/** * 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-19 13:34:16 · 349 阅读 · 0 评论 -
234 Palindrome Linked List
题目要求在O(n)时间和O(1)空间内判断一个链表是不是回文的。 先将链表分成2部分,然后反转其中一个,在进行比较。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }转载 2015-08-17 10:53:24 · 365 阅读 · 0 评论 -
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 Sol转载 2015-07-21 20:08:57 · 326 阅读 · 0 评论 -
238 Product of Array Except Self
网上的public class Solution { public int[] productExceptSelf(int[] nums) { int[] res = new int[nums.length]; res[res.length-1] = 1; for(int i=nums.length-2; i>=0; i--) {转载 2015-07-20 15:37:32 · 378 阅读 · 0 评论 -
8 String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.N原创 2015-06-15 13:44:48 · 350 阅读 · 0 评论 -
7 Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you hav原创 2015-06-15 13:06:49 · 347 阅读 · 0 评论 -
6 ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag(Z字形) pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2015-06-15 01:27:07 · 279 阅读 · 0 评论 -
5 Longest Palindromic Substring
Given a string S, find the longest palindromic(adj. 回文的;复发的) substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.Tags:String提交原创 2015-06-14 21:48:38 · 288 阅读 · 0 评论 -
258 Add Digits
public int addDigits(int num) { while (num > 9) { num = func(num); } return num; } public static int func(int n) { int temp = 0; while (n > 0) {原创 2015-08-23 13:03:42 · 332 阅读 · 0 评论 -
137 Single Number II
public class Solution { public int singleNumber(int[] nums) { int res = 0; Arrays.sort(nums); for (int a : nums) System.out.println(a); int i = 0;转载 2015-08-21 11:05:49 · 361 阅读 · 0 评论 -
51 N-Queens
public class Solution { public List<List<String>> solveNQueens(int n) { int[]a=new int[n]; init(a); int i = 0, j = 0; List<List<String>>list=new ArrayList<List<S原创 2015-08-23 20:29:30 · 325 阅读 · 0 评论 -
LeetCode
1 TwoSum 4 Median of Two Sorted Arrays 5 Longest Palindromic Substring 6 ZigZag Conversion 7 Reverse Integer 8 String to Integer (atoi) 234 Palindrome Linked List 235 Lowest Common Ancestor o原创 2015-08-19 13:41:30 · 389 阅读 · 0 评论 -
14 Longest Common Prefix
public static String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; for (int i = 0; i < strs[0].length(); i++) { for (int j = 1; j < strs.le原创 2015-11-06 22:28:07 · 470 阅读 · 0 评论 -
13 Roman to Integer
public static int romanToInt(String s) { String[] str = { "I", "V", "X", "L", "C", "D", "M" }; int[] nums = { 1, 5, 10, 50, 100, 500, 1000 }; Map<String, Integer> map = new HashMa原创 2015-11-06 20:37:12 · 407 阅读 · 0 评论 -
100 Same Tree
public static boolean isSameTree(TreeNode p, TreeNode q) { if (p == null) return q == null; if (q == null) return p == null; if ((p.left == null) && (p.rig原创 2015-11-06 19:22:30 · 403 阅读 · 0 评论 -
16 3Sum Closest
类似的题目有 1 TwoSum 15 3Sum 18 4Sumpublic static int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int minMinus = 10000; int result = 0; int sum = 0;原创 2015-11-06 19:00:42 · 431 阅读 · 0 评论 -
15 3Sum
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class No15 { public static void main(String[] args) { int[] nums = { -1, 0, 1, 2, -1, -4 }; System.o原创 2015-11-06 16:24:11 · 400 阅读 · 0 评论 -
18 4Sum
第一次使用的是笨办法,四层嵌套for循环,总是超时public static List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> result = new ArrayList<List<Integer>>(); Arrays.sort(nums); for (原创 2015-11-06 11:08:37 · 472 阅读 · 0 评论 -
11 Container With Most Water
public class No11 { public static void main(String[] args) { int[] height = { 3, 4, 5 ,3}; System.out.println(maxArea(height)); } public static int maxArea(int[] height) {转载 2015-10-06 00:23:36 · 266 阅读 · 0 评论 -
10 Regular Expression Matching
public class No10 { public static void main(String[] args) { System.out.println(isMatch("aa", "a")); System.out.println(isMatch("aa", "aa")); System.out.println(isMatch("aaa"转载 2015-10-06 00:21:43 · 265 阅读 · 0 评论 -
12 Integer to Roman
罗马数字的规则 基本字符 I V X L C D M 对应的数字 1 5 10 50 100 500 1000相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12;小的数字、(限于 Ⅰ、X 和 C)在大的数字的左边、所表示的数等于大数减小数转载 2015-10-06 00:17:50 · 342 阅读 · 0 评论 -
36 Valid Sudoku
“` public static boolean isValidSudoku(char[][] board) { Set<Character> set = new HashSet<Character>(); for (int i = 0; i < 9; i++) { set.clear(); for (int j = 0; j < 9; j++) {原创 2015-08-25 16:43:30 · 375 阅读 · 0 评论 -
230 Kth Smallest Element in a BST
public static int kthSmallest(TreeNode root, int k) { Stack<TreeNode>stack=new Stack<>(); TreeNode n= root; while(n!=null){ stack.push(n); n=n.left;转载 2015-08-24 21:11:36 · 381 阅读 · 0 评论 -
231 Power of Two
位运算public class Solution { public boolean isPowerOfTwo(int n) { return n > 0 && ((n & (n - 1)) == 0 ); } }转载 2015-08-24 20:35:01 · 298 阅读 · 0 评论 -
4 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).提交超时,偶尔一次提交能通过。public class No原创 2015-06-14 21:42:32 · 425 阅读 · 0 评论