- 博客(71)
- 资源 (6)
- 收藏
- 关注
原创 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:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”题意:求给定
2015-08-28 11:01:34 469
原创 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: Elements in a quad
2015-08-28 10:59:44 456
原创 LeetCode题解:Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string “23”
2015-08-28 10:58:34 451
原创 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 exactly
2015-08-28 10:57:06 434
原创 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 triplet (a,b,c) must be i
2015-08-28 10:55:52 385
原创 LeetCode题解:Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.题意:给定一个整数,将其转换为罗马数解题思路:按照转换关系换代码:import java.util.LinkedHashMap;public class Solution { pri
2015-08-28 10:54:26 440
原创 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, 0). Find two lin
2015-08-28 10:53:18 452
原创 LeetCode题解:Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.题意:给定一个字符串,找出最长的回文子串解题思路:Ma
2015-08-28 10:50:56 516
原创 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. For “
2015-08-28 10:49:01 421
原创 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 as a linke
2015-08-28 10:45:41 595
原创 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 target, where in
2015-08-28 10:40:30 1378 1
原创 LeetCode题解: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.Note: A word is defined as
2015-08-28 10:35:33 368
原创 LeetCode题解:Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?题意:上n层楼梯的方法数,每次只能走1格或2格解决思路:DP代码:publ
2015-08-28 10:31:17 370
原创 LeetCode题解:Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.题意:给定一个有序脸变,删除重复出现的元素使得所有元素只出现一次
2015-08-28 10:29:20 404
原创 LeetCode题解:Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional e
2015-08-28 10:27:27 347
原创 LeetCode题解:Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.题意:给定两颗二叉树,判断它们是否相同
2015-08-28 10:24:51 370
原创 LeetCode题解:Maximum Depth of Binary Tree
Given 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.题意:求二叉树最长路径题解:DFS代码:public class Solution {
2015-08-28 10:23:17 486
原创 LeetCode题解:Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).For example: Given binary tree {3,9,20,#,#,15,7},
2015-08-28 10:21:58 415
原创 LeetCode题解:Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.题意:合并两个有序链表,并返回新链表解题思路:按序合并……代码:public class Solution {
2015-08-28 10:19:10 389
原创 LeetCode题解:Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by
2015-08-26 20:50:47 403
原创 LeetCode题解:Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.题意:求二叉树的最小深度解决思路:找出最短路径,BFS代码:public class
2015-08-26 20:49:20 434
原创 LeetCode题解:Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example: Given the below binary tree and sum = 2
2015-08-26 20:47:44 406
原创 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 place with cons
2015-08-26 20:46:20 430
原创 LeetCode题解:Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘
2015-08-26 20:44:58 620
原创 LeetCode题解:Compare Version Numbers
Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and conta
2015-08-26 20:43:16 520
原创 LeetCode题解:Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.题意:找出数组中出现次数大于n/2的数解决思路:由于该数出现次数大于n/2,因此它和每一个相异数抵消的话,剩余的个数仍然大于0,所以对每一个数计算出现
2015-08-26 20:42:17 388
原创 LeetCode题解:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.题意:求n阶乘结果中有多少0解决思路:阶乘要出现0必然能分解为5*2,2的数量大于5的数量,所以每一个5必然有一个2匹配,所以计算5的个数即可代码:public class Solution { public int trailingZeroes(int n) {
2015-08-26 20:39:56 457
原创 LeetCode题解:Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].题意:将数组内元素向右移动k步解决思路:当前位置+k除以数组长度的余数为新的位置代码:public class
2015-08-26 20:37:35 555
原创 LeetCode题解:Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110
2015-08-26 20:35:16 429
原创 LeetCode题解:Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representation 0000000000000
2015-08-26 20:27:20 422
原创 LeetCode题解:House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses
2015-08-26 20:25:47 581
原创 LeetCode题解:Count Primes
Count the number of prime numbers less than a non-negative number, n.题意:计算质数数量解决思路:按照质数性质求代码:public class Solution { public int countPrimes(int n) { boolean notPrime[] = new boolean[n + 2]; n
2015-08-26 20:19:18 505
原创 LeetCode题解:Happy Number
Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of i
2015-08-26 20:17:58 1224
原创 LeetCode题解:Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5题意:删除链表中某个节点解决思路:……代码:public class Solution
2015-08-26 20:04:02 429
原创 LeetCode题解:Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara
2015-08-26 19:53:46 902
原创 LeetCode题解:Reverse Linked List
Reverse a singly linked list.题意:逆转单向链表解决思路:依次取得后一个节点,再改变指针指向就可以了代码:public class Solution { public ListNode reverseList(ListNode head) { return reverseListInt(head,null); } public Lis
2015-08-26 19:48:27 383
原创 LeetCode题解:Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 题意:与
2015-08-26 19:42:46 460
原创 LeetCode题解:Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB 题意:给定一个整数,按照给定的对应关系返回列标题解决思路:用一个大小为26的数组代表每个数
2015-08-26 19:40:26 475
原创 LeetCode题解:Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two
2015-08-22 08:34:36 564
原创 LeetCode题解:Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.题意:合并有序的两个链表解决思路:直接创建一个新节点按照顺序接上去就好了代码:/** * Definition for
2015-08-21 15:29:51 315
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人