#include <limits.h>
#define INT_MAX_DIV_BY_10 (INT_MAX / 10)
#define INT_MAX_REM_BY_10 (INT_MAX % 10)
#define INT_MIN_DIV_BY_10 (INT_MIN / 10)
#define INT_MIN_REM_BY_10 (INT_MIN % 10)
int myAtoi(char * str){
char *p = str, c;
int s = 1;
int d;
int v = 0;
while (c = *p++) {
if (c == ' ')
continue;
if ((c == '+' || c == '-') && *p >= '0' && *p <= '9') {
if (c …
Articles in the leetcode category
"[LeetCode][322] Coin Change"
Formular: dp[i] = MIN( dp[i - coins[j]] + 1 ) (j: [0, coinSize - 1])
#include <stdio.h> #include <stdlib.h> #include <limits.h> int coinChange(int *coins, int coinSize, int amount) { int i, j; int *dp = calloc(amount + 1, sizeof(int)); int min_so_far; int prev; int result; dp[0] = 0; for …
"[LeetCode][146] LRU Cache"
class LRUCache(object): def __init__(self, capacity): self.__capacity = capacity self.__dict = {} # oldest to youngest self.__list = [] def get(self, key): if not self.__dict.has_key(key): return -1 # move key to the tail of list if key != self.__list[-1]: self.__list.remove(key) self.__list.append(key) return …
"[LeetCode][155] Min Stack"
class MinStack(object): def __init__(self): # stack with entries of (value, cur_min) self.l = [] def push(self, x): """ :type x: int :rtype: nothing """ prev_min = self.getMin() if prev_min == None or prev_min > x: cur_min = x else: cur_min = prev_min self.l.append((x, cur_min)) def pop(self): """ :rtype: nothing """ if self.l …
"[LeetCode][134] Gas Station"
Python
class Solution(object): def __canCompleteCircuit(self, remains, start): l = remains[start:] + remains[:start] tank = 0 for val in l: tank += val if tank < 0: return False return True def canCompleteCircuit(self, gas, cost): """ :type gas: List[int] :type cost: List[int] :rtype: int """ remains = map(lambda a, b : a …
"[LeetCode][125] Valid Palindrome"
bool isUpperCase(char c) { return c >= 'A' && c <= 'Z'; } bool isLowerCase(char c) { return c >= 'a' && c <= 'z'; } bool isAlpha(char c) { return isUpperCase(c) || isLowerCase(c); } bool isDigit(char c) { return c >= '0' && c <= '9'; } bool isAlphanumeric(char c) { return isAlpha(c) || isDigit(c); } bool isCaseInsensitiveEqual(char c1, char …
"[LeetCode][199] Binary Tree Right Side View"
struct QueueNode { struct TreeNode *val; struct QueueNode *next; }; struct Queue { struct QueueNode *front; struct QueueNode *rear; }; int is_empty(struct Queue *q) { return !q->front && !q->rear; } struct Queue *queue_create(void) { struct Queue *q = malloc(sizeof(struct Queue)); q->front = NULL; q->rear = NULL; return q; } void queue_free(struct Queue *q …
"[LeetCode][007] Reverse Integer"
#define MAX_NUM_OF_DIGITS 10 #define MAX_VALUE_OF_INT 0x7fffffff #define MIN_VALUE_OF_INT -0x80000000 int reverse(int x) { int digits[MAX_NUM_OF_DIGITS] = { 0 }; int sign = x > 0 ? 1 : 0; unsigned abs = sign ? x : -x; unsigned max = sign ? MAX_VALUE_OF_INT : -MIN_VALUE_OF_INT; unsigned result = 0; int i = 0; int num_digits = 0; while (abs) { digits[i++] = abs % 10; abs /= 10 …
"[LeetCode][006] Zigzag Conversion"
char *convert(char *s, int numRows) { /* string length */ int L = strlen(s); /* row numbers */ int R = numRows; /* parameter check */ if (L == 0 || R == 1) return s; /* full block numbers */ /* * one 'block' is defined as: * 0 * 1 5 * 2 4 * 3 */ int B = L / (2 * R - 2); /* remain characters */ int M …
"[LeetCode][003] Longest Substring Without Repeating Characters"
/* * 1. within range [0, i], [beg, end] is the longest substring, and * [candidate, i] is the possible longer substring. * 2. extend range to [0, i + 1], if s[i + 1] can be combined with * [candidate, i] AND if [candidate, i + 1] is longer than [beg, end], * update [beg, end]. * 3 …
"[LeetCode][001] Two Sum"
#include <stdio.h> #include <stdlib.h> #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) struct node { int index; int value; }; void list_qsort(struct node **list, int size) { struct node *anchor = list[0]; int i = 1; int j = size - 1; struct node *tmp …
"[LeetCode][002] Add Two Numbers"
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h" struct ListNode { int val; struct ListNode *next; }; /* * [in] * @nd1: linked list of nodes * @nd2: linked list of nodes * * [out] * none * * [in/out] * @carry(in): carry value of last sum * @carry(out): carry value of current sum */ static struct …