/**
* Note: The returned array must be malloced, assume caller calls free().
*/
/*
从后向前(从个位)开始+1,逢九进一,不为九时结束,全为九时数组长度加一
解法如下: 特别要注意的是returnSize,这个代表返回指针的长度,要不上层代码不知道要取多少个
*/
int* plusOne(int* digits, int digitsSize, int* returnSize)
{
for (int i = digitsSize - 1; i >= 0; i--) {
if (digits[i] == 9) {
digits[i] = 0;
} else {
digits[i]++;
*returnSize = digitsSize;
return digits;
}
}
int *res = malloc(sizeof(int) * (digitsSize + 1));
memset(res, 0, sizeof(int) * (digitsSize + 1));
*returnSize = digitsSize + 1;
res[0] = 1;
return res;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* asteroidCollision(int* asteroids, int asteroidsSize, int* returnSize)
{
*returnSize = 0;
if (asteroids == NULL) {
return NULL;
}
int *res = malloc(sizeof(int) * asteroidsSize);
int idx = 0;
for (int i = 0; i < asteroidsSize; i++) {
res[idx++] = asteroids[i];
// 碰撞条件:左正右负
while (idx >= 2 && res[idx - 2] > 0 && res[idx - 1] < 0) {
int left = res[idx - 2];
int right = res[idx - 1];
if (abs(left) == abs(right)) {
idx -= 2;
} else if (abs(left) < abs(right)) {
res[idx - 2] = right;
idx--;
} else {
idx--;
}
}
}
*returnSize = idx;
return res;
}
typedef struct {
int c; // 要用int
int value;
UT_hash_handle hh;
} hashTable;
hashTable *g_hash = NULL;
hashTable* FindNode(int ic)
{
hashTable* tmp = NULL;
HASH_FIND_INT(g_hash, &ic, tmp);
return tmp;
}
void AddNode(int ic)
{
hashTable *it = FindNode(ic);
if (it == NULL) {
it = malloc(sizeof(hashTable));
it->c = ic;
HASH_ADD_INT(g_hash, c, it);
}
it->value = 1;
}
void DelNode(int ic)
{
hashTable *it = FindNode(ic);
if (it != NULL) {
HASH_DEL(g_hash, it);
it->value = 0;
free(it);
}
}
void ClearAllNode()
{
hashTable *curr, *tmp;
HASH_ITER(hh, g_hash, curr, tmp) {
HASH_DEL(g_hash, curr);
free(curr);
}
}
int lengthOfLongestSubstring(char * s)
{
if (s == NULL) {
return 0;
}
if (s[0] == '