- 浏览: 88672 次
最新评论
-
hcx2013:
mircle 写道Given nums = [1, 2, 1, ...
Single Number III -
mircle:
Given nums = [1, 2, 1, 3, 2, 5, ...
Single Number III -
hcx2013:
恩,可以,代码更简洁
空格替换 -
QuarterLifeForJava:
这样可以吗?
public static void main( ...
空格替换 -
hcx2013:
这个2是指多出2位,按你的理解可以这么改:int len = ...
空格替换
文章列表
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize ...
Ugly Number
- 博客分类:
- leetcode
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
public cl ...
Ugly Number II
- 博客分类:
- leetcode
Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.
public class Solution {
publ ...
Single Number III
- 博客分类:
- leetcode
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 nums = [1, 2, 1, 3, 2, 5], return [3, 5].
public class Solution {
public int[] singleNumber(int[] ...
Add Digits
- 博客分类:
- leetcode
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
public class Solution {
public int addDigits(int num) {
return (num ...
Binary Tree Paths
- 博客分类:
- leetcode
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
/**
* Definition for a binary tree node.
* public class TreeNode {
* ...
Number of Digit One
- 博客分类:
- leetcode
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
public class Solution {
public int countDigitOne(int n) {
...
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
class MyQueue {
public Stack<Integer> stac ...
Power of Two
- 博客分类:
- leetcode
Given an integer, write a function to determine if it is a power of two.
public class Solution {
public boolean isPowerOfTwo(int n) {
if (n == 0) {
return false;
}
while (n % 2 == 0) {
n /= 2;
}
return n == 1 ? true : false;
...
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class So ...
Majority Element II
- 博客分类:
- leetcode
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<>();
...
Basic Calculator II
- 博客分类:
- leetcode
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
" ...
Basic Calculator
- 博客分类:
- leetcode
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" ...
Invert Binary Tree
- 博客分类:
- leetcode
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val ...
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 the stack is empty.
Notes:
You must use only standard operations of a queue -- which me ...