- 博客(152)
- 资源 (3)
- 收藏
- 关注
原创 Java中,如何让System.out.printf("%d", 2 + 2);输出5
代码如下: import java.lang.reflect.Field; public class TwoPlusTwo { public static void main(String[] args) throws Exception { Class cache = Integer.class.getDeclaredClasses()[0]; System.out.println
2014-11-17 17:08:48
1305
原创 单例模式总结
即时加载 public class Singleton { private static final Singleton uniqueInstance = new Singleton(); private Singleton() { } public static Singleton getInstance() { return uniqueInstance; } }延时加载—
2014-09-19 10:24:40
574
原创 IDG | 四则运算表达式计算
分析 首先将中缀表达式转换为后缀表达式(逆波兰式),然后使用栈进行计算。 代码 import java.util.LinkedList; import java.util.List; import java.util.Stack; public class ExpCal { public static double calc(String exp) { if (exp == null
2014-09-18 11:00:04
785
原创 LeetCode | 4Sum
题目 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note:
2014-04-18 16:41:19
602
原创 LeetCode | 3Sum Closest
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have
2014-04-18 14:50:14
583
原创 LeetCode | 3Sum
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triple
2014-04-16 18:06:55
731
1
原创 LeetCode | Median of Two Sorted Arrays
题目 There are two sorted arrays A and B 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)). 分析 转换为找第K大的数,需要十分小心边界条件。
2014-04-15 11:40:36
615
原创 LeetCode | Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 按部就班。 也可以用高大上的trie树,不过有点大材小用,而且空间、时间都没简单的好。 代码 public class LongestCommonPrefix { public String lo
2014-04-10 10:55:42
787
原创 LeetCode | Container With Most Water
题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i,
2014-04-10 10:00:00
606
原创 LeetCode | Reverse Integer
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask
2014-04-09 17:38:11
508
原创 LeetCode | 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
2014-04-09 17:14:01
506
原创 LeetCode | Substring with Concatenation of All Words
题目 You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and wi
2014-04-09 15:53:47
626
原创 LeetCode | Palindrome Number
题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of c
2014-04-09 14:23:10
523
原创 LeetCode | ZigZag Conversion
题目 The string "PAYPALISHIRING" is written in a zigzag 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 N A
2014-04-08 17:40:14
525
原创 LeetCode | Reverse Nodes in k-Group
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain
2014-04-08 14:54:41
652
原创 LeetCode | Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3
2014-04-08 14:52:25
567
原创 LeetCode | Add Two Numbers
题目 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it a
2014-04-07 23:09:31
515
原创 LeetCode | Two Sum
题目 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the tar
2014-04-07 22:40:47
509
原创 LeetCode | Remove Nth Node From End of List
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the
2014-04-06 20:08:06
501
原创 LeetCode | Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. 分析 逐个减太慢,肯定超时。需要指数级增加后再减,思想类似于Linked List Cycle II中的Brent方法。 此外,还要注意零、负数的情况。 代码 public class DivideTwoIntegers
2014-04-06 20:00:30
2057
原创 LeetCode | Sudoku Solver
题目 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution.
2014-04-05 10:40:57
866
原创 LeetCode | Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partial
2014-04-05 10:38:29
678
原创 LeetCode | Valid Parentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are
2014-04-04 11:57:52
580
原创 LeetCode | Subsets II
题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order.The solution set must not conta
2014-04-04 11:35:33
970
原创 LeetCode | Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now
2014-04-04 10:49:45
448
原创 LeetCode | Merge k Sorted Lists
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 分析 经典问题:k路归并,用一个大小为k小根堆保存每个链表的至多一个节点(链表空时,就没节点了),每次取出最小的节点,并插入该节点在链表中的下一个节点(如果不为空),直至取完所有链表
2014-04-04 10:32:56
613
原创 LeetCode | Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())
2014-04-04 10:05:52
502
原创 LeetCode | Swap Nodes in Pairs
题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant
2014-04-03 15:17:51
426
原创 LeetCode | Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in pl
2014-04-03 15:16:00
432
原创 LeetCode | Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new leng
2014-04-03 15:14:10
429
原创 LeetCode | Implement strStr()
题目 Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 分析 KMP算法 代码 public class ImplementStrStr { public String
2014-04-03 15:11:58
428
原创 LeetCode | Next Permutation
题目 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest
2014-04-03 15:09:28
481
原创 LeetCode | Search in Rotated Sorted Array II
题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target i
2014-04-02 19:32:18
510
原创 LeetCode | Search in Rotated Sorted Array
题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the arr
2014-04-02 19:12:09
556
原创 LeetCode | Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is
2014-04-02 19:06:28
948
原创 LeetCode | Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is no
2014-04-01 19:07:49
509
原创 LeetCode | Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in th
2014-04-01 19:05:30
482
原创 LeetCode | Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the
2014-03-31 19:36:47
465
原创 LeetCode | Combination Sum
题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimi
2014-03-31 19:34:38
601
原创 LeetCode | Wildcard Matching
题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should
2014-03-31 19:31:27
4084
1
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人