@StefanPochmann
for integer in python, is ~i == i  1
always correct? is it defined by python standard?
FindRX
@FindRX
Posts made by FindRX

RE: Seven Short Solutions (1 to 7 lines)

RE: Shouldbe6Liner
@StefanPochmann
You mean the code without cache is more readable?
or the improvement is minor so it's not necessary? 
RE: Shouldbe6Liner
Why not add a cache?
class Solution(object): def generateTrees(self, n): if n < 1: return [] cache = {} def generate(first, last): if first > last: return [None] if (first, last) in cache: return cache[first, last] trees = [] for root in range(first, last+1): for left in generate(first, root1): for right in generate(root+1, last): node = TreeNode(root) node.left = left node.right = right trees += node, cache[first, last] = trees return trees return generate(1, n)

Could anyone can explain this tricky thing? Probably related to the platform.
C Solution:
int myAtoi(char* str) { for (; *str == ' ';str++); int sign = 1; if (*str == ''  *str == '+') { sign = *str++ == '' ? 1 : 1; } int limit = INT_MAX / 10; int tlimit = INT_MAX % 10; int res = 0; for (; *str >= '0' && *str <= '9'; str++) { if (res > limit  res == limit && *str  '0' > tlimit) { return sign == 1 ? INT_MAX : INT_MIN; } res = res * 10 + *str  '0'; } return sign * res; }
This solution will encounter Runtime Error.
But, if I changeres = res * 10 + *str  '0';
tores = res * 10 + (*str  '0');
, it will be accepted.Anyone has any idea?

Accepted as best, concise C solution
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ void save(int **res, int *size, int *returnSize, int val) { if (*size == *returnSize) { *size += 1000; *res = realloc(*res, *size * sizeof(int)); } (*res)[(*returnSize)++] = val; } int* findAnagrams(char* s, char* p, int* returnSize) { if (strlen(s) < strlen(p)) return 0; int size = 1000; int *res = malloc(size * sizeof(int)); *returnSize = 0; int i, diff = 0, flag[256] = {0}; for (i = 0; p[i]; i++) { if (s[i] == p[i]) continue; diff += ++flag[s[i]] > 0 ? 1 : 1; diff += flag[p[i]] < 0 ? 1 : 1; } if (!diff) save(&res, &size, returnSize, 0); int j; for (j = i; s[j]; j++) { diff += ++flag[s[j]] > 0 ? 1 : 1; diff += flag[s[j  i]] < 0 ? 1 : 1; if (!diff) save(&res, &size, returnSize, j  i + 1); } return res; }

RE: 12ms C language solution with inhouse HashSet
said in 12ms C language solution with inhouse HashSet:
int idx = val > 0 ? val : val;
This is not good in case val = INT_MIN.

RE: 4ms accepted solution with C
@flyerCheng
This is variablelength arrays which are allowed in C99.
But variablelength arrays are subsequently relegated in C11 to a conditional feature which implementations are not required to support. 
RE: The most elegant binary search(c 3ms)
@JPBlack
Forint
type, the C standard only require that it has at least 16 bits.
ifint
is represented as 16 bits, thenq = 46341
will result in overflow.
ifint
is represented as 64 bits, thenq = 46341
is not enough to get the right answer forx = 2147580964 (46342 * 46342)
.
q = x
will overflow even whenint
is 32bits. 
RE: The most elegant binary search(c 3ms)
I am not quite sure whether
q = 46341
is a good idea.
Instead of using multiplication, division can avoid the overflow.int mySqrt(int x) { int l = 1, r = INT_MAX; while (l != r  1) { int m = l + (r  l) / 2; if (x / m >= m) l = m; else r = m; } if (x / l >= l) return l; return l  1; }