LeetCode Solutions In Java
LeetCode刷题之路。
Alan_Xiang
在读学生
展开
-
22. 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:[ "((()))", "(()())", "(())()", "()(())", "原创 2017-09-21 20:39:48 · 1352 阅读 · 1 评论 -
18. 4Sum(求数组中和为指定值的4个数)
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: The solution set m原创 2017-09-20 22:27:20 · 1288 阅读 · 0 评论 -
16. 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原创 2017-09-18 19:55:31 · 1720 阅读 · 0 评论 -
17. 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"原创 2017-09-19 11:48:55 · 1251 阅读 · 0 评论 -
15. 3Sum(求数组中和为0的3个数)
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 dupli原创 2017-09-13 20:27:20 · 1920 阅读 · 0 评论 -
11. 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原创 2017-09-08 10:27:25 · 1339 阅读 · 0 评论 -
5. 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.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.原创 2017-09-05 19:40:54 · 884 阅读 · 0 评论 -
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-09-05 18:02:48 · 1140 阅读 · 2 评论 -
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 return i原创 2017-07-13 00:30:16 · 695 阅读 · 0 评论 -
463. Island Perimeter(计算岛的周长)
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely原创 2017-03-28 16:31:21 · 928 阅读 · 0 评论 -
476. Number Complement(求补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note:The given integer is guaranteed to fit within the range原创 2017-03-12 20:59:11 · 872 阅读 · 0 评论 -
500. Keyboard Row(判断单词能否由键盘上的某一行字符表示)
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.Example 1:Input: ["Hello", "Alaska", "Da原创 2017-03-12 21:24:21 · 861 阅读 · 0 评论 -
496. Next Greater Element I(寻找下一个较大的数)
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums原创 2017-03-13 12:22:46 · 1030 阅读 · 0 评论 -
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 that j is on原创 2016-12-03 23:33:39 · 953 阅读 · 0 评论 -
13. Roman to Integer (整数转罗马数字)
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.题目大意:给定[1,3999]之间的某个整数,将其转为罗马数字。解题思路:代码如下:(93ms,beats 75.93%)public class Solu原创 2016-12-01 09:45:26 · 715 阅读 · 0 评论 -
144. Binary Tree Preorder Traversal (二叉树的先序遍历)
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive soluti原创 2016-11-30 16:53:37 · 705 阅读 · 0 评论 -
378. Kth Smallest Element in a Sorted Matrix(查找矩阵中第k小的数)
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not原创 2016-11-30 16:22:35 · 657 阅读 · 0 评论 -
268. Missing Number(查找遗漏的数)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm sho原创 2016-11-29 21:14:06 · 667 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].Note: Recursive solu原创 2016-11-29 20:57:42 · 568 阅读 · 0 评论 -
392. Is Subsequence (子序列判断)
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) strin原创 2016-11-29 10:53:35 · 1198 阅读 · 0 评论 -
343. Integer Break (划分整数使乘积最大)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, ret原创 2016-11-26 21:42:09 · 1776 阅读 · 0 评论 -
384. Shuffle an Array(打乱数组)
Shuffle a set of numbers without duplicates.Example:// Init an array with set 1, 2, and 3.int[] nums = {1,2,3};Solution solution = new Solution(nums);// Shuffle the array [1,2,3] and return原创 2016-11-22 21:43:28 · 2426 阅读 · 0 评论 -
357. Count Numbers with Unique Digits (计算各位数字不都不相同的整数个数)
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x [11,22,33,44,原创 2016-11-21 18:50:54 · 1112 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II (求股票买卖最大收益)
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2016-11-20 22:19:10 · 405 阅读 · 0 评论 -
347. Top K Frequent Elements(找出数组中出现次数最多的前k个元素)
Given a non-empty array of integers, return the k most frequent elements.For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number原创 2016-11-16 11:28:34 · 2508 阅读 · 0 评论 -
238. Product of Array Except Self (计算整型数组中除了某元素之外所有元素的积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements ofnums except nums[i].Solve it without division and in O(原创 2016-11-11 23:47:01 · 729 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted (在有序数组中确定和为给定值的两个元素的下标)
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 two number原创 2016-11-11 21:41:10 · 848 阅读 · 0 评论 -
382. Linked List Random Node(返回链表中随机一个节点的值)
Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.Follow up:What if the linked list is extremely large and i原创 2016-11-12 00:32:06 · 1660 阅读 · 0 评论 -
260. Single Number III (找到数组中仅出现一次的两个数)
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given原创 2016-11-10 10:08:44 · 506 阅读 · 0 评论 -
413. Arithmetic Slices (找等差数列)
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequenc原创 2016-11-09 11:24:48 · 799 阅读 · 0 评论 -
406. Queue Reconstruction by Height(根据身高重排队列)
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of t原创 2016-11-09 00:32:15 · 1035 阅读 · 0 评论 -
338. Counting Bits(计算整数二进制表示中1的位数)
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5原创 2016-11-07 13:08:40 · 591 阅读 · 0 评论 -
419. Battleships in a Board(计算甲板上的军舰数)
Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:You receiv原创 2016-11-04 13:23:28 · 1646 阅读 · 0 评论 -
383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; ot原创 2016-10-28 11:34:11 · 710 阅读 · 0 评论 -
387. First Unique Character in a String (返回字符串中第一个不重复元素的下标)
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note:原创 2016-10-28 11:36:58 · 504 阅读 · 0 评论 -
389. Find the Difference
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was原创 2016-10-28 11:37:49 · 288 阅读 · 0 评论 -
396. Rotate Function
Given an array of integers A and let n to be its length.Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:原创 2016-10-28 11:38:45 · 299 阅读 · 0 评论 -
400. Nth Digit (找第n个数字)
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...Note:n is positive and will fit within the range of a 32-bit signed integer (n 31).Example 1:Input原创 2016-10-28 11:39:22 · 897 阅读 · 0 评论 -
401. Binary Watch (二进制手表)
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit o原创 2016-10-28 11:40:08 · 948 阅读 · 0 评论 -
404. Sum of Left Leaves (二叉树中左叶子值之和)
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24原创 2016-10-28 11:41:12 · 974 阅读 · 0 评论