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
|
36
| 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
|
armv8-a clang 21.1.0
|
addTwoNumbers:
sub sp, sp, #64
stp x29, x30, [sp, #48]
add x29, sp, #48
stur x0, [x29, #-8]
stur x1, [x29, #-16]
str xzr, [sp, #24]
str wzr, [sp, #4]
b .LBB0_1
.LBB0_1:
ldur x8, [x29, #-8]
mov w9, #1
str w9, [sp]
cbnz x8, .LBB0_4
b .LBB0_2
.LBB0_2:
ldur x8, [x29, #-16]
mov w9, #1
str w9, [sp]
cbnz x8, .LBB0_4
b .LBB0_3
.LBB0_3:
ldr w8, [sp, #4]
subs w8, w8, #0
cset w8, ne
str w8, [sp]
b .LBB0_4
.LBB0_4:
ldr w8, [sp]
tbz w8, #0, .LBB0_14
b .LBB0_5
.LBB0_5:
mov x0, #16
bl malloc
str x0, [sp, #16]
ldur x8, [x29, #-8]
cbz x8, .LBB0_7
b .LBB0_6
.LBB0_6:
ldur x8, [x29, #-8]
ldr w9, [x8]
ldr w8, [sp, #4]
add w8, w8, w9
str w8, [sp, #4]
ldur x8, [x29, #-8]
ldr x8, [x8, #8]
stur x8, [x29, #-8]
b .LBB0_7
.LBB0_7:
ldur x8, [x29, #-16]
cbz x8, .LBB0_9
b .LBB0_8
.LBB0_8:
ldur x8, [x29, #-16]
ldr w9, [x8]
ldr w8, [sp, #4]
add w8, w8, w9
str w8, [sp, #4]
ldur x8, [x29, #-16]
ldr x8, [x8, #8]
stur x8, [x29, #-16]
b .LBB0_9
.LBB0_9:
ldr w8, [sp, #4]
mov w9, #10
sdiv w10, w8, w9
mul w10, w10, w9
subs w8, w8, w10
ldr x10, [sp, #16]
str w8, [x10]
ldr x8, [sp, #16]
str xzr, [x8, #8]
ldr w8, [sp, #4]
sdiv w8, w8, w9
str w8, [sp, #4]
ldr x8, [sp, #24]
cbnz x8, .LBB0_11
b .LBB0_10
.LBB0_10:
ldr x8, [sp, #16]
str x8, [sp, #24]
b .LBB0_11
.LBB0_11:
ldr x8, [sp, #8]
cbz x8, .LBB0_13
b .LBB0_12
.LBB0_12:
ldr x8, [sp, #16]
ldr x9, [sp, #8]
str x8, [x9, #8]
b .LBB0_13
.LBB0_13:
ldr x8, [sp, #16]
str x8, [sp, #8]
b .LBB0_1
.LBB0_14:
ldr x0, [sp, #24]
ldp x29, x30, [sp, #48]
add sp, sp, #64
ret
|
37
| 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
|
armv8-a clang 21.1.0
|
addTwoNumbers:
stp x29, x30, [sp, #-96]!
str x27, [sp, #16]
stp x26, x25, [sp, #32]
stp x24, x23, [sp, #48]
stp x22, x21, [sp, #64]
stp x20, x19, [sp, #80]
mov x29, sp
cmp x0, #0
orr x8, x0, x1
cset w27, ne
cmp x1, #0
cset w26, ne
cbz x8, .LBB0_12
mov w22, #26215
mov x19, x1
mov x20, x0
mov w24, wzr
mov x21, xzr
movk w22, #26214, lsl #16
mov w23, #10
.LBB0_2:
mov w0, #16
bl malloc
tbz w27, #0, .LBB0_5
ldr w8, [x20]
ldr x20, [x20, #8]
add w24, w8, w24
tbnz w26, #0, .LBB0_6
.LBB0_4:
mov x19, xzr
mov w8, w24
b .LBB0_7
.LBB0_5:
mov x20, xzr
tbz w26, #0, .LBB0_4
.LBB0_6:
ldr w8, [x19]
ldr x19, [x19, #8]
add w8, w8, w24
.LBB0_7:
smull x9, w8, w22
cmp x21, #0
str xzr, [x0, #8]
csel x21, x0, x21, eq
asr x9, x9, #34
add w24, w9, w9, lsr #31
msub w9, w24, w23, w8
str w9, [x0]
cbz x25, .LBB0_9
str x0, [x25, #8]
.LBB0_9:
cmp x20, #0
mov x25, x0
cset w27, ne
cmp x19, #0
cset w26, ne
cbnz x20, .LBB0_2
mov x25, x0
cbnz x19, .LBB0_2
sub w8, w8, #10
mov x25, x0
cmn w8, #19
b.lo .LBB0_2
b .LBB0_13
.LBB0_12:
mov x21, xzr
.LBB0_13:
mov x0, x21
ldp x20, x19, [sp, #80]
ldr x27, [sp, #16]
ldp x22, x21, [sp, #64]
ldp x24, x23, [sp, #48]
ldp x26, x25, [sp, #32]
ldp x29, x30, [sp], #96
ret
|
38
| 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
|
armv8-a clang 21.1.0
|
addTwoNumbers:
stp x29, x30, [sp, #-96]!
str x27, [sp, #16]
stp x26, x25, [sp, #32]
stp x24, x23, [sp, #48]
stp x22, x21, [sp, #64]
stp x20, x19, [sp, #80]
mov x29, sp
cmp x0, #0
orr x8, x0, x1
cset w27, ne
cmp x1, #0
cset w26, ne
cbz x8, .LBB0_12
mov w22, #26215
mov x19, x1
mov x20, x0
mov w24, wzr
mov x21, xzr
movk w22, #26214, lsl #16
mov w23, #10
.LBB0_2:
mov w0, #16
bl malloc
tbz w27, #0, .LBB0_5
ldr w8, [x20]
ldr x20, [x20, #8]
add w24, w8, w24
tbnz w26, #0, .LBB0_6
.LBB0_4:
mov x19, xzr
mov w8, w24
b .LBB0_7
.LBB0_5:
mov x20, xzr
tbz w26, #0, .LBB0_4
.LBB0_6:
ldr w8, [x19]
ldr x19, [x19, #8]
add w8, w8, w24
.LBB0_7:
smull x9, w8, w22
cmp x21, #0
str xzr, [x0, #8]
csel x21, x0, x21, eq
asr x9, x9, #34
add w24, w9, w9, lsr #31
msub w9, w24, w23, w8
str w9, [x0]
cbz x25, .LBB0_9
str x0, [x25, #8]
.LBB0_9:
cmp x20, #0
mov x25, x0
cset w27, ne
cmp x19, #0
cset w26, ne
cbnz x20, .LBB0_2
mov x25, x0
cbnz x19, .LBB0_2
sub w8, w8, #10
mov x25, x0
cmn w8, #19
b.lo .LBB0_2
b .LBB0_13
.LBB0_12:
mov x21, xzr
.LBB0_13:
mov x0, x21
ldp x20, x19, [sp, #80]
ldr x27, [sp, #16]
ldp x22, x21, [sp, #64]
ldp x24, x23, [sp, #48]
ldp x26, x25, [sp, #32]
ldp x29, x30, [sp], #96
ret
|
39
| 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
|
armv8-a clang 21.1.0
|
addTwoNumbers:
stp x29, x30, [sp, #-96]!
str x27, [sp, #16]
stp x26, x25, [sp, #32]
stp x24, x23, [sp, #48]
stp x22, x21, [sp, #64]
stp x20, x19, [sp, #80]
mov x29, sp
cmp x0, #0
orr x8, x0, x1
cset w27, ne
cmp x1, #0
cset w26, ne
cbz x8, .LBB0_12
mov w22, #26215
mov x19, x1
mov x20, x0
mov w24, wzr
mov x21, xzr
movk w22, #26214, lsl #16
mov w23, #10
.LBB0_2:
mov w0, #16
bl malloc
tbz w27, #0, .LBB0_5
ldr w8, [x20]
ldr x20, [x20, #8]
add w24, w8, w24
tbnz w26, #0, .LBB0_6
.LBB0_4:
mov x19, xzr
mov w8, w24
b .LBB0_7
.LBB0_5:
mov x20, xzr
tbz w26, #0, .LBB0_4
.LBB0_6:
ldr w8, [x19]
ldr x19, [x19, #8]
add w8, w8, w24
.LBB0_7:
smull x9, w8, w22
cmp x21, #0
str xzr, [x0, #8]
csel x21, x0, x21, eq
asr x9, x9, #34
add w24, w9, w9, lsr #31
msub w9, w24, w23, w8
str w9, [x0]
cbz x25, .LBB0_9
str x0, [x25, #8]
.LBB0_9:
cmp x20, #0
mov x25, x0
cset w27, ne
cmp x19, #0
cset w26, ne
cbnz x20, .LBB0_2
mov x25, x0
cbnz x19, .LBB0_2
sub w8, w8, #10
mov x25, x0
cmn w8, #19
b.lo .LBB0_2
b .LBB0_13
.LBB0_12:
mov x21, xzr
.LBB0_13:
mov x0, x21
ldp x20, x19, [sp, #80]
ldr x27, [sp, #16]
ldp x22, x21, [sp, #64]
ldp x24, x23, [sp, #48]
ldp x26, x25, [sp, #32]
ldp x29, x30, [sp], #96
ret
|
40
| 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
*/
|
mips64
|
-O0
|
mips64 clang 21.1.0
|
addTwoNumbers:
.Lfunc_begin0 = .Ltmp0
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(addTwoNumbers)))
daddu $1, $1, $25
daddiu $1, $1, %lo(%neg(%gp_rel(addTwoNumbers)))
sd $1, 16($fp)
sd $4, 64($fp)
sd $5, 56($fp)
daddiu $1, $zero, 0
sd $zero, 48($fp)
sw $zero, 28($fp)
b .LBB0_1
nop
.LBB0_1:
ld $1, 64($fp)
addiu $2, $zero, 1
sw $2, 12($fp)
bnez $1, .LBB0_6
nop
b .LBB0_3
nop
.LBB0_3:
ld $1, 56($fp)
addiu $2, $zero, 1
sw $2, 12($fp)
bnez $1, .LBB0_6
nop
b .LBB0_5
nop
.LBB0_5:
lw $1, 28($fp)
sltu $1, $zero, $1
sw $1, 12($fp)
b .LBB0_6
nop
.LBB0_6:
lw $1, 12($fp)
andi $1, $1, 1
beqz $1, .LBB0_21
nop
b .LBB0_8
nop
.LBB0_8:
ld $gp, 16($fp)
ld $25, %call16(malloc)($gp)
daddiu $4, $zero, 16
jalr $25
nop
sd $2, 40($fp)
ld $1, 64($fp)
beqz $1, .LBB0_11
nop
b .LBB0_10
nop
.LBB0_10:
ld $1, 64($fp)
lw $2, 0($1)
lw $1, 28($fp)
addu $1, $1, $2
sw $1, 28($fp)
ld $1, 64($fp)
ld $1, 8($1)
sd $1, 64($fp)
b .LBB0_11
nop
.LBB0_11:
ld $1, 56($fp)
beqz $1, .LBB0_14
nop
b .LBB0_13
nop
.LBB0_13:
ld $1, 56($fp)
lw $2, 0($1)
lw $1, 28($fp)
addu $1, $1, $2
sw $1, 28($fp)
ld $1, 56($fp)
ld $1, 8($1)
sd $1, 56($fp)
b .LBB0_14
nop
.LBB0_14:
lw $1, 28($fp)
lui $2, 26214
ori $2, $2, 26215
mult $1, $2
mfhi $3
srl $4, $3, 31
sra $3, $3, 2
addu $3, $3, $4
sll $4, $3, 1
sll $3, $3, 3
addu $3, $3, $4
subu $1, $1, $3
ld $3, 40($fp)
sw $1, 0($3)
ld $1, 40($fp)
daddiu $3, $zero, 0
sd $zero, 8($1)
lw $1, 28($fp)
mult $1, $2
mfhi $1
srl $2, $1, 31
sra $1, $1, 2
addu $1, $1, $2
sw $1, 28($fp)
ld $1, 48($fp)
bnez $1, .LBB0_17
nop
b .LBB0_16
nop
.LBB0_16:
ld $1, 40($fp)
sd $1, 48($fp)
b .LBB0_17
nop
.LBB0_17:
ld $1, 32($fp)
beqz $1, .LBB0_20
nop
b .LBB0_19
nop
.LBB0_19:
ld $1, 40($fp)
ld $2, 32($fp)
sd $1, 8($2)
b .LBB0_20
nop
.LBB0_20:
ld $1, 40($fp)
sd $1, 32($fp)
b .LBB0_1
nop
.LBB0_21:
ld $2, 48($fp)
move $sp, $fp
ld $gp, 72($sp)
ld $fp, 80($sp)
ld $ra, 88($sp)
daddiu $sp, $sp, 96
jr $ra
nop
|
41
| 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
*/
|
mips64
|
-O1
|
mips64 clang 21.1.0
|
addTwoNumbers:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -96
sd $ra, 88($sp)
sd $fp, 80($sp)
sd $gp, 72($sp)
sd $23, 64($sp)
sd $22, 56($sp)
sd $21, 48($sp)
sd $20, 40($sp)
sd $19, 32($sp)
sd $18, 24($sp)
sd $17, 16($sp)
sd $16, 8($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(addTwoNumbers)))
daddu $2, $1, $25
or $1, $4, $5
beqz $1, .LBB0_12
daddiu $18, $zero, 0
move $16, $5
move $17, $4
daddiu $gp, $2, %lo(%neg(%gp_rel(addTwoNumbers)))
sltu $22, $zero, $5
sltu $23, $zero, $4
daddiu $18, $zero, 0
addiu $20, $zero, 0
lui $1, 26214
ori $19, $1, 26215
.LBB0_2:
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $zero, 16
andi $1, $23, 1
beqz $1, .LBB0_5
nop
lw $1, 0($17)
addu $20, $1, $20
ld $17, 8($17)
andi $1, $22, 1
bnez $1, .LBB0_6
nop
.LBB0_4:
daddiu $16, $zero, 0
b .LBB0_7
move $3, $20
.LBB0_5:
andi $1, $22, 1
beqz $1, .LBB0_4
daddiu $17, $zero, 0
.LBB0_6:
lw $1, 0($16)
addu $3, $1, $20
ld $16, 8($16)
.LBB0_7:
mult $3, $19
mfhi $1
srl $4, $1, 31
sra $1, $1, 2
addu $20, $1, $4
sd $zero, 8($2)
sll $1, $20, 1
sll $4, $20, 3
addu $1, $4, $1
subu $1, $3, $1
beqz $21, .LBB0_9
sw $1, 0($2)
sd $2, 8($21)
.LBB0_9:
movz $18, $2, $18
sltu $22, $zero, $16
sltu $23, $zero, $17
bnez $17, .LBB0_2
move $21, $2
bnez $16, .LBB0_2
move $21, $2
addiu $1, $3, -10
sltiu $1, $1, -19
bnez $1, .LBB0_2
move $21, $2
.LBB0_12:
move $2, $18
move $sp, $fp
ld $16, 8($sp)
ld $17, 16($sp)
ld $18, 24($sp)
ld $19, 32($sp)
ld $20, 40($sp)
ld $21, 48($sp)
ld $22, 56($sp)
ld $23, 64($sp)
ld $gp, 72($sp)
ld $fp, 80($sp)
ld $ra, 88($sp)
jr $ra
daddiu $sp, $sp, 96
|
42
| 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
*/
|
mips64
|
-O2
|
mips64 clang 21.1.0
|
addTwoNumbers:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -96
sd $ra, 88($sp)
sd $fp, 80($sp)
sd $gp, 72($sp)
sd $23, 64($sp)
sd $22, 56($sp)
sd $21, 48($sp)
sd $20, 40($sp)
sd $19, 32($sp)
sd $18, 24($sp)
sd $17, 16($sp)
sd $16, 8($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(addTwoNumbers)))
daddu $2, $1, $25
or $1, $4, $5
beqz $1, .LBB0_12
daddiu $18, $zero, 0
move $16, $5
move $17, $4
daddiu $gp, $2, %lo(%neg(%gp_rel(addTwoNumbers)))
sltu $22, $zero, $5
sltu $23, $zero, $4
daddiu $18, $zero, 0
addiu $20, $zero, 0
lui $1, 26214
ori $19, $1, 26215
.LBB0_2:
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $zero, 16
andi $1, $23, 1
beqz $1, .LBB0_5
nop
lw $1, 0($17)
addu $20, $1, $20
ld $17, 8($17)
andi $1, $22, 1
bnez $1, .LBB0_6
nop
.LBB0_4:
daddiu $16, $zero, 0
b .LBB0_7
move $3, $20
.LBB0_5:
andi $1, $22, 1
beqz $1, .LBB0_4
daddiu $17, $zero, 0
.LBB0_6:
lw $1, 0($16)
addu $3, $1, $20
ld $16, 8($16)
.LBB0_7:
mult $3, $19
mfhi $1
srl $4, $1, 31
sra $1, $1, 2
addu $20, $1, $4
sd $zero, 8($2)
sll $1, $20, 1
sll $4, $20, 3
addu $1, $4, $1
subu $1, $3, $1
beqz $21, .LBB0_9
sw $1, 0($2)
sd $2, 8($21)
.LBB0_9:
movz $18, $2, $18
sltu $22, $zero, $16
sltu $23, $zero, $17
bnez $17, .LBB0_2
move $21, $2
bnez $16, .LBB0_2
move $21, $2
addiu $1, $3, -10
sltiu $1, $1, -19
bnez $1, .LBB0_2
move $21, $2
.LBB0_12:
move $2, $18
move $sp, $fp
ld $16, 8($sp)
ld $17, 16($sp)
ld $18, 24($sp)
ld $19, 32($sp)
ld $20, 40($sp)
ld $21, 48($sp)
ld $22, 56($sp)
ld $23, 64($sp)
ld $gp, 72($sp)
ld $fp, 80($sp)
ld $ra, 88($sp)
jr $ra
daddiu $sp, $sp, 96
|
43
| 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
*/
|
mips64
|
-O3
|
mips64 clang 21.1.0
|
addTwoNumbers:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -96
sd $ra, 88($sp)
sd $fp, 80($sp)
sd $gp, 72($sp)
sd $23, 64($sp)
sd $22, 56($sp)
sd $21, 48($sp)
sd $20, 40($sp)
sd $19, 32($sp)
sd $18, 24($sp)
sd $17, 16($sp)
sd $16, 8($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(addTwoNumbers)))
daddu $2, $1, $25
or $1, $4, $5
beqz $1, .LBB0_12
daddiu $18, $zero, 0
lui $1, 26214
move $16, $5
move $17, $4
daddiu $gp, $2, %lo(%neg(%gp_rel(addTwoNumbers)))
sltu $22, $zero, $5
sltu $23, $zero, $4
daddiu $18, $zero, 0
addiu $20, $zero, 0
ori $19, $1, 26215
.LBB0_2:
ld $25, %call16(malloc)($gp)
jalr $25
daddiu $4, $zero, 16
andi $1, $23, 1
beqz $1, .LBB0_5
nop
lw $1, 0($17)
ld $17, 8($17)
addu $20, $1, $20
andi $1, $22, 1
bnez $1, .LBB0_6
nop
.LBB0_4:
daddiu $16, $zero, 0
b .LBB0_7
move $3, $20
.LBB0_5:
andi $1, $22, 1
beqz $1, .LBB0_4
daddiu $17, $zero, 0
.LBB0_6:
lw $1, 0($16)
ld $16, 8($16)
addu $3, $1, $20
.LBB0_7:
mult $3, $19
sd $zero, 8($2)
mfhi $1
srl $4, $1, 31
sra $1, $1, 2
addu $20, $1, $4
sll $1, $20, 1
sll $4, $20, 3
addu $1, $4, $1
subu $1, $3, $1
beqz $21, .LBB0_9
sw $1, 0($2)
sd $2, 8($21)
.LBB0_9:
movz $18, $2, $18
sltu $22, $zero, $16
sltu $23, $zero, $17
bnez $17, .LBB0_2
move $21, $2
bnez $16, .LBB0_2
move $21, $2
addiu $1, $3, -10
sltiu $1, $1, -19
bnez $1, .LBB0_2
move $21, $2
.LBB0_12:
move $2, $18
move $sp, $fp
ld $16, 8($sp)
ld $17, 16($sp)
ld $18, 24($sp)
ld $19, 32($sp)
ld $20, 40($sp)
ld $21, 48($sp)
ld $22, 56($sp)
ld $23, 64($sp)
ld $gp, 72($sp)
ld $fp, 80($sp)
ld $ra, 88($sp)
jr $ra
daddiu $sp, $sp, 96
|
44
| 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
*/
|
mips64
|
-O0
|
mips64 gcc 15.2.0
|
addTwoNumbers:
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(addTwoNumbers)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(addTwoNumbers)))
sd $4,32($fp)
sd $5,40($fp)
sd $0,0($fp)
sw $0,16($fp)
b .L2
nop
.L7:
li $4,16 # 0x10
ld $2,%call16(malloc)($28)
mtlo $2
mflo $25
jalr $25
nop
sd $2,24($fp)
ld $2,32($fp)
beq $2,$0,.L3
nop
ld $2,32($fp)
lw $2,0($2)
lw $3,16($fp)
addu $2,$3,$2
sw $2,16($fp)
ld $2,32($fp)
ld $2,8($2)
sd $2,32($fp)
.L3:
ld $2,40($fp)
beq $2,$0,.L4
nop
ld $2,40($fp)
lw $2,0($2)
lw $3,16($fp)
addu $2,$3,$2
sw $2,16($fp)
ld $2,40($fp)
ld $2,8($2)
sd $2,40($fp)
.L4:
lw $4,16($fp)
move $3,$4
move $2,$3
dsll $2,$2,1
daddu $2,$2,$3
dsll $5,$2,4
daddu $2,$2,$5
dsll $5,$2,8
daddu $2,$2,$5
dsll $5,$2,16
daddu $2,$2,$5
dsll $2,$2,1
daddu $2,$2,$3
dsrl $2,$2,32
sll $2,$2,0
sra $3,$2,2
sra $2,$4,31
subu $3,$3,$2
move $2,$3
sll $2,$2,2
addu $2,$2,$3
sll $2,$2,1
subu $2,$4,$2
move $3,$2
ld $2,24($fp)
sw $3,0($2)
ld $2,24($fp)
sd $0,8($2)
lw $5,16($fp)
move $3,$5
move $2,$3
dsll $2,$2,1
daddu $2,$2,$3
dsll $4,$2,4
daddu $2,$2,$4
dsll $4,$2,8
daddu $2,$2,$4
dsll $4,$2,16
daddu $2,$2,$4
dsll $2,$2,1
daddu $2,$2,$3
dsrl $2,$2,32
sll $2,$2,0
sra $3,$2,2
sra $2,$5,31
subu $2,$3,$2
sw $2,16($fp)
ld $2,0($fp)
bne $2,$0,.L5
nop
ld $2,24($fp)
sd $2,0($fp)
.L5:
ld $2,8($fp)
beq $2,$0,.L6
nop
ld $2,8($fp)
ld $3,24($fp)
sd $3,8($2)
.L6:
ld $2,24($fp)
sd $2,8($fp)
.L2:
ld $2,32($fp)
bne $2,$0,.L7
nop
ld $2,40($fp)
bne $2,$0,.L7
nop
lw $2,16($fp)
bne $2,$0,.L7
nop
ld $2,0($fp)
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
daddiu $sp,$sp,80
jr $31
nop
|
45
| 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
*/
|
mips64
|
-O1
|
mips64 gcc 15.2.0
|
addTwoNumbers:
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(addTwoNumbers)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(addTwoNumbers)))
move $18,$4
move $17,$5
move $16,$0
b .L2
move $20,$0
.L12:
b .L4
move $20,$2
.L13:
bne $16,$0,.L14
ld $25,%call16(malloc)($28)
move $2,$20
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
.L6:
1: jalr $25
li $4,16 # 0x10
lw $3,0($18)
addu $16,$3,$16
ld $18,8($18)
.L8:
beq $17,$0,.L15
dsll $3,$16,1
lw $3,0($17)
addu $16,$3,$16
ld $17,8($17)
dsll $3,$16,1
.L15:
daddu $3,$3,$16
dsll $4,$3,4
daddu $3,$3,$4
dsll $4,$3,8
daddu $3,$3,$4
dsll $4,$3,16
daddu $3,$3,$4
dsll $3,$3,1
daddu $3,$3,$16
dsra $3,$3,32
sra $3,$3,2
sra $4,$16,31
subu $3,$3,$4
sll $4,$3,2
addu $4,$4,$3
sll $4,$4,1
subu $4,$16,$4
sw $4,0($2)
sd $0,8($2)
beq $20,$0,.L12
move $16,$3
.L4:
bnel $19,$0,.L5
sd $2,8($19)
.L5:
move $19,$2
.L2:
bne $18,$0,.L6
ld $25,%call16(malloc)($28)
beq $17,$0,.L13
nop
.L14:
1: jalr $25
li $4,16 # 0x10
b .L8
nop
|
46
| 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
*/
|
mips64
|
-O2
|
mips64 gcc 15.2.0
|
addTwoNumbers:
daddiu $sp,$sp,-64
sd $28,48($sp)
lui $28,%hi(%neg(%gp_rel(addTwoNumbers)))
sd $17,16($sp)
daddu $28,$28,$25
move $17,$4
sd $20,40($sp)
sd $19,32($sp)
sd $16,8($sp)
sd $31,56($sp)
sd $18,24($sp)
daddiu $28,$28,%lo(%neg(%gp_rel(addTwoNumbers)))
move $16,$5
move $20,$0
beq $17,$0,.L16
move $19,$0
.L7:
ld $25,%call16(malloc)($28)
1: jalr $25
li $4,16 # 0x10
lw $3,0($17)
ld $17,8($17)
beq $16,$0,.L4
addu $20,$3,$20
lw $3,0($16)
.L20:
ld $16,8($16)
addu $20,$3,$20
.L4:
dsll $3,$20,1
.L21:
daddu $3,$3,$20
dsll $4,$3,4
daddu $3,$3,$4
dsll $4,$3,8
daddu $3,$3,$4
dsll $4,$3,16
daddu $3,$3,$4
dsll $3,$3,1
daddu $3,$3,$20
dsra $3,$3,32
sra $4,$20,31
sra $3,$3,2
subu $3,$3,$4
sll $4,$3,2
addu $4,$4,$3
sll $4,$4,1
subu $4,$20,$4
sw $4,0($2)
sd $0,8($2)
beq $19,$0,.L17
move $20,$3
.L5:
bnel $18,$0,.L6
sd $2,8($18)
.L6:
bne $17,$0,.L7
move $18,$2
.L16:
beq $16,$0,.L18
ld $25,%call16(malloc)($28)
1: jalr $25
li $4,16 # 0x10
b .L20
lw $3,0($16)
.L17:
b .L5
move $19,$2
.L18:
beq $20,$0,.L19
ld $31,56($sp)
1: jalr $25
li $4,16 # 0x10
b .L21
dsll $3,$20,1
.L19:
ld $28,48($sp)
ld $20,40($sp)
ld $18,24($sp)
ld $17,16($sp)
ld $16,8($sp)
move $2,$19
ld $19,32($sp)
jr $31
daddiu $sp,$sp,64
|
47
| 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
*/
|
mips64
|
-O3
|
mips64 gcc 15.2.0
|
addTwoNumbers:
daddiu $sp,$sp,-64
sd $28,48($sp)
lui $28,%hi(%neg(%gp_rel(addTwoNumbers)))
sd $17,16($sp)
daddu $28,$28,$25
move $17,$4
sd $20,40($sp)
sd $19,32($sp)
sd $16,8($sp)
sd $31,56($sp)
sd $18,24($sp)
daddiu $28,$28,%lo(%neg(%gp_rel(addTwoNumbers)))
move $16,$5
move $20,$0
beq $17,$0,.L16
move $19,$0
.L7:
ld $25,%call16(malloc)($28)
1: jalr $25
li $4,16 # 0x10
lw $3,0($17)
ld $17,8($17)
beq $16,$0,.L4
addu $20,$3,$20
lw $3,0($16)
.L20:
ld $16,8($16)
addu $20,$3,$20
.L4:
dsll $3,$20,1
.L21:
daddu $3,$3,$20
dsll $4,$3,4
daddu $3,$3,$4
dsll $4,$3,8
daddu $3,$3,$4
dsll $4,$3,16
daddu $3,$3,$4
dsll $3,$3,1
daddu $3,$3,$20
dsra $3,$3,32
sra $4,$20,31
sra $3,$3,2
subu $3,$3,$4
sll $4,$3,2
addu $4,$4,$3
sll $4,$4,1
subu $4,$20,$4
sw $4,0($2)
sd $0,8($2)
beq $19,$0,.L17
move $20,$3
.L5:
bnel $18,$0,.L6
sd $2,8($18)
.L6:
bne $17,$0,.L7
move $18,$2
.L16:
beq $16,$0,.L18
ld $25,%call16(malloc)($28)
1: jalr $25
li $4,16 # 0x10
b .L20
lw $3,0($16)
.L17:
b .L5
move $19,$2
.L18:
beq $20,$0,.L19
ld $31,56($sp)
1: jalr $25
li $4,16 # 0x10
b .L21
dsll $3,$20,1
.L19:
ld $28,48($sp)
ld $20,40($sp)
ld $18,24($sp)
ld $17,16($sp)
ld $16,8($sp)
move $2,$19
ld $19,32($sp)
jr $31
daddiu $sp,$sp,64
|
48
| 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
*/
|
riscv64
|
-O0
|
RISC-V 64 clang 21.1.0
|
addTwoNumbers:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
addi s0, sp, 80
sd a0, -24(s0)
sd a1, -32(s0)
li a0, 0
sd a0, -40(s0)
sw a0, -60(s0)
j .LBB0_1
.LBB0_1:
ld a0, -24(s0)
li a1, 1
sd a1, -72(s0)
bnez a0, .LBB0_4
j .LBB0_2
.LBB0_2:
ld a0, -32(s0)
li a1, 1
sd a1, -72(s0)
bnez a0, .LBB0_4
j .LBB0_3
.LBB0_3:
lw a0, -60(s0)
snez a0, a0
sd a0, -72(s0)
j .LBB0_4
.LBB0_4:
ld a0, -72(s0)
andi a0, a0, 1
beqz a0, .LBB0_14
j .LBB0_5
.LBB0_5:
li a0, 16
call malloc
sd a0, -48(s0)
ld a0, -24(s0)
beqz a0, .LBB0_7
j .LBB0_6
.LBB0_6:
ld a0, -24(s0)
lw a1, 0(a0)
lw a0, -60(s0)
addw a0, a0, a1
sw a0, -60(s0)
ld a0, -24(s0)
ld a0, 8(a0)
sd a0, -24(s0)
j .LBB0_7
.LBB0_7:
ld a0, -32(s0)
beqz a0, .LBB0_9
j .LBB0_8
.LBB0_8:
ld a0, -32(s0)
lw a1, 0(a0)
lw a0, -60(s0)
addw a0, a0, a1
sw a0, -60(s0)
ld a0, -32(s0)
ld a0, 8(a0)
sd a0, -32(s0)
j .LBB0_9
.LBB0_9:
lw a0, -60(s0)
lui a1, 419430
addi a1, a1, 1639
mul a2, a0, a1
srli a3, a2, 63
srai a2, a2, 34
addw a2, a2, a3
slliw a3, a2, 1
slliw a2, a2, 3
addw a2, a2, a3
subw a0, a0, a2
ld a2, -48(s0)
sw a0, 0(a2)
ld a2, -48(s0)
li a0, 0
sd a0, 8(a2)
lw a0, -60(s0)
mul a0, a0, a1
srli a1, a0, 63
srai a0, a0, 34
addw a0, a0, a1
sw a0, -60(s0)
ld a0, -40(s0)
bnez a0, .LBB0_11
j .LBB0_10
.LBB0_10:
ld a0, -48(s0)
sd a0, -40(s0)
j .LBB0_11
.LBB0_11:
ld a0, -56(s0)
beqz a0, .LBB0_13
j .LBB0_12
.LBB0_12:
ld a0, -48(s0)
ld a1, -56(s0)
sd a0, 8(a1)
j .LBB0_13
.LBB0_13:
ld a0, -48(s0)
sd a0, -56(s0)
j .LBB0_1
.LBB0_14:
ld a0, -40(s0)
ld ra, 72(sp)
ld s0, 64(sp)
addi sp, sp, 80
ret
|
49
| 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
*/
|
riscv64
|
-O1
|
RISC-V 64 clang 21.1.0
|
addTwoNumbers:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
sd s1, 56(sp)
sd s2, 48(sp)
sd s3, 40(sp)
sd s4, 32(sp)
sd s5, 24(sp)
sd s6, 16(sp)
sd s7, 8(sp)
sd s8, 0(sp)
mv s5, a0
or a0, a0, a1
beqz a0, .LBB0_14
mv s4, a1
li s6, 0
li s0, 0
snez s8, s5
snez s7, a1
lui a0, 419430
addi s2, a0, 1639
li s3, -19
.LBB0_2:
li a0, 16
call malloc
andi a1, s8, 1
beqz a1, .LBB0_5
lw a1, 0(s5)
ld s5, 8(s5)
addw s6, a1, s6
andi a1, s7, 1
bnez a1, .LBB0_6
.LBB0_4:
li s4, 0
mv a2, s6
j .LBB0_7
.LBB0_5:
li s5, 0
andi a1, s7, 1
beqz a1, .LBB0_4
.LBB0_6:
lw a1, 0(s4)
ld s4, 8(s4)
addw a2, a1, s6
.LBB0_7:
mul a1, a2, s2
srli a3, a1, 63
srai a1, a1, 34
addw s6, a1, a3
slli a1, s6, 1
slli a3, s6, 3
add a1, a1, a3
subw a1, a2, a1
sw a1, 0(a0)
sd zero, 8(a0)
mv a1, a0
beqz s0, .LBB0_9
mv a1, s0
.LBB0_9:
beqz s1, .LBB0_11
sd a0, 8(s1)
.LBB0_11:
snez s8, s5
snez s7, s4
mv s1, a0
mv s0, a1
bnez s5, .LBB0_2
mv s1, a0
mv s0, a1
bnez s4, .LBB0_2
addiw a2, a2, -10
mv s1, a0
mv s0, a1
bltu a2, s3, .LBB0_2
j .LBB0_15
.LBB0_14:
li a1, 0
.LBB0_15:
mv a0, a1
ld ra, 72(sp)
ld s0, 64(sp)
ld s1, 56(sp)
ld s2, 48(sp)
ld s3, 40(sp)
ld s4, 32(sp)
ld s5, 24(sp)
ld s6, 16(sp)
ld s7, 8(sp)
ld s8, 0(sp)
addi sp, sp, 80
ret
|
50
| 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
*/
|
riscv64
|
-O2
|
RISC-V 64 clang 21.1.0
|
addTwoNumbers:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
sd s1, 56(sp)
sd s2, 48(sp)
sd s3, 40(sp)
sd s4, 32(sp)
sd s5, 24(sp)
sd s6, 16(sp)
sd s7, 8(sp)
sd s8, 0(sp)
mv s5, a0
or a0, a0, a1
beqz a0, .LBB0_14
mv s4, a1
li s6, 0
li s0, 0
snez s8, s5
snez s7, a1
lui a0, 419430
addi s2, a0, 1639
li s3, -19
.LBB0_2:
li a0, 16
call malloc
andi a1, s8, 1
beqz a1, .LBB0_5
lw a1, 0(s5)
ld s5, 8(s5)
addw s6, a1, s6
andi a1, s7, 1
bnez a1, .LBB0_6
.LBB0_4:
li s4, 0
mv a2, s6
j .LBB0_7
.LBB0_5:
li s5, 0
andi a1, s7, 1
beqz a1, .LBB0_4
.LBB0_6:
lw a1, 0(s4)
ld s4, 8(s4)
addw a2, a1, s6
.LBB0_7:
mul a1, a2, s2
srli a3, a1, 63
srai a1, a1, 34
addw s6, a1, a3
slli a1, s6, 1
slli a3, s6, 3
add a1, a1, a3
subw a1, a2, a1
sw a1, 0(a0)
sd zero, 8(a0)
mv a1, a0
beqz s0, .LBB0_9
mv a1, s0
.LBB0_9:
beqz s1, .LBB0_11
sd a0, 8(s1)
.LBB0_11:
snez s8, s5
snez s7, s4
mv s1, a0
mv s0, a1
bnez s5, .LBB0_2
mv s1, a0
mv s0, a1
bnez s4, .LBB0_2
addiw a2, a2, -10
mv s1, a0
mv s0, a1
bltu a2, s3, .LBB0_2
j .LBB0_15
.LBB0_14:
li a1, 0
.LBB0_15:
mv a0, a1
ld ra, 72(sp)
ld s0, 64(sp)
ld s1, 56(sp)
ld s2, 48(sp)
ld s3, 40(sp)
ld s4, 32(sp)
ld s5, 24(sp)
ld s6, 16(sp)
ld s7, 8(sp)
ld s8, 0(sp)
addi sp, sp, 80
ret
|
51
| 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
*/
|
riscv64
|
-O3
|
RISC-V 64 clang 21.1.0
|
addTwoNumbers:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
sd s1, 56(sp)
sd s2, 48(sp)
sd s3, 40(sp)
sd s4, 32(sp)
sd s5, 24(sp)
sd s6, 16(sp)
sd s7, 8(sp)
sd s8, 0(sp)
mv s5, a0
or a0, a0, a1
beqz a0, .LBB0_14
mv s4, a1
li s6, 0
li s0, 0
snez s8, s5
snez s7, a1
lui a0, 419430
addi s2, a0, 1639
li s3, -19
.LBB0_2:
li a0, 16
call malloc
andi a1, s8, 1
beqz a1, .LBB0_5
lw a1, 0(s5)
ld s5, 8(s5)
addw s6, a1, s6
andi a1, s7, 1
bnez a1, .LBB0_6
.LBB0_4:
li s4, 0
mv a2, s6
j .LBB0_7
.LBB0_5:
li s5, 0
andi a1, s7, 1
beqz a1, .LBB0_4
.LBB0_6:
lw a1, 0(s4)
ld s4, 8(s4)
addw a2, a1, s6
.LBB0_7:
mul a1, a2, s2
srli a3, a1, 63
srai a1, a1, 34
addw s6, a1, a3
slli a1, s6, 1
slli a3, s6, 3
add a1, a1, a3
subw a1, a2, a1
sw a1, 0(a0)
sd zero, 8(a0)
mv a1, a0
beqz s0, .LBB0_9
mv a1, s0
.LBB0_9:
beqz s1, .LBB0_11
sd a0, 8(s1)
.LBB0_11:
snez s8, s5
snez s7, s4
mv s1, a0
mv s0, a1
bnez s5, .LBB0_2
mv s1, a0
mv s0, a1
bnez s4, .LBB0_2
addiw a2, a2, -10
mv s1, a0
mv s0, a1
bltu a2, s3, .LBB0_2
j .LBB0_15
.LBB0_14:
li a1, 0
.LBB0_15:
mv a0, a1
ld ra, 72(sp)
ld s0, 64(sp)
ld s1, 56(sp)
ld s2, 48(sp)
ld s3, 40(sp)
ld s4, 32(sp)
ld s5, 24(sp)
ld s6, 16(sp)
ld s7, 8(sp)
ld s8, 0(sp)
addi sp, sp, 80
ret
|
52
| 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
*/
|
riscv64
|
-O0
|
RISC-V 64 gcc 15.2.0
|
addTwoNumbers:
addi sp,sp,-64
sd ra,56(sp)
sd s0,48(sp)
addi s0,sp,64
sd a0,-56(s0)
sd a1,-64(s0)
sd zero,-24(s0)
sw zero,-36(s0)
j .L2
.L7:
li a0,16
call malloc
mv a5,a0
sd a5,-48(s0)
ld a5,-56(s0)
beq a5,zero,.L3
ld a5,-56(s0)
lw a5,0(a5)
lw a4,-36(s0)
addw a5,a4,a5
sw a5,-36(s0)
ld a5,-56(s0)
ld a5,8(a5)
sd a5,-56(s0)
.L3:
ld a5,-64(s0)
beq a5,zero,.L4
ld a5,-64(s0)
lw a5,0(a5)
lw a4,-36(s0)
addw a5,a4,a5
sw a5,-36(s0)
ld a5,-64(s0)
ld a5,8(a5)
sd a5,-64(s0)
.L4:
lw a5,-36(s0)
mv a4,a5
sext.w a3,a4
li a5,1717985280
addi a5,a5,1639
mul a5,a3,a5
srli a5,a5,32
sraiw a5,a5,2
mv a3,a5
sraiw a5,a4,31
subw a5,a3,a5
mv a3,a5
mv a5,a3
slliw a5,a5,2
addw a5,a5,a3
slliw a5,a5,1
subw a5,a4,a5
sext.w a4,a5
ld a5,-48(s0)
sw a4,0(a5)
ld a5,-48(s0)
sd zero,8(a5)
lw a5,-36(s0)
mv a3,a5
sext.w a4,a3
li a5,1717985280
addi a5,a5,1639
mul a5,a4,a5
srli a5,a5,32
sraiw a5,a5,2
mv a4,a5
sraiw a5,a3,31
subw a5,a4,a5
sw a5,-36(s0)
ld a5,-24(s0)
bne a5,zero,.L5
ld a5,-48(s0)
sd a5,-24(s0)
.L5:
ld a5,-32(s0)
beq a5,zero,.L6
ld a5,-32(s0)
ld a4,-48(s0)
sd a4,8(a5)
.L6:
ld a5,-48(s0)
sd a5,-32(s0)
.L2:
ld a5,-56(s0)
bne a5,zero,.L7
ld a5,-64(s0)
bne a5,zero,.L7
lw a5,-36(s0)
sext.w a5,a5
bne a5,zero,.L7
ld a5,-24(s0)
mv a0,a5
ld ra,56(sp)
ld s0,48(sp)
addi sp,sp,64
jr ra
|
53
| 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
*/
|
riscv64
|
-O1
|
RISC-V 64 gcc 15.2.0
|
addTwoNumbers:
addi sp,sp,-64
sd ra,56(sp)
sd s3,24(sp)
or a5,a0,a1
beq a5,zero,.L8
sd s0,48(sp)
sd s1,40(sp)
sd s2,32(sp)
sd s4,16(sp)
sd s5,8(sp)
sd s6,0(sp)
mv s0,a0
mv s1,a1
li s6,0
li s3,0
li s5,16
li s4,1717985280
addi s4,s4,1639
.L10:
mv s2,a3
mv a0,s5
call malloc
mv a3,a0
beq s0,zero,.L3
lw a5,0(s0)
addw s6,a5,s6
ld s0,8(s0)
.L3:
beq s1,zero,.L4
lw a5,0(s1)
addw s6,a5,s6
ld s1,8(s1)
.L4:
mul a5,s6,s4
srai a5,a5,34
sraiw a4,s6,31
subw a5,a5,a4
slliw a4,a5,2
addw a4,a4,a5
slliw a4,a4,1
subw a4,s6,a4
sw a4,0(a3)
sd zero,8(a3)
mv s6,a5
beq s3,zero,.L12
.L5:
beq s2,zero,.L6
sd a3,8(s2)
.L6:
or a4,s1,s0
bne a4,zero,.L10
bne a5,zero,.L10
ld s0,48(sp)
ld s1,40(sp)
ld s2,32(sp)
ld s4,16(sp)
ld s5,8(sp)
ld s6,0(sp)
.L1:
mv a0,s3
ld ra,56(sp)
ld s3,24(sp)
addi sp,sp,64
jr ra
.L12:
mv s3,a3
j .L5
.L8:
li s3,0
j .L1
|
54
| 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
*/
|
riscv64
|
-O2
|
RISC-V 64 gcc 15.2.0
|
addTwoNumbers:
addi sp,sp,-64
sd s3,24(sp)
sd s4,16(sp)
mv s3,a1
mv s4,a0
sd s0,48(sp)
sd s1,40(sp)
sd s2,32(sp)
sd ra,56(sp)
li s2,1717985280
sd s5,8(sp)
or a5,s4,s3
addi s2,s2,1639
li s0,0
li s1,0
bne a5,zero,.L7
.L22:
beq s0,zero,.L1
li a0,16
call malloc
li s4,0
li s3,0
.L4:
mul a5,s0,s2
sraiw a4,s0,31
sd zero,8(a0)
srai a5,a5,34
subw a5,a5,a4
slliw a4,a5,2
addw a4,a4,a5
slliw a4,a4,1
subw s0,s0,a4
sw s0,0(a0)
mv s0,a5
beq s1,zero,.L21
.L5:
beq s5,zero,.L6
sd a0,8(s5)
.L6:
or a5,s4,s3
mv s5,a0
beq a5,zero,.L22
.L7:
li a0,16
call malloc
beq s4,zero,.L3
lw a5,0(s4)
ld s4,8(s4)
addw s0,a5,s0
beq s3,zero,.L4
.L3:
lw a5,0(s3)
ld s3,8(s3)
addw s0,a5,s0
j .L4
.L21:
mv s1,a0
j .L5
.L1:
ld ra,56(sp)
ld s0,48(sp)
ld s2,32(sp)
ld s3,24(sp)
ld s4,16(sp)
ld s5,8(sp)
mv a0,s1
ld s1,40(sp)
addi sp,sp,64
jr ra
|
55
| 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
*/
|
riscv64
|
-O3
|
RISC-V 64 gcc 15.2.0
|
addTwoNumbers:
addi sp,sp,-64
sd s3,24(sp)
sd s4,16(sp)
mv s3,a1
mv s4,a0
sd s0,48(sp)
sd s1,40(sp)
sd s2,32(sp)
sd ra,56(sp)
li s2,1717985280
sd s5,8(sp)
or a5,s4,s3
addi s2,s2,1639
li s0,0
li s1,0
bne a5,zero,.L7
.L22:
beq s0,zero,.L1
li a0,16
call malloc
li s4,0
li s3,0
.L4:
mul a5,s0,s2
sraiw a4,s0,31
sd zero,8(a0)
srai a5,a5,34
subw a5,a5,a4
slliw a4,a5,2
addw a4,a4,a5
slliw a4,a4,1
subw s0,s0,a4
sw s0,0(a0)
mv s0,a5
beq s1,zero,.L21
.L5:
beq s5,zero,.L6
sd a0,8(s5)
.L6:
or a5,s4,s3
mv s5,a0
beq a5,zero,.L22
.L7:
li a0,16
call malloc
beq s4,zero,.L3
lw a5,0(s4)
ld s4,8(s4)
addw s0,a5,s0
beq s3,zero,.L4
.L3:
lw a5,0(s3)
ld s3,8(s3)
addw s0,a5,s0
j .L4
.L21:
mv s1,a0
j .L5
.L1:
ld ra,56(sp)
ld s0,48(sp)
ld s2,32(sp)
ld s3,24(sp)
ld s4,16(sp)
ld s5,8(sp)
mv a0,s1
ld s1,40(sp)
addi sp,sp,64
jr ra
|
56
| 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
*/
|
x86-64
|
-O0
|
x86-64 clang 21.1.0
|
addTwoNumbers:
push rbp
mov rbp, rsp
sub rsp, 48
mov qword ptr [rbp - 8], rdi
mov qword ptr [rbp - 16], rsi
mov qword ptr [rbp - 24], 0
mov dword ptr [rbp - 44], 0
.LBB0_1:
mov al, 1
cmp qword ptr [rbp - 8], 0
mov byte ptr [rbp - 45], al
jne .LBB0_4
mov al, 1
cmp qword ptr [rbp - 16], 0
mov byte ptr [rbp - 45], al
jne .LBB0_4
cmp dword ptr [rbp - 44], 0
setne al
mov byte ptr [rbp - 45], al
.LBB0_4:
mov al, byte ptr [rbp - 45]
test al, 1
jne .LBB0_5
jmp .LBB0_14
.LBB0_5:
mov edi, 16
call malloc@PLT
mov qword ptr [rbp - 32], rax
cmp qword ptr [rbp - 8], 0
je .LBB0_7
mov rax, qword ptr [rbp - 8]
mov eax, dword ptr [rax]
add eax, dword ptr [rbp - 44]
mov dword ptr [rbp - 44], eax
mov rax, qword ptr [rbp - 8]
mov rax, qword ptr [rax + 8]
mov qword ptr [rbp - 8], rax
.LBB0_7:
cmp qword ptr [rbp - 16], 0
je .LBB0_9
mov rax, qword ptr [rbp - 16]
mov eax, dword ptr [rax]
add eax, dword ptr [rbp - 44]
mov dword ptr [rbp - 44], eax
mov rax, qword ptr [rbp - 16]
mov rax, qword ptr [rax + 8]
mov qword ptr [rbp - 16], rax
.LBB0_9:
mov eax, dword ptr [rbp - 44]
mov ecx, 10
cdq
idiv ecx
mov rax, qword ptr [rbp - 32]
mov dword ptr [rax], edx
mov rax, qword ptr [rbp - 32]
mov qword ptr [rax + 8], 0
mov eax, dword ptr [rbp - 44]
mov ecx, 10
cdq
idiv ecx
mov dword ptr [rbp - 44], eax
cmp qword ptr [rbp - 24], 0
jne .LBB0_11
mov rax, qword ptr [rbp - 32]
mov qword ptr [rbp - 24], rax
.LBB0_11:
cmp qword ptr [rbp - 40], 0
je .LBB0_13
mov rcx, qword ptr [rbp - 32]
mov rax, qword ptr [rbp - 40]
mov qword ptr [rax + 8], rcx
.LBB0_13:
mov rax, qword ptr [rbp - 32]
mov qword ptr [rbp - 40], rax
jmp .LBB0_1
.LBB0_14:
mov rax, qword ptr [rbp - 24]
add rsp, 48
pop rbp
ret
|
57
| 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
*/
|
x86-64
|
-O1
|
x86-64 clang 21.1.0
|
addTwoNumbers:
push rbp
push r15
push r14
push r13
push r12
push rbx
push rax
test rdi, rdi
setne bpl
test rsi, rsi
setne r14b
xor r12d, r12d
mov rax, rdi
or rax, rsi
je .LBB0_1
mov rbx, rsi
mov r15, rdi
xor esi, esi
.LBB0_3:
mov qword ptr [rsp], rsi
mov edi, 16
call malloc@PLT
test bpl, 1
je .LBB0_4
add r12d, dword ptr [r15]
mov r15, qword ptr [r15 + 8]
test r14b, 1
jne .LBB0_8
.LBB0_7:
xor ebx, ebx
jmp .LBB0_9
.LBB0_4:
xor r15d, r15d
test r14b, 1
je .LBB0_7
.LBB0_8:
add r12d, dword ptr [rbx]
mov rbx, qword ptr [rbx + 8]
.LBB0_9:
mov ecx, r12d
movsxd rdx, r12d
imul r12, rdx, 1717986919
mov rsi, r12
shr rsi, 63
sar r12, 34
add r12d, esi
lea esi, [r12 + r12]
lea esi, [rsi + 4*rsi]
sub edx, esi
mov dword ptr [rax], edx
mov qword ptr [rax + 8], 0
mov rsi, qword ptr [rsp]
test rsi, rsi
cmove rsi, rax
test r13, r13
je .LBB0_11
mov qword ptr [r13 + 8], rax
.LBB0_11:
test rbx, rbx
setne r14b
mov r13, rax
test r15, r15
setne bpl
jne .LBB0_3
mov r13, rax
test rbx, rbx
jne .LBB0_3
add ecx, -10
mov r13, rax
cmp ecx, -19
jb .LBB0_3
jmp .LBB0_14
.LBB0_1:
xor esi, esi
.LBB0_14:
mov rax, rsi
add rsp, 8
pop rbx
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
|
58
| 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
*/
|
x86-64
|
-O2
|
x86-64 clang 21.1.0
|
addTwoNumbers:
push rbp
push r15
push r14
push r13
push r12
push rbx
push rax
test rdi, rdi
setne bpl
test rsi, rsi
setne r14b
xor r12d, r12d
mov rax, rdi
or rax, rsi
je .LBB0_1
mov rbx, rsi
mov r15, rdi
xor esi, esi
.LBB0_3:
mov qword ptr [rsp], rsi
mov edi, 16
call malloc@PLT
test bpl, 1
je .LBB0_4
add r12d, dword ptr [r15]
mov r15, qword ptr [r15 + 8]
test r14b, 1
jne .LBB0_8
.LBB0_7:
xor ebx, ebx
jmp .LBB0_9
.LBB0_4:
xor r15d, r15d
test r14b, 1
je .LBB0_7
.LBB0_8:
add r12d, dword ptr [rbx]
mov rbx, qword ptr [rbx + 8]
.LBB0_9:
mov ecx, r12d
movsxd rdx, r12d
imul r12, rdx, 1717986919
mov rsi, r12
shr rsi, 63
sar r12, 34
add r12d, esi
lea esi, [r12 + r12]
lea esi, [rsi + 4*rsi]
sub edx, esi
mov dword ptr [rax], edx
mov qword ptr [rax + 8], 0
mov rsi, qword ptr [rsp]
test rsi, rsi
cmove rsi, rax
test r13, r13
je .LBB0_11
mov qword ptr [r13 + 8], rax
.LBB0_11:
test rbx, rbx
setne r14b
mov r13, rax
test r15, r15
setne bpl
jne .LBB0_3
mov r13, rax
test rbx, rbx
jne .LBB0_3
add ecx, -10
mov r13, rax
cmp ecx, -19
jb .LBB0_3
jmp .LBB0_14
.LBB0_1:
xor esi, esi
.LBB0_14:
mov rax, rsi
add rsp, 8
pop rbx
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
|
59
| 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
*/
|
x86-64
|
-O3
|
x86-64 clang 21.1.0
|
addTwoNumbers:
push rbp
push r15
push r14
push r13
push r12
push rbx
push rax
test rdi, rdi
setne bpl
test rsi, rsi
setne r14b
xor r12d, r12d
mov rax, rdi
or rax, rsi
je .LBB0_1
mov rbx, rsi
mov r15, rdi
xor esi, esi
.LBB0_3:
mov qword ptr [rsp], rsi
mov edi, 16
call malloc@PLT
test bpl, 1
je .LBB0_4
add r12d, dword ptr [r15]
mov r15, qword ptr [r15 + 8]
test r14b, 1
jne .LBB0_8
.LBB0_7:
xor ebx, ebx
jmp .LBB0_9
.LBB0_4:
xor r15d, r15d
test r14b, 1
je .LBB0_7
.LBB0_8:
add r12d, dword ptr [rbx]
mov rbx, qword ptr [rbx + 8]
.LBB0_9:
mov ecx, r12d
movsxd rdx, r12d
imul r12, rdx, 1717986919
mov rsi, r12
shr rsi, 63
sar r12, 34
add r12d, esi
lea esi, [r12 + r12]
lea esi, [rsi + 4*rsi]
sub edx, esi
mov dword ptr [rax], edx
mov qword ptr [rax + 8], 0
mov rsi, qword ptr [rsp]
test rsi, rsi
cmove rsi, rax
test r13, r13
je .LBB0_11
mov qword ptr [r13 + 8], rax
.LBB0_11:
test rbx, rbx
setne r14b
mov r13, rax
test r15, r15
setne bpl
jne .LBB0_3
mov r13, rax
test rbx, rbx
jne .LBB0_3
add ecx, -10
mov r13, rax
cmp ecx, -19
jb .LBB0_3
jmp .LBB0_14
.LBB0_1:
xor esi, esi
.LBB0_14:
mov rax, rsi
add rsp, 8
pop rbx
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
|
60
| 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
*/
|
x86-64
|
-O0
|
x86-64 gcc 15.2
|
addTwoNumbers:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-40], rdi
mov QWORD PTR [rbp-48], rsi
mov QWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-20], 0
jmp .L2
.L7:
mov edi, 16
call malloc
mov QWORD PTR [rbp-32], rax
cmp QWORD PTR [rbp-40], 0
je .L3
mov rax, QWORD PTR [rbp-40]
mov eax, DWORD PTR [rax]
add DWORD PTR [rbp-20], eax
mov rax, QWORD PTR [rbp-40]
mov rax, QWORD PTR [rax+8]
mov QWORD PTR [rbp-40], rax
.L3:
cmp QWORD PTR [rbp-48], 0
je .L4
mov rax, QWORD PTR [rbp-48]
mov eax, DWORD PTR [rax]
add DWORD PTR [rbp-20], eax
mov rax, QWORD PTR [rbp-48]
mov rax, QWORD PTR [rax+8]
mov QWORD PTR [rbp-48], rax
.L4:
mov ecx, DWORD PTR [rbp-20]
movsx rax, ecx
imul rax, rax, 1717986919
shr rax, 32
mov edx, eax
sar edx, 2
mov eax, ecx
sar eax, 31
sub edx, eax
mov eax, edx
sal eax, 2
add eax, edx
add eax, eax
sub ecx, eax
mov edx, ecx
mov rax, QWORD PTR [rbp-32]
mov DWORD PTR [rax], edx
mov rax, QWORD PTR [rbp-32]
mov QWORD PTR [rax+8], 0
mov eax, DWORD PTR [rbp-20]
movsx rdx, eax
imul rdx, rdx, 1717986919
shr rdx, 32
mov ecx, edx
sar ecx, 2
cdq
mov eax, ecx
sub eax, edx
mov DWORD PTR [rbp-20], eax
cmp QWORD PTR [rbp-8], 0
jne .L5
mov rax, QWORD PTR [rbp-32]
mov QWORD PTR [rbp-8], rax
.L5:
cmp QWORD PTR [rbp-16], 0
je .L6
mov rax, QWORD PTR [rbp-16]
mov rdx, QWORD PTR [rbp-32]
mov QWORD PTR [rax+8], rdx
.L6:
mov rax, QWORD PTR [rbp-32]
mov QWORD PTR [rbp-16], rax
.L2:
cmp QWORD PTR [rbp-40], 0
jne .L7
cmp QWORD PTR [rbp-48], 0
jne .L7
cmp DWORD PTR [rbp-20], 0
jne .L7
mov rax, QWORD PTR [rbp-8]
leave
ret
|
61
| 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
*/
|
x86-64
|
-O1
|
x86-64 gcc 15.2
|
addTwoNumbers:
push r14
push r13
push r12
push rbp
push rbx
mov rax, rdi
or rax, rsi
je .L8
mov rbp, rdi
mov r12, rsi
mov ebx, 0
mov r13d, 0
.L10:
mov r14, rdx
mov edi, 16
call malloc
mov rdx, rax
test rbp, rbp
je .L3
add ebx, DWORD PTR [rbp+0]
mov rbp, QWORD PTR [rbp+8]
.L3:
test r12, r12
je .L4
add ebx, DWORD PTR [r12]
mov r12, QWORD PTR [r12+8]
.L4:
movsx rax, ebx
imul rax, rax, 1717986919
sar rax, 34
mov ecx, ebx
sar ecx, 31
sub eax, ecx
lea ecx, [rax+rax*4]
add ecx, ecx
sub ebx, ecx
mov DWORD PTR [rdx], ebx
mov QWORD PTR [rdx+8], 0
mov ebx, eax
test r13, r13
cmove r13, rdx
test r14, r14
je .L6
mov QWORD PTR [r14+8], rdx
.L6:
mov rsi, r12
or rsi, rbp
jne .L10
test eax, eax
jne .L10
.L1:
mov rax, r13
pop rbx
pop rbp
pop r12
pop r13
pop r14
ret
.L8:
mov r13d, 0
jmp .L1
|
62
| 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
*/
|
x86-64
|
-O2
|
x86-64 gcc 15.2
|
addTwoNumbers:
push r14
xor r14d, r14d
push r13
push r12
mov r12, rdi
push rbp
mov rbp, rsi
push rbx
xor ebx, ebx
jmp .L2
.L21:
test ebx, ebx
je .L1
mov edi, 16
xor r12d, r12d
xor ebp, ebp
call malloc
.L4:
movsx rdx, ebx
mov ecx, ebx
mov QWORD PTR [rax+8], 0
imul rdx, rdx, 1717986919
sar ecx, 31
sar rdx, 34
sub edx, ecx
lea ecx, [rdx+rdx*4]
add ecx, ecx
sub ebx, ecx
test r14, r14
mov DWORD PTR [rax], ebx
cmove r14, rax
mov ebx, edx
test r13, r13
je .L6
mov QWORD PTR [r13+8], rax
.L6:
mov r13, rax
.L2:
mov rax, r12
or rax, rbp
je .L21
mov edi, 16
call malloc
test r12, r12
je .L3
add ebx, DWORD PTR [r12]
mov r12, QWORD PTR [r12+8]
test rbp, rbp
je .L4
.L3:
add ebx, DWORD PTR [rbp+0]
mov rbp, QWORD PTR [rbp+8]
jmp .L4
.L1:
pop rbx
mov rax, r14
pop rbp
pop r12
pop r13
pop r14
ret
|
63
| 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
*/
|
x86-64
|
-O3
|
x86-64 gcc 15.2
|
addTwoNumbers:
push r14
xor r14d, r14d
push r13
push r12
mov r12, rdi
push rbp
mov rbp, rsi
push rbx
xor ebx, ebx
jmp .L2
.L21:
test ebx, ebx
je .L1
mov edi, 16
xor r12d, r12d
xor ebp, ebp
call malloc
.L4:
movsx rdx, ebx
mov ecx, ebx
mov QWORD PTR [rax+8], 0
imul rdx, rdx, 1717986919
sar ecx, 31
sar rdx, 34
sub edx, ecx
lea ecx, [rdx+rdx*4]
add ecx, ecx
sub ebx, ecx
test r14, r14
mov DWORD PTR [rax], ebx
cmove r14, rax
mov ebx, edx
test r13, r13
je .L6
mov QWORD PTR [r13+8], rax
.L6:
mov r13, rax
.L2:
mov rax, r12
or rax, rbp
je .L21
mov edi, 16
call malloc
test r12, r12
je .L3
add ebx, DWORD PTR [r12]
mov r12, QWORD PTR [r12+8]
test rbp, rbp
je .L4
.L3:
add ebx, DWORD PTR [rbp+0]
mov rbp, QWORD PTR [rbp+8]
jmp .L4
.L1:
pop rbx
mov rax, r14
pop rbp
pop r12
pop r13
pop r14
ret
|
64
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O0
|
ARM64 gcc 15.2.0
|
lengthOfLongestSubstring:
sub sp, sp, #576
stp x29, x30, [sp]
mov x29, sp
str x0, [sp, 24]
str wzr, [sp, 568]
add x0, sp, 40
mov x1, 512
mov x2, x1
mov w1, 0
bl memset
str wzr, [sp, 564]
str wzr, [sp, 572]
b .L2
.L3:
ldr w0, [sp, 564]
add w0, w0, 1
str w0, [sp, 564]
ldrsw x0, [sp, 572]
ldr x1, [sp, 24]
add x0, x1, x0
ldrb w0, [x0]
strb w0, [sp, 563]
ldrb w0, [sp, 563]
sxtw x0, w0
lsl x0, x0, 2
add x1, sp, 40
ldr w0, [x1, x0]
ldr w1, [sp, 572]
sub w0, w1, w0
add w0, w0, 1
str w0, [sp, 556]
ldrb w1, [sp, 563]
ldr w0, [sp, 572]
add w2, w0, 1
sxtw x0, w1
lsl x0, x0, 2
add x1, sp, 40
str w2, [x1, x0]
ldr w0, [sp, 564]
ldr w2, [sp, 556]
ldr w1, [sp, 556]
cmp w2, w0
csel w0, w1, w0, le
str w0, [sp, 564]
ldr w0, [sp, 568]
ldr w2, [sp, 564]
ldr w1, [sp, 564]
cmp w2, w0
csel w0, w1, w0, ge
str w0, [sp, 568]
ldr w0, [sp, 572]
add w0, w0, 1
str w0, [sp, 572]
.L2:
ldrsw x0, [sp, 572]
ldr x1, [sp, 24]
add x0, x1, x0
ldrb w0, [x0]
cmp w0, 0
bne .L3
ldr w0, [sp, 568]
ldp x29, x30, [sp]
add sp, sp, 576
ret
|
65
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O1
|
ARM64 gcc 15.2.0
|
lengthOfLongestSubstring:
sub sp, sp, #544
stp x29, x30, [sp]
mov x29, sp
str x19, [sp, 16]
mov x19, x0
mov x2, 512
mov w1, 0
add x0, sp, 32
bl memset
ldrb w2, [x19]
cbz w2, .L4
mov x3, 1
mov w1, 0
mov w0, 0
add x5, sp, 32
sub x6, x19, #1
.L3:
add w1, w1, 1
sbfiz x2, x2, 2, 32
ldr w4, [x5, x2]
sub w4, w3, w4
str w3, [x5, x2]
cmp w1, w4
csel w1, w1, w4, le
cmp w0, w1
csel w0, w0, w1, ge
add x3, x3, 1
ldrb w2, [x6, x3]
cbnz w2, .L3
.L1:
ldp x29, x30, [sp]
ldr x19, [sp, 16]
add sp, sp, 544
ret
.L4:
mov w0, 0
b .L1
|
66
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O2
|
ARM64 gcc 15.2.0
|
lengthOfLongestSubstring:
sub sp, sp, #544
mov x2, 512
mov w1, 0
stp x29, x30, [sp]
mov x29, sp
str x19, [sp, 16]
mov x19, x0
add x0, sp, 32
bl memset
ldrb w2, [x19]
cbz w2, .L4
sub x6, x19, #1
mov x5, x0
mov x3, 1
mov w1, 0
mov w0, 0
.L3:
ubfiz x2, x2, 2, 8
add w1, w1, 1
ldr w4, [x5, x2]
str w3, [x5, x2]
sub w4, w3, w4
add x3, x3, 1
cmp w1, w4
csel w1, w1, w4, le
ldrb w2, [x6, x3]
cmp w0, w1
csel w0, w0, w1, ge
cbnz w2, .L3
ldr x19, [sp, 16]
ldp x29, x30, [sp]
add sp, sp, 544
ret
.L4:
ldr x19, [sp, 16]
mov w0, 0
ldp x29, x30, [sp]
add sp, sp, 544
ret
|
67
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O3
|
ARM64 gcc 15.2.0
|
lengthOfLongestSubstring:
sub sp, sp, #544
mov x2, 512
mov w1, 0
stp x29, x30, [sp]
mov x29, sp
str x19, [sp, 16]
mov x19, x0
add x0, sp, 32
bl memset
ldrb w2, [x19]
cbz w2, .L4
sub x6, x19, #1
mov x5, x0
mov w1, 0
mov w0, 0
mov x3, 1
.L3:
ubfiz x2, x2, 2, 8
add w1, w1, 1
ldr w4, [x5, x2]
str w3, [x5, x2]
sub w2, w3, w4
add x3, x3, 1
cmp w1, w2
csel w1, w1, w2, le
ldrb w2, [x6, x3]
cmp w0, w1
csel w0, w0, w1, ge
cbnz w2, .L3
ldr x19, [sp, 16]
ldp x29, x30, [sp]
add sp, sp, 544
ret
.L4:
ldr x19, [sp, 16]
mov w0, 0
ldp x29, x30, [sp]
add sp, sp, 544
ret
|
68
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O0
|
armv8-a clang 21.1.0
|
lengthOfLongestSubstring:
stp x29, x30, [sp, #-32]!
str x28, [sp, #16]
mov x29, sp
sub sp, sp, #576
sub x8, x29, #8
str x8, [sp, #8]
str x0, [x8]
mov w1, wzr
stur wzr, [x29, #-24]
add x0, sp, #36
mov x2, #512
bl memset
str wzr, [sp, #20]
stur wzr, [x29, #-12]
b .LBB0_1
.LBB0_1:
ldr x8, [sp, #8]
ldr x8, [x8]
ldursw x9, [x29, #-12]
add x8, x8, x9
ldrb w8, [x8]
cbz w8, .LBB0_10
b .LBB0_2
.LBB0_2:
ldr x8, [sp, #8]
ldr w9, [sp, #20]
add w9, w9, #1
str w9, [sp, #20]
ldr x8, [x8]
ldursw x9, [x29, #-12]
add x8, x8, x9
ldrb w8, [x8]
sturb w8, [x29, #-25]
ldur w8, [x29, #-12]
ldurb w9, [x29, #-25]
mov w10, w9
add x9, sp, #36
ldr w10, [x9, x10, lsl #2]
subs w8, w8, w10
add w8, w8, #1
stur w8, [x29, #-20]
ldur w8, [x29, #-12]
add w8, w8, #1
ldurb w10, [x29, #-25]
str w8, [x9, x10, lsl #2]
ldr w8, [sp, #20]
ldur w9, [x29, #-20]
subs w8, w8, w9
b.ge .LBB0_4
b .LBB0_3
.LBB0_3:
ldr w8, [sp, #20]
str w8, [sp, #4]
b .LBB0_5
.LBB0_4:
ldur w8, [x29, #-20]
str w8, [sp, #4]
b .LBB0_5
.LBB0_5:
ldr w8, [sp, #4]
str w8, [sp, #20]
ldur w8, [x29, #-24]
ldr w9, [sp, #20]
subs w8, w8, w9
b.le .LBB0_7
b .LBB0_6
.LBB0_6:
ldur w8, [x29, #-24]
str w8, [sp]
b .LBB0_8
.LBB0_7:
ldr w8, [sp, #20]
str w8, [sp]
b .LBB0_8
.LBB0_8:
ldr w8, [sp]
stur w8, [x29, #-24]
b .LBB0_9
.LBB0_9:
ldur w8, [x29, #-12]
add w8, w8, #1
stur w8, [x29, #-12]
b .LBB0_1
.LBB0_10:
ldur w0, [x29, #-24]
add sp, sp, #576
ldr x28, [sp, #16]
ldp x29, x30, [sp], #32
ret
|
69
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O1
|
armv8-a clang 21.1.0
|
lengthOfLongestSubstring:
str x30, [sp, #-16]!
sub sp, sp, #512
movi v0.2d, #0000000000000000
ldrb w9, [x0]
stp q0, q0, [sp]
stp q0, q0, [sp, #32]
stp q0, q0, [sp, #64]
stp q0, q0, [sp, #96]
stp q0, q0, [sp, #128]
stp q0, q0, [sp, #160]
stp q0, q0, [sp, #192]
stp q0, q0, [sp, #224]
stp q0, q0, [sp, #256]
stp q0, q0, [sp, #288]
stp q0, q0, [sp, #320]
stp q0, q0, [sp, #352]
stp q0, q0, [sp, #384]
stp q0, q0, [sp, #416]
stp q0, q0, [sp, #448]
stp q0, q0, [sp, #480]
cbz w9, .LBB0_3
mov x8, x0
mov x11, xzr
mov w10, wzr
mov w0, wzr
add x8, x8, #1
mov x12, sp
.LBB0_2:
ldr w13, [x12, w9, uxtw #2]
add x14, x11, #1
str w14, [x12, w9, uxtw #2]
ldrb w9, [x8, x11]
sub w13, w11, w13
mov x11, x14
cmp w10, w13
csel w13, w10, w13, lt
add w10, w13, #1
cmp w0, w10
csinc w0, w0, w13, gt
cbnz w9, .LBB0_2
b .LBB0_4
.LBB0_3:
mov w0, wzr
.LBB0_4:
add sp, sp, #512
ldr x30, [sp], #16
ret
|
70
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O2
|
armv8-a clang 21.1.0
|
lengthOfLongestSubstring:
str x30, [sp, #-16]!
sub sp, sp, #512
movi v0.2d, #0000000000000000
ldrb w9, [x0]
stp q0, q0, [sp]
stp q0, q0, [sp, #32]
stp q0, q0, [sp, #64]
stp q0, q0, [sp, #96]
stp q0, q0, [sp, #128]
stp q0, q0, [sp, #160]
stp q0, q0, [sp, #192]
stp q0, q0, [sp, #224]
stp q0, q0, [sp, #256]
stp q0, q0, [sp, #288]
stp q0, q0, [sp, #320]
stp q0, q0, [sp, #352]
stp q0, q0, [sp, #384]
stp q0, q0, [sp, #416]
stp q0, q0, [sp, #448]
stp q0, q0, [sp, #480]
cbz w9, .LBB0_3
mov x8, x0
mov x11, xzr
mov w10, wzr
mov w0, wzr
add x8, x8, #1
mov x12, sp
.LBB0_2:
ldr w13, [x12, w9, uxtw #2]
add x14, x11, #1
str w14, [x12, w9, uxtw #2]
ldrb w9, [x8, x11]
sub w13, w11, w13
mov x11, x14
cmp w10, w13
csel w13, w10, w13, lt
add w10, w13, #1
cmp w0, w10
csinc w0, w0, w13, gt
cbnz w9, .LBB0_2
b .LBB0_4
.LBB0_3:
mov w0, wzr
.LBB0_4:
add sp, sp, #512
ldr x30, [sp], #16
ret
|
71
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
aarch64
|
-O3
|
armv8-a clang 21.1.0
|
lengthOfLongestSubstring:
str x30, [sp, #-16]!
sub sp, sp, #512
movi v0.2d, #0000000000000000
ldrb w9, [x0]
stp q0, q0, [sp]
stp q0, q0, [sp, #32]
stp q0, q0, [sp, #64]
stp q0, q0, [sp, #96]
stp q0, q0, [sp, #128]
stp q0, q0, [sp, #160]
stp q0, q0, [sp, #192]
stp q0, q0, [sp, #224]
stp q0, q0, [sp, #256]
stp q0, q0, [sp, #288]
stp q0, q0, [sp, #320]
stp q0, q0, [sp, #352]
stp q0, q0, [sp, #384]
stp q0, q0, [sp, #416]
stp q0, q0, [sp, #448]
stp q0, q0, [sp, #480]
cbz w9, .LBB0_4
mov x8, x0
mov x11, xzr
mov w10, wzr
mov w0, wzr
add x8, x8, #1
mov x12, sp
.LBB0_2:
ldr w13, [x12, w9, uxtw #2]
add x14, x11, #1
str w14, [x12, w9, uxtw #2]
ldrb w9, [x8, x11]
sub w13, w11, w13
mov x11, x14
cmp w10, w13
csel w13, w10, w13, lt
add w10, w13, #1
cmp w0, w10
csinc w0, w0, w13, gt
cbnz w9, .LBB0_2
add sp, sp, #512
ldr x30, [sp], #16
ret
.LBB0_4:
mov w0, wzr
add sp, sp, #512
ldr x30, [sp], #16
ret
|
72
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O0
|
mips64 clang 21.1.0
|
lengthOfLongestSubstring:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -592
sd $ra, 584($sp)
sd $fp, 576($sp)
sd $gp, 568($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(lengthOfLongestSubstring)))
sd $4, 560($fp)
sw $zero, 544($fp)
ld $25, %call16(memset)($gp)
daddiu $4, $fp, 28
daddiu $5, $zero, 0
daddiu $6, $zero, 512
jalr $25
nop
sw $zero, 12($fp)
sw $zero, 556($fp)
b .LBB0_1
nop
.LBB0_1:
ld $1, 560($fp)
lw $2, 556($fp)
daddu $1, $1, $2
lbu $1, 0($1)
beqz $1, .LBB0_13
nop
b .LBB0_3
nop
.LBB0_3:
lw $1, 12($fp)
addiu $1, $1, 1
sw $1, 12($fp)
ld $1, 560($fp)
lw $2, 556($fp)
daddu $1, $1, $2
lbu $1, 0($1)
sb $1, 543($fp)
lw $1, 556($fp)
lb $2, 543($fp)
dsll $3, $2, 2
daddiu $2, $fp, 28
daddu $3, $2, $3
lw $3, 0($3)
subu $1, $1, $3
addiu $1, $1, 1
sw $1, 548($fp)
lw $1, 556($fp)
addiu $1, $1, 1
lb $3, 543($fp)
dsll $3, $3, 2
daddu $2, $2, $3
sw $1, 0($2)
lw $1, 12($fp)
lw $2, 548($fp)
slt $1, $1, $2
beqz $1, .LBB0_6
nop
b .LBB0_5
nop
.LBB0_5:
lw $1, 12($fp)
sw $1, 8($fp)
b .LBB0_7
nop
.LBB0_6:
lw $1, 548($fp)
sw $1, 8($fp)
b .LBB0_7
nop
.LBB0_7:
lw $1, 8($fp)
sw $1, 12($fp)
lw $2, 544($fp)
lw $1, 12($fp)
slt $1, $1, $2
beqz $1, .LBB0_10
nop
b .LBB0_9
nop
.LBB0_9:
lw $1, 544($fp)
sw $1, 4($fp)
b .LBB0_11
nop
.LBB0_10:
lw $1, 12($fp)
sw $1, 4($fp)
b .LBB0_11
nop
.LBB0_11:
lw $1, 4($fp)
sw $1, 544($fp)
b .LBB0_12
nop
.LBB0_12:
lw $1, 556($fp)
addiu $1, $1, 1
sw $1, 556($fp)
b .LBB0_1
nop
.LBB0_13:
lw $2, 544($fp)
move $sp, $fp
ld $gp, 568($sp)
ld $fp, 576($sp)
ld $ra, 584($sp)
daddiu $sp, $sp, 592
jr $ra
nop
|
73
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O1
|
mips64 clang 21.1.0
|
lengthOfLongestSubstring:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -560
sd $ra, 552($sp)
sd $fp, 544($sp)
sd $gp, 536($sp)
sd $17, 528($sp)
sd $16, 520($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(lengthOfLongestSubstring)))
move $17, $4
daddiu $16, $fp, 8
ld $25, %call16(memset)($gp)
move $4, $16
daddiu $5, $zero, 0
jalr $25
daddiu $6, $zero, 512
lbu $4, 0($17)
beqz $4, .LBB0_4
nop
daddiu $2, $17, 1
addiu $5, $zero, 0
addiu $3, $zero, 0
addiu $6, $zero, 0
.LBB0_2:
seb $1, $4
dsll $1, $1, 2
daddu $1, $16, $1
lw $4, 0($1)
addiu $8, $5, 1
sw $8, 0($1)
subu $1, $5, $4
daddiu $5, $2, 1
slt $4, $3, $1
movn $1, $3, $4
addiu $3, $1, 1
slt $1, $3, $6
move $7, $3
movn $7, $6, $1
lbu $4, 0($2)
move $2, $5
move $5, $8
bnez $4, .LBB0_2
move $6, $7
b .LBB0_5
nop
.LBB0_4:
addiu $7, $zero, 0
.LBB0_5:
sll $2, $7, 0
move $sp, $fp
ld $16, 520($sp)
ld $17, 528($sp)
ld $gp, 536($sp)
ld $fp, 544($sp)
ld $ra, 552($sp)
jr $ra
daddiu $sp, $sp, 560
|
74
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O2
|
mips64 clang 21.1.0
|
lengthOfLongestSubstring:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -560
sd $ra, 552($sp)
sd $fp, 544($sp)
sd $gp, 536($sp)
sd $17, 528($sp)
sd $16, 520($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(lengthOfLongestSubstring)))
move $17, $4
daddiu $16, $fp, 8
ld $25, %call16(memset)($gp)
move $4, $16
daddiu $5, $zero, 0
jalr $25
daddiu $6, $zero, 512
lbu $4, 0($17)
beqz $4, .LBB0_4
nop
daddiu $2, $17, 1
addiu $5, $zero, 0
addiu $3, $zero, 0
addiu $6, $zero, 0
.LBB0_2:
seb $1, $4
dsll $1, $1, 2
daddu $1, $16, $1
lw $4, 0($1)
addiu $8, $5, 1
sw $8, 0($1)
subu $1, $5, $4
daddiu $5, $2, 1
slt $4, $3, $1
movn $1, $3, $4
addiu $3, $1, 1
slt $1, $3, $6
move $7, $3
movn $7, $6, $1
lbu $4, 0($2)
move $2, $5
move $5, $8
bnez $4, .LBB0_2
move $6, $7
b .LBB0_5
nop
.LBB0_4:
addiu $7, $zero, 0
.LBB0_5:
sll $2, $7, 0
move $sp, $fp
ld $16, 520($sp)
ld $17, 528($sp)
ld $gp, 536($sp)
ld $fp, 544($sp)
ld $ra, 552($sp)
jr $ra
daddiu $sp, $sp, 560
|
75
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O3
|
mips64 clang 21.1.0
|
lengthOfLongestSubstring:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -560
sd $ra, 552($sp)
sd $fp, 544($sp)
sd $gp, 536($sp)
sd $17, 528($sp)
sd $16, 520($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddiu $16, $fp, 8
move $17, $4
daddiu $5, $zero, 0
daddiu $6, $zero, 512
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(lengthOfLongestSubstring)))
ld $25, %call16(memset)($gp)
jalr $25
move $4, $16
lbu $4, 0($17)
beqz $4, .LBB0_4
nop
daddiu $2, $17, 1
addiu $5, $zero, 0
addiu $3, $zero, 0
addiu $6, $zero, 0
.LBB0_2:
seb $1, $4
addiu $8, $5, 1
dsll $1, $1, 2
daddu $1, $16, $1
lw $4, 0($1)
sw $8, 0($1)
subu $1, $5, $4
daddiu $5, $2, 1
slt $4, $3, $1
movn $1, $3, $4
lbu $4, 0($2)
move $2, $5
move $5, $8
addiu $3, $1, 1
slt $1, $3, $6
move $7, $3
movn $7, $6, $1
bnez $4, .LBB0_2
move $6, $7
b .LBB0_5
nop
.LBB0_4:
addiu $7, $zero, 0
.LBB0_5:
sll $2, $7, 0
move $sp, $fp
ld $16, 520($sp)
ld $17, 528($sp)
ld $gp, 536($sp)
ld $fp, 544($sp)
ld $ra, 552($sp)
jr $ra
daddiu $sp, $sp, 560
|
76
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O0
|
mips64 gcc 15.2.0
|
lengthOfLongestSubstring:
daddiu $sp,$sp,-592
sd $31,584($sp)
sd $fp,576($sp)
sd $28,568($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(lengthOfLongestSubstring)))
sd $4,544($fp)
sw $0,4($fp)
daddiu $3,$fp,24
li $2,512 # 0x200
move $6,$2
move $5,$0
move $4,$3
ld $2,%call16(memset)($28)
mtlo $2
mflo $25
jalr $25
nop
sw $0,8($fp)
sw $0,0($fp)
b .L2
nop
.L5:
lw $2,8($fp)
addiu $2,$2,1
sw $2,8($fp)
lw $2,0($fp)
ld $3,544($fp)
daddu $2,$3,$2
lbu $2,0($2)
sb $2,12($fp)
lb $2,12($fp)
dsll $2,$2,2
daddu $2,$fp,$2
lw $2,24($2)
lw $3,0($fp)
subu $2,$3,$2
addiu $2,$2,1
sw $2,16($fp)
lb $2,12($fp)
lw $3,0($fp)
addiu $3,$3,1
dsll $2,$2,2
daddu $2,$fp,$2
sw $3,24($2)
lw $3,8($fp)
lw $2,16($fp)
slt $4,$3,$2
beq $4,$0,.L3
nop
move $2,$3
.L3:
sw $2,8($fp)
lw $3,4($fp)
lw $2,8($fp)
slt $4,$2,$3
beq $4,$0,.L4
nop
move $2,$3
.L4:
sw $2,4($fp)
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
.L2:
lw $2,0($fp)
ld $3,544($fp)
daddu $2,$3,$2
lb $2,0($2)
bne $2,$0,.L5
nop
lw $2,4($fp)
move $sp,$fp
ld $31,584($sp)
ld $fp,576($sp)
ld $28,568($sp)
daddiu $sp,$sp,592
jr $31
nop
|
77
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O1
|
mips64 gcc 15.2.0
|
lengthOfLongestSubstring:
daddiu $sp,$sp,-544
sd $31,536($sp)
sd $28,528($sp)
sd $16,520($sp)
lui $28,%hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(lengthOfLongestSubstring)))
move $16,$4
li $6,512 # 0x200
move $5,$0
ld $25,%call16(memset)($28)
1: jalr $25
move $4,$sp
lb $3,0($16)
beq $3,$0,.L6
daddiu $16,$16,1
move $5,$0
move $2,$0
b .L5
move $8,$0
.L4:
move $2,$3
daddiu $16,$16,1
lb $3,-1($16)
beq $3,$0,.L9
ld $31,536($sp)
.L5:
addiu $7,$5,1
dsll $3,$3,2
daddu $3,$sp,$3
lw $6,0($3)
subu $6,$8,$6
addiu $6,$6,1
addiu $8,$8,1
move $5,$7
slt $7,$6,$7
beq $7,$0,.L3
sw $8,0($3)
move $5,$6
.L3:
slt $6,$5,$2
beq $6,$0,.L4
move $3,$5
b .L4
move $3,$2
.L6:
move $2,$0
ld $31,536($sp)
.L9:
ld $28,528($sp)
ld $16,520($sp)
jr $31
daddiu $sp,$sp,544
|
78
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O2
|
mips64 gcc 15.2.0
|
lengthOfLongestSubstring:
daddiu $sp,$sp,-544
sd $28,528($sp)
lui $28,%hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(lengthOfLongestSubstring)))
ld $25,%call16(memset)($28)
sd $16,520($sp)
move $5,$0
move $16,$4
sd $31,536($sp)
li $6,512 # 0x200
1: jalr $25
move $4,$sp
lb $5,0($16)
beq $5,$0,.L6
daddiu $16,$16,1
move $3,$0
move $2,$0
move $7,$0
.L5:
dsll $5,$5,2
daddu $5,$sp,$5
lw $6,0($5)
addiu $3,$3,1
daddiu $16,$16,1
subu $6,$7,$6
addiu $6,$6,1
addiu $7,$7,1
slt $8,$6,$3
beq $8,$0,.L3
sw $7,0($5)
move $3,$6
.L3:
slt $5,$3,$2
beq $5,$0,.L4
move $6,$3
move $6,$2
.L4:
lb $5,-1($16)
bne $5,$0,.L5
move $2,$6
ld $31,536($sp)
ld $28,528($sp)
ld $16,520($sp)
jr $31
daddiu $sp,$sp,544
.L6:
ld $31,536($sp)
ld $28,528($sp)
ld $16,520($sp)
move $2,$0
jr $31
daddiu $sp,$sp,544
|
79
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
mips64
|
-O3
|
mips64 gcc 15.2.0
|
lengthOfLongestSubstring:
daddiu $sp,$sp,-544
sd $28,528($sp)
lui $28,%hi(%neg(%gp_rel(lengthOfLongestSubstring)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(lengthOfLongestSubstring)))
ld $25,%call16(memset)($28)
sd $16,520($sp)
move $5,$0
move $16,$4
sd $31,536($sp)
li $6,512 # 0x200
1: jalr $25
move $4,$sp
lb $5,0($16)
beq $5,$0,.L6
daddiu $16,$16,1
move $3,$0
move $2,$0
move $7,$0
.L5:
dsll $5,$5,2
daddu $5,$sp,$5
lw $6,0($5)
addiu $3,$3,1
daddiu $16,$16,1
subu $6,$7,$6
addiu $6,$6,1
addiu $7,$7,1
slt $8,$6,$3
beq $8,$0,.L3
sw $7,0($5)
move $3,$6
.L3:
slt $5,$3,$2
beq $5,$0,.L4
move $6,$3
move $6,$2
.L4:
lb $5,-1($16)
bne $5,$0,.L5
move $2,$6
ld $31,536($sp)
ld $28,528($sp)
ld $16,520($sp)
jr $31
daddiu $sp,$sp,544
.L6:
ld $31,536($sp)
ld $28,528($sp)
ld $16,520($sp)
move $2,$0
jr $31
daddiu $sp,$sp,544
|
80
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O0
|
RISC-V 64 clang 21.1.0
|
lengthOfLongestSubstring:
addi sp, sp, -608
sd ra, 600(sp)
sd s0, 592(sp)
addi s0, sp, 608
sd a0, -24(s0)
li a1, 0
sd a1, -584(s0)
sw a1, -40(s0)
addi a0, s0, -556
li a2, 512
call memset
ld a0, -584(s0)
sw a0, -572(s0)
sw a0, -28(s0)
j .LBB0_1
.LBB0_1:
ld a0, -24(s0)
lw a1, -28(s0)
add a0, a0, a1
lbu a0, 0(a0)
beqz a0, .LBB0_10
j .LBB0_2
.LBB0_2:
lw a0, -572(s0)
addiw a0, a0, 1
sw a0, -572(s0)
ld a0, -24(s0)
lw a1, -28(s0)
add a0, a0, a1
lbu a0, 0(a0)
sb a0, -41(s0)
lw a0, -28(s0)
lbu a1, -41(s0)
slli a2, a1, 2
addi a1, s0, -556
add a2, a2, a1
lw a2, 0(a2)
subw a0, a0, a2
addiw a0, a0, 1
sw a0, -36(s0)
lw a0, -28(s0)
addiw a0, a0, 1
lbu a2, -41(s0)
slli a2, a2, 2
add a1, a1, a2
sw a0, 0(a1)
lw a0, -572(s0)
lw a1, -36(s0)
bge a0, a1, .LBB0_4
j .LBB0_3
.LBB0_3:
lw a0, -572(s0)
sd a0, -592(s0)
j .LBB0_5
.LBB0_4:
lw a0, -36(s0)
sd a0, -592(s0)
j .LBB0_5
.LBB0_5:
ld a0, -592(s0)
sw a0, -572(s0)
lw a1, -40(s0)
lw a0, -572(s0)
bge a0, a1, .LBB0_7
j .LBB0_6
.LBB0_6:
lw a0, -40(s0)
sd a0, -600(s0)
j .LBB0_8
.LBB0_7:
lw a0, -572(s0)
sd a0, -600(s0)
j .LBB0_8
.LBB0_8:
ld a0, -600(s0)
sw a0, -40(s0)
j .LBB0_9
.LBB0_9:
lw a0, -28(s0)
addiw a0, a0, 1
sw a0, -28(s0)
j .LBB0_1
.LBB0_10:
lw a0, -40(s0)
ld ra, 600(sp)
ld s0, 592(sp)
addi sp, sp, 608
ret
|
81
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O1
|
RISC-V 64 clang 21.1.0
|
lengthOfLongestSubstring:
addi sp, sp, -544
sd ra, 536(sp)
sd s0, 528(sp)
sd s1, 520(sp)
mv s0, a0
addi a0, sp, 8
li a2, 512
addi s1, sp, 8
li a1, 0
call memset
lbu a3, 0(s0)
beqz a3, .LBB0_7
li a1, 0
li a2, 0
li a0, 0
addi s0, s0, 1
j .LBB0_3
.LBB0_2:
lbu a3, 0(s0)
addi s0, s0, 1
beqz a3, .LBB0_8
.LBB0_3:
slli a3, a3, 2
add a4, s1, a3
lw a3, 0(a4)
subw a3, a1, a3
addi a1, a1, 1
sw a1, 0(a4)
blt a2, a3, .LBB0_5
mv a2, a3
.LBB0_5:
addiw a2, a2, 1
blt a2, a0, .LBB0_2
mv a0, a2
j .LBB0_2
.LBB0_7:
li a0, 0
.LBB0_8:
ld ra, 536(sp)
ld s0, 528(sp)
ld s1, 520(sp)
addi sp, sp, 544
ret
|
82
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O2
|
RISC-V 64 clang 21.1.0
|
lengthOfLongestSubstring:
addi sp, sp, -544
sd ra, 536(sp)
sd s0, 528(sp)
sd s1, 520(sp)
mv s0, a0
addi a0, sp, 8
li a2, 512
addi s1, sp, 8
li a1, 0
call memset
lbu a3, 0(s0)
beqz a3, .LBB0_7
li a1, 0
li a2, 0
li a0, 0
addi s0, s0, 1
j .LBB0_3
.LBB0_2:
lbu a3, 0(s0)
addi s0, s0, 1
beqz a3, .LBB0_8
.LBB0_3:
slli a3, a3, 2
add a4, s1, a3
lw a3, 0(a4)
subw a3, a1, a3
addi a1, a1, 1
sw a1, 0(a4)
blt a2, a3, .LBB0_5
mv a2, a3
.LBB0_5:
addiw a2, a2, 1
blt a2, a0, .LBB0_2
mv a0, a2
j .LBB0_2
.LBB0_7:
li a0, 0
.LBB0_8:
ld ra, 536(sp)
ld s0, 528(sp)
ld s1, 520(sp)
addi sp, sp, 544
ret
|
83
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O3
|
RISC-V 64 clang 21.1.0
|
lengthOfLongestSubstring:
addi sp, sp, -544
sd ra, 536(sp)
sd s0, 528(sp)
sd s1, 520(sp)
mv s0, a0
addi a0, sp, 8
li a2, 512
addi s1, sp, 8
li a1, 0
call memset
lbu a3, 0(s0)
beqz a3, .LBB0_7
li a1, 0
li a2, 0
li a0, 0
addi s0, s0, 1
j .LBB0_3
.LBB0_2:
lbu a3, 0(s0)
addi s0, s0, 1
beqz a3, .LBB0_8
.LBB0_3:
slli a3, a3, 2
add a4, s1, a3
lw a3, 0(a4)
subw a3, a1, a3
addi a1, a1, 1
sw a1, 0(a4)
bge a2, a3, .LBB0_5
addiw a2, a2, 1
blt a2, a0, .LBB0_2
j .LBB0_6
.LBB0_5:
addiw a2, a3, 1
blt a2, a0, .LBB0_2
.LBB0_6:
mv a0, a2
j .LBB0_2
.LBB0_7:
li a0, 0
.LBB0_8:
ld ra, 536(sp)
ld s0, 528(sp)
ld s1, 520(sp)
addi sp, sp, 544
ret
|
84
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O0
|
RISC-V 64 gcc 15.2.0
|
lengthOfLongestSubstring:
addi sp,sp,-576
sd ra,568(sp)
sd s0,560(sp)
addi s0,sp,576
sd a0,-568(s0)
sw zero,-24(s0)
addi a5,s0,-552
li a4,512
mv a2,a4
li a1,0
mv a0,a5
call memset
sw zero,-28(s0)
sw zero,-20(s0)
j .L2
.L5:
lw a5,-28(s0)
addiw a5,a5,1
sw a5,-28(s0)
lw a5,-20(s0)
ld a4,-568(s0)
add a5,a4,a5
lbu a5,0(a5)
sb a5,-29(s0)
lbu a5,-29(s0)
sext.w a4,a5
addi a5,s0,-552
slli a4,a4,2
add a5,a4,a5
lw a5,0(a5)
lw a4,-20(s0)
subw a5,a4,a5
sext.w a5,a5
addiw a5,a5,1
sw a5,-36(s0)
lbu a5,-29(s0)
sext.w a3,a5
lw a5,-20(s0)
addiw a5,a5,1
sext.w a4,a5
addi a5,s0,-552
slli a3,a3,2
add a5,a3,a5
sw a4,0(a5)
lw a5,-28(s0)
mv a2,a5
lw a5,-36(s0)
sext.w a3,a5
sext.w a4,a2
ble a3,a4,.L3
mv a5,a2
.L3:
sw a5,-28(s0)
lw a5,-24(s0)
mv a2,a5
lw a5,-28(s0)
sext.w a3,a5
sext.w a4,a2
bge a3,a4,.L4
mv a5,a2
.L4:
sw a5,-24(s0)
lw a5,-20(s0)
addiw a5,a5,1
sw a5,-20(s0)
.L2:
lw a5,-20(s0)
ld a4,-568(s0)
add a5,a4,a5
lbu a5,0(a5)
bne a5,zero,.L5
lw a5,-24(s0)
mv a0,a5
ld ra,568(sp)
ld s0,560(sp)
addi sp,sp,576
jr ra
|
85
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O1
|
RISC-V 64 gcc 15.2.0
|
lengthOfLongestSubstring:
addi sp,sp,-528
sd ra,520(sp)
sd s0,512(sp)
mv s0,a0
li a2,512
li a1,0
mv a0,sp
call memset
lbu a5,0(s0)
beq a5,zero,.L6
addi a2,s0,1
li a3,0
li a0,0
li a1,0
mv a7,sp
j .L5
.L4:
sext.w a0,a4
addi a2,a2,1
lbu a5,-1(a2)
beq a5,zero,.L2
.L5:
addiw a3,a3,1
slli a5,a5,2
add a5,a5,a7
lw a4,0(a5)
subw a4,a1,a4
addiw a4,a4,1
addiw a6,a1,1
mv a1,a6
sw a6,0(a5)
mv a5,a3
ble a3,a4,.L3
mv a5,a4
.L3:
sext.w a3,a5
mv a4,a5
bge a3,a0,.L4
mv a4,a0
j .L4
.L6:
li a0,0
.L2:
ld ra,520(sp)
ld s0,512(sp)
addi sp,sp,528
jr ra
|
86
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O2
|
RISC-V 64 gcc 15.2.0
|
lengthOfLongestSubstring:
addi sp,sp,-528
sd s0,512(sp)
li a2,512
mv s0,a0
li a1,0
mv a0,sp
sd ra,520(sp)
call memset
lbu a5,0(s0)
beq a5,zero,.L6
mv a6,a0
addi a1,s0,1
li a3,0
li a0,0
li a2,0
.L5:
slli a5,a5,2
add a5,a6,a5
lw a4,0(a5)
addiw a3,a3,1
addi a1,a1,1
subw a4,a2,a4
addiw a2,a2,1
sw a2,0(a5)
addiw a4,a4,1
mv a5,a3
ble a3,a4,.L3
mv a5,a4
.L3:
sext.w a3,a5
mv a4,a5
bge a3,a0,.L4
mv a4,a0
.L4:
lbu a5,-1(a1)
sext.w a0,a4
bne a5,zero,.L5
ld ra,520(sp)
ld s0,512(sp)
addi sp,sp,528
jr ra
.L6:
ld ra,520(sp)
ld s0,512(sp)
li a0,0
addi sp,sp,528
jr ra
|
87
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
riscv64
|
-O3
|
RISC-V 64 gcc 15.2.0
|
lengthOfLongestSubstring:
addi sp,sp,-528
sd s0,512(sp)
li a2,512
mv s0,a0
li a1,0
mv a0,sp
sd ra,520(sp)
call memset
lbu a5,0(s0)
beq a5,zero,.L6
mv a6,a0
addi a1,s0,1
li a3,0
li a0,0
li a2,0
.L5:
slli a5,a5,2
add a5,a6,a5
lw a4,0(a5)
addiw a3,a3,1
addi a1,a1,1
subw a4,a2,a4
addiw a2,a2,1
sw a2,0(a5)
addiw a4,a4,1
mv a5,a3
ble a3,a4,.L3
mv a5,a4
.L3:
sext.w a3,a5
mv a4,a5
bge a3,a0,.L4
mv a4,a0
.L4:
lbu a5,-1(a1)
sext.w a0,a4
bne a5,zero,.L5
ld ra,520(sp)
ld s0,512(sp)
addi sp,sp,528
jr ra
.L6:
ld ra,520(sp)
ld s0,512(sp)
li a0,0
addi sp,sp,528
jr ra
|
88
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O0
|
x86-64 clang 21.1.0
|
lengthOfLongestSubstring:
push rbp
mov rbp, rsp
sub rsp, 576
mov qword ptr [rbp - 8], rdi
mov dword ptr [rbp - 24], 0
lea rdi, [rbp - 544]
xor esi, esi
mov edx, 512
call memset@PLT
mov dword ptr [rbp - 556], 0
mov dword ptr [rbp - 12], 0
.LBB0_1:
mov rax, qword ptr [rbp - 8]
movsxd rcx, dword ptr [rbp - 12]
cmp byte ptr [rax + rcx], 0
je .LBB0_10
mov eax, dword ptr [rbp - 556]
add eax, 1
mov dword ptr [rbp - 556], eax
mov rax, qword ptr [rbp - 8]
movsxd rcx, dword ptr [rbp - 12]
mov al, byte ptr [rax + rcx]
mov byte ptr [rbp - 25], al
mov eax, dword ptr [rbp - 12]
movsx rcx, byte ptr [rbp - 25]
sub eax, dword ptr [rbp + 4*rcx - 544]
add eax, 1
mov dword ptr [rbp - 20], eax
mov ecx, dword ptr [rbp - 12]
add ecx, 1
movsx rax, byte ptr [rbp - 25]
mov dword ptr [rbp + 4*rax - 544], ecx
mov eax, dword ptr [rbp - 556]
cmp eax, dword ptr [rbp - 20]
jge .LBB0_4
mov eax, dword ptr [rbp - 556]
mov dword ptr [rbp - 560], eax
jmp .LBB0_5
.LBB0_4:
mov eax, dword ptr [rbp - 20]
mov dword ptr [rbp - 560], eax
.LBB0_5:
mov eax, dword ptr [rbp - 560]
mov dword ptr [rbp - 556], eax
mov eax, dword ptr [rbp - 24]
cmp eax, dword ptr [rbp - 556]
jle .LBB0_7
mov eax, dword ptr [rbp - 24]
mov dword ptr [rbp - 564], eax
jmp .LBB0_8
.LBB0_7:
mov eax, dword ptr [rbp - 556]
mov dword ptr [rbp - 564], eax
.LBB0_8:
mov eax, dword ptr [rbp - 564]
mov dword ptr [rbp - 24], eax
mov eax, dword ptr [rbp - 12]
add eax, 1
mov dword ptr [rbp - 12], eax
jmp .LBB0_1
.LBB0_10:
mov eax, dword ptr [rbp - 24]
add rsp, 576
pop rbp
ret
|
89
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O1
|
x86-64 clang 21.1.0
|
lengthOfLongestSubstring:
push rbp
push rbx
sub rsp, 520
mov rbx, rdi
mov rdi, rsp
xor ebp, ebp
mov edx, 512
xor esi, esi
call memset@PLT
movzx ecx, byte ptr [rbx]
test cl, cl
je .LBB0_3
xor eax, eax
xor edx, edx
xor ebp, ebp
.LBB0_2:
movsx rcx, cl
mov esi, dword ptr [rsp + 4*rcx]
lea rdi, [rax + 1]
mov r8d, eax
sub r8d, esi
mov dword ptr [rsp + 4*rcx], edi
cmp edx, r8d
cmovl r8d, edx
inc r8d
cmp ebp, r8d
cmovle ebp, r8d
movzx ecx, byte ptr [rbx + rax + 1]
mov edx, r8d
mov rax, rdi
test cl, cl
jne .LBB0_2
.LBB0_3:
mov eax, ebp
add rsp, 520
pop rbx
pop rbp
ret
|
90
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O2
|
x86-64 clang 21.1.0
|
lengthOfLongestSubstring:
push rbp
push rbx
sub rsp, 520
mov rbx, rdi
mov rdi, rsp
xor ebp, ebp
mov edx, 512
xor esi, esi
call memset@PLT
movzx ecx, byte ptr [rbx]
test cl, cl
je .LBB0_3
xor eax, eax
xor edx, edx
xor ebp, ebp
.LBB0_2:
movsx rcx, cl
mov esi, dword ptr [rsp + 4*rcx]
lea rdi, [rax + 1]
mov r8d, eax
sub r8d, esi
mov dword ptr [rsp + 4*rcx], edi
cmp edx, r8d
cmovl r8d, edx
inc r8d
cmp ebp, r8d
cmovle ebp, r8d
movzx ecx, byte ptr [rbx + rax + 1]
mov edx, r8d
mov rax, rdi
test cl, cl
jne .LBB0_2
.LBB0_3:
mov eax, ebp
add rsp, 520
pop rbx
pop rbp
ret
|
91
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O3
|
x86-64 clang 21.1.0
|
lengthOfLongestSubstring:
push rbp
push rbx
sub rsp, 520
mov rbx, rdi
mov rdi, rsp
xor ebp, ebp
mov edx, 512
xor esi, esi
call memset@PLT
movzx ecx, byte ptr [rbx]
test cl, cl
je .LBB0_3
xor eax, eax
xor edx, edx
xor ebp, ebp
.LBB0_2:
movsx rcx, cl
mov esi, dword ptr [rsp + 4*rcx]
lea rdi, [rax + 1]
mov r8d, eax
sub r8d, esi
mov dword ptr [rsp + 4*rcx], edi
cmp edx, r8d
cmovl r8d, edx
inc r8d
cmp ebp, r8d
cmovle ebp, r8d
movzx ecx, byte ptr [rbx + rax + 1]
mov edx, r8d
mov rax, rdi
test cl, cl
jne .LBB0_2
.LBB0_3:
mov eax, ebp
add rsp, 520
pop rbx
pop rbp
ret
|
92
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O0
|
x86-64 gcc 15.2
|
lengthOfLongestSubstring:
push rbp
mov rbp, rsp
sub rsp, 440
mov QWORD PTR [rbp-552], rdi
mov DWORD PTR [rbp-8], 0
lea rdx, [rbp-544]
mov eax, 0
mov ecx, 64
mov rdi, rdx
rep stosq
mov DWORD PTR [rbp-12], 0
mov DWORD PTR [rbp-4], 0
jmp .L2
.L3:
add DWORD PTR [rbp-12], 1
mov eax, DWORD PTR [rbp-4]
movsx rdx, eax
mov rax, QWORD PTR [rbp-552]
add rax, rdx
movzx eax, BYTE PTR [rax]
mov BYTE PTR [rbp-13], al
movsx eax, BYTE PTR [rbp-13]
cdqe
mov eax, DWORD PTR [rbp-544+rax*4]
mov edx, DWORD PTR [rbp-4]
sub edx, eax
lea eax, [rdx+1]
mov DWORD PTR [rbp-20], eax
movsx eax, BYTE PTR [rbp-13]
mov edx, DWORD PTR [rbp-4]
add edx, 1
cdqe
mov DWORD PTR [rbp-544+rax*4], edx
mov edx, DWORD PTR [rbp-12]
mov eax, DWORD PTR [rbp-20]
cmp edx, eax
cmovle eax, edx
mov DWORD PTR [rbp-12], eax
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-12]
cmp edx, eax
cmovge eax, edx
mov DWORD PTR [rbp-8], eax
add DWORD PTR [rbp-4], 1
.L2:
mov eax, DWORD PTR [rbp-4]
movsx rdx, eax
mov rax, QWORD PTR [rbp-552]
add rax, rdx
movzx eax, BYTE PTR [rax]
test al, al
jne .L3
mov eax, DWORD PTR [rbp-8]
leave
ret
|
93
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O1
|
x86-64 gcc 15.2
|
lengthOfLongestSubstring:
sub rsp, 400
mov rsi, rdi
lea rdi, [rsp-120]
mov ecx, 64
mov eax, 0
rep stosq
movzx edx, BYTE PTR [rsi]
test dl, dl
je .L4
lea rdi, [rsi+1]
mov r8d, 0
mov esi, 0
.L3:
add eax, 1
movsx rdx, dl
mov ecx, esi
sub ecx, DWORD PTR [rsp-120+rdx*4]
add ecx, 1
add esi, 1
mov DWORD PTR [rsp-120+rdx*4], esi
cmp eax, ecx
cmovg eax, ecx
cmp r8d, eax
cmovl r8d, eax
add rdi, 1
movzx edx, BYTE PTR [rdi-1]
test dl, dl
jne .L3
.L1:
mov eax, r8d
add rsp, 400
ret
.L4:
mov r8d, 0
jmp .L1
|
94
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O2
|
x86-64 gcc 15.2
|
lengthOfLongestSubstring:
mov rsi, rdi
sub rsp, 400
mov ecx, 64
xor eax, eax
movsx rdx, BYTE PTR [rsi]
lea rdi, [rsp-120]
rep stosq
test dl, dl
je .L4
lea rdi, [rsi+1]
xor r8d, r8d
xor esi, esi
.L3:
mov ecx, esi
sub ecx, DWORD PTR [rsp-120+rdx*4]
add eax, 1
add esi, 1
add ecx, 1
mov DWORD PTR [rsp-120+rdx*4], esi
cmp eax, ecx
cmovg eax, ecx
cmp r8d, eax
cmovl r8d, eax
movsx rdx, BYTE PTR [rdi]
add rdi, 1
test dl, dl
jne .L3
mov eax, r8d
add rsp, 400
ret
.L4:
xor r8d, r8d
add rsp, 400
mov eax, r8d
ret
|
95
| 3
|
Longest Substring Without Repeating Characters
|
Medium
|
/*
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/
int lengthOfLongestSubstring(char* s) {
int i, j, l, k = 0;
char c;
int pos[128] = { 0 };
char *p;
int n = 0;
for (i = 0; s[i]; i ++) {
n ++;
c = s[i];
l = i - pos[c] + 1;
pos[c] = i + 1;
n = n < l ? n : l;
k = k > n ? k : n;
}
return k;
}
/*
Difficulty:Medium
Total Accepted:330.3K
Total Submissions:1.4M
Companies Amazon Adobe Bloomberg Yelp
Related Topics Hash Table Two Pointers String
Similar Questions
Longest Substring with At Most Two Distinct Characters
*/
|
x86-64
|
-O3
|
x86-64 gcc 15.2
|
lengthOfLongestSubstring:
mov rsi, rdi
sub rsp, 400
mov ecx, 64
xor eax, eax
movsx rdx, BYTE PTR [rsi]
lea rdi, [rsp-120]
rep stosq
test dl, dl
je .L4
lea rdi, [rsi+1]
xor r8d, r8d
xor esi, esi
.L3:
mov ecx, esi
sub ecx, DWORD PTR [rsp-120+rdx*4]
add eax, 1
add esi, 1
add ecx, 1
mov DWORD PTR [rsp-120+rdx*4], esi
cmp eax, ecx
cmovg eax, ecx
cmp r8d, eax
cmovl r8d, eax
movsx rdx, BYTE PTR [rdi]
add rdi, 1
test dl, dl
jne .L3
mov eax, r8d
add rsp, 400
ret
.L4:
xor r8d, r8d
add rsp, 400
mov eax, r8d
ret
|
96
| 4
|
Median of Two Sorted Arrays
|
Hard
|
/*
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
*/
int bs(int *n, int sz, int k) {
int i, j, m;
i = 0;
j = sz - 1;
while (i <= j) {
m = i + (j - i) / 2;
if (n[m] > k) {
j = m - 1;
} else {
i = m + 1;
}
}
return j; // this is the lastest one which is not greater than k
}
double bs2(int *n1, int sz1, int *n2, int sz2, int m, int m1) {
double d;
int i;
i = bs(n1, sz1, n2[0]); // search in first array
if (i >= m) { // median is in the first array
d = n1[m];
if (m1) {
if (i > m) {
d += n1[m + 1];
} else {
d += n2[0];
}
d /= 2;
}
} else if (i == sz1 - 1) { // median is in the second array
d = n2[m - i - 1];
if (m1) {
d += n2[m - i];
d /= 2;
}
} else { // reverse arrays and search again
d = bs2(n2, sz2, &n1[i + 1], sz1 - i - 1, m - i - 1, m1);
}
return d;
}
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
double d;
#if 0 // sliding & binary search
int i1_left = 0, i1_right = nums1Size;
int i1, i2;
if (nums1Size > nums2Size) { // make nums1 as a short array
return findMedianSortedArrays(nums2, nums2Size, nums1, nums1Size);
}
// O(log(min(m, n)))
while (i1_left <= i1_right) {
i1 = i1_left + (i1_right - i1_left) / 2;
i2 = (nums1Size + nums2Size + 1) / 2 - i1;
if (i1 > 0 && nums1[i1 - 1] > nums2[i2]) {
i1_right = i1 - 1;
} else if (i1 < nums1Size && nums1[i1] < nums2[i2 - 1]) {
i1_left = i1 + 1;
} else {
// found it!
if (i1 == 0) d = nums2[i2 - 1];
else if (i2 == 0) d = nums1[i1 - 1];
else if (nums1[i1 - 1] > nums2[i2 - 1]) d = nums1[i1 - 1];
else d = nums2[i2 - 1];
if (((nums1Size + nums2Size) % 2) == 0) {
if (i2 == nums2Size) {
d = (d + nums1[i1]) / 2;
} else if (i1 == nums1Size) {
d = (d + nums2[i2]) / 2;
} else if (nums1[i1] < nums2[i2]) {
d = (d + nums1[i1]) / 2;
} else {
d = (d + nums2[i2]) / 2;
}
}
break;
}
}
#else // binary search 40+ms
int p1, p2;
p1 = (nums1Size + nums2Size - 1) / 2;
p2 = ((nums1Size + nums2Size) % 2) ? 0 : 1;
if (nums2Size == 0) {
d = nums1[p1];
if (p2) {
d += nums1[p1 + 1];
d /= 2;
}
return d;
}
if (nums1Size == 0) {
d = nums2[p1];
if (p2) {
d += nums2[p1 + 1];
d /= 2;
}
return d;
}
if (nums2[0] < nums1[0]) return bs2(nums2, nums2Size, nums1, nums1Size, p1, p2);
return bs2(nums1, nums1Size, nums2, nums2Size, p1, p2);
#endif
return d;
}
/*
Difficulty:Hard
Total Accepted:182.7K
Total Submissions:839.6K
Companies Google Zenefits Microsoft Apple Yahoo Dropbox Adobe
Related Topics Binary Search Array Divide and Conquer
*/
|
aarch64
|
-O0
|
ARM64 gcc 15.2.0
|
bs:
sub sp, sp, #32
str x0, [sp, 8]
str w1, [sp, 4]
str w2, [sp]
str wzr, [sp, 28]
ldr w0, [sp, 4]
sub w0, w0, #1
str w0, [sp, 24]
b .L2
.L4:
ldr w1, [sp, 24]
ldr w0, [sp, 28]
sub w0, w1, w0
lsr w1, w0, 31
add w0, w1, w0
asr w0, w0, 1
mov w1, w0
ldr w0, [sp, 28]
add w0, w0, w1
str w0, [sp, 20]
ldrsw x0, [sp, 20]
lsl x0, x0, 2
ldr x1, [sp, 8]
add x0, x1, x0
ldr w0, [x0]
ldr w1, [sp]
cmp w1, w0
bge .L3
ldr w0, [sp, 20]
sub w0, w0, #1
str w0, [sp, 24]
b .L2
.L3:
ldr w0, [sp, 20]
add w0, w0, 1
str w0, [sp, 28]
.L2:
ldr w1, [sp, 28]
ldr w0, [sp, 24]
cmp w1, w0
ble .L4
ldr w0, [sp, 24]
add sp, sp, 32
ret
bs2:
stp x29, x30, [sp, -64]!
mov x29, sp
str x0, [sp, 40]
str w1, [sp, 36]
str x2, [sp, 24]
str w3, [sp, 32]
str w4, [sp, 20]
str w5, [sp, 16]
ldr x0, [sp, 24]
ldr w0, [x0]
mov w2, w0
ldr w1, [sp, 36]
ldr x0, [sp, 40]
bl bs
str w0, [sp, 52]
ldr w1, [sp, 52]
ldr w0, [sp, 20]
cmp w1, w0
blt .L7
ldrsw x0, [sp, 20]
lsl x0, x0, 2
ldr x1, [sp, 40]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
str d31, [sp, 56]
ldr w0, [sp, 16]
cmp w0, 0
beq .L8
ldr w1, [sp, 52]
ldr w0, [sp, 20]
cmp w1, w0
ble .L9
ldrsw x0, [sp, 20]
add x0, x0, 1
lsl x0, x0, 2
ldr x1, [sp, 40]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
ldr d30, [sp, 56]
fadd d31, d30, d31
str d31, [sp, 56]
b .L10
.L9:
ldr x0, [sp, 24]
ldr w0, [x0]
scvtf d31, w0
ldr d30, [sp, 56]
fadd d31, d30, d31
str d31, [sp, 56]
.L10:
fmov d31, 2.0e+0
ldr d30, [sp, 56]
fdiv d31, d30, d31
str d31, [sp, 56]
b .L8
.L7:
ldr w0, [sp, 36]
sub w0, w0, #1
ldr w1, [sp, 52]
cmp w1, w0
bne .L11
ldr w1, [sp, 20]
ldr w0, [sp, 52]
sub w0, w1, w0
sxtw x0, w0
lsl x0, x0, 2
sub x0, x0, #4
ldr x1, [sp, 24]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
str d31, [sp, 56]
ldr w0, [sp, 16]
cmp w0, 0
beq .L8
ldr w1, [sp, 20]
ldr w0, [sp, 52]
sub w0, w1, w0
sxtw x0, w0
lsl x0, x0, 2
ldr x1, [sp, 24]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
ldr d30, [sp, 56]
fadd d31, d30, d31
str d31, [sp, 56]
fmov d31, 2.0e+0
ldr d30, [sp, 56]
fdiv d31, d30, d31
str d31, [sp, 56]
b .L8
.L11:
ldrsw x0, [sp, 52]
add x0, x0, 1
lsl x0, x0, 2
ldr x1, [sp, 40]
add x2, x1, x0
ldr w1, [sp, 36]
ldr w0, [sp, 52]
sub w0, w1, w0
sub w3, w0, #1
ldr w1, [sp, 20]
ldr w0, [sp, 52]
sub w0, w1, w0
sub w0, w0, #1
ldr w5, [sp, 16]
mov w4, w0
ldr w1, [sp, 32]
ldr x0, [sp, 24]
bl bs2
str d0, [sp, 56]
.L8:
ldr d31, [sp, 56]
fmov d0, d31
ldp x29, x30, [sp], 64
ret
min:
sub sp, sp, #16
str w0, [sp, 12]
str w1, [sp, 8]
ldr w0, [sp, 12]
ldr w2, [sp, 8]
ldr w1, [sp, 8]
cmp w2, w0
csel w0, w1, w0, le
add sp, sp, 16
ret
max:
sub sp, sp, #16
str w0, [sp, 12]
str w1, [sp, 8]
ldr w0, [sp, 12]
ldr w2, [sp, 8]
ldr w1, [sp, 8]
cmp w2, w0
csel w0, w1, w0, ge
add sp, sp, 16
ret
findMedianSortedArrays:
stp x29, x30, [sp, -64]!
mov x29, sp
str x0, [sp, 40]
str w1, [sp, 36]
str x2, [sp, 24]
str w3, [sp, 32]
ldr w1, [sp, 36]
ldr w0, [sp, 32]
add w0, w1, w0
sub w0, w0, #1
lsr w1, w0, 31
add w0, w1, w0
asr w0, w0, 1
str w0, [sp, 52]
ldr w1, [sp, 36]
ldr w0, [sp, 32]
add w0, w1, w0
and w0, w0, 1
ubfx x0, x0, 0, 1
eor w0, w0, 1
and w0, w0, 255
str w0, [sp, 48]
ldr w0, [sp, 32]
cmp w0, 0
bne .L18
ldrsw x0, [sp, 52]
lsl x0, x0, 2
ldr x1, [sp, 40]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
str d31, [sp, 56]
ldr w0, [sp, 48]
cmp w0, 0
beq .L19
ldrsw x0, [sp, 52]
add x0, x0, 1
lsl x0, x0, 2
ldr x1, [sp, 40]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
ldr d30, [sp, 56]
fadd d31, d30, d31
str d31, [sp, 56]
fmov d31, 2.0e+0
ldr d30, [sp, 56]
fdiv d31, d30, d31
str d31, [sp, 56]
.L19:
ldr d31, [sp, 56]
b .L20
.L18:
ldr w0, [sp, 36]
cmp w0, 0
bne .L21
ldrsw x0, [sp, 52]
lsl x0, x0, 2
ldr x1, [sp, 24]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
str d31, [sp, 56]
ldr w0, [sp, 48]
cmp w0, 0
beq .L22
ldrsw x0, [sp, 52]
add x0, x0, 1
lsl x0, x0, 2
ldr x1, [sp, 24]
add x0, x1, x0
ldr w0, [x0]
scvtf d31, w0
ldr d30, [sp, 56]
fadd d31, d30, d31
str d31, [sp, 56]
fmov d31, 2.0e+0
ldr d30, [sp, 56]
fdiv d31, d30, d31
str d31, [sp, 56]
.L22:
ldr d31, [sp, 56]
b .L20
.L21:
ldr x0, [sp, 24]
ldr w1, [x0]
ldr x0, [sp, 40]
ldr w0, [x0]
cmp w1, w0
bge .L23
ldr w5, [sp, 48]
ldr w4, [sp, 52]
ldr w3, [sp, 36]
ldr x2, [sp, 40]
ldr w1, [sp, 32]
ldr x0, [sp, 24]
bl bs2
fmov d31, d0
b .L20
.L23:
ldr w5, [sp, 48]
ldr w4, [sp, 52]
ldr w3, [sp, 32]
ldr x2, [sp, 24]
ldr w1, [sp, 36]
ldr x0, [sp, 40]
bl bs2
fmov d31, d0
.L20:
fmov d0, d31
ldp x29, x30, [sp], 64
ret
|
97
| 4
|
Median of Two Sorted Arrays
|
Hard
|
/*
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
*/
int bs(int *n, int sz, int k) {
int i, j, m;
i = 0;
j = sz - 1;
while (i <= j) {
m = i + (j - i) / 2;
if (n[m] > k) {
j = m - 1;
} else {
i = m + 1;
}
}
return j; // this is the lastest one which is not greater than k
}
double bs2(int *n1, int sz1, int *n2, int sz2, int m, int m1) {
double d;
int i;
i = bs(n1, sz1, n2[0]); // search in first array
if (i >= m) { // median is in the first array
d = n1[m];
if (m1) {
if (i > m) {
d += n1[m + 1];
} else {
d += n2[0];
}
d /= 2;
}
} else if (i == sz1 - 1) { // median is in the second array
d = n2[m - i - 1];
if (m1) {
d += n2[m - i];
d /= 2;
}
} else { // reverse arrays and search again
d = bs2(n2, sz2, &n1[i + 1], sz1 - i - 1, m - i - 1, m1);
}
return d;
}
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
double d;
#if 0 // sliding & binary search
int i1_left = 0, i1_right = nums1Size;
int i1, i2;
if (nums1Size > nums2Size) { // make nums1 as a short array
return findMedianSortedArrays(nums2, nums2Size, nums1, nums1Size);
}
// O(log(min(m, n)))
while (i1_left <= i1_right) {
i1 = i1_left + (i1_right - i1_left) / 2;
i2 = (nums1Size + nums2Size + 1) / 2 - i1;
if (i1 > 0 && nums1[i1 - 1] > nums2[i2]) {
i1_right = i1 - 1;
} else if (i1 < nums1Size && nums1[i1] < nums2[i2 - 1]) {
i1_left = i1 + 1;
} else {
// found it!
if (i1 == 0) d = nums2[i2 - 1];
else if (i2 == 0) d = nums1[i1 - 1];
else if (nums1[i1 - 1] > nums2[i2 - 1]) d = nums1[i1 - 1];
else d = nums2[i2 - 1];
if (((nums1Size + nums2Size) % 2) == 0) {
if (i2 == nums2Size) {
d = (d + nums1[i1]) / 2;
} else if (i1 == nums1Size) {
d = (d + nums2[i2]) / 2;
} else if (nums1[i1] < nums2[i2]) {
d = (d + nums1[i1]) / 2;
} else {
d = (d + nums2[i2]) / 2;
}
}
break;
}
}
#else // binary search 40+ms
int p1, p2;
p1 = (nums1Size + nums2Size - 1) / 2;
p2 = ((nums1Size + nums2Size) % 2) ? 0 : 1;
if (nums2Size == 0) {
d = nums1[p1];
if (p2) {
d += nums1[p1 + 1];
d /= 2;
}
return d;
}
if (nums1Size == 0) {
d = nums2[p1];
if (p2) {
d += nums2[p1 + 1];
d /= 2;
}
return d;
}
if (nums2[0] < nums1[0]) return bs2(nums2, nums2Size, nums1, nums1Size, p1, p2);
return bs2(nums1, nums1Size, nums2, nums2Size, p1, p2);
#endif
return d;
}
/*
Difficulty:Hard
Total Accepted:182.7K
Total Submissions:839.6K
Companies Google Zenefits Microsoft Apple Yahoo Dropbox Adobe
Related Topics Binary Search Array Divide and Conquer
*/
|
aarch64
|
-O1
|
ARM64 gcc 15.2.0
|
bs:
mov x5, x0
subs w0, w1, #1
bmi .L1
mov w4, 0
b .L5
.L3:
add w4, w3, 1
.L4:
cmp w0, w4
blt .L1
.L5:
sub w3, w0, w4
add w3, w3, w3, lsr 31
add w3, w4, w3, asr 1
ldr w1, [x5, w3, sxtw 2]
cmp w1, w2
ble .L3
sub w0, w3, #1
b .L4
.L1:
ret
bs2:
stp x29, x30, [sp, -80]!
mov x29, sp
stp x19, x20, [sp, 16]
stp x21, x22, [sp, 32]
stp x23, x24, [sp, 48]
str x25, [sp, 64]
mov x21, x0
mov w24, w1
mov x20, x2
mov w25, w3
mov w19, w4
mov w23, w5
ldr w22, [x2]
mov w2, w22
bl bs
cmp w0, w19
blt .L8
sbfiz x1, x19, 2, 32
ldr w2, [x21, x1]
scvtf d0, w2
cbz w23, .L7
ble .L10
add x1, x21, x1
ldr w0, [x1, 4]
scvtf d31, w0
fadd d0, d31, d0
.L11:
fmov d31, 5.0e-1
fmul d0, d0, d31
.L7:
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x23, x24, [sp, 48]
ldr x25, [sp, 64]
ldp x29, x30, [sp], 80
ret
.L10:
scvtf d31, w22
fadd d0, d31, d0
b .L11
.L8:
sub w1, w24, #1
cmp w1, w0
bne .L12
sub w0, w19, w0
add x1, x20, w0, sxtw 2
ldr w1, [x1, -4]
scvtf d0, w1
cbz w23, .L7
ldr w0, [x20, w0, sxtw 2]
scvtf d31, w0
fadd d0, d31, d0
fmov d31, 5.0e-1
fmul d0, d0, d31
b .L7
.L12:
sub w4, w19, w0
sub w3, w24, w0
add x0, x21, w0, sxtw 2
mov w5, w23
sub w4, w4, #1
sub w3, w3, #1
add x2, x0, 4
mov w1, w25
mov x0, x20
bl bs2
b .L7
min:
cmp w1, w0
csel w0, w1, w0, le
ret
max:
cmp w1, w0
csel w0, w1, w0, ge
ret
findMedianSortedArrays:
add w5, w1, w3
sub w4, w5, #1
add w4, w4, w4, lsr 31
asr w4, w4, 1
and w5, w5, 1
cbz w3, .L24
mov x7, x2
mov w6, w3
cbz w1, .L25
stp x29, x30, [sp, -16]!
mov x29, sp
eor w5, w5, 1
ldr w3, [x2]
ldr w2, [x0]
cmp w3, w2
blt .L26
mov w3, w6
mov x2, x7
bl bs2
.L16:
ldp x29, x30, [sp], 16
ret
.L24:
sbfiz x4, x4, 2, 32
ldr w1, [x0, x4]
scvtf d0, w1
cbnz w5, .L22
add x0, x0, x4
ldr w0, [x0, 4]
scvtf d31, w0
fadd d0, d31, d0
fmov d31, 5.0e-1
fmul d0, d0, d31
ret
.L25:
sbfiz x4, x4, 2, 32
ldr w0, [x2, x4]
scvtf d0, w0
cbnz w5, .L22
add x7, x2, x4
ldr w0, [x7, 4]
scvtf d31, w0
fadd d0, d31, d0
fmov d31, 5.0e-1
fmul d0, d0, d31
ret
.L26:
mov w3, w1
mov x2, x0
mov w1, w6
mov x0, x7
bl bs2
b .L16
.L22:
ret
|
98
| 4
|
Median of Two Sorted Arrays
|
Hard
|
/*
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
*/
int bs(int *n, int sz, int k) {
int i, j, m;
i = 0;
j = sz - 1;
while (i <= j) {
m = i + (j - i) / 2;
if (n[m] > k) {
j = m - 1;
} else {
i = m + 1;
}
}
return j; // this is the lastest one which is not greater than k
}
double bs2(int *n1, int sz1, int *n2, int sz2, int m, int m1) {
double d;
int i;
i = bs(n1, sz1, n2[0]); // search in first array
if (i >= m) { // median is in the first array
d = n1[m];
if (m1) {
if (i > m) {
d += n1[m + 1];
} else {
d += n2[0];
}
d /= 2;
}
} else if (i == sz1 - 1) { // median is in the second array
d = n2[m - i - 1];
if (m1) {
d += n2[m - i];
d /= 2;
}
} else { // reverse arrays and search again
d = bs2(n2, sz2, &n1[i + 1], sz1 - i - 1, m - i - 1, m1);
}
return d;
}
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
double d;
#if 0 // sliding & binary search
int i1_left = 0, i1_right = nums1Size;
int i1, i2;
if (nums1Size > nums2Size) { // make nums1 as a short array
return findMedianSortedArrays(nums2, nums2Size, nums1, nums1Size);
}
// O(log(min(m, n)))
while (i1_left <= i1_right) {
i1 = i1_left + (i1_right - i1_left) / 2;
i2 = (nums1Size + nums2Size + 1) / 2 - i1;
if (i1 > 0 && nums1[i1 - 1] > nums2[i2]) {
i1_right = i1 - 1;
} else if (i1 < nums1Size && nums1[i1] < nums2[i2 - 1]) {
i1_left = i1 + 1;
} else {
// found it!
if (i1 == 0) d = nums2[i2 - 1];
else if (i2 == 0) d = nums1[i1 - 1];
else if (nums1[i1 - 1] > nums2[i2 - 1]) d = nums1[i1 - 1];
else d = nums2[i2 - 1];
if (((nums1Size + nums2Size) % 2) == 0) {
if (i2 == nums2Size) {
d = (d + nums1[i1]) / 2;
} else if (i1 == nums1Size) {
d = (d + nums2[i2]) / 2;
} else if (nums1[i1] < nums2[i2]) {
d = (d + nums1[i1]) / 2;
} else {
d = (d + nums2[i2]) / 2;
}
}
break;
}
}
#else // binary search 40+ms
int p1, p2;
p1 = (nums1Size + nums2Size - 1) / 2;
p2 = ((nums1Size + nums2Size) % 2) ? 0 : 1;
if (nums2Size == 0) {
d = nums1[p1];
if (p2) {
d += nums1[p1 + 1];
d /= 2;
}
return d;
}
if (nums1Size == 0) {
d = nums2[p1];
if (p2) {
d += nums2[p1 + 1];
d /= 2;
}
return d;
}
if (nums2[0] < nums1[0]) return bs2(nums2, nums2Size, nums1, nums1Size, p1, p2);
return bs2(nums1, nums1Size, nums2, nums2Size, p1, p2);
#endif
return d;
}
/*
Difficulty:Hard
Total Accepted:182.7K
Total Submissions:839.6K
Companies Google Zenefits Microsoft Apple Yahoo Dropbox Adobe
Related Topics Binary Search Array Divide and Conquer
*/
|
aarch64
|
-O2
|
ARM64 gcc 15.2.0
|
bs:
subs w1, w1, #1
bmi .L1
mov w4, 0
b .L5
.L8:
sub w1, w3, #1
cmp w4, w1
bgt .L1
.L5:
sub w3, w1, w4
add w3, w4, w3, asr 1
ldr w5, [x0, w3, sxtw 2]
cmp w2, w5
blt .L8
add w4, w3, 1
cmp w4, w1
ble .L5
.L1:
mov w0, w1
ret
bs2:
ldr w10, [x2]
subs w11, w1, #1
bmi .L10
.L32:
mov w8, w11
mov w7, 0
b .L13
.L30:
sub w8, w6, #1
cmp w7, w8
bgt .L29
.L13:
sub w6, w8, w7
add w6, w7, w6, asr 1
ldr w9, [x0, w6, sxtw 2]
cmp w10, w9
blt .L30
add w7, w6, 1
cmp w7, w8
ble .L13
.L29:
cmp w4, w8
ble .L31
sub w7, w4, w8
cmp w11, w8
beq .L21
add w6, w8, 1
sub w8, w1, w8
mov w1, w3
sub w4, w7, #1
add x6, x0, w6, sxtw 2
mov x0, x2
mov x2, x6
sub w3, w8, #1
subs w11, w1, #1
ldr w10, [x2]
bpl .L32
.L10:
cmp w4, w11
bgt .L21
.L20:
ldr w1, [x0, w4, sxtw 2]
scvtf d0, w1
cbz w5, .L9
cmp w4, w11
bge .L16
add x4, x0, w4, sxtw 2
ldr w0, [x4, 4]
scvtf d31, w0
fadd d31, d31, d0
.L17:
fmov d30, 5.0e-1
fmul d0, d31, d30
.L9:
ret
.L21:
sub w4, w4, w11
sub w0, w4, #1
ldr w0, [x2, x0, lsl 2]
scvtf d0, w0
cbz w5, .L9
ldr w0, [x2, w4, uxtw 2]
fmov d2, 5.0e-1
scvtf d3, w0
fadd d0, d3, d0
fmul d0, d0, d2
ret
.L16:
scvtf d1, w10
fadd d31, d1, d0
b .L17
.L31:
mov w11, w8
b .L20
min:
cmp w1, w0
csel w0, w1, w0, le
ret
max:
cmp w1, w0
csel w0, w1, w0, ge
ret
findMedianSortedArrays:
add w5, w1, w3
mov x8, x0
sub w4, w5, #1
mov x9, x2
mov w6, w3
and w5, w5, 1
add w4, w4, w4, lsr 31
asr w4, w4, 1
cbz w3, .L40
cbz w1, .L41
ldr w10, [x0]
eor w5, w5, 1
ldr w11, [x2]
cmp w11, w10
blt .L42
b bs2
.L41:
ldr w0, [x2, w4, sxtw 2]
scvtf d0, w0
cbnz w5, .L35
add x4, x2, w4, sxtw 2
fmov d2, 5.0e-1
ldr w0, [x4, 4]
scvtf d3, w0
fadd d0, d3, d0
fmul d0, d0, d2
.L35:
ret
.L40:
ldr w0, [x0, w4, sxtw 2]
scvtf d0, w0
cbnz w5, .L35
add x4, x8, w4, sxtw 2
fmov d31, 5.0e-1
ldr w0, [x4, 4]
scvtf d1, w0
fadd d0, d1, d0
fmul d0, d0, d31
ret
.L42:
mov w3, w1
mov x2, x0
mov w1, w6
mov x0, x9
b bs2
|
99
| 4
|
Median of Two Sorted Arrays
|
Hard
|
/*
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
*/
int bs(int *n, int sz, int k) {
int i, j, m;
i = 0;
j = sz - 1;
while (i <= j) {
m = i + (j - i) / 2;
if (n[m] > k) {
j = m - 1;
} else {
i = m + 1;
}
}
return j; // this is the lastest one which is not greater than k
}
double bs2(int *n1, int sz1, int *n2, int sz2, int m, int m1) {
double d;
int i;
i = bs(n1, sz1, n2[0]); // search in first array
if (i >= m) { // median is in the first array
d = n1[m];
if (m1) {
if (i > m) {
d += n1[m + 1];
} else {
d += n2[0];
}
d /= 2;
}
} else if (i == sz1 - 1) { // median is in the second array
d = n2[m - i - 1];
if (m1) {
d += n2[m - i];
d /= 2;
}
} else { // reverse arrays and search again
d = bs2(n2, sz2, &n1[i + 1], sz1 - i - 1, m - i - 1, m1);
}
return d;
}
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
double d;
#if 0 // sliding & binary search
int i1_left = 0, i1_right = nums1Size;
int i1, i2;
if (nums1Size > nums2Size) { // make nums1 as a short array
return findMedianSortedArrays(nums2, nums2Size, nums1, nums1Size);
}
// O(log(min(m, n)))
while (i1_left <= i1_right) {
i1 = i1_left + (i1_right - i1_left) / 2;
i2 = (nums1Size + nums2Size + 1) / 2 - i1;
if (i1 > 0 && nums1[i1 - 1] > nums2[i2]) {
i1_right = i1 - 1;
} else if (i1 < nums1Size && nums1[i1] < nums2[i2 - 1]) {
i1_left = i1 + 1;
} else {
// found it!
if (i1 == 0) d = nums2[i2 - 1];
else if (i2 == 0) d = nums1[i1 - 1];
else if (nums1[i1 - 1] > nums2[i2 - 1]) d = nums1[i1 - 1];
else d = nums2[i2 - 1];
if (((nums1Size + nums2Size) % 2) == 0) {
if (i2 == nums2Size) {
d = (d + nums1[i1]) / 2;
} else if (i1 == nums1Size) {
d = (d + nums2[i2]) / 2;
} else if (nums1[i1] < nums2[i2]) {
d = (d + nums1[i1]) / 2;
} else {
d = (d + nums2[i2]) / 2;
}
}
break;
}
}
#else // binary search 40+ms
int p1, p2;
p1 = (nums1Size + nums2Size - 1) / 2;
p2 = ((nums1Size + nums2Size) % 2) ? 0 : 1;
if (nums2Size == 0) {
d = nums1[p1];
if (p2) {
d += nums1[p1 + 1];
d /= 2;
}
return d;
}
if (nums1Size == 0) {
d = nums2[p1];
if (p2) {
d += nums2[p1 + 1];
d /= 2;
}
return d;
}
if (nums2[0] < nums1[0]) return bs2(nums2, nums2Size, nums1, nums1Size, p1, p2);
return bs2(nums1, nums1Size, nums2, nums2Size, p1, p2);
#endif
return d;
}
/*
Difficulty:Hard
Total Accepted:182.7K
Total Submissions:839.6K
Companies Google Zenefits Microsoft Apple Yahoo Dropbox Adobe
Related Topics Binary Search Array Divide and Conquer
*/
|
aarch64
|
-O3
|
ARM64 gcc 15.2.0
|
bs:
subs w1, w1, #1
bmi .L1
mov w4, 0
b .L5
.L8:
sub w1, w3, #1
cmp w4, w1
bgt .L1
.L5:
sub w3, w1, w4
add w3, w4, w3, asr 1
ldr w5, [x0, w3, sxtw 2]
cmp w2, w5
blt .L8
add w4, w3, 1
cmp w4, w1
ble .L5
.L1:
mov w0, w1
ret
bs2:
ldr w10, [x2]
subs w11, w1, #1
bmi .L10
.L32:
mov w8, w11
mov w7, 0
b .L13
.L30:
sub w8, w6, #1
cmp w8, w7
blt .L29
.L13:
sub w6, w8, w7
add w6, w7, w6, asr 1
ldr w9, [x0, w6, sxtw 2]
cmp w10, w9
blt .L30
add w7, w6, 1
cmp w8, w7
bge .L13
.L29:
cmp w4, w8
ble .L31
sub w7, w4, w8
cmp w11, w8
beq .L21
add w6, w8, 1
sub w8, w1, w8
mov w1, w3
sub w4, w7, #1
add x6, x0, w6, sxtw 2
mov x0, x2
mov x2, x6
sub w3, w8, #1
subs w11, w1, #1
ldr w10, [x2]
bpl .L32
.L10:
cmp w11, w4
blt .L21
.L20:
ldr w1, [x0, w4, sxtw 2]
scvtf d0, w1
cbz w5, .L9
cmp w4, w11
bge .L16
add x4, x0, w4, sxtw 2
ldr w0, [x4, 4]
scvtf d31, w0
fadd d31, d31, d0
.L17:
fmov d30, 5.0e-1
fmul d0, d31, d30
.L9:
ret
.L21:
sub w4, w4, w11
sub w0, w4, #1
ldr w0, [x2, x0, lsl 2]
scvtf d0, w0
cbz w5, .L9
ldr w0, [x2, w4, uxtw 2]
fmov d2, 5.0e-1
scvtf d3, w0
fadd d0, d3, d0
fmul d0, d0, d2
ret
.L16:
scvtf d1, w10
fadd d31, d1, d0
b .L17
.L31:
mov w11, w8
b .L20
min:
cmp w1, w0
csel w0, w1, w0, le
ret
max:
cmp w1, w0
csel w0, w1, w0, ge
ret
findMedianSortedArrays:
add w5, w1, w3
mov w6, w3
sub w4, w5, #1
mov x8, x0
and w5, w5, 1
mov x9, x2
add w4, w4, w4, lsr 31
asr w4, w4, 1
cbz w3, .L40
cbz w1, .L41
ldr w10, [x0]
eor w5, w5, 1
ldr w11, [x2]
cmp w11, w10
blt .L42
b bs2
.L41:
ldr w0, [x2, w4, sxtw 2]
scvtf d0, w0
cbnz w5, .L35
add x4, x2, w4, sxtw 2
fmov d2, 5.0e-1
ldr w0, [x4, 4]
scvtf d3, w0
fadd d0, d3, d0
fmul d0, d0, d2
.L35:
ret
.L40:
ldr w0, [x0, w4, sxtw 2]
scvtf d0, w0
cbnz w5, .L35
add x4, x8, w4, sxtw 2
fmov d31, 5.0e-1
ldr w0, [x4, 4]
scvtf d1, w0
fadd d0, d1, d0
fmul d0, d0, d31
ret
.L42:
mov w3, w1
mov x2, x0
mov w1, w6
mov x0, x9
b bs2
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.