The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211.
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1
Output: "1"
Explanation: This is the base case.
Example 2:
Input: 4
Output: "1211"
Explanation: For n = 3 the term was "21" in which we have two groups "2" and "1", "2" can be read as "12" which means frequency = 1 and value = 2, the same way "1" is read as "11", so the answer is the concatenation of "12" and "11" which is "1211".
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character '.'.
class Solution {
public:
bool isValidSudoku(vector<vector<char> >& board)
{
int m = board.size();
if (m != 9)
return false;
for (int i = 0; i < m; i++) { // 行
int n = board[i].size();
if (n != 9)
return false;
vector<int> rows(n, 0);
for (int j = 0; j < n; j++) {
if (board[i][j] != ‘.’) {
rows[board[i][j] – ‘1’]++;
if (rows[board[i][j] – ‘1’] > 1)
return false;
}
}
}
for (int j = 0; j < m; j++) { //列
vector<int> cols(m, 0);
for (int i = 0; i < m; i++) {
if (board[i][j] != ‘.’) {
cols[board[i][j] – ‘1’]++;
if (cols[board[i][j] – ‘1’] > 1)
return false;
}
}
}
for (int i = 0; i < m; i += 3) { // 小方格
for (int j = 0; j < m; j += 3) {
vector<int> cubes(m, 0);
for (int k = 0; k < m; k++) {
if (board[i + k / 3][j + k % 3] != ‘.’) {
cubes[board[i + k / 3][j + k % 3] – ‘1’]++;
if (cubes[board[i + k / 3][j + k % 3] – ‘1’] > 1)
return false;
}
}
}
}
return true;
}
};
$$next[j]=\begin{cases} \max\{k|0<k<j|t_0t_1…t_{k-1}==t_{j-k}t_{j-k+1}…t_{j-1}\}, & \mbox{if this set is nonempty}\\-1, & \mbox{if }j==0\\ 0, & \mbox{otherwise}\end{cases}$$
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;
}
};
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n = nums.size(), i = -1;
for (int j = 0; j < n; ++j) {
if (nums[j] != val)nums[++i] = nums[j];
}
return i + 1;
}
};