- 博客(62)
- 收藏
- 关注
原创 Binary Tree的相关练习 104,111
104. Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node
2017-06-03 21:58:12 338
原创 54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]
2017-05-29 11:22:11 254
原创 50. Pow(x, n)
实现power的功能就是Pow(x,n) = x^n但实际上按照今天学习的分治法里面的内容,其实x^n = x^(n/2)* x^(n/2) * x在这道题目里,x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)还需要考虑n为负数的情况利用递归求解public class Solution { public double power(
2017-05-27 22:39:08 190
原创 48. Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?意思就是把一个n*n的二维数组90°旋转一下二维数组的相关知识:比如 :[ [1,2,3,4
2017-05-25 20:54:00 151
原创 Multiply String
题意就是给你两个字符串型的数字,给这两个数字做乘法。 如果直接转换成Integer做乘法就会溢出。 所以要一步一步来。先给出代码:public class Solution { public String multiply(String num1, String num2) { int m=num1.length(); i
2017-05-20 16:46:49 383
原创 46 permutations && 47 Permutation II
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,2], [3,2,1
2017-05-20 10:31:04 247
原创 36. Valid Sudoku
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially filled sudoku which is valid.Note:A valid Sudoku board (partially filled
2017-05-17 14:33:31 152
原创 31. 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 possible
2017-05-15 22:29:49 184
原创 33. Search in Rotated Sorted Array 二分法
Suppose an array sorted in ascending order 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 foun
2017-05-15 21:44:39 194
原创 24. Swap Nodes in Pairs using recursion
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 space. Y
2017-05-14 19:48:46 212
原创 22. Generate Parentheses----dfs
深度优先算法dfs==回溯法backtrackingGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "
2017-05-13 22:42:03 147
原创 19. 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 end, the
2017-05-13 16:58:27 152
原创 77. Combinations
Given 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], [1,4],]
2017-05-11 16:58:54 266
原创 40. 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 combina
2017-05-11 10:48:15 159
原创 39. Combination Sum-dfs
Given a set of candidate numbers (C) (without duplicates) 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
2017-05-10 22:28:11 171
原创 17. Letter Combinations of a Phone Number 回溯算法
这道题是我第一次接触回溯算法,没写出来,感觉很难,小白真的是忧伤。。。看了别人的答案,有些理解这个算法了,可是感觉自己还是独立写不出来,然后盲敲了一遍这个答案Given a digit string, return all possible letter combinations that the number could represent.A mapping of di
2017-05-10 20:22:32 288
原创 28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.题目是要求找到第一个出现指定字符串的字符串中的index我的代码:public class Solution {
2017-05-09 16:57:56 188
原创 3SUM &&16. 3SUM closest && 18.4SUM
3 sum closest就是3sum 的变形, 输出和target最相近的值基本思路和3sum相同,但是添加了一个closedistance变量public class Solution { public int threeSumClosest(int[] nums, int target) { if(nums.length<3||nums==null) retu
2017-05-09 15:56:04 182
原创 15 three sum 用到Arrays.sort 和 Arrays.asList()的方法
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: The solution set must not contain
2017-05-07 22:23:10 406
原创 12. Integer to Roman StringBuilder的运用以及StringBuffer和StringBuilder的比较
public class Solution { public String intToRoman(int num) { int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; String[] strs = {"M","CM","D","CD","C","XC","L","XL","X","IX"
2017-05-05 21:15:25 158
原创 13. Roman to Integer
罗马数字转换成整数:首先要来了解一下罗马数字表示法,基本字符有7个:I,V,X,L,C,D,M,分别表示1,5,10,50,100,500,1000。在构成数字的时候,有下列规则:1、相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;3、小的数
2017-05-05 20:05:37 203
原创 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "
2017-05-02 10:49:04 207
原创 441. Arranging Coins
ou have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be formed.n
2017-05-01 20:15:29 124
原创 2. Add Two Numbers
题目:You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and
2017-04-29 22:17:12 138
原创 496. Next Greater Element I
public class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int len1 = nums1.length; int len2 = nums2.length; int[] ans = new int[len1]; Arr
2017-04-29 10:35:32 132
原创 225. Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet
2017-04-28 11:18:17 145
原创 162. Find Peak Element 再做一遍!
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in
2017-04-25 22:06:44 146
原创 240. Search a 2D Matrix II && 74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in
2017-04-25 20:39:30 187
原创 436. Find Right Interval 要重新做!!
题目:Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called tha
2017-04-15 18:55:07 213
原创 454. 4Sum II
题目:Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D ha
2017-04-15 18:45:21 136
原创 287. Find the Duplicate Number
题目:Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate n
2017-04-15 18:43:43 111
原创 349. Intersection of Two Arrays & 350. Intersection of Two Arrays II
349的题目要求:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be
2017-04-12 09:49:56 146
原创 278. First Bad Version
题目:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed
2017-04-10 23:55:14 130
原创 69. Sqrt(x)----binary search
mplement int sqrt(int x).Compute and return the square root of x.题目就是要实现平方根的功能,但是要注意!!!当输入的数字不是完全平方数的时候需要返回距离它最下的完全平方数的平方根比如当为7的时候,返回2改了好几遍代码,注意了要把输入的数字变成long最后成功的是因为改了跳出循环的判断,跳出循环是因为left>
2017-04-10 20:37:47 409
原创 374. Guess Number Higher or Lower---------binary search
题目是:We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the n
2017-04-10 19:58:54 159
原创 167. Two Sum II - Input array is sorted-----binary search
题目如下:Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the
2017-04-10 15:40:03 263
原创 367. Valid Perfect Square
题目:Given a positive integer num, write a function which returns True if num is a perfect square else False.Note: Do not use any built-in library function such as sqrt.Example 1:Input: 16
2017-04-09 22:42:07 172
原创 61. Rotate List 再做一次
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL./** * Definition for singly-linked
2017-04-09 17:50:09 174
原创 160. Intersection of Two Linked Lists 要再做一遍
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2017-04-07 19:15:05 167
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人