Datasets:
id
int64 0
14.1k
| problem_id
int64 1
1.31k
| problem_title
stringclasses 441
values | difficulty
stringclasses 3
values | c_source
stringclasses 441
values | architecture
stringclasses 4
values | optimization
stringclasses 4
values | compiler
stringclasses 8
values | assembly
stringlengths 31
174k
|
|---|---|---|---|---|---|---|---|---|
0
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O0
|
ARM64 gcc 15.2.0
|
cmp:
sub sp, sp, #16
str x0, [sp, 8]
str x1, [sp]
ldr x0, [sp, 8]
ldr w1, [x0]
ldr x0, [sp]
ldr w0, [x0]
sub w0, w1, w0
add sp, sp, 16
ret
twoSum:
stp x29, x30, [sp, -64]!
mov x29, sp
str x0, [sp, 24]
str w1, [sp, 20]
str w2, [sp, 16]
ldr w0, [sp, 20]
add w0, w0, 1
sxtw x0, w0
lsl x0, x0, 3
bl malloc
str x0, [sp, 48]
str wzr, [sp, 60]
b .L4
.L5:
ldrsw x0, [sp, 60]
lsl x0, x0, 2
ldr x1, [sp, 24]
add x1, x1, x0
ldrsw x0, [sp, 60]
lsl x0, x0, 3
ldr x2, [sp, 48]
add x0, x2, x0
ldr w1, [x1]
str w1, [x0]
ldrsw x0, [sp, 60]
lsl x0, x0, 3
ldr x1, [sp, 48]
add x0, x1, x0
ldr w1, [sp, 60]
str w1, [x0, 4]
ldr w0, [sp, 60]
add w0, w0, 1
str w0, [sp, 60]
.L4:
ldr w1, [sp, 60]
ldr w0, [sp, 20]
cmp w1, w0
blt .L5
ldrsw x1, [sp, 20]
adrp x0, cmp
add x3, x0, :lo12:cmp
mov x2, 8
ldr x0, [sp, 48]
bl qsort
str wzr, [sp, 60]
ldr w0, [sp, 20]
sub w0, w0, #1
str w0, [sp, 56]
b .L6
.L10:
ldrsw x0, [sp, 60]
lsl x0, x0, 3
ldr x1, [sp, 48]
add x0, x1, x0
ldr w1, [x0]
ldrsw x0, [sp, 56]
lsl x0, x0, 3
ldr x2, [sp, 48]
add x0, x2, x0
ldr w0, [x0]
add w0, w1, w0
str w0, [sp, 44]
ldr w1, [sp, 44]
ldr w0, [sp, 16]
cmp w1, w0
bne .L7
mov x0, 8
bl malloc
str x0, [sp, 32]
ldrsw x0, [sp, 60]
lsl x0, x0, 3
ldr x1, [sp, 48]
add x0, x1, x0
ldr w1, [x0, 4]
ldr x0, [sp, 32]
str w1, [x0]
ldrsw x0, [sp, 56]
lsl x0, x0, 3
ldr x1, [sp, 48]
add x1, x1, x0
ldr x0, [sp, 32]
add x0, x0, 4
ldr w1, [x1, 4]
str w1, [x0]
ldr x0, [sp, 48]
bl free
ldr x0, [sp, 32]
b .L8
.L7:
ldr w1, [sp, 44]
ldr w0, [sp, 16]
cmp w1, w0
bge .L9
ldr w0, [sp, 60]
add w0, w0, 1
str w0, [sp, 60]
b .L6
.L9:
ldr w0, [sp, 56]
sub w0, w0, #1
str w0, [sp, 56]
.L6:
ldr w1, [sp, 60]
ldr w0, [sp, 56]
cmp w1, w0
blt .L10
ldr x0, [sp, 48]
bl free
mov x0, 0
.L8:
ldp x29, x30, [sp], 64
ret
|
1
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O1
|
ARM64 gcc 15.2.0
|
cmp:
ldr w2, [x0]
ldr w0, [x1]
sub w0, w2, w0
ret
twoSum:
stp x29, x30, [sp, -48]!
mov x29, sp
stp x19, x20, [sp, 16]
stp x21, x22, [sp, 32]
mov x22, x0
mov w20, w1
mov w21, w2
add w0, w1, 1
sbfiz x0, x0, 3, 32
bl malloc
mov x19, x0
cmp w20, 0
ble .L3
mov x3, x0
sxtw x0, w20
mov x1, 0
.L4:
ldr w2, [x22, x1, lsl 2]
stp w2, w1, [x3], 8
add x1, x1, 1
cmp x1, x0
bne .L4
adrp x3, cmp
add x3, x3, :lo12:cmp
mov x2, 8
mov x0, x19
bl qsort
sub w0, w20, #1
cmp w0, 0
ble .L5
mov w2, 0
b .L10
.L14:
mov x0, 8
bl malloc
mov x21, x0
ldr w0, [x22, 4]
ldr w1, [x20, 4]
stp w0, w1, [x21]
mov x0, x19
bl free
b .L2
.L8:
sub w0, w0, #1
.L9:
cmp w2, w0
bge .L5
.L10:
sbfiz x1, x2, 3, 32
add x22, x19, x1
sbfiz x3, x0, 3, 32
add x20, x19, x3
ldr w1, [x19, x1]
ldr w3, [x19, x3]
add w1, w1, w3
cmp w1, w21
beq .L14
bge .L8
add w2, w2, 1
b .L9
.L3:
adrp x3, cmp
add x3, x3, :lo12:cmp
mov x2, 8
sxtw x1, w20
bl qsort
.L5:
mov x0, x19
bl free
mov x21, 0
.L2:
mov x0, x21
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 48
ret
|
2
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O2
|
ARM64 gcc 15.2.0
|
cmp:
ldr w2, [x0]
ldr w0, [x1]
sub w0, w2, w0
ret
twoSum:
stp x29, x30, [sp, -48]!
mov x29, sp
stp x21, x22, [sp, 32]
mov x22, x0
add w0, w1, 1
stp x19, x20, [sp, 16]
mov w20, w1
mov w21, w2
sbfiz x0, x0, 3, 32
bl malloc
sxtw x4, w20
mov x19, x0
cmp w20, 0
ble .L4
mov x1, 0
.L5:
ldr w2, [x22, x1, lsl 2]
stp w2, w1, [x0], 8
add x1, x1, 1
cmp x1, x4
bne .L5
mov x0, x19
adrp x3, cmp
mov x2, 8
add x3, x3, :lo12:cmp
bl qsort
subs w1, w20, #1
beq .L7
mov w3, 0
b .L12
.L21:
add w3, w3, 1
cmp w1, w3
ble .L7
.L12:
ubfiz x0, x3, 3, 32
ubfiz x2, x1, 3, 32
add x22, x19, x0
add x20, x19, x2
ldr w0, [x19, x0]
ldr w2, [x19, x2]
add w0, w0, w2
cmp w0, w21
beq .L20
blt .L21
sub w1, w1, #1
cmp w1, w3
bgt .L12
.L7:
mov x0, x19
mov x21, 0
bl free
mov x0, x21
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 48
ret
.L20:
mov x0, 8
bl malloc
mov x21, x0
ldr w1, [x20, 4]
ldr w0, [x22, 4]
stp w0, w1, [x21]
mov x0, x19
bl free
mov x0, x21
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 48
ret
.L4:
mov x1, x4
adrp x3, cmp
mov x2, 8
add x3, x3, :lo12:cmp
bl qsort
b .L7
|
3
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O3
|
ARM64 gcc 15.2.0
|
cmp:
ldr w2, [x0]
ldr w0, [x1]
sub w0, w2, w0
ret
twoSum:
stp x29, x30, [sp, -64]!
mov x29, sp
str x23, [sp, 48]
mov x23, x0
add w0, w1, 1
stp x21, x22, [sp, 32]
mov w22, w1
mov w21, w2
sbfiz x0, x0, 3, 32
stp x19, x20, [sp, 16]
bl malloc
sxtw x1, w22
mov x19, x0
cmp w22, 0
ble .L4
sub w20, w22, #1
cmp w20, 2
bls .L16
adrp x2, .LC0
lsr w3, w22, 2
movi v29.4s, 0x4
mov x0, x23
ldr q31, [x2, #:lo12:.LC0]
add x3, x23, w3, uxtw 4
mov x2, x19
.L6:
ldr q30, [x0], 16
st2 {v30.4s - v31.4s}, [x2], 32
add v31.4s, v31.4s, v29.4s
cmp x3, x0
bne .L6
and w0, w22, -4
tst x22, 3
beq .L7
.L5:
ubfiz x2, x0, 3, 32
add x3, x19, w0, uxtw 3
ldr w5, [x23, w0, uxtw 2]
add w4, w0, 1
str w5, [x19, x2]
str w0, [x3, 4]
cmp w22, w4
ble .L8
add x23, x23, w0, uxtw 2
add x3, x2, 8
add x5, x19, x3
add w0, w0, 2
ldr w6, [x23, 4]
str w6, [x19, x3]
str w4, [x5, 4]
cmp w22, w0
ble .L7
add x2, x2, 16
ldr w4, [x23, 8]
add x3, x19, x2
str w4, [x19, x2]
str w0, [x3, 4]
.L7:
mov x0, x19
adrp x3, cmp
mov x2, 8
add x3, x3, :lo12:cmp
bl qsort
mov w3, w20
.L15:
mov w4, 0
b .L14
.L27:
add w4, w4, 1
cmp w3, w4
ble .L9
.L14:
ubfiz x2, x4, 3, 32
ubfiz x1, x3, 3, 32
add x20, x19, x1
add x22, x19, x2
ldr w0, [x19, x2]
ldr w1, [x19, x1]
add w0, w0, w1
cmp w0, w21
beq .L26
blt .L27
sub w3, w3, #1
cmp w3, w4
bgt .L14
.L9:
mov x0, x19
mov x21, 0
bl free
ldr x23, [sp, 48]
mov x0, x21
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 64
ret
.L8:
mov x0, x19
adrp x3, cmp
mov x2, 8
add x3, x3, :lo12:cmp
bl qsort
mov w3, w20
cbnz w20, .L15
b .L9
.L26:
mov x0, 8
bl malloc
mov x21, x0
ldr w2, [x20, 4]
ldr w1, [x22, 4]
mov x0, x19
stp w1, w2, [x21]
bl free
ldr x23, [sp, 48]
mov x0, x21
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 64
ret
.L4:
adrp x3, cmp
mov x2, 8
add x3, x3, :lo12:cmp
bl qsort
b .L9
.L16:
mov w0, 0
b .L5
.LC0:
.word 0
.word 1
.word 2
.word 3
|
4
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O0
|
armv8-a clang 21.1.0
|
cmp:
sub sp, sp, #16
str x0, [sp, #8]
str x1, [sp]
ldr x8, [sp, #8]
ldr w8, [x8]
ldr x9, [sp]
ldr w9, [x9]
subs w0, w8, w9
add sp, sp, #16
ret
twoSum:
sub sp, sp, #80
stp x29, x30, [sp, #64]
add x29, sp, #64
stur x0, [x29, #-16]
stur w1, [x29, #-20]
stur w2, [x29, #-24]
ldur w8, [x29, #-20]
add w9, w8, #1
mov w8, w9
sxtw x8, w8
lsl x0, x8, #3
bl malloc
str x0, [sp, #8]
str wzr, [sp, #28]
b .LBB1_1
.LBB1_1:
ldr w8, [sp, #28]
ldur w9, [x29, #-20]
subs w8, w8, w9
b.ge .LBB1_4
b .LBB1_2
.LBB1_2:
ldur x8, [x29, #-16]
ldrsw x9, [sp, #28]
ldr w8, [x8, x9, lsl #2]
ldr x9, [sp, #8]
ldrsw x10, [sp, #28]
lsl x10, x10, #3
str w8, [x9, x10]
ldr w8, [sp, #28]
ldr x9, [sp, #8]
ldrsw x10, [sp, #28]
add x9, x9, x10, lsl #3
str w8, [x9, #4]
b .LBB1_3
.LBB1_3:
ldr w8, [sp, #28]
add w8, w8, #1
str w8, [sp, #28]
b .LBB1_1
.LBB1_4:
ldr x0, [sp, #8]
ldursw x1, [x29, #-20]
mov x2, #8
adrp x3, cmp
add x3, x3, :lo12:cmp
bl qsort
str wzr, [sp, #28]
ldur w8, [x29, #-20]
subs w8, w8, #1
str w8, [sp, #24]
b .LBB1_5
.LBB1_5:
ldr w8, [sp, #28]
ldr w9, [sp, #24]
subs w8, w8, w9
b.ge .LBB1_13
b .LBB1_6
.LBB1_6:
ldr x8, [sp, #8]
ldrsw x9, [sp, #28]
lsl x9, x9, #3
ldr w8, [x8, x9]
ldr x9, [sp, #8]
ldrsw x10, [sp, #24]
lsl x10, x10, #3
ldr w9, [x9, x10]
add w8, w8, w9
str w8, [sp, #20]
ldr w8, [sp, #20]
ldur w9, [x29, #-24]
subs w8, w8, w9
b.ne .LBB1_8
b .LBB1_7
.LBB1_7:
mov x0, #8
bl malloc
str x0, [sp, #32]
ldr x8, [sp, #8]
ldrsw x9, [sp, #28]
add x8, x8, x9, lsl #3
ldr w8, [x8, #4]
ldr x9, [sp, #32]
str w8, [x9]
ldr x8, [sp, #8]
ldrsw x9, [sp, #24]
add x8, x8, x9, lsl #3
ldr w8, [x8, #4]
ldr x9, [sp, #32]
str w8, [x9, #4]
ldr x0, [sp, #8]
bl free
ldr x8, [sp, #32]
stur x8, [x29, #-8]
b .LBB1_14
.LBB1_8:
ldr w8, [sp, #20]
ldur w9, [x29, #-24]
subs w8, w8, w9
b.ge .LBB1_10
b .LBB1_9
.LBB1_9:
ldr w8, [sp, #28]
add w8, w8, #1
str w8, [sp, #28]
b .LBB1_11
.LBB1_10:
ldr w8, [sp, #24]
subs w8, w8, #1
str w8, [sp, #24]
b .LBB1_11
.LBB1_11:
b .LBB1_12
.LBB1_12:
b .LBB1_5
.LBB1_13:
ldr x0, [sp, #8]
bl free
stur xzr, [x29, #-8]
b .LBB1_14
.LBB1_14:
ldur x0, [x29, #-8]
ldp x29, x30, [sp, #64]
add sp, sp, #80
ret
|
5
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O1
|
armv8-a clang 21.1.0
|
cmp:
ldr w8, [x0]
ldr w9, [x1]
sub w0, w8, w9
ret
twoSum:
stp x29, x30, [sp, #-48]!
stp x22, x21, [sp, #16]
stp x20, x19, [sp, #32]
mov x29, sp
mov w8, w1
mov x22, x0
mov w20, w2
sbfiz x8, x8, #3, #32
mov w21, w1
add x0, x8, #8
bl malloc
cmp w21, #1
mov x19, x0
b.lt .LBB1_3
mov x8, xzr
mov w9, w21
add x10, x19, #4
.LBB1_2:
ldr w11, [x22, x8, lsl #2]
stp w11, w8, [x10, #-4]
add x8, x8, #1
add x10, x10, #8
cmp x9, x8
b.ne .LBB1_2
.LBB1_3:
sxtw x1, w21
adrp x3, cmp
add x3, x3, :lo12:cmp
mov x0, x19
mov w2, #8
bl qsort
subs w8, w21, #1
b.le .LBB1_7
mov w9, wzr
.LBB1_5:
ubfiz x10, x9, #3, #32
sbfiz x11, x8, #3, #32
sxtw x21, w8
mov w22, w9
ldr w10, [x19, x10]
ldr w11, [x19, x11]
add w10, w11, w10
cmp w10, w20
b.eq .LBB1_8
cset w8, ge
cinc w9, w22, lt
sub w8, w21, w8
cmp w9, w8
b.lt .LBB1_5
.LBB1_7:
mov x20, xzr
b .LBB1_9
.LBB1_8:
mov w0, #8
bl malloc
add x8, x19, x22, lsl #3
add x9, x19, x21, lsl #3
mov x20, x0
ldr w8, [x8, #4]
ldr w9, [x9, #4]
stp w8, w9, [x0]
.LBB1_9:
mov x0, x19
bl free
mov x0, x20
ldp x20, x19, [sp, #32]
ldp x22, x21, [sp, #16]
ldp x29, x30, [sp], #48
ret
|
6
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O2
|
armv8-a clang 21.1.0
|
cmp:
ldr w8, [x0]
ldr w9, [x1]
sub w0, w8, w9
ret
.LCPI1_0:
.word 0
.word 1
.word 2
.word 3
twoSum:
stp x29, x30, [sp, #-48]!
stp x22, x21, [sp, #16]
stp x20, x19, [sp, #32]
mov x29, sp
mov w21, w1
mov x22, x0
mov w20, w2
sbfiz x8, x21, #3, #32
add x0, x8, #8
bl malloc
cmp w21, #0
mov x19, x0
b.le .LBB1_3
cmp w21, #8
mov w1, w21
b.hs .LBB1_4
mov x8, xzr
b .LBB1_7
.LBB1_3:
sxtw x1, w21
adrp x3, cmp
add x3, x3, :lo12:cmp
mov x0, x19
mov w2, #8
bl qsort
b .LBB1_13
.LBB1_4:
movi v0.4s, #4
movi v1.4s, #8
adrp x9, .LCPI1_0
and x8, x1, #0x7ffffff8
ldr q3, [x9, :lo12:.LCPI1_0]
add x9, x19, #32
add x10, x22, #16
mov x11, x8
.LBB1_5:
add v5.4s, v3.4s, v0.4s
sub x12, x9, #32
subs x11, x11, #8
ldp q2, q4, [x10, #-16]
add x10, x10, #32
st2 { v2.4s, v3.4s }, [x12]
add v3.4s, v3.4s, v1.4s
st2 { v4.4s, v5.4s }, [x9]
add x9, x9, #64
b.ne .LBB1_5
cmp x8, x1
b.eq .LBB1_9
.LBB1_7:
add x9, x19, x8, lsl #3
add x9, x9, #4
.LBB1_8:
ldr w10, [x22, x8, lsl #2]
stp w10, w8, [x9, #-4]
add x8, x8, #1
add x9, x9, #8
cmp x1, x8
b.ne .LBB1_8
.LBB1_9:
adrp x3, cmp
add x3, x3, :lo12:cmp
mov x0, x19
mov w2, #8
bl qsort
subs w8, w21, #1
b.eq .LBB1_13
mov w9, wzr
.LBB1_11:
ubfiz x10, x9, #3, #32
sbfiz x11, x8, #3, #32
sxtw x21, w8
mov w22, w9
ldr w10, [x19, x10]
ldr w11, [x19, x11]
add w10, w11, w10
cmp w10, w20
b.eq .LBB1_15
cset w8, ge
cinc w9, w22, lt
sub w8, w21, w8
cmp w9, w8
b.lt .LBB1_11
.LBB1_13:
mov x20, xzr
.LBB1_14:
mov x0, x19
bl free
mov x0, x20
ldp x20, x19, [sp, #32]
ldp x22, x21, [sp, #16]
ldp x29, x30, [sp], #48
ret
.LBB1_15:
mov w0, #8
bl malloc
add x8, x19, x22, lsl #3
add x9, x19, x21, lsl #3
mov x20, x0
ldr w8, [x8, #4]
ldr w9, [x9, #4]
stp w8, w9, [x0]
b .LBB1_14
|
7
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
aarch64
|
-O3
|
armv8-a clang 21.1.0
|
cmp:
ldr w8, [x0]
ldr w9, [x1]
sub w0, w8, w9
ret
.LCPI1_0:
.word 0
.word 1
.word 2
.word 3
twoSum:
stp x29, x30, [sp, #-48]!
stp x22, x21, [sp, #16]
stp x20, x19, [sp, #32]
mov x29, sp
mov w21, w1
mov x22, x0
mov w20, w2
sbfiz x8, x21, #3, #32
add x0, x8, #8
bl malloc
cmp w21, #0
mov x19, x0
b.le .LBB1_3
cmp w21, #8
mov w1, w21
b.hs .LBB1_4
mov x8, xzr
b .LBB1_7
.LBB1_3:
sxtw x1, w21
adrp x3, cmp
add x3, x3, :lo12:cmp
mov x0, x19
mov w2, #8
bl qsort
b .LBB1_10
.LBB1_4:
movi v0.4s, #4
movi v1.4s, #8
adrp x9, .LCPI1_0
and x8, x1, #0x7ffffff8
ldr q3, [x9, :lo12:.LCPI1_0]
add x9, x19, #32
add x10, x22, #16
mov x11, x8
.LBB1_5:
add v5.4s, v3.4s, v0.4s
sub x12, x9, #32
subs x11, x11, #8
ldp q2, q4, [x10, #-16]
add x10, x10, #32
st2 { v2.4s, v3.4s }, [x12]
add v3.4s, v3.4s, v1.4s
st2 { v4.4s, v5.4s }, [x9]
add x9, x9, #64
b.ne .LBB1_5
cmp x8, x1
b.eq .LBB1_9
.LBB1_7:
add x9, x19, x8, lsl #3
add x9, x9, #4
.LBB1_8:
ldr w10, [x22, x8, lsl #2]
stp w10, w8, [x9, #-4]
add x8, x8, #1
add x9, x9, #8
cmp x1, x8
b.ne .LBB1_8
.LBB1_9:
adrp x3, cmp
add x3, x3, :lo12:cmp
mov x0, x19
mov w2, #8
bl qsort
subs w8, w21, #1
b.ne .LBB1_12
.LBB1_10:
mov x20, xzr
.LBB1_11:
mov x0, x19
bl free
mov x0, x20
ldp x20, x19, [sp, #32]
ldp x22, x21, [sp, #16]
ldp x29, x30, [sp], #48
ret
.LBB1_12:
mov x21, xzr
b .LBB1_14
.LBB1_13:
add x21, x21, #1
cmp w21, w8
b.ge .LBB1_10
.LBB1_14:
lsl x9, x21, #3
sbfiz x10, x8, #3, #32
ldr w9, [x19, x9]
ldr w10, [x19, x10]
add w9, w10, w9
cmp w9, w20
b.eq .LBB1_17
b.lt .LBB1_13
sub w8, w8, #1
cmp w21, w8
b.lt .LBB1_14
b .LBB1_10
.LBB1_17:
mov w0, #8
sxtw x22, w8
bl malloc
add x8, x19, x21, lsl #3
add x9, x19, x22, lsl #3
mov x20, x0
ldr w8, [x8, #4]
ldr w9, [x9, #4]
stp w8, w9, [x0]
b .LBB1_11
|
8
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O0
|
mips64 clang 21.1.0
|
cmp:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -32
sd $ra, 24($sp)
sd $fp, 16($sp)
move $fp, $sp
sd $4, 8($fp)
sd $5, 0($fp)
ld $1, 8($fp)
lw $1, 0($1)
ld $2, 0($fp)
lw $2, 0($2)
subu $1, $1, $2
move $2, $1
move $sp, $fp
ld $fp, 16($sp)
ld $ra, 24($sp)
daddiu $sp, $sp, 32
jr $ra
nop
twoSum:
.Lfunc_begin1 = .Ltmp3
daddiu $sp, $sp, -96
sd $ra, 88($sp)
sd $fp, 80($sp)
sd $gp, 72($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(twoSum)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(twoSum)))
sd $gp, 8($fp)
move $1, $6
move $2, $5
sd $4, 56($fp)
sw $2, 52($fp)
sw $1, 48($fp)
lw $1, 52($fp)
dsll $1, $1, 3
daddiu $4, $1, 8
ld $25, %call16(malloc)($gp)
jalr $25
nop
sd $2, 16($fp)
sw $zero, 36($fp)
b .LBB1_1
nop
.LBB1_1:
lw $1, 36($fp)
lw $2, 52($fp)
slt $1, $1, $2
beqz $1, .LBB1_5
nop
b .LBB1_3
nop
.LBB1_3:
ld $1, 56($fp)
lw $3, 36($fp)
dsll $2, $3, 2
daddu $1, $1, $2
lw $1, 0($1)
ld $2, 16($fp)
dsll $3, $3, 3
daddu $2, $2, $3
sw $1, 0($2)
lw $1, 36($fp)
ld $2, 16($fp)
sll $3, $1, 0
dsll $3, $3, 3
daddu $2, $2, $3
sw $1, 4($2)
b .LBB1_4
nop
.LBB1_4:
lw $1, 36($fp)
addiu $1, $1, 1
sw $1, 36($fp)
b .LBB1_1
nop
.LBB1_5:
ld $gp, 8($fp)
ld $4, 16($fp)
lw $5, 52($fp)
ld $7, %got_disp(cmp)($gp)
ld $25, %call16(qsort)($gp)
daddiu $6, $zero, 8
jalr $25
nop
sw $zero, 36($fp)
lw $1, 52($fp)
addiu $1, $1, -1
sw $1, 32($fp)
b .LBB1_6
nop
.LBB1_6:
lw $1, 36($fp)
lw $2, 32($fp)
slt $1, $1, $2
beqz $1, .LBB1_17
nop
b .LBB1_8
nop
.LBB1_8:
ld $2, 16($fp)
lw $1, 36($fp)
dsll $1, $1, 3
daddu $1, $2, $1
lw $1, 0($1)
lw $3, 32($fp)
dsll $3, $3, 3
daddu $2, $2, $3
lw $2, 0($2)
addu $1, $1, $2
sw $1, 28($fp)
lw $1, 28($fp)
lw $2, 48($fp)
bne $1, $2, .LBB1_11
nop
b .LBB1_10
nop
.LBB1_10:
ld $gp, 8($fp)
ld $25, %call16(malloc)($gp)
daddiu $4, $zero, 8
ld $gp, 8($fp)
jalr $25
nop
sd $2, 40($fp)
ld $1, 16($fp)
lw $2, 36($fp)
dsll $2, $2, 3
daddu $1, $1, $2
lw $1, 4($1)
ld $2, 40($fp)
sw $1, 0($2)
ld $1, 16($fp)
lw $2, 32($fp)
dsll $2, $2, 3
daddu $1, $1, $2
lw $1, 4($1)
ld $2, 40($fp)
sw $1, 4($2)
ld $4, 16($fp)
ld $25, %call16(free)($gp)
jalr $25
nop
ld $1, 40($fp)
sd $1, 64($fp)
b .LBB1_18
nop
.LBB1_11:
lw $1, 28($fp)
lw $2, 48($fp)
slt $1, $1, $2
beqz $1, .LBB1_14
nop
b .LBB1_13
nop
.LBB1_13:
lw $1, 36($fp)
addiu $1, $1, 1
sw $1, 36($fp)
b .LBB1_15
nop
.LBB1_14:
lw $1, 32($fp)
addiu $1, $1, -1
sw $1, 32($fp)
b .LBB1_15
nop
.LBB1_15:
b .LBB1_16
nop
.LBB1_16:
b .LBB1_6
nop
.LBB1_17:
ld $gp, 8($fp)
ld $4, 16($fp)
ld $25, %call16(free)($gp)
jalr $25
nop
daddiu $1, $zero, 0
sd $zero, 64($fp)
b .LBB1_18
nop
.LBB1_18:
ld $2, 64($fp)
move $sp, $fp
ld $gp, 72($sp)
ld $fp, 80($sp)
ld $ra, 88($sp)
daddiu $sp, $sp, 96
jr $ra
nop
|
9
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O1
|
mips64 clang 21.1.0
|
cmp:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lw $1, 0($5)
lw $2, 0($4)
subu $2, $2, $1
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
twoSum:
.Lfunc_begin1 = .Ltmp3
daddiu $sp, $sp, -64
sd $ra, 56($sp)
sd $fp, 48($sp)
sd $gp, 40($sp)
sd $20, 32($sp)
sd $19, 24($sp)
sd $18, 16($sp)
sd $17, 8($sp)
sd $16, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(twoSum)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(twoSum)))
move $17, $6
move $18, $5
move $19, $4
dsll $1, $5, 3
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $1, 8
move $16, $2
blez $18, .LBB1_3
sll $5, $18, 0
daddiu $2, $16, 4
dsll $1, $5, 3
daddu $3, $2, $1
addiu $4, $zero, 0
.LBB1_2:
lw $1, 0($19)
sw $4, 0($2)
sw $1, -4($2)
daddiu $19, $19, 4
daddiu $2, $2, 8
bne $2, $3, .LBB1_2
addiu $4, $4, 1
.LBB1_3:
ld $7, %got_disp(cmp)($gp)
ld $25, %call16(qsort)($gp)
move $4, $16
jalr $25
daddiu $6, $zero, 8
slti $1, $18, 2
bnez $1, .LBB1_9
daddiu $19, $zero, 0
addiu $2, $18, -1
addiu $3, $zero, 0
.LBB1_5:
sll $1, $3, 0
dsll $1, $1, 3
daddu $18, $16, $1
lw $1, 0($18)
sll $4, $2, 0
dsll $4, $4, 3
daddu $20, $16, $4
lw $4, 0($20)
addu $4, $4, $1
beq $4, $17, .LBB1_8
nop
slt $1, $4, $17
addu $3, $3, $1
xori $1, $1, 1
subu $2, $2, $1
slt $1, $3, $2
bnez $1, .LBB1_5
nop
b .LBB1_9
nop
.LBB1_8:
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $zero, 8
move $19, $2
lw $1, 4($18)
sw $1, 0($2)
lw $1, 4($20)
sw $1, 4($2)
.LBB1_9:
ld $25, %call16(free)($gp)
jalr $25
move $4, $16
move $2, $19
move $sp, $fp
ld $16, 0($sp)
ld $17, 8($sp)
ld $18, 16($sp)
ld $19, 24($sp)
ld $20, 32($sp)
ld $gp, 40($sp)
ld $fp, 48($sp)
ld $ra, 56($sp)
jr $ra
daddiu $sp, $sp, 64
|
10
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O2
|
mips64 clang 21.1.0
|
cmp:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lw $1, 0($5)
lw $2, 0($4)
subu $2, $2, $1
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
twoSum:
.Lfunc_begin1 = .Ltmp3
daddiu $sp, $sp, -64
sd $ra, 56($sp)
sd $fp, 48($sp)
sd $gp, 40($sp)
sd $20, 32($sp)
sd $19, 24($sp)
sd $18, 16($sp)
sd $17, 8($sp)
sd $16, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(twoSum)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(twoSum)))
move $17, $6
move $18, $5
move $19, $4
dsll $1, $5, 3
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $1, 8
blez $18, .LBB1_8
move $16, $2
daddiu $2, $16, 4
sll $5, $18, 0
dsll $1, $5, 3
daddu $3, $2, $1
addiu $4, $zero, 0
.LBB1_2:
lw $1, 0($19)
sw $4, 0($2)
sw $1, -4($2)
daddiu $19, $19, 4
daddiu $2, $2, 8
bne $2, $3, .LBB1_2
addiu $4, $4, 1
ld $7, %got_disp(cmp)($gp)
ld $25, %call16(qsort)($gp)
move $4, $16
jalr $25
daddiu $6, $zero, 8
addiu $1, $zero, 1
beq $18, $1, .LBB1_10
daddiu $19, $zero, 0
addiu $2, $18, -1
addiu $3, $zero, 0
.LBB1_5:
sll $1, $3, 0
dsll $1, $1, 3
daddu $18, $16, $1
lw $1, 0($18)
sll $4, $2, 0
dsll $4, $4, 3
daddu $20, $16, $4
lw $4, 0($20)
addu $4, $4, $1
beq $4, $17, .LBB1_9
nop
slt $1, $4, $17
addu $3, $3, $1
xori $1, $1, 1
subu $2, $2, $1
slt $1, $3, $2
bnez $1, .LBB1_5
nop
b .LBB1_10
nop
.LBB1_8:
sll $5, $18, 0
ld $7, %got_disp(cmp)($gp)
ld $25, %call16(qsort)($gp)
move $4, $16
jalr $25
daddiu $6, $zero, 8
b .LBB1_10
daddiu $19, $zero, 0
.LBB1_9:
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $zero, 8
move $19, $2
lw $1, 4($18)
sw $1, 0($2)
lw $1, 4($20)
sw $1, 4($2)
.LBB1_10:
ld $25, %call16(free)($gp)
jalr $25
move $4, $16
move $2, $19
move $sp, $fp
ld $16, 0($sp)
ld $17, 8($sp)
ld $18, 16($sp)
ld $19, 24($sp)
ld $20, 32($sp)
ld $gp, 40($sp)
ld $fp, 48($sp)
ld $ra, 56($sp)
jr $ra
daddiu $sp, $sp, 64
|
11
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O3
|
mips64 clang 21.1.0
|
cmp:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lw $1, 0($5)
lw $2, 0($4)
subu $2, $2, $1
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
twoSum:
.Lfunc_begin1 = .Ltmp3
daddiu $sp, $sp, -64
sd $ra, 56($sp)
sd $fp, 48($sp)
sd $gp, 40($sp)
sd $20, 32($sp)
sd $19, 24($sp)
sd $18, 16($sp)
sd $17, 8($sp)
sd $16, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(twoSum)))
move $19, $4
move $17, $6
move $18, $5
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(twoSum)))
dsll $1, $5, 3
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $1, 8
blez $18, .LBB1_8
move $16, $2
sll $5, $18, 0
daddiu $2, $16, 4
addiu $4, $zero, 0
dsll $1, $5, 3
daddu $3, $2, $1
.LBB1_2:
lw $1, 0($19)
sw $4, 0($2)
daddiu $19, $19, 4
sw $1, -4($2)
daddiu $2, $2, 8
bne $2, $3, .LBB1_2
addiu $4, $4, 1
ld $7, %got_disp(cmp)($gp)
ld $25, %call16(qsort)($gp)
move $4, $16
jalr $25
daddiu $6, $zero, 8
addiu $1, $zero, 1
beq $18, $1, .LBB1_10
daddiu $19, $zero, 0
addiu $2, $18, -1
addiu $3, $zero, 0
.LBB1_5:
sll $1, $3, 0
sll $4, $2, 0
dsll $1, $1, 3
dsll $4, $4, 3
daddu $18, $16, $1
daddu $20, $16, $4
lw $1, 0($18)
lw $4, 0($20)
addu $4, $4, $1
beq $4, $17, .LBB1_9
nop
slt $1, $4, $17
addu $3, $3, $1
xori $1, $1, 1
subu $2, $2, $1
slt $1, $3, $2
bnez $1, .LBB1_5
nop
b .LBB1_10
nop
.LBB1_8:
ld $7, %got_disp(cmp)($gp)
ld $25, %call16(qsort)($gp)
sll $5, $18, 0
move $4, $16
jalr $25
daddiu $6, $zero, 8
b .LBB1_10
daddiu $19, $zero, 0
.LBB1_9:
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $zero, 8
lw $1, 4($18)
move $19, $2
sw $1, 0($2)
lw $1, 4($20)
sw $1, 4($2)
.LBB1_10:
ld $25, %call16(free)($gp)
jalr $25
move $4, $16
move $2, $19
move $sp, $fp
ld $16, 0($sp)
ld $17, 8($sp)
ld $18, 16($sp)
ld $19, 24($sp)
ld $20, 32($sp)
ld $gp, 40($sp)
ld $fp, 48($sp)
ld $ra, 56($sp)
jr $ra
daddiu $sp, $sp, 64
|
12
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O0
|
mips64 gcc 15.2.0
|
cmp:
daddiu $sp,$sp,-32
sd $fp,24($sp)
move $fp,$sp
sd $4,0($fp)
sd $5,8($fp)
ld $2,0($fp)
lw $3,0($2)
ld $2,8($fp)
lw $2,0($2)
subu $2,$3,$2
move $sp,$fp
ld $fp,24($sp)
daddiu $sp,$sp,32
jr $31
nop
twoSum:
daddiu $sp,$sp,-80
sd $31,72($sp)
sd $fp,64($sp)
sd $28,56($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(twoSum)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(twoSum)))
sd $4,32($fp)
move $3,$5
move $2,$6
sll $3,$3,0
sw $3,40($fp)
sll $2,$2,0
sw $2,44($fp)
lw $2,40($fp)
addiu $2,$2,1
dsll $2,$2,3
move $4,$2
ld $2,%call16(malloc)($28)
mtlo $2
mflo $25
jalr $25
nop
sd $2,8($fp)
sw $0,0($fp)
b .L4
nop
.L5:
lw $2,0($fp)
dsll $2,$2,2
ld $3,32($fp)
daddu $3,$3,$2
lw $2,0($fp)
dsll $2,$2,3
ld $4,8($fp)
daddu $2,$4,$2
lw $3,0($3)
sw $3,0($2)
lw $2,0($fp)
dsll $2,$2,3
ld $3,8($fp)
daddu $2,$3,$2
lw $3,0($fp)
sw $3,4($2)
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
.L4:
lw $3,0($fp)
lw $2,40($fp)
slt $2,$3,$2
bne $2,$0,.L5
nop
lw $2,40($fp)
ld $7,%got_disp(cmp)($28)
li $6,8 # 0x8
move $5,$2
ld $4,8($fp)
ld $2,%call16(qsort)($28)
mtlo $2
mflo $25
jalr $25
nop
sw $0,0($fp)
lw $2,40($fp)
addiu $2,$2,-1
sw $2,4($fp)
b .L6
nop
.L10:
lw $2,0($fp)
dsll $2,$2,3
ld $3,8($fp)
daddu $2,$3,$2
lw $3,0($2)
lw $2,4($fp)
dsll $2,$2,3
ld $4,8($fp)
daddu $2,$4,$2
lw $2,0($2)
addu $2,$3,$2
sw $2,16($fp)
lw $3,16($fp)
lw $2,44($fp)
bne $3,$2,.L7
nop
li $4,8 # 0x8
ld $2,%call16(malloc)($28)
mtlo $2
mflo $25
jalr $25
nop
sd $2,24($fp)
lw $2,0($fp)
dsll $2,$2,3
ld $3,8($fp)
daddu $2,$3,$2
lw $3,4($2)
ld $2,24($fp)
sw $3,0($2)
lw $2,4($fp)
dsll $2,$2,3
ld $3,8($fp)
daddu $3,$3,$2
ld $2,24($fp)
daddiu $2,$2,4
lw $3,4($3)
sw $3,0($2)
ld $4,8($fp)
ld $2,%call16(free)($28)
mtlo $2
mflo $25
jalr $25
nop
ld $2,24($fp)
b .L8
nop
.L7:
lw $3,16($fp)
lw $2,44($fp)
slt $2,$3,$2
beq $2,$0,.L9
nop
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
b .L6
nop
.L9:
lw $2,4($fp)
addiu $2,$2,-1
sw $2,4($fp)
.L6:
lw $3,0($fp)
lw $2,4($fp)
slt $2,$3,$2
bne $2,$0,.L10
nop
ld $4,8($fp)
ld $2,%call16(free)($28)
mtlo $2
mflo $25
jalr $25
nop
move $2,$0
.L8:
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
daddiu $sp,$sp,80
jr $31
nop
|
13
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O1
|
mips64 gcc 15.2.0
|
cmp:
lw $3,0($4)
lw $2,0($5)
jr $31
subu $2,$3,$2
twoSum:
daddiu $sp,$sp,-64
sd $31,56($sp)
sd $28,48($sp)
sd $20,40($sp)
sd $19,32($sp)
sd $18,24($sp)
sd $17,16($sp)
sd $16,8($sp)
lui $28,%hi(%neg(%gp_rel(twoSum)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(twoSum)))
move $16,$4
move $17,$5
move $20,$6
addiu $4,$5,1
ld $25,%call16(malloc)($28)
1: jalr $25
dsll $4,$4,3
blez $17,.L4
move $19,$2
move $4,$16
move $5,$0
.L5:
lw $3,0($4)
sw $3,0($2)
sw $5,4($2)
move $18,$5
addiu $5,$5,1
daddiu $4,$4,4
bne $17,$5,.L5
daddiu $2,$2,8
ld $7,%got_disp(cmp)($28)
li $6,8 # 0x8
ld $25,%call16(qsort)($28)
1: jalr $25
move $4,$19
blez $18,.L6
move $2,$0
b .L11
move $5,$20
.L15:
ld $25,%call16(malloc)($28)
1: jalr $25
li $4,8 # 0x8
move $18,$2
lw $2,4($17)
sw $2,0($18)
lw $2,4($16)
sw $2,4($18)
ld $25,%call16(free)($28)
1: jalr $25
move $4,$19
b .L16
move $2,$18
.L9:
.L10:
slt $3,$2,$18
beq $3,$0,.L17
ld $25,%call16(free)($28)
.L11:
dsll $17,$2,3
daddu $17,$19,$17
dsll $16,$18,3
daddu $16,$19,$16
lw $3,0($17)
lw $4,0($16)
addu $3,$3,$4
beq $3,$5,.L15
slt $3,$3,$20
beql $3,$0,.L9
addiu $18,$18,-1
b .L10
addiu $2,$2,1
.L4:
ld $7,%got_disp(cmp)($28)
li $6,8 # 0x8
move $5,$17
ld $25,%call16(qsort)($28)
1: jalr $25
move $4,$2
.L6:
ld $25,%call16(free)($28)
.L17:
1: jalr $25
move $4,$19
move $18,$0
move $2,$18
.L16:
ld $31,56($sp)
ld $28,48($sp)
ld $20,40($sp)
ld $19,32($sp)
ld $18,24($sp)
ld $17,16($sp)
ld $16,8($sp)
jr $31
daddiu $sp,$sp,64
|
14
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O2
|
mips64 gcc 15.2.0
|
cmp:
lw $3,0($4)
lw $2,0($5)
jr $31
subu $2,$3,$2
twoSum:
daddiu $sp,$sp,-80
sd $28,64($sp)
lui $28,%hi(%neg(%gp_rel(twoSum)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(twoSum)))
ld $25,%call16(malloc)($28)
addiu $2,$5,1
sd $20,56($sp)
sd $19,48($sp)
sd $17,32($sp)
sd $16,24($sp)
sd $31,72($sp)
sd $18,40($sp)
move $16,$4
dsll $4,$2,3
move $17,$5
1: jalr $25
move $20,$6
blez $17,.L5
move $19,$2
move $6,$2
move $3,$0
.L6:
lwu $2,0($16)
dsll $7,$3,32
dsrl $7,$7,32
dsll $2,$2,32
move $18,$3
or $2,$2,$7
addiu $3,$3,1
sd $2,0($6)
daddiu $16,$16,4
bne $17,$3,.L6
daddiu $6,$6,8
ld $25,%call16(qsort)($28)
ld $7,%got_disp(cmp)($28)
li $6,8 # 0x8
move $5,$17
1: jalr $25
move $4,$19
beq $18,$0,.L23
ld $25,%call16(free)($28)
b .L13
move $3,$0
.L22:
slt $2,$3,$18
beq $2,$0,.L23
ld $25,%call16(free)($28)
.L13:
sll $16,$18,0
.L24:
dsll $17,$3,32
dsll $16,$16,32
dsrl $17,$17,32
dsrl $16,$16,32
dsll $17,$17,3
dsll $16,$16,3
daddu $17,$19,$17
daddu $16,$19,$16
lw $4,0($16)
lw $2,0($17)
addu $2,$2,$4
beq $2,$20,.L21
slt $4,$2,$20
bnel $4,$0,.L22
addiu $3,$3,1
addiu $18,$18,-1
slt $2,$3,$18
bnel $2,$0,.L24
sll $16,$18,0
ld $25,%call16(free)($28)
.L23:
1: jalr $25
move $4,$19
ld $31,72($sp)
ld $28,64($sp)
ld $20,56($sp)
ld $19,48($sp)
ld $18,40($sp)
ld $17,32($sp)
ld $16,24($sp)
move $2,$0
jr $31
daddiu $sp,$sp,80
.L21:
ld $25,%call16(malloc)($28)
1: jalr $25
li $4,8 # 0x8
lwu $3,4($17)
lwu $4,4($16)
ld $25,%call16(free)($28)
dsll $3,$3,32
or $3,$3,$4
sd $3,0($2)
move $4,$19
1: jalr $25
sd $2,0($sp)
ld $31,72($sp)
ld $2,0($sp)
ld $28,64($sp)
ld $20,56($sp)
ld $19,48($sp)
ld $18,40($sp)
ld $17,32($sp)
ld $16,24($sp)
jr $31
daddiu $sp,$sp,80
.L5:
ld $25,%call16(qsort)($28)
ld $7,%got_disp(cmp)($28)
li $6,8 # 0x8
move $5,$17
1: jalr $25
move $4,$2
b .L23
ld $25,%call16(free)($28)
|
15
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
mips64
|
-O3
|
mips64 gcc 15.2.0
|
cmp:
lw $3,0($4)
lw $2,0($5)
jr $31
subu $2,$3,$2
twoSum:
daddiu $sp,$sp,-80
sd $28,64($sp)
lui $28,%hi(%neg(%gp_rel(twoSum)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(twoSum)))
ld $25,%call16(malloc)($28)
addiu $2,$5,1
sd $20,56($sp)
sd $19,48($sp)
sd $17,32($sp)
sd $16,24($sp)
sd $31,72($sp)
sd $18,40($sp)
move $16,$4
dsll $4,$2,3
move $17,$5
1: jalr $25
move $20,$6
blez $17,.L5
move $19,$2
move $6,$2
move $3,$0
.L6:
lwu $2,0($16)
dsll $7,$3,32
dsrl $7,$7,32
dsll $2,$2,32
move $18,$3
or $2,$2,$7
addiu $3,$3,1
sd $2,0($6)
daddiu $16,$16,4
bne $17,$3,.L6
daddiu $6,$6,8
ld $25,%call16(qsort)($28)
ld $7,%got_disp(cmp)($28)
li $6,8 # 0x8
move $5,$17
1: jalr $25
move $4,$19
beq $18,$0,.L23
ld $25,%call16(free)($28)
b .L13
move $3,$0
.L22:
slt $2,$3,$18
beq $2,$0,.L23
ld $25,%call16(free)($28)
.L13:
sll $16,$18,0
.L24:
dsll $17,$3,32
dsll $16,$16,32
dsrl $17,$17,32
dsrl $16,$16,32
dsll $17,$17,3
dsll $16,$16,3
daddu $17,$19,$17
daddu $16,$19,$16
lw $4,0($16)
lw $2,0($17)
addu $2,$2,$4
beq $2,$20,.L21
slt $4,$2,$20
bnel $4,$0,.L22
addiu $3,$3,1
addiu $18,$18,-1
slt $2,$3,$18
bnel $2,$0,.L24
sll $16,$18,0
ld $25,%call16(free)($28)
.L23:
1: jalr $25
move $4,$19
ld $31,72($sp)
ld $28,64($sp)
ld $20,56($sp)
ld $19,48($sp)
ld $18,40($sp)
ld $17,32($sp)
ld $16,24($sp)
move $2,$0
jr $31
daddiu $sp,$sp,80
.L21:
ld $25,%call16(malloc)($28)
1: jalr $25
li $4,8 # 0x8
lwu $3,4($17)
lwu $4,4($16)
ld $25,%call16(free)($28)
dsll $3,$3,32
or $3,$3,$4
sd $3,0($2)
move $4,$19
1: jalr $25
sd $2,0($sp)
ld $31,72($sp)
ld $2,0($sp)
ld $28,64($sp)
ld $20,56($sp)
ld $19,48($sp)
ld $18,40($sp)
ld $17,32($sp)
ld $16,24($sp)
jr $31
daddiu $sp,$sp,80
.L5:
ld $25,%call16(qsort)($28)
ld $7,%got_disp(cmp)($28)
li $6,8 # 0x8
move $5,$17
1: jalr $25
move $4,$2
b .L23
ld $25,%call16(free)($28)
|
16
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O0
|
RISC-V 64 clang 21.1.0
|
cmp:
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
addi s0, sp, 32
sd a0, -24(s0)
sd a1, -32(s0)
ld a0, -24(s0)
lw a0, 0(a0)
ld a1, -32(s0)
lw a1, 0(a1)
subw a0, a0, a1
ld ra, 24(sp)
ld s0, 16(sp)
addi sp, sp, 32
ret
twoSum:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
addi s0, sp, 80
sd a0, -32(s0)
sw a1, -36(s0)
sw a2, -40(s0)
lw a0, -36(s0)
slli a0, a0, 3
addi a0, a0, 8
call malloc
sd a0, -72(s0)
li a0, 0
sw a0, -52(s0)
j .LBB1_1
.LBB1_1:
lw a0, -52(s0)
lw a1, -36(s0)
bge a0, a1, .LBB1_4
j .LBB1_2
.LBB1_2:
ld a0, -32(s0)
lw a2, -52(s0)
slli a1, a2, 2
add a0, a0, a1
lw a0, 0(a0)
ld a1, -72(s0)
slli a2, a2, 3
add a1, a1, a2
sw a0, 0(a1)
lw a0, -52(s0)
ld a1, -72(s0)
slli a2, a0, 3
add a1, a1, a2
sw a0, 4(a1)
j .LBB1_3
.LBB1_3:
lw a0, -52(s0)
addiw a0, a0, 1
sw a0, -52(s0)
j .LBB1_1
.LBB1_4:
ld a0, -72(s0)
lw a1, -36(s0)
.Lpcrel_hi0:
auipc a2, %pcrel_hi(cmp)
addi a3, a2, %pcrel_lo(.Lpcrel_hi0)
li a2, 8
call qsort
li a0, 0
sw a0, -52(s0)
lw a0, -36(s0)
addiw a0, a0, -1
sw a0, -56(s0)
j .LBB1_5
.LBB1_5:
lw a0, -52(s0)
lw a1, -56(s0)
bge a0, a1, .LBB1_13
j .LBB1_6
.LBB1_6:
ld a1, -72(s0)
lw a0, -52(s0)
slli a0, a0, 3
add a0, a0, a1
lw a0, 0(a0)
lw a2, -56(s0)
slli a2, a2, 3
add a1, a1, a2
lw a1, 0(a1)
addw a0, a0, a1
sw a0, -60(s0)
lw a0, -60(s0)
lw a1, -40(s0)
bne a0, a1, .LBB1_8
j .LBB1_7
.LBB1_7:
li a0, 8
call malloc
sd a0, -48(s0)
ld a0, -72(s0)
lw a1, -52(s0)
slli a1, a1, 3
add a0, a0, a1
lw a0, 4(a0)
ld a1, -48(s0)
sw a0, 0(a1)
ld a0, -72(s0)
lw a1, -56(s0)
slli a1, a1, 3
add a0, a0, a1
lw a0, 4(a0)
ld a1, -48(s0)
sw a0, 4(a1)
ld a0, -72(s0)
call free
ld a0, -48(s0)
sd a0, -24(s0)
j .LBB1_14
.LBB1_8:
lw a0, -60(s0)
lw a1, -40(s0)
bge a0, a1, .LBB1_10
j .LBB1_9
.LBB1_9:
lw a0, -52(s0)
addiw a0, a0, 1
sw a0, -52(s0)
j .LBB1_11
.LBB1_10:
lw a0, -56(s0)
addiw a0, a0, -1
sw a0, -56(s0)
j .LBB1_11
.LBB1_11:
j .LBB1_12
.LBB1_12:
j .LBB1_5
.LBB1_13:
ld a0, -72(s0)
call free
li a0, 0
sd a0, -24(s0)
j .LBB1_14
.LBB1_14:
ld a0, -24(s0)
ld ra, 72(sp)
ld s0, 64(sp)
addi sp, sp, 80
ret
|
17
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O1
|
RISC-V 64 clang 21.1.0
|
cmp:
lw a0, 0(a0)
lw a1, 0(a1)
subw a0, a0, a1
ret
twoSum:
addi sp, sp, -48
sd ra, 40(sp)
sd s0, 32(sp)
sd s1, 24(sp)
sd s2, 16(sp)
sd s3, 8(sp)
sd s4, 0(sp)
mv s3, a2
mv s2, a1
mv s1, a0
slli s0, a1, 3
addi a0, s0, 8
call malloc
mv s4, a0
blez s2, .LBB1_3
li a0, 0
addi a1, s4, 4
add a2, a1, s0
.LBB1_2:
lw a3, 0(s1)
sw a3, -4(a1)
sw a0, 0(a1)
addi a0, a0, 1
addi a1, a1, 8
addi s1, s1, 4
bne a1, a2, .LBB1_2
.LBB1_3:
.Lpcrel_hi0:
auipc a0, %pcrel_hi(cmp)
addi a3, a0, %pcrel_lo(.Lpcrel_hi0)
li a2, 8
mv a0, s4
mv a1, s2
call qsort
li a0, 2
blt s2, a0, .LBB1_7
li a0, 0
addiw s2, s2, -1
.LBB1_5:
slli s1, a0, 3
slli s0, s2, 3
add s1, s1, s4
add s0, s0, s4
lw a1, 0(s1)
lw a2, 0(s0)
addw a1, a1, a2
beq a1, s3, .LBB1_8
slt a1, a1, s3
xori a2, a1, 1
subw s2, s2, a2
addw a0, a0, a1
blt a0, s2, .LBB1_5
.LBB1_7:
li s2, 0
j .LBB1_9
.LBB1_8:
li a0, 8
call malloc
mv s2, a0
lw a0, 4(s1)
lw a1, 4(s0)
sw a0, 0(s2)
sw a1, 4(s2)
.LBB1_9:
mv a0, s4
call free
mv a0, s2
ld ra, 40(sp)
ld s0, 32(sp)
ld s1, 24(sp)
ld s2, 16(sp)
ld s3, 8(sp)
ld s4, 0(sp)
addi sp, sp, 48
ret
|
18
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O2
|
RISC-V 64 clang 21.1.0
|
cmp:
lw a0, 0(a0)
lw a1, 0(a1)
subw a0, a0, a1
ret
twoSum:
addi sp, sp, -64
sd ra, 56(sp)
sd s0, 48(sp)
sd s1, 40(sp)
sd s2, 32(sp)
sd s3, 24(sp)
sd s4, 16(sp)
sd s5, 8(sp)
mv s5, a2
mv s2, a1
mv s0, a0
slli s4, a1, 3
addi a0, s4, 8
call malloc
mv s3, a0
blez s2, .LBB1_7
li a0, 0
addi a1, s3, 4
add a2, a1, s4
.LBB1_2:
lw a3, 0(s0)
sw a3, -4(a1)
sw a0, 0(a1)
addi a0, a0, 1
addi a1, a1, 8
addi s0, s0, 4
bne a1, a2, .LBB1_2
.Lpcrel_hi1:
auipc a0, %pcrel_hi(cmp)
addi a3, a0, %pcrel_lo(.Lpcrel_hi1)
li a2, 8
mv a0, s3
mv a1, s2
call qsort
addiw s2, s2, -1
beqz s2, .LBB1_10
li a0, 0
.LBB1_5:
slli s1, a0, 3
slli s0, s2, 3
add s1, s1, s3
add s0, s0, s3
lw a1, 0(s1)
lw a2, 0(s0)
addw a1, a1, a2
beq a1, s5, .LBB1_9
slt a1, a1, s5
xori a2, a1, 1
subw s2, s2, a2
addw a0, a0, a1
blt a0, s2, .LBB1_5
j .LBB1_8
.LBB1_7:
.Lpcrel_hi0:
auipc a0, %pcrel_hi(cmp)
addi a3, a0, %pcrel_lo(.Lpcrel_hi0)
li a2, 8
mv a0, s3
mv a1, s2
call qsort
.LBB1_8:
li s2, 0
j .LBB1_10
.LBB1_9:
li a0, 8
call malloc
mv s2, a0
lw a0, 4(s1)
lw a1, 4(s0)
sw a0, 0(s2)
sw a1, 4(s2)
.LBB1_10:
mv a0, s3
call free
mv a0, s2
ld ra, 56(sp)
ld s0, 48(sp)
ld s1, 40(sp)
ld s2, 32(sp)
ld s3, 24(sp)
ld s4, 16(sp)
ld s5, 8(sp)
addi sp, sp, 64
ret
|
19
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O3
|
RISC-V 64 clang 21.1.0
|
cmp:
lw a0, 0(a0)
lw a1, 0(a1)
subw a0, a0, a1
ret
twoSum:
addi sp, sp, -64
sd ra, 56(sp)
sd s0, 48(sp)
sd s1, 40(sp)
sd s2, 32(sp)
sd s3, 24(sp)
sd s4, 16(sp)
sd s5, 8(sp)
mv s5, a2
mv s2, a1
mv s0, a0
slli s4, a1, 3
addi a0, s4, 8
call malloc
mv s3, a0
blez s2, .LBB1_7
li a0, 0
addi a1, s3, 4
add a2, a1, s4
.LBB1_2:
lw a3, 0(s0)
sw a3, -4(a1)
sw a0, 0(a1)
addi a0, a0, 1
addi a1, a1, 8
addi s0, s0, 4
bne a1, a2, .LBB1_2
.Lpcrel_hi1:
auipc a0, %pcrel_hi(cmp)
addi a3, a0, %pcrel_lo(.Lpcrel_hi1)
li a2, 8
mv a0, s3
mv a1, s2
call qsort
addiw s2, s2, -1
beqz s2, .LBB1_10
li a0, 0
.LBB1_5:
slli s1, a0, 3
slli s0, s2, 3
add s1, s1, s3
add s0, s0, s3
lw a1, 0(s1)
lw a2, 0(s0)
addw a1, a1, a2
beq a1, s5, .LBB1_9
slt a1, a1, s5
xori a2, a1, 1
subw s2, s2, a2
addw a0, a0, a1
blt a0, s2, .LBB1_5
j .LBB1_8
.LBB1_7:
.Lpcrel_hi0:
auipc a0, %pcrel_hi(cmp)
addi a3, a0, %pcrel_lo(.Lpcrel_hi0)
li a2, 8
mv a0, s3
mv a1, s2
call qsort
.LBB1_8:
li s2, 0
j .LBB1_10
.LBB1_9:
li a0, 8
call malloc
mv s2, a0
lw a0, 4(s1)
lw a1, 4(s0)
sw a0, 0(s2)
sw a1, 4(s2)
.LBB1_10:
mv a0, s3
call free
mv a0, s2
ld ra, 56(sp)
ld s0, 48(sp)
ld s1, 40(sp)
ld s2, 32(sp)
ld s3, 24(sp)
ld s4, 16(sp)
ld s5, 8(sp)
addi sp, sp, 64
ret
|
20
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O0
|
RISC-V 64 gcc 15.2.0
|
cmp:
addi sp,sp,-32
sd ra,24(sp)
sd s0,16(sp)
addi s0,sp,32
sd a0,-24(s0)
sd a1,-32(s0)
ld a5,-24(s0)
lw a4,0(a5)
ld a5,-32(s0)
lw a5,0(a5)
subw a5,a4,a5
sext.w a5,a5
mv a0,a5
ld ra,24(sp)
ld s0,16(sp)
addi sp,sp,32
jr ra
twoSum:
addi sp,sp,-64
sd ra,56(sp)
sd s0,48(sp)
addi s0,sp,64
sd a0,-56(s0)
mv a5,a1
mv a4,a2
sw a5,-60(s0)
mv a5,a4
sw a5,-64(s0)
lw a5,-60(s0)
addiw a5,a5,1
sext.w a5,a5
slli a5,a5,3
mv a0,a5
call malloc
mv a5,a0
sd a5,-32(s0)
sw zero,-20(s0)
j .L4
.L5:
lw a5,-20(s0)
slli a5,a5,2
ld a4,-56(s0)
add a4,a4,a5
lw a5,-20(s0)
slli a5,a5,3
ld a3,-32(s0)
add a5,a3,a5
lw a4,0(a4)
sw a4,0(a5)
lw a5,-20(s0)
slli a5,a5,3
ld a4,-32(s0)
add a5,a4,a5
lw a4,-20(s0)
sw a4,4(a5)
lw a5,-20(s0)
addiw a5,a5,1
sw a5,-20(s0)
.L4:
lw a5,-20(s0)
mv a4,a5
lw a5,-60(s0)
sext.w a4,a4
sext.w a5,a5
blt a4,a5,.L5
lw a4,-60(s0)
lui a5,%hi(cmp)
addi a3,a5,%lo(cmp)
li a2,8
mv a1,a4
ld a0,-32(s0)
call qsort
sw zero,-20(s0)
lw a5,-60(s0)
addiw a5,a5,-1
sw a5,-24(s0)
j .L6
.L10:
lw a5,-20(s0)
slli a5,a5,3
ld a4,-32(s0)
add a5,a4,a5
lw a4,0(a5)
lw a5,-24(s0)
slli a5,a5,3
ld a3,-32(s0)
add a5,a3,a5
lw a5,0(a5)
addw a5,a4,a5
sw a5,-36(s0)
lw a5,-36(s0)
mv a4,a5
lw a5,-64(s0)
sext.w a4,a4
sext.w a5,a5
bne a4,a5,.L7
li a0,8
call malloc
mv a5,a0
sd a5,-48(s0)
lw a5,-20(s0)
slli a5,a5,3
ld a4,-32(s0)
add a5,a4,a5
lw a4,4(a5)
ld a5,-48(s0)
sw a4,0(a5)
lw a5,-24(s0)
slli a5,a5,3
ld a4,-32(s0)
add a4,a4,a5
ld a5,-48(s0)
addi a5,a5,4
lw a4,4(a4)
sw a4,0(a5)
ld a0,-32(s0)
call free
ld a5,-48(s0)
j .L8
.L7:
lw a5,-36(s0)
mv a4,a5
lw a5,-64(s0)
sext.w a4,a4
sext.w a5,a5
bge a4,a5,.L9
lw a5,-20(s0)
addiw a5,a5,1
sw a5,-20(s0)
j .L6
.L9:
lw a5,-24(s0)
addiw a5,a5,-1
sw a5,-24(s0)
.L6:
lw a5,-20(s0)
mv a4,a5
lw a5,-24(s0)
sext.w a4,a4
sext.w a5,a5
blt a4,a5,.L10
ld a0,-32(s0)
call free
li a5,0
.L8:
mv a0,a5
ld ra,56(sp)
ld s0,48(sp)
addi sp,sp,64
jr ra
|
21
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O1
|
RISC-V 64 gcc 15.2.0
|
cmp:
lw a0,0(a0)
lw a5,0(a1)
subw a0,a0,a5
ret
twoSum:
addi sp,sp,-48
sd ra,40(sp)
sd s0,32(sp)
sd s1,24(sp)
sd s2,16(sp)
sd s3,8(sp)
sd s4,0(sp)
mv s0,a0
mv s1,a1
mv s4,a2
addiw a0,a1,1
slli a0,a0,3
call malloc
mv s3,a0
ble s1,zero,.L3
mv a3,s0
mv a4,a0
li a5,0
.L4:
lw a2,0(a3)
sw a2,0(a4)
sw a5,4(a4)
mv s2,a5
addiw a1,a5,1
mv a5,a1
addi a3,a3,4
addi a4,a4,8
bne s1,a1,.L4
lui a3,%hi(cmp)
addi a3,a3,%lo(cmp)
li a2,8
mv a0,s3
call qsort
ble s2,zero,.L5
li a4,0
j .L10
.L14:
li a0,8
call malloc
mv s2,a0
lw a5,4(s1)
sw a5,0(a0)
lw a5,4(s0)
sw a5,4(a0)
mv a0,s3
call free
j .L2
.L8:
addiw s2,s2,-1
.L9:
bge a4,s2,.L5
.L10:
slli s1,a4,3
add s1,s3,s1
slli s0,s2,3
add s0,s3,s0
lw a3,0(s1)
lw a5,0(s0)
addw a5,a5,a3
beq a5,s4,.L14
bge a5,s4,.L8
addiw a4,a4,1
j .L9
.L3:
lui a3,%hi(cmp)
addi a3,a3,%lo(cmp)
li a2,8
mv a1,s1
call qsort
.L5:
mv a0,s3
call free
li s2,0
.L2:
mv a0,s2
ld ra,40(sp)
ld s0,32(sp)
ld s1,24(sp)
ld s2,16(sp)
ld s3,8(sp)
ld s4,0(sp)
addi sp,sp,48
jr ra
|
22
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O2
|
RISC-V 64 gcc 15.2.0
|
cmp:
lw a0,0(a0)
lw a5,0(a1)
subw a0,a0,a5
ret
twoSum:
addi sp,sp,-48
addiw a5,a1,1
sd s2,16(sp)
mv s2,a0
slli a0,a5,3
sd s0,32(sp)
sd s1,24(sp)
sd a1,0(sp)
sd ra,40(sp)
mv s1,a2
call malloc
ld a1,0(sp)
mv s0,a0
ble a1,zero,.L4
mv a0,s2
mv a4,s0
li a5,0
.L5:
lw a3,0(a0)
sw a5,4(a4)
mv a6,a5
sw a3,0(a4)
addiw a5,a5,1
addi a0,a0,4
addi a4,a4,8
bne a1,a5,.L5
lui a3,%hi(cmp)
addi a3,a3,%lo(cmp)
li a2,8
mv a0,s0
sd a6,0(sp)
call qsort
ld a6,0(sp)
beq a6,zero,.L7
li a2,0
.L12:
slli a4,a2,3
slli a5,a6,3
add a4,s0,a4
add a5,s0,a5
lw a1,0(a4)
lw a3,0(a5)
addw a3,a3,a1
beq a3,s1,.L20
bge a3,s1,.L10
addiw a2,a2,1
bgt a6,a2,.L12
.L7:
mv a0,s0
call free
ld ra,40(sp)
ld s0,32(sp)
li s1,0
ld s2,16(sp)
mv a0,s1
ld s1,24(sp)
addi sp,sp,48
jr ra
.L10:
addiw a6,a6,-1
bgt a6,a2,.L12
j .L7
.L20:
li a0,8
sd a5,8(sp)
sd a4,0(sp)
call malloc
ld a4,0(sp)
ld a5,8(sp)
mv s1,a0
lw a4,4(a4)
lw a5,4(a5)
mv a0,s0
sw a4,0(s1)
sw a5,4(s1)
call free
ld ra,40(sp)
ld s0,32(sp)
ld s2,16(sp)
mv a0,s1
ld s1,24(sp)
addi sp,sp,48
jr ra
.L4:
lui a3,%hi(cmp)
addi a3,a3,%lo(cmp)
li a2,8
call qsort
j .L7
|
23
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
riscv64
|
-O3
|
RISC-V 64 gcc 15.2.0
|
cmp:
lw a0,0(a0)
lw a5,0(a1)
subw a0,a0,a5
ret
twoSum:
addi sp,sp,-48
addiw a5,a1,1
sd s2,16(sp)
mv s2,a0
slli a0,a5,3
sd s0,32(sp)
sd s1,24(sp)
sd a1,0(sp)
sd ra,40(sp)
mv s1,a2
call malloc
ld a1,0(sp)
mv s0,a0
ble a1,zero,.L4
mv a0,s2
mv a4,s0
li a5,0
.L5:
lw a3,0(a0)
sw a5,4(a4)
mv a6,a5
sw a3,0(a4)
addiw a5,a5,1
addi a0,a0,4
addi a4,a4,8
bne a1,a5,.L5
lui a3,%hi(cmp)
addi a3,a3,%lo(cmp)
li a2,8
mv a0,s0
sd a6,0(sp)
call qsort
ld a6,0(sp)
beq a6,zero,.L7
li a2,0
.L12:
slli a4,a2,3
slli a5,a6,3
add a4,s0,a4
add a5,s0,a5
lw a1,0(a4)
lw a3,0(a5)
addw a3,a3,a1
beq a3,s1,.L20
bge a3,s1,.L10
addiw a2,a2,1
bgt a6,a2,.L12
.L7:
mv a0,s0
call free
ld ra,40(sp)
ld s0,32(sp)
li s1,0
ld s2,16(sp)
mv a0,s1
ld s1,24(sp)
addi sp,sp,48
jr ra
.L10:
addiw a6,a6,-1
bgt a6,a2,.L12
j .L7
.L20:
li a0,8
sd a5,8(sp)
sd a4,0(sp)
call malloc
ld a4,0(sp)
ld a5,8(sp)
mv s1,a0
lw a4,4(a4)
lw a5,4(a5)
mv a0,s0
sw a4,0(s1)
sw a5,4(s1)
call free
ld ra,40(sp)
ld s0,32(sp)
ld s2,16(sp)
mv a0,s1
ld s1,24(sp)
addi sp,sp,48
jr ra
.L4:
lui a3,%hi(cmp)
addi a3,a3,%lo(cmp)
li a2,8
call qsort
j .L7
|
24
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O0
|
x86-64 clang 21.1.0
|
cmp:
push rbp
mov rbp, rsp
mov qword ptr [rbp - 8], rdi
mov qword ptr [rbp - 16], rsi
mov rax, qword ptr [rbp - 8]
mov eax, dword ptr [rax]
mov rcx, qword ptr [rbp - 16]
sub eax, dword ptr [rcx]
pop rbp
ret
twoSum:
push rbp
mov rbp, rsp
sub rsp, 64
mov qword ptr [rbp - 16], rdi
mov dword ptr [rbp - 20], esi
mov dword ptr [rbp - 24], edx
mov eax, dword ptr [rbp - 20]
add eax, 1
movsxd rdi, eax
shl rdi, 3
call malloc@PLT
mov qword ptr [rbp - 56], rax
mov dword ptr [rbp - 36], 0
.LBB1_1:
mov eax, dword ptr [rbp - 36]
cmp eax, dword ptr [rbp - 20]
jge .LBB1_4
mov rax, qword ptr [rbp - 16]
movsxd rcx, dword ptr [rbp - 36]
mov edx, dword ptr [rax + 4*rcx]
mov rax, qword ptr [rbp - 56]
movsxd rcx, dword ptr [rbp - 36]
mov dword ptr [rax + 8*rcx], edx
mov edx, dword ptr [rbp - 36]
mov rax, qword ptr [rbp - 56]
movsxd rcx, dword ptr [rbp - 36]
mov dword ptr [rax + 8*rcx + 4], edx
mov eax, dword ptr [rbp - 36]
add eax, 1
mov dword ptr [rbp - 36], eax
jmp .LBB1_1
.LBB1_4:
mov rdi, qword ptr [rbp - 56]
movsxd rsi, dword ptr [rbp - 20]
mov edx, 8
lea rcx, [rip + cmp]
call qsort@PLT
mov dword ptr [rbp - 36], 0
mov eax, dword ptr [rbp - 20]
sub eax, 1
mov dword ptr [rbp - 40], eax
.LBB1_5:
mov eax, dword ptr [rbp - 36]
cmp eax, dword ptr [rbp - 40]
jge .LBB1_13
mov rax, qword ptr [rbp - 56]
movsxd rcx, dword ptr [rbp - 36]
mov eax, dword ptr [rax + 8*rcx]
mov rcx, qword ptr [rbp - 56]
movsxd rdx, dword ptr [rbp - 40]
add eax, dword ptr [rcx + 8*rdx]
mov dword ptr [rbp - 44], eax
mov eax, dword ptr [rbp - 44]
cmp eax, dword ptr [rbp - 24]
jne .LBB1_8
mov edi, 8
call malloc@PLT
mov qword ptr [rbp - 32], rax
mov rax, qword ptr [rbp - 56]
movsxd rcx, dword ptr [rbp - 36]
mov ecx, dword ptr [rax + 8*rcx + 4]
mov rax, qword ptr [rbp - 32]
mov dword ptr [rax], ecx
mov rax, qword ptr [rbp - 56]
movsxd rcx, dword ptr [rbp - 40]
mov ecx, dword ptr [rax + 8*rcx + 4]
mov rax, qword ptr [rbp - 32]
mov dword ptr [rax + 4], ecx
mov rdi, qword ptr [rbp - 56]
call free@PLT
mov rax, qword ptr [rbp - 32]
mov qword ptr [rbp - 8], rax
jmp .LBB1_14
.LBB1_8:
mov eax, dword ptr [rbp - 44]
cmp eax, dword ptr [rbp - 24]
jge .LBB1_10
mov eax, dword ptr [rbp - 36]
add eax, 1
mov dword ptr [rbp - 36], eax
jmp .LBB1_11
.LBB1_10:
mov eax, dword ptr [rbp - 40]
add eax, -1
mov dword ptr [rbp - 40], eax
.LBB1_11:
jmp .LBB1_12
.LBB1_12:
jmp .LBB1_5
.LBB1_13:
mov rdi, qword ptr [rbp - 56]
call free@PLT
mov qword ptr [rbp - 8], 0
.LBB1_14:
mov rax, qword ptr [rbp - 8]
add rsp, 64
pop rbp
ret
|
25
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O1
|
x86-64 clang 21.1.0
|
cmp:
mov eax, dword ptr [rdi]
sub eax, dword ptr [rsi]
ret
twoSum:
push rbp
push r15
push r14
push r13
push r12
push rbx
push rax
mov ebp, edx
mov r14d, esi
mov r12, rdi
movsxd r15, esi
lea rdi, [8*r15 + 8]
call malloc@PLT
mov rbx, rax
test r15d, r15d
jle .LBB1_3
mov eax, r14d
xor ecx, ecx
.LBB1_2:
mov edx, dword ptr [r12 + 4*rcx]
mov dword ptr [rbx + 8*rcx], edx
mov dword ptr [rbx + 8*rcx + 4], ecx
inc rcx
cmp rax, rcx
jne .LBB1_2
.LBB1_3:
lea rcx, [rip + cmp]
mov edx, 8
mov rdi, rbx
mov rsi, r15
call qsort@PLT
xor r15d, r15d
cmp r14d, 2
jl .LBB1_8
dec r14d
xor eax, eax
.LBB1_5:
mov r12d, eax
movsxd r13, r14d
mov eax, dword ptr [rbx + 8*r13]
add eax, dword ptr [rbx + 8*r12]
cmp eax, ebp
je .LBB1_6
setl al
setge cl
movzx ecx, cl
sub r14d, ecx
movzx eax, al
add eax, r12d
cmp eax, r14d
jl .LBB1_5
jmp .LBB1_8
.LBB1_6:
mov edi, 8
call malloc@PLT
mov r15, rax
mov eax, dword ptr [rbx + 8*r12 + 4]
mov dword ptr [r15], eax
mov eax, dword ptr [rbx + 8*r13 + 4]
mov dword ptr [r15 + 4], eax
.LBB1_8:
mov rdi, rbx
call free@PLT
mov rax, r15
add rsp, 8
pop rbx
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
|
26
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O2
|
x86-64 clang 21.1.0
|
cmp:
mov eax, dword ptr [rdi]
sub eax, dword ptr [rsi]
ret
.LCPI1_1:
.long 2
.long 2
.zero 4
.zero 4
.LCPI1_2:
.long 4
.long 4
.zero 4
.zero 4
.LCPI1_3:
.long 0
.long 1
.long 0
.long 0
twoSum:
push rbp
push r15
push r14
push r13
push r12
push rbx
push rax
mov ebp, edx
mov r14d, esi
mov r15, rdi
movsxd r12, esi
lea rdi, [8*r12 + 8]
call malloc@PLT
mov rbx, rax
test r12d, r12d
jle .LBB1_1
mov esi, r14d
cmp r14d, 4
jae .LBB1_4
xor eax, eax
jmp .LBB1_7
.LBB1_1:
lea rcx, [rip + cmp]
mov edx, 8
mov rdi, rbx
mov rsi, r12
call qsort@PLT
xor r15d, r15d
jmp .LBB1_13
.LBB1_4:
mov eax, esi
and eax, 2147483644
movq xmm0, qword ptr [rip + .LCPI1_3]
xor ecx, ecx
movdqa xmm1, xmmword ptr [rip + .LCPI1_1]
movdqa xmm2, xmmword ptr [rip + .LCPI1_2]
.LBB1_5:
movdqa xmm3, xmm0
paddd xmm3, xmm1
movq xmm4, qword ptr [r15 + 4*rcx]
movq xmm5, qword ptr [r15 + 4*rcx + 8]
punpckldq xmm5, xmm3
punpckldq xmm4, xmm0
movdqu xmmword ptr [rbx + 8*rcx], xmm4
movdqu xmmword ptr [rbx + 8*rcx + 16], xmm5
add rcx, 4
paddd xmm0, xmm2
cmp rax, rcx
jne .LBB1_5
cmp eax, esi
je .LBB1_8
.LBB1_7:
mov ecx, dword ptr [r15 + 4*rax]
mov dword ptr [rbx + 8*rax], ecx
mov dword ptr [rbx + 8*rax + 4], eax
inc rax
cmp rsi, rax
jne .LBB1_7
.LBB1_8:
lea rcx, [rip + cmp]
mov edx, 8
mov rdi, rbx
call qsort@PLT
xor r15d, r15d
cmp r14d, 1
je .LBB1_13
dec r14d
xor eax, eax
.LBB1_10:
mov r12d, eax
movsxd r13, r14d
mov eax, dword ptr [rbx + 8*r13]
add eax, dword ptr [rbx + 8*r12]
cmp eax, ebp
je .LBB1_11
setl al
setge cl
movzx ecx, cl
sub r14d, ecx
movzx eax, al
add eax, r12d
cmp eax, r14d
jl .LBB1_10
jmp .LBB1_13
.LBB1_11:
mov edi, 8
call malloc@PLT
mov r15, rax
mov eax, dword ptr [rbx + 8*r12 + 4]
mov dword ptr [r15], eax
mov eax, dword ptr [rbx + 8*r13 + 4]
mov dword ptr [r15 + 4], eax
.LBB1_13:
mov rdi, rbx
call free@PLT
mov rax, r15
add rsp, 8
pop rbx
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
|
27
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O3
|
x86-64 clang 21.1.0
|
cmp:
mov eax, dword ptr [rdi]
sub eax, dword ptr [rsi]
ret
.LCPI1_1:
.long 2
.long 2
.zero 4
.zero 4
.LCPI1_2:
.long 4
.long 4
.zero 4
.zero 4
.LCPI1_3:
.long 0
.long 1
.long 0
.long 0
twoSum:
push rbp
push r15
push r14
push r13
push r12
push rbx
push rax
mov ebp, edx
mov r14d, esi
mov r15, rdi
movsxd r12, esi
lea rdi, [8*r12 + 8]
call malloc@PLT
mov rbx, rax
test r12d, r12d
jle .LBB1_1
mov esi, r14d
cmp r14d, 4
jae .LBB1_4
xor eax, eax
jmp .LBB1_7
.LBB1_1:
lea rcx, [rip + cmp]
mov edx, 8
mov rdi, rbx
mov rsi, r12
call qsort@PLT
xor r15d, r15d
jmp .LBB1_13
.LBB1_4:
mov eax, esi
and eax, 2147483644
movq xmm0, qword ptr [rip + .LCPI1_3]
xor ecx, ecx
movdqa xmm1, xmmword ptr [rip + .LCPI1_1]
movdqa xmm2, xmmword ptr [rip + .LCPI1_2]
.LBB1_5:
movdqa xmm3, xmm0
paddd xmm3, xmm1
movq xmm4, qword ptr [r15 + 4*rcx]
movq xmm5, qword ptr [r15 + 4*rcx + 8]
punpckldq xmm5, xmm3
punpckldq xmm4, xmm0
movdqu xmmword ptr [rbx + 8*rcx], xmm4
movdqu xmmword ptr [rbx + 8*rcx + 16], xmm5
add rcx, 4
paddd xmm0, xmm2
cmp rax, rcx
jne .LBB1_5
cmp eax, esi
je .LBB1_8
.LBB1_7:
mov ecx, dword ptr [r15 + 4*rax]
mov dword ptr [rbx + 8*rax], ecx
mov dword ptr [rbx + 8*rax + 4], eax
inc rax
cmp rsi, rax
jne .LBB1_7
.LBB1_8:
lea rcx, [rip + cmp]
mov edx, 8
mov rdi, rbx
call qsort@PLT
xor r15d, r15d
cmp r14d, 1
je .LBB1_13
dec r14d
xor eax, eax
.LBB1_10:
mov r12d, eax
movsxd r13, r14d
mov eax, dword ptr [rbx + 8*r13]
add eax, dword ptr [rbx + 8*r12]
cmp eax, ebp
je .LBB1_11
setl al
setge cl
movzx ecx, cl
sub r14d, ecx
movzx eax, al
add eax, r12d
cmp eax, r14d
jl .LBB1_10
jmp .LBB1_13
.LBB1_11:
mov edi, 8
call malloc@PLT
mov r15, rax
mov eax, dword ptr [rbx + 8*r12 + 4]
mov dword ptr [r15], eax
mov eax, dword ptr [rbx + 8*r13 + 4]
mov dword ptr [r15 + 4], eax
.LBB1_13:
mov rdi, rbx
call free@PLT
mov rax, r15
add rsp, 8
pop rbx
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
|
28
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O0
|
x86-64 gcc 15.2
|
cmp:
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-8], rdi
mov QWORD PTR [rbp-16], rsi
mov rax, QWORD PTR [rbp-8]
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR [rbp-16]
mov eax, DWORD PTR [rax]
sub edx, eax
mov eax, edx
pop rbp
ret
twoSum:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-40], rdi
mov DWORD PTR [rbp-44], esi
mov DWORD PTR [rbp-48], edx
mov eax, DWORD PTR [rbp-44]
add eax, 1
cdqe
sal rax, 3
mov rdi, rax
call malloc
mov QWORD PTR [rbp-16], rax
mov DWORD PTR [rbp-4], 0
jmp .L4
.L5:
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-40]
add rax, rdx
mov edx, DWORD PTR [rbp-4]
movsx rdx, edx
lea rcx, [0+rdx*8]
mov rdx, QWORD PTR [rbp-16]
add rdx, rcx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rdx], eax
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*8]
mov rax, QWORD PTR [rbp-16]
add rdx, rax
mov eax, DWORD PTR [rbp-4]
mov DWORD PTR [rdx+4], eax
add DWORD PTR [rbp-4], 1
.L4:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-44]
jl .L5
mov eax, DWORD PTR [rbp-44]
movsx rsi, eax
mov rax, QWORD PTR [rbp-16]
mov ecx, OFFSET FLAT:cmp
mov edx, 8
mov rdi, rax
call qsort
mov DWORD PTR [rbp-4], 0
mov eax, DWORD PTR [rbp-44]
sub eax, 1
mov DWORD PTR [rbp-8], eax
jmp .L6
.L10:
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*8]
mov rax, QWORD PTR [rbp-16]
add rax, rdx
mov edx, DWORD PTR [rax]
mov eax, DWORD PTR [rbp-8]
cdqe
lea rcx, [0+rax*8]
mov rax, QWORD PTR [rbp-16]
add rax, rcx
mov eax, DWORD PTR [rax]
add eax, edx
mov DWORD PTR [rbp-20], eax
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-48]
jne .L7
mov edi, 8
call malloc
mov QWORD PTR [rbp-32], rax
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*8]
mov rax, QWORD PTR [rbp-16]
add rax, rdx
mov edx, DWORD PTR [rax+4]
mov rax, QWORD PTR [rbp-32]
mov DWORD PTR [rax], edx
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*8]
mov rax, QWORD PTR [rbp-16]
add rax, rdx
mov rdx, QWORD PTR [rbp-32]
add rdx, 4
mov eax, DWORD PTR [rax+4]
mov DWORD PTR [rdx], eax
mov rax, QWORD PTR [rbp-16]
mov rdi, rax
call free
mov rax, QWORD PTR [rbp-32]
jmp .L8
.L7:
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-48]
jge .L9
add DWORD PTR [rbp-4], 1
jmp .L6
.L9:
sub DWORD PTR [rbp-8], 1
.L6:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-8]
jl .L10
mov rax, QWORD PTR [rbp-16]
mov rdi, rax
call free
mov eax, 0
.L8:
leave
ret
|
29
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O1
|
x86-64 gcc 15.2
|
cmp:
mov eax, DWORD PTR [rdi]
sub eax, DWORD PTR [rsi]
ret
twoSum:
push r13
push r12
push rbp
push rbx
sub rsp, 8
mov r13, rdi
mov r12d, esi
mov ebp, edx
lea edi, [rsi+1]
movsx rdi, edi
sal rdi, 3
call malloc
mov rbx, rax
test r12d, r12d
jle .L3
movsx rdx, r12d
mov esi, 0
.L4:
mov eax, DWORD PTR [r13+0+rsi*4]
mov DWORD PTR [rbx+rsi*8], eax
mov DWORD PTR [rbx+4+rsi*8], esi
add rsi, 1
cmp rsi, rdx
jne .L4
mov ecx, OFFSET FLAT:cmp
mov edx, 8
mov rdi, rbx
call qsort
lea edx, [r12-1]
test edx, edx
jle .L5
mov ecx, 0
jmp .L10
.L14:
mov edi, 8
call malloc
mov rbp, rax
mov eax, DWORD PTR [r13+4]
mov DWORD PTR [rbp+0], eax
mov eax, DWORD PTR [r12+4]
mov DWORD PTR [rbp+4], eax
mov rdi, rbx
call free
jmp .L2
.L8:
sub edx, 1
.L9:
cmp ecx, edx
jge .L5
.L10:
movsx rax, ecx
lea r13, [rbx+rax*8]
movsx rax, edx
lea r12, [rbx+rax*8]
mov eax, DWORD PTR [r12]
add eax, DWORD PTR [r13+0]
cmp eax, ebp
je .L14
jge .L8
add ecx, 1
jmp .L9
.L3:
movsx rsi, r12d
mov ecx, OFFSET FLAT:cmp
mov edx, 8
mov rdi, rax
call qsort
.L5:
mov rdi, rbx
call free
mov ebp, 0
.L2:
mov rax, rbp
add rsp, 8
pop rbx
pop rbp
pop r12
pop r13
ret
|
30
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O2
|
x86-64 gcc 15.2
|
cmp:
mov eax, DWORD PTR [rdi]
sub eax, DWORD PTR [rsi]
ret
twoSum:
push r14
mov r14d, esi
push r13
mov r13, rdi
lea edi, [rsi+1]
push r12
movsx rdi, edi
mov r12d, edx
push rbp
sal rdi, 3
push rbx
call malloc
movsx rsi, r14d
mov rbx, rax
test r14d, r14d
jle .L4
lea ebp, [r14-1]
cmp ebp, 2
jbe .L17
mov edx, r14d
mov edi, 4
movdqa xmm1, XMMWORD PTR .LC0[rip]
xor eax, eax
shr edx, 2
movd xmm3, edi
sal rdx, 4
pshufd xmm3, xmm3, 0
.L6:
movdqu xmm0, XMMWORD PTR [r13+0+rax]
movdqa xmm2, xmm0
punpckhdq xmm0, xmm1
punpckldq xmm2, xmm1
movups XMMWORD PTR [rbx+16+rax*2], xmm0
paddd xmm1, xmm3
movups XMMWORD PTR [rbx+rax*2], xmm2
add rax, 16
cmp rdx, rax
jne .L6
mov eax, r14d
and eax, -4
test r14b, 3
je .L26
.L5:
cdqe
.L8:
movd xmm0, DWORD PTR [r13+0+rax*4]
movd xmm4, eax
punpckldq xmm0, xmm4
movq QWORD PTR [rbx+rax*8], xmm0
add rax, 1
cmp r14d, eax
jg .L8
mov edx, 8
mov ecx, OFFSET FLAT:cmp
mov rdi, rbx
call qsort
mov edx, ebp
test ebp, ebp
je .L10
.L16:
xor ecx, ecx
jmp .L15
.L28:
add ecx, 1
cmp edx, ecx
jle .L10
.L15:
movsx rax, ecx
lea rbp, [rbx+rax*8]
movsx rax, edx
lea r13, [rbx+rax*8]
mov eax, DWORD PTR [r13+0]
add eax, DWORD PTR [rbp+0]
cmp eax, r12d
je .L27
jl .L28
sub edx, 1
cmp edx, ecx
jg .L15
.L10:
mov rdi, rbx
xor r12d, r12d
call free
pop rbx
mov rax, r12
pop rbp
pop r12
pop r13
pop r14
ret
.L26:
mov edx, 8
mov ecx, OFFSET FLAT:cmp
mov rdi, rbx
call qsort
mov edx, ebp
jmp .L16
.L27:
mov edi, 8
call malloc
movd xmm1, DWORD PTR [r13+4]
movd xmm0, DWORD PTR [rbp+4]
mov rdi, rbx
mov r12, rax
punpckldq xmm0, xmm1
movq QWORD PTR [rax], xmm0
call free
pop rbx
mov rax, r12
pop rbp
pop r12
pop r13
pop r14
ret
.L4:
mov ecx, OFFSET FLAT:cmp
mov edx, 8
mov rdi, rax
call qsort
jmp .L10
.L17:
xor eax, eax
jmp .L5
.LC0:
.long 0
.long 1
.long 2
.long 3
|
31
| 1
|
Two Sum
|
Easy
|
/*
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct data_s {
int val;
int idx;
} data_t;
int cmp(const void *a, const void *b) {
return ((data_t *)a)->val - ((data_t *)b)->val;
}
int* twoSum(int* nums, int numsSize, int target) {
int *indices;
int i, j, k;
#if 0
for (i = 0; i < numsSize - 1; i ++) {
for (j = i + 1; j < numsSize; j ++) {
if (nums[i] + nums[j] == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
#else
data_t *array;
array = malloc((numsSize + 1) * sizeof(data_t));
//assert(array);
for (i = 0; i < numsSize; i ++) {
array[i].val = nums[i];
array[i].idx = i;
}
qsort(array, numsSize, sizeof(data_t), cmp);
i = 0;
j = numsSize - 1;
while (i < j) {
k = array[i].val + array[j].val;
if (k == target) {
indices = malloc(2 * sizeof(int));
//assert(indices);
indices[0] = array[i].idx;
indices[1] = array[j].idx;
free(array);
return indices;
} else if (k < target) {
i ++;
} else {
j --;
}
}
free(array);
#endif
return NULL;
}
/*
Difficulty:Easy
Total Accepted:586.7K
Total Submissions:1.7M
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
Related Topics Array Hash Table
Similar Questions
3Sum
4Sum
Two Sum II - Input array is sorted
Two Sum III - Data structure design
Subarray Sum Equals K
Two Sum IV - Input is a BST
*/
|
x86-64
|
-O3
|
x86-64 gcc 15.2
|
cmp:
mov eax, DWORD PTR [rdi]
sub eax, DWORD PTR [rsi]
ret
twoSum:
push r14
mov r14d, esi
push r13
mov r13, rdi
lea edi, [rsi+1]
push r12
movsx rdi, edi
mov r12d, edx
push rbp
sal rdi, 3
push rbx
call malloc
movsx rsi, r14d
mov rbx, rax
test r14d, r14d
jle .L4
lea ebp, [r14-1]
cmp ebp, 2
jbe .L16
mov edx, r14d
mov edi, 4
movdqa xmm1, XMMWORD PTR .LC0[rip]
xor eax, eax
shr edx, 2
movd xmm3, edi
sal rdx, 4
pshufd xmm3, xmm3, 0
.L6:
movdqu xmm0, XMMWORD PTR [r13+0+rax]
movdqa xmm2, xmm0
punpckhdq xmm0, xmm1
punpckldq xmm2, xmm1
movups XMMWORD PTR [rbx+16+rax*2], xmm0
paddd xmm1, xmm3
movups XMMWORD PTR [rbx+rax*2], xmm2
add rax, 16
cmp rdx, rax
jne .L6
test r14b, 3
je .L7
mov eax, r14d
and eax, -4
.L5:
movsx rdx, eax
movd xmm4, eax
lea ecx, [rax+1]
movd xmm0, DWORD PTR [r13+0+rdx*4]
punpckldq xmm0, xmm4
movq QWORD PTR [rbx+rdx*8], xmm0
cmp r14d, ecx
jle .L8
movd xmm0, DWORD PTR [r13+4+rdx*4]
movd xmm5, ecx
add eax, 2
punpckldq xmm0, xmm5
movq QWORD PTR [rbx+8+rdx*8], xmm0
cmp r14d, eax
jle .L7
movd xmm0, DWORD PTR [r13+8+rdx*4]
movd xmm6, eax
punpckldq xmm0, xmm6
movq QWORD PTR [rbx+16+rdx*8], xmm0
.L7:
mov edx, 8
mov ecx, OFFSET FLAT:cmp
mov rdi, rbx
call qsort
mov edx, ebp
.L15:
xor ecx, ecx
jmp .L14
.L27:
add ecx, 1
cmp edx, ecx
jle .L9
.L14:
movsx rax, ecx
lea rbp, [rbx+rax*8]
movsx rax, edx
lea r13, [rbx+rax*8]
mov eax, DWORD PTR [r13+0]
add eax, DWORD PTR [rbp+0]
cmp eax, r12d
je .L26
jl .L27
sub edx, 1
cmp edx, ecx
jg .L14
.L9:
mov rdi, rbx
xor r12d, r12d
call free
pop rbx
mov rax, r12
pop rbp
pop r12
pop r13
pop r14
ret
.L8:
mov edx, 8
mov ecx, OFFSET FLAT:cmp
mov rdi, rbx
call qsort
mov edx, ebp
test ebp, ebp
jne .L15
jmp .L9
.L26:
mov edi, 8
call malloc
movd xmm1, DWORD PTR [r13+4]
movd xmm0, DWORD PTR [rbp+4]
mov rdi, rbx
mov r12, rax
punpckldq xmm0, xmm1
movq QWORD PTR [rax], xmm0
call free
pop rbx
mov rax, r12
pop rbp
pop r12
pop r13
pop r14
ret
.L4:
mov ecx, OFFSET FLAT:cmp
mov edx, 8
mov rdi, rax
call qsort
jmp .L9
.L16:
xor eax, eax
jmp .L5
.LC0:
.long 0
.long 1
.long 2
.long 3
|
32
| 2
|
Add Two Numbers
|
Medium
|
/*
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = NULL;
struct ListNode *p, *q;
int s = 0;
while (l1 || l2 || s != 0) {
p = malloc(sizeof(struct ListNode));
//assert(p);
if (l1) {
s += l1->val;
l1 = l1->next;
}
if (l2) {
s += l2->val;
l2 = l2->next;
}
p->val = s % 10;
p->next = NULL;
s = s / 10;
if (!head) {
head = p;
}
if (q) {
q->next = p;
}
q = p;
}
return head;
}
/*
Difficulty:Medium
Total Accepted:329.9K
Total Submissions:1.2M
Companies Amazon Microsoft Bloomberg Airbnb Adobe
Related Topics Linked List Math
Similar Questions
Multiply Strings
Add Binary
Sum of Two Integers
Add Strings
Add Two Numbers II
*/
|
aarch64
|
-O0
|
ARM64 gcc 15.2.0
|
addTwoNumbers:
stp x29, x30, [sp, -64]!
mov x29, sp
str x0, [sp, 24]
str x1, [sp, 16]
str xzr, [sp, 56]
str wzr, [sp, 44]
b .L2
.L7:
mov x0, 16
bl malloc
str x0, [sp, 32]
ldr x0, [sp, 24]
cmp x0, 0
beq .L3
ldr x0, [sp, 24]
ldr w0, [x0]
ldr w1, [sp, 44]
add w0, w1, w0
str w0, [sp, 44]
ldr x0, [sp, 24]
ldr x0, [x0, 8]
str x0, [sp, 24]
.L3:
ldr x0, [sp, 16]
cmp x0, 0
beq .L4
ldr x0, [sp, 16]
ldr w0, [x0]
ldr w1, [sp, 44]
add w0, w1, w0
str w0, [sp, 44]
ldr x0, [sp, 16]
ldr x0, [x0, 8]
str x0, [sp, 16]
.L4:
ldr w1, [sp, 44]
mov w0, 10
sdiv w2, w1, w0
mov w0, w2
lsl w0, w0, 2
add w0, w0, w2
lsl w0, w0, 1
sub w1, w1, w0
ldr x0, [sp, 32]
str w1, [x0]
ldr x0, [sp, 32]
str xzr, [x0, 8]
ldr w0, [sp, 44]
mov w1, 26215
movk w1, 0x6666, lsl 16
smull x1, w0, w1
lsr x1, x1, 32
asr w1, w1, 2
asr w0, w0, 31
sub w0, w1, w0
str w0, [sp, 44]
ldr x0, [sp, 56]
cmp x0, 0
bne .L5
ldr x0, [sp, 32]
str x0, [sp, 56]
.L5:
ldr x0, [sp, 48]
cmp x0, 0
beq .L6
ldr x0, [sp, 48]
ldr x1, [sp, 32]
str x1, [x0, 8]
.L6:
ldr x0, [sp, 32]
str x0, [sp, 48]
.L2:
ldr x0, [sp, 24]
cmp x0, 0
bne .L7
ldr x0, [sp, 16]
cmp x0, 0
bne .L7
ldr w0, [sp, 44]
cmp w0, 0
bne .L7
ldr x0, [sp, 56]
ldp x29, x30, [sp], 64
ret
|
33
| 2
|
Add Two Numbers
|
Medium
|
/*
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = NULL;
struct ListNode *p, *q;
int s = 0;
while (l1 || l2 || s != 0) {
p = malloc(sizeof(struct ListNode));
//assert(p);
if (l1) {
s += l1->val;
l1 = l1->next;
}
if (l2) {
s += l2->val;
l2 = l2->next;
}
p->val = s % 10;
p->next = NULL;
s = s / 10;
if (!head) {
head = p;
}
if (q) {
q->next = p;
}
q = p;
}
return head;
}
/*
Difficulty:Medium
Total Accepted:329.9K
Total Submissions:1.2M
Companies Amazon Microsoft Bloomberg Airbnb Adobe
Related Topics Linked List Math
Similar Questions
Multiply Strings
Add Binary
Sum of Two Integers
Add Strings
Add Two Numbers II
*/
|
aarch64
|
-O1
|
ARM64 gcc 15.2.0
|
addTwoNumbers:
stp x29, x30, [sp, -80]!
mov x29, sp
stp x19, x20, [sp, 16]
stp x21, x22, [sp, 32]
mov x20, x0
orr x0, x0, x1
cbz x0, .L8
stp x23, x24, [sp, 48]
stp x25, x26, [sp, 64]
mov x21, x1
mov w19, 0
mov x22, 0
mov x26, 16
mov w25, 10
mov w24, 26215
movk w24, 0x6666, lsl 16
b .L7
.L6:
orr x0, x21, x20
cmp x0, 0
ccmp w19, 0, 0, eq
beq .L12
.L7:
mov x23, x2
mov x0, x26
bl malloc
mov x2, x0
cbz x20, .L3
ldr w0, [x20]
add w19, w19, w0
ldr x20, [x20, 8]
.L3:
cbz x21, .L4
ldr w0, [x21]
add w19, w19, w0
ldr x21, [x21, 8]
.L4:
sdiv w0, w19, w25
add w0, w0, w0, lsl 2
sub w0, w19, w0, lsl 1
str w0, [x2]
str xzr, [x2, 8]
smull x0, w19, w24
asr x0, x0, 34
sub w19, w0, w19, asr 31
cmp x22, 0
csel x22, x22, x2, ne
cbz x23, .L6
str x2, [x23, 8]
b .L6
.L12:
ldp x23, x24, [sp, 48]
ldp x25, x26, [sp, 64]
.L1:
mov x0, x22
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x29, x30, [sp], 80
ret
.L8:
mov x22, 0
b .L1
|
34
| 2
|
Add Two Numbers
|
Medium
|
/*
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = NULL;
struct ListNode *p, *q;
int s = 0;
while (l1 || l2 || s != 0) {
p = malloc(sizeof(struct ListNode));
//assert(p);
if (l1) {
s += l1->val;
l1 = l1->next;
}
if (l2) {
s += l2->val;
l2 = l2->next;
}
p->val = s % 10;
p->next = NULL;
s = s / 10;
if (!head) {
head = p;
}
if (q) {
q->next = p;
}
q = p;
}
return head;
}
/*
Difficulty:Medium
Total Accepted:329.9K
Total Submissions:1.2M
Companies Amazon Microsoft Bloomberg Airbnb Adobe
Related Topics Linked List Math
Similar Questions
Multiply Strings
Add Binary
Sum of Two Integers
Add Strings
Add Two Numbers II
*/
|
aarch64
|
-O2
|
ARM64 gcc 15.2.0
|
addTwoNumbers:
stp x29, x30, [sp, -80]!
mov x29, sp
stp x19, x20, [sp, 16]
mov x20, x1
mov w19, 0
stp x21, x22, [sp, 32]
mov w21, 26215
mov w22, 10
stp x23, x24, [sp, 48]
mov x23, x0
orr x0, x23, x20
str x25, [sp, 64]
mov x24, 0
movk w21, 0x6666, lsl 16
cbnz x0, .L7
.L21:
cbz w19, .L1
mov x23, 0
mov x20, 0
mov x0, 16
bl malloc
.L4:
sdiv w2, w19, w22
smull x1, w19, w21
str xzr, [x0, 8]
cmp x24, 0
csel x24, x24, x0, ne
asr x1, x1, 34
add w2, w2, w2, lsl 2
sub w2, w19, w2, lsl 1
str w2, [x0]
sub w19, w1, w19, asr 31
cbz x25, .L6
str x0, [x25, 8]
.L6:
mov x25, x0
orr x0, x23, x20
cbz x0, .L21
.L7:
mov x0, 16
bl malloc
cbz x23, .L3
ldr w1, [x23]
ldr x23, [x23, 8]
add w19, w19, w1
cbz x20, .L4
.L3:
ldr w1, [x20]
ldr x20, [x20, 8]
add w19, w19, w1
b .L4
.L1:
ldr x25, [sp, 64]
mov x0, x24
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x23, x24, [sp, 48]
ldp x29, x30, [sp], 80
ret
|
35
| 2
|
Add Two Numbers
|
Medium
|
/*
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = NULL;
struct ListNode *p, *q;
int s = 0;
while (l1 || l2 || s != 0) {
p = malloc(sizeof(struct ListNode));
//assert(p);
if (l1) {
s += l1->val;
l1 = l1->next;
}
if (l2) {
s += l2->val;
l2 = l2->next;
}
p->val = s % 10;
p->next = NULL;
s = s / 10;
if (!head) {
head = p;
}
if (q) {
q->next = p;
}
q = p;
}
return head;
}
/*
Difficulty:Medium
Total Accepted:329.9K
Total Submissions:1.2M
Companies Amazon Microsoft Bloomberg Airbnb Adobe
Related Topics Linked List Math
Similar Questions
Multiply Strings
Add Binary
Sum of Two Integers
Add Strings
Add Two Numbers II
*/
|
aarch64
|
-O3
|
ARM64 gcc 15.2.0
|
addTwoNumbers:
stp x29, x30, [sp, -80]!
mov x29, sp
stp x19, x20, [sp, 16]
mov x20, x1
mov w19, 0
stp x21, x22, [sp, 32]
mov w21, 26215
mov w22, 10
stp x23, x24, [sp, 48]
mov x23, x0
orr x1, x23, x20
str x25, [sp, 64]
mov x24, 0
movk w21, 0x6666, lsl 16
mov x0, 16
cbnz x1, .L7
.L21:
cbz w19, .L1
mov x23, 0
mov x20, 0
bl malloc
.L4:
sdiv w2, w19, w22
smull x1, w19, w21
str xzr, [x0, 8]
cmp x24, 0
csel x24, x24, x0, ne
asr x1, x1, 34
add w2, w2, w2, lsl 2
sub w2, w19, w2, lsl 1
str w2, [x0]
sub w19, w1, w19, asr 31
cbz x25, .L6
str x0, [x25, 8]
.L6:
mov x25, x0
orr x1, x23, x20
mov x0, 16
cbz x1, .L21
.L7:
mov x0, 16
bl malloc
cbz x23, .L3
ldr w1, [x23]
ldr x23, [x23, 8]
add w19, w19, w1
cbz x20, .L4
.L3:
ldr w1, [x20]
ldr x20, [x20, 8]
add w19, w19, w1
b .L4
.L1:
ldr x25, [sp, 64]
mov x0, x24
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x23, x24, [sp, 48]
ldp x29, x30, [sp], 80
ret
|
End of preview. Expand
in Data Studio
LeetCode Assembly Dataset
441 LeetCode problems solved in C, compiled to assembly across 4 architectures, 2 compilers, and 4 optimization levels using GCC and Clang via the Godbolt Compiler Explorer API.
Dataset Summary
| Stat | Value |
|---|---|
| Total rows | 14,112 |
| Unique problems | 441 |
| Architectures | x86-64, AArch64, MIPS64, RISC-V 64 |
| Compilers | GCC 15.2, Clang 21.1.0 |
| Optimization levels | -O0, -O1, -O2, -O3 |
| Compilation success rate | 100% |
| Difficulty split | Easy: 98, Medium: 259, Hard: 84 |
Each problem has 32 assembly variants (4 architectures x 2 compilers x 4 optimization levels), making this useful for studying how the same algorithm compiles differently across ISAs, compilers, and optimization settings.
Key Insights
- -O3 often produces more code than -O0, not less. Across all architectures, -O3 output is 37-73% larger on average due to aggressive inlining and loop unrolling.
- -O1 consistently produces the most compact assembly across all four architectures.
- Recursive tree problems see extreme -O3 blowup. The top 10 largest O3-vs-O0 ratios are all binary tree problems. Worst case: Longest Univalue Path goes from 90 lines (O0) to 3,809 lines (O3) -- a 42x increase.
- Iterative and math problems shrink dramatically at -O3. Flip String to Monotone Increasing drops from 89 to 8 lines (91% reduction). Nim Game compiles down to just 4 lines.
- AArch64 produces the most compact assembly at -O2 (avg 153 lines), followed by x86-64 (170), RISC-V (170), and MIPS64 (236). MIPS is 39% more verbose than AArch64.
- Hard problems produce 2x more assembly than Easy problems at -O2 on x86-64 (247 vs 121 avg lines). Algorithmic complexity maps directly to code size.
- The largest output is LFU Cache (1,121 lines at -O0 on x86-64), a complex doubly-linked list plus hash table implementation.
- The smallest output is Nim Game at -O3 (4 lines on x86-64) -- the entire solution optimizes to a single bitwise AND.
Use Cases
- Training or evaluating models on C-to-assembly translation
- Studying how optimization levels affect generated code
- Compiler behavior research
Schema
| Column | Type | Description |
|---|---|---|
id |
int | Unique row identifier (0-14111) |
problem_id |
int | LeetCode problem number |
problem_title |
string | Problem name (e.g. "Two Sum") |
difficulty |
string | Easy, Medium, or Hard |
c_source |
string | Complete C source code |
architecture |
string | Target ISA: x86-64, aarch64, mips64, riscv64 |
optimization |
string | Optimization flag: -O0, -O1, -O2, -O3 |
compiler |
string | Compiler version used (e.g. x86-64 gcc 15.2, x86-64 clang 21.1.0) |
assembly |
string | Compiled assembly output |
Usage
from datasets import load_dataset
ds = load_dataset("ronantakizawa/leetcode-assembly", split="train")
# Get all x86-64 GCC assembly at -O2
x86_gcc_O2 = ds.filter(lambda r: r["architecture"] == "x86-64" and r["compiler"].startswith("x86-64 gcc") and r["optimization"] == "-O2")
# Compare GCC vs Clang for the same problem
two_sum = ds.filter(lambda r: r["problem_id"] == 1 and r["architecture"] == "x86-64" and r["optimization"] == "-O2")
for row in two_sum:
print(f"--- {row['compiler']} ---")
print(row["assembly"][:200])
print()
Sources
C solutions were collected from three open-source GitHub repositories of pure-C LeetCode solutions:
- vli02/leetcode (383 solutions)
- begeekmyfriend/leetcode (215 solutions)
- lennylxx/leetcode (159 solutions)
After deduplication, 441 unique problems remained. Solutions are self-contained C files using only the standard library (no C++ STL), producing clean and readable assembly output.
- Downloads last month
- -