
Leetcode题解
Xd_Yu
这个作者很懒,什么都没留下…
展开
-
Leetcode题解 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:F(k) = 0 * Bk[原创 2016-10-08 15:30:38 · 479 阅读 · 0 评论 -
Leetcode题解 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 /原创 2016-07-16 10:58:55 · 314 阅读 · 0 评论 -
Leetcode题解 107. 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,null,null,15,7],原创 2016-07-16 11:00:21 · 286 阅读 · 0 评论 -
Leetcode题解 119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Inte原创 2016-07-16 11:09:28 · 317 阅读 · 0 评论 -
Leetcode题解 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 whether原创 2016-06-19 22:40:09 · 301 阅读 · 0 评论 -
Leetcode题解 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 of nums except nums[i].Solve it without division and in O(n).For e原创 2016-07-16 22:28:34 · 351 阅读 · 0 评论 -
Leetcode题解 168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB public class Solut原创 2016-07-16 23:09:06 · 421 阅读 · 0 评论 -
Leetcode题解 303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange(0, 5) ->原创 2016-07-16 23:56:21 · 404 阅读 · 0 评论 -
Leetcode题解 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 based on the原创 2016-07-17 00:12:44 · 511 阅读 · 0 评论 -
Leetcode题解 118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]public class Solution { public Li原创 2016-07-16 10:57:57 · 379 阅读 · 0 评论 -
Leetcode题解 299. Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that原创 2016-06-19 17:53:37 · 278 阅读 · 0 评论 -
Leetcode题解 198. 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原创 2016-06-18 20:21:38 · 293 阅读 · 0 评论 -
Leetcode题解 112. 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原创 2016-06-18 20:57:56 · 283 阅读 · 0 评论 -
Leetcode题解 206. Reverse Linked List
Reverse a singly linked list.用三个节点保存前、中、后三个接口即可。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */p原创 2016-06-18 15:23:32 · 308 阅读 · 0 评论 -
Leetcode题解 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2016-06-18 21:34:03 · 290 阅读 · 0 评论 -
Leetcode题解 231. Power of Two
Given an integer, write a function to determine if it is a power of two.这道题让我们判断一个数是否为2的次方数,而且要求时间和空间复杂度都为常数,那么对于这种玩数字的题,我们应该首先考虑位操作 Bit Operation。在LeetCode中,位操作的题有很多,比如比如Repeated DNA Sequences 求重复的DNA原创 2016-06-19 15:03:25 · 251 阅读 · 0 评论 -
Leetcode题解 326. Power of Three
Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? 这道题让我们判断一个数是不是3的次方数,在LeetCode中,有一道类似的题目Power of Two,那道题有个非常简原创 2016-06-19 15:09:17 · 425 阅读 · 0 评论 -
Leetcode题解 342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example: Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without loops/re原创 2016-06-19 15:23:24 · 304 阅读 · 0 评论 -
Leetcode题解 66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.细节比较繁琐,感觉没啥技术含量。public class原创 2016-06-19 17:27:46 · 249 阅读 · 0 评论 -
Leetcode题解 374. Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gue-1 : My number is lower 1 : My number is higher 0 : Congrats! You got it!Example:n = 10, I pick 6.R原创 2016-07-16 23:33:24 · 1088 阅读 · 0 评论 -
Leetcode题解 165. 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原创 2016-07-17 11:49:25 · 288 阅读 · 0 评论 -
Leetcode题解 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, return 1原创 2016-07-20 18:06:37 · 370 阅读 · 0 评论 -
Leetcode题解 88. Merge Sorted Array(todo)
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原创 2016-06-19 15:30:30 · 310 阅读 · 0 评论 -
Leetcode题解 219. Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.用hashMap原创 2016-08-16 00:05:44 · 899 阅读 · 0 评论 -
Leetcode题解 122. Best Time to Buy and Sell Stock II
假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格。设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。 解题思路 在Best Time to Buy and Sell Stock系列中,和本题最像的是Best Time to Buy and Sell Stock IV Best Time to Buy原创 2016-08-17 09:45:40 · 1024 阅读 · 2 评论 -
Leetcode题解 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle [ [2], [3,4], [6,5,原创 2016-08-17 23:40:07 · 406 阅读 · 0 评论 -
Leetcode题解 321. Create Maximum Number
Create Maximum Number 题目描述Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the原创 2016-08-28 22:35:01 · 778 阅读 · 0 评论 -
Leetcode题解 15. 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: The solution set must not contain duplic原创 2016-08-18 15:58:28 · 564 阅读 · 0 评论 -
Leetcode题解 136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one.第一种解法就是《剑指offer》上的,下面是一种通用的解法:public class Solution { public static int singleNumber(int[] nums) {原创 2016-08-11 10:53:40 · 327 阅读 · 0 评论 -
Leetcode题解 347. Top K Frequent Elements
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].用桶排序来做最后处理。public class Solution { public List<Integer> topKFreque原创 2016-07-20 16:03:51 · 401 阅读 · 0 评论 -
Leetcode题解 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.解法一:(用等差数列的做法)public class Solution {原创 2016-07-20 16:02:46 · 456 阅读 · 0 评论 -
Leetcode题解14 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example: Given nums = [2, 7, 11,原创 2016-04-16 11:28:07 · 222 阅读 · 0 评论 -
疯狂java讲义 琐碎知识点
Java的子类不能获得父类的构造器,但在子类构造器中可以调用父类构造器的初始化代码。严格讲,Java类只能有一个父类,这种说法是错误的,应该换成如下说法:Java类只能有一个直接父类。实际上,Java可以有无限多个间接父类。如果在构造器中使用super,则super用于限定该构造器初始化的是该对象从父类继承得到的实例变量,而不是该类自己定义的实例变量。在继承中,实例变量也会发生类似于函数覆盖的原创 2016-07-14 17:34:24 · 842 阅读 · 0 评论 -
Leetcode题解 204. Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.厄拉多塞筛法public class Solution { public int countPrimes(int n) { int[] res=new int[n]; int count=0;原创 2016-07-18 11:34:33 · 404 阅读 · 0 评论 -
Leetcode题解 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.解法一:(暴力法)public class Solution { public static int strStr(String haystack原创 2016-07-18 12:09:21 · 463 阅读 · 0 评论 -
剑指offer题解 二叉树的镜像
题目描述操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述:二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10原创 2016-06-21 21:23:04 · 293 阅读 · 0 评论 -
Leetcode题解 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 number, fin原创 2016-07-19 22:11:28 · 458 阅读 · 0 评论 -
Leetcode题解 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?解法一:不用任何额外空间去做。/** * Definition for singly-linked list. * class ListNode { * int val原创 2016-07-19 22:21:50 · 369 阅读 · 0 评论 -
Leetcode题解 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.《程序员面试金典》原题/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode n原创 2016-07-19 22:23:22 · 520 阅读 · 0 评论 -
Leetcode题解 21. 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 singly-linked list. * public cla原创 2016-06-18 17:59:11 · 235 阅读 · 0 评论