hcx
hcx2013 · 共 222 篇文章
Serialize and Deserialize Binary Tree
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 …
2015-11-02 11:09 · leetcode · 评论(0)
Ugly Number
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 class…
2015-11-01 11:45 · leetcode · 评论(0)
Ugly Number II
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 { public in…
2015-11-01 11:43 · leetcode · 评论(0)
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 nums = [1, 2, 1, 3, 2, 5] , return [3, 5] . public class Solution { public int[] singleNumber(int[] nums) …
2015-11-01 10:54 · leetcode · 评论(2)
Add Digits
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 - 1) % 9 + 1; …
2015-11-01 10:46 · leetcode · 评论(0)
Binary Tree Paths
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 { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = …
2015-11-01 10:31 · leetcode · 评论(0)
Number of Digit One
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) { long res = 0; in…
2015-10-31 22:33 · leetcode · 评论(0)
Implement Queue using Stacks
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> stackPush; public Sta…
2015-10-31 21:02 · leetcode · 评论(0)
Power of Two
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; } }
2015-10-31 20:52 · leetcode · 评论(0)
Kth Smallest Element in a BST
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 Solution { public int kthSmallest(Tree…
2015-10-31 20:46 · leetcode · 评论(0)
Majority Element II
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<>(); printKMajor(nums, 3, res); return re…
2015-10-31 20:36 · leetcode · 评论(0)
Basic Calculator II
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: "3+2*…
2015-10-29 15:27 · leetcode · 评论(0)
Basic Calculator
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" = 2 " 2-1 …
2015-10-29 14:44 · leetcode · 评论(0)
Invert Binary Tree
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 = x; } * } */ public class Solution { public TreeNode invertTree(TreeNode root) { if …
2015-10-29 14:32 · leetcode · 评论(0)
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 the stack is empty. Notes: You must use only standard operations of a queue -- which means only…
2015-10-29 14:20 · leetcode · 评论(0)