- 博客(32)
- 收藏
- 关注
原创 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 306
原创 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 342
原创 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 384
原创 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 351
原创 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 307
原创 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 384
原创 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 417
原创 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 362
原创 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 248
原创 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 438
原创 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 363
原创 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 1014
原创 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 365
原创 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 300
原创 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 274
原创 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 224
原创 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 269
原创 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 324
原创 Leetcode题解 160. 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 ↘ c1
2016-07-14 22:35:32 267
原创 Leetcode题解 189. 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%len; 也就是说,当k比数组长度还要大时,就需要对k取余。public cla
2016-07-14 22:16:18 272
原创 Leetcode题解 24. Swap Nodes in Pairs
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. You may no
2016-07-14 22:02:39 253
原创 Leetcode题解 190. 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
2016-07-14 21:27:46 340
原创 Leetcode题解 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 linked list be
2016-07-14 21:26:53 239
原创 Leetcode题解 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example: Given a = 1 and b = 2, return 3. 用位运算来做public class Solution { public int getSum(int a, int
2016-07-14 17:35:31 366
原创 疯狂java讲义 琐碎知识点
Java的子类不能获得父类的构造器,但在子类构造器中可以调用父类构造器的初始化代码。严格讲,Java类只能有一个父类,这种说法是错误的,应该换成如下说法:Java类只能有一个直接父类。实际上,Java可以有无限多个间接父类。如果在构造器中使用super,则super用于限定该构造器初始化的是该对象从父类继承得到的实例变量,而不是该类自己定义的实例变量。在继承中,实例变量也会发生类似于函数覆盖的
2016-07-14 17:34:24 757
原创 程序员面试金典题解 有向路径检查
题目描述对于一个有向图,请实现一个算法,找出两点之间是否存在一条路径。 给定图中的两个结点的指针UndirectedGraphNode* a,UndirectedGraphNode* b(请不要在意数据类型,图是有向图),请返回一个bool,代表两点之间是否存在一条路径(a到b或b到a)。import java.util.*;/*public class UndirectedGraphNode
2016-07-10 18:22:30 357
原创 程序员面试金典题解 二叉树平衡检查
题目描述实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1。 给定指向树根结点的指针TreeNode* root,请返回一个bool,代表这棵树是否平衡。解法一:(递归解法,但是getHeight()没有任何优化,同一个节点的高度可能会计算多次) 复杂度 Nlog(N)import java.util.*;/*public class TreeN
2016-07-10 17:31:32 277
原创 程序员面试金典题解 双栈排序
题目描述请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。 给定一个int[] numbers(C++中为vector<int>),其中第一个元素为栈顶,请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只能访问到第一个元素。 测试样例:[1,2,3,4,5]返回:[5,4,3,2,1]import ja
2016-07-10 16:04:33 431
原创 程序员面试金典题解 猫狗收容所
题目描述 有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的。 给定一个操作序列int[][2] ope(C++中为vector<vector<int>>)代表所有事件。若第一个元素为1,则代表有动物进入收容所,第二个元素为动物的编号,正数代表狗,负数代表
2016-07-10 15:51:05 594
原创 程序员面试金典题解 回文链表
题目描述请编写一个函数,检查链表是否为回文。 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。 测试样例: {1,2,3,2,1} 返回:true {1,2,3,2,3} 返回:falseimport java.util.*;/*public class ListNode { int val; ListNode next = null
2016-07-04 10:53:51 312
原创 程序员面试金典题解 链式A+B
题目描述有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。 给定两个链表ListNode* A,ListNode* B,请返回A+B的结果(ListNode*)。 测试样例: {1,2,3},{3,2,1} 返回:{4,4,4}import java.util.*;/*public class L
2016-07-03 22:14:00 322
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人