1.
Two sum
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
*returnSize = 2;
int* result = (int*)malloc(2 * sizeof(int));
for (int i = 0; i < numsSize; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[i] + nums[j] == target) {
result[0] = i;
result[1] = j;
return result;
return NULL; // never reached because solution always exists
[Link] of two integers
int getSum(int a, int b) {
// Iterate till there is no carry
while (b != 0) {
unsigned carry = (unsigned)(a & b) << 1; // carry = common set bits of a and b
a = a ^ b; // sum of bits where at least one is not set
b = carry; // carry is shifted by one so that adding it gives the required sum
return a;
[Link] parenthesis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool isMatchingPair(char open, char close) {
return (open == '(' && close == ')') ||
(open == '{' && close == '}') ||
(open == '[' && close == ']');
bool isValid(char *s) {
if (s == NULL) return true;
int n = strlen(s);
if (n == 0) return true;
// dynamic stack sized to input length
char *stack = malloc(n * sizeof(char));
if (stack == NULL) return false; // out of memory (very unlikely on LeetCode)
int top = 0; // number of elements in stack
for (int i = 0; i < n; ++i) {
char c = s[i];
if (c == '(' || c == '{' || c == '[') {
stack[top++] = c; // push
} else if (c == ')' || c == '}' || c == ']') {
if (top == 0) { // no matching opening
free(stack);
return false;
char open = stack[--top]; // pop
if (!isMatchingPair(open, c)) {
free(stack);
return false;
// ignore other chars if any (not needed for LeetCode problem)
bool valid = (top == 0);
free(stack);
return valid;
[Link] string
#include <stdio.h>
#include <string.h>
// stack-based reversal version (matches LeetCode function name)
void reverseString(char* s, int sSize) {
char stack[100000];
int top = -1;
// Push all characters
for (int i = 0; i < sSize; i++) {
stack[++top] = s[i];
// Pop back into original string
for (int i = 0; i < sSize; i++) {
s[i] = stack[top--];
#include <stdio.h>
#include <string.h>
// stack-based reversal version (matches LeetCode function name)
void reverseString(char* s, int sSize) {
char stack[100000];
int top = -1;
// Push all characters
for (int i = 0; i < sSize; i++) {
stack[++top] = s[i];
// Pop back into original string
for (int i = 0; i < sSize; i++) {
s[i] = stack[top--];
[Link] insert position
int searchInsert(int* nums, int numsSize, int target) {
int left = 0, right = numsSize - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid; // Found the target
else if (nums[mid] < target)
left = mid + 1; // Search right half
else
right = mid - 1; // Search left half
// If not found, left is the insert position
return left;
[Link] queue using stack
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
int *stack1;
int *stack2;
int top1;
int top2;
int size;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
obj->size = 10000; // maximum input size per constraints
obj->stack1 = (int*)malloc(obj->size * sizeof(int));
obj->stack2 = (int*)malloc(obj->size * sizeof(int));
obj->top1 = -1;
obj->top2 = -1;
return obj;
void myQueuePush(MyQueue* obj, int x) {
obj->stack1[++(obj->top1)] = x;
int myQueuePop(MyQueue* obj) {
if (obj->top2 == -1) {
while (obj->top1 != -1) {
obj->stack2[++(obj->top2)] = obj->stack1[(obj->top1)--];
}
return obj->stack2[(obj->top2)--];
int myQueuePeek(MyQueue* obj) {
if (obj->top2 == -1) {
while (obj->top1 != -1) {
obj->stack2[++(obj->top2)] = obj->stack1[(obj->top1)--];
return obj->stack2[obj->top2];
bool myQueueEmpty(MyQueue* obj) {
return (obj->top1 == -1 && obj->top2 == -1);
void myQueueFree(MyQueue* obj) {
free(obj->stack1);
free(obj->stack2);
free(obj);