前言

这一篇博客把ugly numbers系列的题目做一个整理。这三道题正好是一个思路的循序渐进,所以放在一篇博客当中。

Ugly Number

1
2
3
4
5
6
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.

丑数是指只包含2,3,5质因数的数。因此6,8是丑数因为6=2*3,8=2*2*2,而14不是丑数因为14包含质因数7。现在写一个方法判断一个数字是否是丑数。

这题只需要将所有的2,3,5质数消去之后,余下的质数是不是1来进行判断。代码如下:

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.

Given target = 20, return false.

假设存在这样一个矩阵,该矩阵从左至右以及从上到下的值均为由小到大排列。现在输入一个目标值,要求判断该矩阵中是否存在该值。

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
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].

Note:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

假设一个整数数组中,除了两个数字以外,所有的数字都出现了两遍。要求我们找到这两个数字。

可以先参考Single Number ISingle Number II

阅读全文 »

题目要求

1
2
3
4
5
6
7
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Try to solve it in linear time/space.

Return 0 if the array contains less than 2 elements.

You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.

一个乱序的数组,找到其有序排列后相邻两个数之间最大的间隔。试着用线性时间和空间复杂度来解决这个问题。

阅读全文 »

题目要求

1
2
3
4
5
6
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 at most k transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

有一个整数数组,每一位上的值代表那一天的股票价格。现在假设最多能够进行k次交易,问最大的收入是多少?

阅读全文 »

题目要求

1
2
3
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

判断一个数字最少由几个平方数的和构成。比如12=9+1+1+1=4+4+4,那么最少的数量为3。

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.

Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Therefore, return the max sliding window as [3,3,5,5,6,7].

Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.

假设有一个数组和一个长度为k的窗口,1 ≤ k ≤ 数组长度。这个窗口每次向右移动一位。现在问该窗口在各个位置上,能够得到的子数组的最大值是多少?

阅读全文 »

题目要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. 
The valid operators are +, - and *.


Example 1
Input: "2-1-1".

((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]


Example 2
Input: "2*3-4*5"

(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Output: [-34, -14, -10, -10, 10]

现在有一个字符串形式的算式,求该算式在任意位置加上任意数量的括号后能够得出的所有可能的值。

阅读全文 »

题目要求

1
2
3
4
5
6
7
There are N children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?

假设有N个孩子站成一排,每个孩子拥有一个评估值。现在要根据以下的需求给孩子们分发糖果:

  • 每个孩子至少有一颗糖
  • 有更高评估值的孩子应当比两侧孩子得到的糖更多

问最少要发出多少颗糖?

阅读全文 »

题目要求

1
2
3
4
5
6
Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

现在有一个字符串s,将s分割为多个子字符串从而保证每个子字符串都是回数。问最少需要分割多少次。

阅读全文 »
0%