Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
Input:[1,2,3,4]Output:[24,12,8,6]
Constraint: It’s guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.
Note: Please solve it without division and in O(n).
Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
LeetCode Super Ugly Number
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.
Note:
(1) 1 is a super ugly number for any given primes.
(2) The given numbers in primes are in ascending order.
(3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.
这题是超级丑数,再也不想见到丑数了…
其实本质和LeetCode Ugly Number II没什么两样,只不过素数不局限于2,3,5了,只要用一个数组idx来存储不同素数对应的小丑数的下标。完整代码如下:
[cpp]
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
if (n == 1)return 1;
vector<int> ugly_nums = { 1 };
vector<int> idx(primes.size(), 0);
int next_ugly_num = 0;
while (ugly_nums.size() < n) {
next_ugly_num = INT_MAX;
for (int i = 0; i < primes.size(); i++) {
next_ugly_num = min(next_ugly_num, primes[i] * ugly_nums[idx[i]]);
}
for (int i = 0; i < primes.size(); i++) {
if (next_ugly_num == primes[i] * ugly_nums[idx[i]]) {
idx[i]++;
}
}
ugly_nums.push_back(next_ugly_num);
}
return next_ugly_num;
}
};
[/cpp]
本代码提交AC,用时143MS。]]>
class Solution2 {
public:
int addDigits(int num)
{
int sum = num;
while (sum > 9) {
int cur_sum = 0;
while (sum) {
cur_sum += sum % 10;
sum = sum / 10;
}
sum = cur_sum;
}
return sum;
}
};
Both dividend and divisor will be 32-bit signed integers.
The divisor will never be 0.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend == INT_MIN && divisor == -1)return INT_MAX;
if (dividend == INT_MIN && divisor == 1)return INT_MIN;
unsigned int a = dividend, b = divisor;
if (dividend < 0)
a = (~a+1);
if (divisor < 0)
b = (~b+1);
int ans = 0;
while (a >= b) {
int p = 0;
int tmp = b;
while (a > tmp && tmp < INT_MAX / 2) {
tmp <<= 1;
++p;
}
if (p > 0) {
ans += (1 << (p - 1));
a -= (tmp >> 1);
}
else if (p == 0) {
if (a >= tmp) {
++ans;
a -= tmp;
}
}
}
int sign = dividend ^ divisor;
if (sign >= 0)return ans;
else return -ans;
}
};
HDOJ 5391-Zball in Tina Town
Zball in Tina Town
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 817 Accepted Submission(s): 471
Problem Description
Tina Town is a friendly place. People there care about each other.
Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day, it becomes 1 time as large as its original size. On the second day,it will become 2 times as large as the size on the first day. On the n-th day,it will become n times as large as the size on the (n-1)-th day. Tina want to know its size on the (n-1)-th day modulo n.
Input
The first line of input contains an integer T, representing the number of cases.
The following T lines, each line contains an integer n, according to the description.
T≤$$10^5$$,2≤n≤$$10^9$$
Output
For each test case, output an integer representing the answer.
Sample Input
2
3
10
Sample Output
2
Source
BestCoder Round #51 (div.2)
Recommend
hujie | We have carefully selected several similar problems for you: 5395 5394 5393 5392 5390