# LeetCode Count and Say

LeetCode Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 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, generate the nth sequence.
Note: The sequence of integers will be represented as a string.

class Solution {
public:
string countAndSay(int n) {
if (n == 1)return "1";
string pre = countAndSay(n - 1);
string ans = "";
int i = 0, j;
while (i < pre.size()) {
j = i + 1;
while (j < pre.size() && pre[j] == pre[i])j++;
ans += to_string(j - i) + pre.substr(i, 1);
i = j;
}
return ans;
}
};


# LeetCode Valid Sudoku

LeetCode Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

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;
}
};


class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int m = 9;
vector<vector<int>> rows(9, vector<int>(9, 0)), cols(9, vector<int>(9, 0)), cubes(9, vector<int>(9, 0));
for(int  i = 0; i < board.size(); ++i) {
for(int j = 0; j < board[i].size(); ++j) {
if(board[i][j] != '.') {
int num = board[i][j] - '0' - 1;
int k = i / 3 * 3 + j / 3;
if(rows[i][num] > 0 || cols[j][num] > 0 || cubes[k][num] > 0) return false;
rows[i][num] = cols[j][num] = cubes[k][num] = 1;
}
}
}
return true;
}
};


# LeetCode Implement strStr()

LeetCode Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

class Solution {
public:
int strStr(string haystack, string needle) {
int n1 = haystack.size(), n2 = needle.size();
if (n2 > n1)return -1;
for (int i = 0; i < n1 - n2 + 1; i++) {
bool found = true;
for (int j = 0; j < n2; j++) {
if (needle[j] != haystack[i + j]) {
found = false;
break;
}
}
if (found)return i;
}
return -1;
}
};


class Solution {
public:
void getNextVal(vector<int>& nextval, string t) {
int n = t.size();
nextval.resize(n);
nextval = -1;
int j = 0, k = -1;
while (j < n - 1) {
if (k == -1 || t[j] == t[k]) {
j++;
k++;
if (t[j] != t[k])
nextval[j] = k;
else
nextval[j] = nextval[k];
}
else
k = nextval[k];
}
}
int strStr(string haystack, string needle) {
if (needle == "")return 0;
int n1 = haystack.size(), n2 = needle.size();
if (n2 > n1)return -1;
vector<int> nextval;
getNextVal(nextval, needle);
int i = 0, j = 0;
while (i < n1&&j < n2) {
if (j == -1 || haystack[i] == needle[j]) {
i++;
j++;
}
else
j = nextval[j];
}
if (j >= n2)
return i - n2;
else
return -1;
}
};  （t[0,...,j]的前缀要以t开头，后缀要以t[j]结尾，但都不能完全等于t[0,...,j]，即前缀不能以t[j]结尾，后缀不能以t开头。）   //优化过后的next 数组求法
void GetNextval(char* p, int next[])
{
int pLen = strlen(p);
next = -1;
int k = -1;
int j = 0;
while (j < pLen - 1)
{
//p[k]表示前缀，p[j]表示后缀
if (k == -1 || p[j] == p[k])
{
++j;
++k;
//较之前next数组求法，改动在下面4行
if (p[j] != p[k])
next[j] = k;   //之前只有这一行
else
//因为不能出现p[j] = p[ next[j ]]，所以当出现时需要继续递归，k = next[k] = next[next[k]]
next[j] = next[k];
}
else
{
k = next[k];
}
}
}


# LeetCode Divide Two Integers

LeetCode Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.

class Solution {
public:
int divide(int dividend, int divisor) {
if (divisor == 0)return INT_MAX;
if (divisor == -1 && dividend == INT_MIN)return INT_MAX;
unsigned int divd = dividend, divr = divisor;
if (dividend < 0)divd = -divd; // 先都转换为正数，方便处理
if (divisor < 0)divr = -divr;
int ans = 0;
while (divd >= divr){
unsigned long long tmp = divr; // 注意类型，防止溢出
int p = 0;
while (divd >= tmp) {
tmp <<= 1;
p++;
}
ans += 1 << (p - 1);
divd -= divr << (p - 1);
}
int sign = dividend^divisor; // 首位为符号位，如果符号相同，异或之后为正数
return sign >= 0 ? ans : -ans;
}
};


P.S.一直以来对数论的题都没底，这次在将负数转换为正数时也花了不少时间。

// (a)_2 = 1111 1111 1111 1111 1111 1111 1100 0010; (a)_10 = -62;
int a = -62;
// (b)_2 = 1111 1111 1111 1111 1111 1111 1100 0010; (b)_10 = 4294967234;
unsigned int b = a;
// (b)_2 = 0000 0000 0000 0000 0000 0000 0011 1110; (b)_10 = 62;
b = -b;


# LeetCode Remove Element

LeetCode Remove Element
Given an array and a value, 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 in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if (nums.size() == 0)return 0;
if (nums.size() == 1)return (nums != val) ? 1 : 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == val) {
for (int j = i + 1; j < nums.size(); j++) {
if (nums[j] != val) {
nums[i] = nums[j];
nums[j] = val;
break;
}
}
}
}
if (nums == val)
return 0;
else
{
int i;
for (i = nums.size() - 1; i >= 0; i--)
if (nums[i] != val)
break;
return i + 1;
}
}
};