- 博客(41)
- 收藏
- 关注
原创 95. Unique Binary Search Trees II
Unique Binary Search Trees IIGiven n = 3, your program should return all 5 unique BST’s shown below. 需要把所有的5给表示出来代码:/** * Definition for a binary tree node. * public class TreeNode { * int va
2016-03-31 16:10:28 202
原创 96. Unique Binary Search Trees
Unique Binary Search TreesGiven n = 3, there are a total of 5 unique BST’s. 给出一个n 主要计算能排出多少种不同的n代码:使用了动态规划dp :sum += dp[j] * dp[i - j - 1]; 如果给出2个节点则:sum = dp[0]*dp[1]+dp[1]*dp[0] 如果给出3个节点则:sum
2016-03-31 15:16:36 159
原创 小明的筷子java
代码如下:有待改进 请指教import java.util.Scanner;public class iptest { public static void main(String[] args){ Scanner s = new Scanner(System.in); long data = s.nextLong(); long[] tt =
2016-03-30 19:14:45 535
原创 IP地址转换java
代码如下:有待改进 请指教import java.util.Scanner;public class iptest { public static void main(String[] args){ Scanner s = new Scanner(System.in); long data = s.nextLong(); long[] tt =
2016-03-30 19:12:49 363
原创 java进制转换成字符串
java中进行二进制,八进制,十六进制,十进制间进行相互转换 十进制转成十六进制: Integer.toHexString(int i) 十进制转成八进制 Integer.toOctalString(int i) 十进制转成二进制 Integer.toBinaryString(int i) 十六进制转成十进制 Integer.valueOf(“FFFF”,16).toString(
2016-03-29 14:50:48 362
原创 44. Wildcard Matching
Wildcard Matching44.Wildcard Matching和10.Regular Expression Matching这两个题目咋一看以为是一样的,其实不是,第一题其实是通配符匹配,即是说星号可以匹配任何的子字符串,而第二题其实是正则表达式匹配,即是说星号表示它之前的字符可以出现任意多次(包括0),这其实就是编译原理龙书上讲的克林闭包。 Some examples: isMat
2016-03-28 19:15:41 205
原创 java的自己进行输入输出
自己的想法:一直不是特别会 java的输入 像c中的scanf那样 下面是个简单的小程序做个实例让自己记住吧 代码:import java.util.Scanner;import java.util.ArrayList;public class Main{ public static void main(String[] args) throws Exception{
2016-03-28 15:55:03 479
原创 64. Minimum Path Sum
Minimum Path SumGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either dow
2016-03-28 11:21:06 158
原创 63. Unique Paths II
Unique Paths IIThere is one obstacle in the middle of a 3x3 grid as illustrated below. [ [0,0,0], [0,1,0], [0,0,0] ] The total number of unique paths is 2. 如果加了障碍会怎么样 其实也同样使用动态规划 多增加一步
2016-03-27 16:20:41 256
原创 62. Unique Paths
Unique Paths从起点走到终点有多少种方法 思路使用简单的动态规划: 首先对两边进行赋值 因为机器人不能向上走 或者向左走 所以两边的格子里都是1 其次再通过动态规划【F(m)(n)=F(m-1)(n)+F(m)(n-1)】计算走到最后一格有多少种方法代码public class Solution { public int uniquePaths(int m, int n)
2016-03-27 16:00:45 183
原创 45. Jump Game II
Jump Game II算出能跳到最后一步的最小的步数 For example: Given array A = [2,3,1,1,4] The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)代码同样用贪心
2016-03-24 11:31:39 189
原创 55. Jump Game
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determi
2016-03-24 10:39:49 203
原创 67. Add Binary
Add Binary给定两个二进制字符串,将它们相加并将结果输出 For example, a = “11” b = “1” Return “100”代码public class Solution { public String addBinary(String a, String b) { int alength = a.length(); int
2016-03-23 15:22:42 233
原创 66. Plus One
Plus One题解:一个整数按位存储于一个int数组中,排列顺序为:最高位在array[0] ,最低位在[n-1],例如:98,存储为:array[0]=9; array[1]=8;思路:从数组的最后一位开始加1,需要考虑进位,如果到[0]位之后仍然有进位存在,需要新开一个长度为(n.length + 1)的数组,拷贝原来的数组。代码:public class Solution { pub
2016-03-23 09:29:08 195
原创 43. Multiply Strings
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. 给俩个字符串的数字,计算想乘的结果public class Solution { public String multiply(String num1, String num2
2016-03-21 14:38:24 163
原创 42. Trapping Rain Water
Trapping Rain Water等雨水的凹槽容量 For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 代码1://先找出中间最高的 然后再计算两边public class Solution { public int trap(int[] height) { if (height.length <=
2016-03-18 19:39:58 218
原创 41. First Missing Positive
First Missing Positive寻找第一个缺失的正整数 即大致就是将给出的数组进行排序 然后找出第一个缺失的正整数 For example: Given [1,2,0] return 3, and [3,4,-1,1] return 2.public class Solution { public int firstMissingPositive(int[] A) {
2016-03-17 20:32:10 221
原创 77. Combinations
CombinationsGiven two integers n and k, return all possible combinations of k numbers out of 1 … n. For example, If n = 4 and k = 2, a solution is: [2,4], [3,4], [2,3], [1,2], [1,3],
2016-03-16 19:08:40 183
原创 40. Combination Sum II
Combination Sum II题意: For example, given candidate set 10,1,2,7,6,1,5 and target 8, A solution set is: [1, 7] [1, 2, 5] [2, 6] [1, 1, 6] 类似于上一道Combination Sum代码:public class Solution {
2016-03-15 20:28:14 153
转载 Java序列化与反序列化
Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨。 1.Java序列化与反序列化Java序列化是指把Java对象转换为字节序列的过程;而Java反序列化是指把字节序列恢复为Java对象的过程。2.为什么需要序列化与反序列化我们知道,当两个进程进行远程通信时,可以相互发送各种类型的数据,包括文本、图片、音频、视频等, 而这些数
2016-03-15 09:41:41 193
原创 39. Combination Sum
39. 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 fr
2016-03-14 20:26:58 209
原创 58. Length of Last Word
题目:Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string. If the last word does not exist, return 0.For example, Given
2016-03-11 11:26:07 183
原创 35. Search Insert Position
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 du
2016-03-10 16:55:46 160
原创 34. Search for a Range
Search for a RangeGiven 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
2016-03-10 11:06:28 147
原创 154. Find Minimum in Rotated Sorted Array II
题解:(153) 如果出现重复怎么办 for example:3,3,1,3 其实也简单 只需要多加一句判断即可public class Solution { public int findMin(int[] nums) { int start = 0, end = nums.length - 1, mid; while (start < end) {
2016-03-09 16:51:07 150
原创 153. Find Minimum in Rotated Sorted Array
题解:题意跟33题一样 只是用二分法找出其中的最小值public class Solution { public int findMin(int[] nums) { int start = 0, end = nums.length - 1, mid; while (start < end) { mid = (start + end
2016-03-09 16:38:57 161
原创 81. Search in Rotated Sorted Array II
同33题一样 只是验证给出的target在不在此数组中;代码:public class Solution { public boolean search(int[] nums, int target) { int start = 0, end = nums.length - 1, mid = -1; while(start <= end) {
2016-03-09 16:00:08 158
原创 33. Search in Rotated Sorted Array
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. I
2016-03-09 15:36:02 202
原创 38. Count and Say
Count and SayThe count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as
2016-03-08 17:03:24 287
原创 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].//不懂 存
2016-03-08 10:59:29 153
原创 46. Permutations
Permutations题意很容易明白:Given a collection of distinct numbers, return all possible permutations. For example: [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,
2016-03-07 17:16:20 148
原创 31. Next Permutation
Next Permutation题意:求下一个排列 主要原理可以看下面这个链接: http://blog.csdn.net/qq575787460/article/details/41215475代码实现基本原理: 在当前序列中,从尾端往前寻找两个相邻元素,前一个记为first,后一个记为second,并且满足first 小于 second。然后再从尾端寻找另一个元素number,如果满足fi
2016-03-07 16:15:20 187
原创 30.Substring with Concatenation of All Words
Substring with Concatenation of All Words题意:给定一个字符串S和一个字符串数组L,L中的字符串长度都相等,找出S中所有的子串恰好包含L中所有字符各一次,返回子串的起始位置。For example, given:s: “barfoothefoobarman” words: [“foo”, “bar”] You should return the indic
2016-03-04 17:17:00 207
原创 37. Sudoku Solver
Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. 非常好理解代码public class Solution { public void solveSudoku(char[][] board) { if(board == null || board.length
2016-03-03 20:14:12 194
原创 36. Valid Sudoku
Valid SudokuThe Sudoku board could be partially filled, where empty cells are filled with the character ‘.’ 看这个数独是否有效? 代码public class Solution { public boolean isValidSudoku(char[][] board) {
2016-03-03 16:20:21 181
原创 29. Divide Two Integers
Divide Two IntegersDivide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.题解分析:题目意思很容易理解,就是不用乘除法和模运算求来做除法,很容易想到的一个方法是一直做减法,然后计数,但是提交之后显示超时,在网上找到
2016-03-02 20:06:29 344
原创 Java序列化与反序列化
Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨。1.Java序列化与反序列化Java序列化是指把Java对象转换为字节序列的过程;而Java反序列化是指把字节序列恢复为Java对象的过程。2.为什么需要序列化与反序列化我们知道,当两个进程进行远程通信时,可以相互发送各种类型的数据,包括文本、图片、音频、视频等, 而这些数据都会
2016-03-02 10:45:02 201
原创 32. Longest Valid Parentheses
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 vali
2016-03-01 18:37:23 178
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人