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
|
|---|---|---|---|---|---|---|---|---|
100
| 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
|
armv8-a clang 21.1.0
|
bs:
sub sp, sp, #32
str x0, [sp, #24]
str w1, [sp, #20]
str w2, [sp, #16]
str wzr, [sp, #12]
ldr w8, [sp, #20]
subs w8, w8, #1
str w8, [sp, #8]
b .LBB0_1
.LBB0_1:
ldr w8, [sp, #12]
ldr w9, [sp, #8]
subs w8, w8, w9
b.gt .LBB0_6
b .LBB0_2
.LBB0_2:
ldr w8, [sp, #12]
ldr w9, [sp, #8]
ldr w10, [sp, #12]
subs w9, w9, w10
mov w10, #2
sdiv w9, w9, w10
add w8, w8, w9
str w8, [sp, #4]
ldr x8, [sp, #24]
ldrsw x9, [sp, #4]
ldr w8, [x8, x9, lsl #2]
ldr w9, [sp, #16]
subs w8, w8, w9
b.le .LBB0_4
b .LBB0_3
.LBB0_3:
ldr w8, [sp, #4]
subs w8, w8, #1
str w8, [sp, #8]
b .LBB0_5
.LBB0_4:
ldr w8, [sp, #4]
add w8, w8, #1
str w8, [sp, #12]
b .LBB0_5
.LBB0_5:
b .LBB0_1
.LBB0_6:
ldr w0, [sp, #8]
add sp, sp, #32
ret
bs2:
sub sp, sp, #80
stp x29, x30, [sp, #64]
add x29, sp, #64
stur x0, [x29, #-8]
stur w1, [x29, #-12]
stur x2, [x29, #-24]
stur w3, [x29, #-28]
str w4, [sp, #32]
str w5, [sp, #28]
ldur x0, [x29, #-8]
ldur w1, [x29, #-12]
ldur x8, [x29, #-24]
ldr w2, [x8]
bl bs
str w0, [sp, #12]
ldr w8, [sp, #12]
ldr w9, [sp, #32]
subs w8, w8, w9
b.lt .LBB1_7
b .LBB1_1
.LBB1_1:
ldur x8, [x29, #-8]
ldrsw x9, [sp, #32]
ldr s0, [x8, x9, lsl #2]
fmov w8, s0
scvtf d0, w8
str d0, [sp, #16]
ldr w8, [sp, #28]
cbz w8, .LBB1_6
b .LBB1_2
.LBB1_2:
ldr w8, [sp, #12]
ldr w9, [sp, #32]
subs w8, w8, w9
b.le .LBB1_4
b .LBB1_3
.LBB1_3:
ldur x8, [x29, #-8]
ldr w9, [sp, #32]
add w9, w9, #1
ldr s0, [x8, w9, sxtw #2]
fmov w8, s0
scvtf d1, w8
ldr d0, [sp, #16]
fadd d0, d0, d1
str d0, [sp, #16]
b .LBB1_5
.LBB1_4:
ldur x8, [x29, #-24]
ldr s0, [x8]
fmov w8, s0
scvtf d1, w8
ldr d0, [sp, #16]
fadd d0, d0, d1
str d0, [sp, #16]
b .LBB1_5
.LBB1_5:
ldr d0, [sp, #16]
fmov d1, #2.00000000
fdiv d0, d0, d1
str d0, [sp, #16]
b .LBB1_6
.LBB1_6:
b .LBB1_13
.LBB1_7:
ldr w8, [sp, #12]
ldur w9, [x29, #-12]
subs w9, w9, #1
subs w8, w8, w9
b.ne .LBB1_11
b .LBB1_8
.LBB1_8:
ldur x8, [x29, #-24]
ldr w9, [sp, #32]
ldr w10, [sp, #12]
subs w9, w9, w10
subs w9, w9, #1
ldr s0, [x8, w9, sxtw #2]
fmov w8, s0
scvtf d0, w8
str d0, [sp, #16]
ldr w8, [sp, #28]
cbz w8, .LBB1_10
b .LBB1_9
.LBB1_9:
ldur x8, [x29, #-24]
ldr w9, [sp, #32]
ldr w10, [sp, #12]
subs w9, w9, w10
ldr s0, [x8, w9, sxtw #2]
fmov w8, s0
scvtf d1, w8
ldr d0, [sp, #16]
fadd d0, d0, d1
str d0, [sp, #16]
ldr d0, [sp, #16]
fmov d1, #2.00000000
fdiv d0, d0, d1
str d0, [sp, #16]
b .LBB1_10
.LBB1_10:
b .LBB1_12
.LBB1_11:
ldur x0, [x29, #-24]
ldur w1, [x29, #-28]
ldur x8, [x29, #-8]
ldr w9, [sp, #12]
add w9, w9, #1
add x2, x8, w9, sxtw #2
ldur w8, [x29, #-12]
ldr w9, [sp, #12]
subs w8, w8, w9
subs w3, w8, #1
ldr w8, [sp, #32]
ldr w9, [sp, #12]
subs w8, w8, w9
subs w4, w8, #1
ldr w5, [sp, #28]
bl bs2
str d0, [sp, #16]
b .LBB1_12
.LBB1_12:
b .LBB1_13
.LBB1_13:
ldr d0, [sp, #16]
ldp x29, x30, [sp, #64]
add sp, sp, #80
ret
min:
sub sp, sp, #16
str w0, [sp, #12]
str w1, [sp, #8]
ldr w8, [sp, #12]
ldr w9, [sp, #8]
subs w8, w8, w9
b.ge .LBB2_2
b .LBB2_1
.LBB2_1:
ldr w8, [sp, #12]
str w8, [sp, #4]
b .LBB2_3
.LBB2_2:
ldr w8, [sp, #8]
str w8, [sp, #4]
b .LBB2_3
.LBB2_3:
ldr w0, [sp, #4]
add sp, sp, #16
ret
max:
sub sp, sp, #16
str w0, [sp, #12]
str w1, [sp, #8]
ldr w8, [sp, #12]
ldr w9, [sp, #8]
subs w8, w8, w9
b.le .LBB3_2
b .LBB3_1
.LBB3_1:
ldr w8, [sp, #12]
str w8, [sp, #4]
b .LBB3_3
.LBB3_2:
ldr w8, [sp, #8]
str w8, [sp, #4]
b .LBB3_3
.LBB3_3:
ldr w0, [sp, #4]
add sp, sp, #16
ret
findMedianSortedArrays:
sub sp, sp, #80
stp x29, x30, [sp, #64]
add x29, sp, #64
stur x0, [x29, #-16]
stur w1, [x29, #-20]
str x2, [sp, #32]
str w3, [sp, #28]
ldur w8, [x29, #-20]
ldr w9, [sp, #28]
add w8, w8, w9
subs w8, w8, #1
mov w10, #2
sdiv w8, w8, w10
str w8, [sp, #12]
ldur w8, [x29, #-20]
ldr w9, [sp, #28]
add w8, w8, w9
sdiv w9, w8, w10
mul w9, w9, w10
subs w8, w8, w9
subs w8, w8, #0
cset w8, eq
str w8, [sp, #8]
ldr w8, [sp, #28]
cbnz w8, .LBB4_4
b .LBB4_1
.LBB4_1:
ldur x8, [x29, #-16]
ldrsw x9, [sp, #12]
ldr s0, [x8, x9, lsl #2]
fmov w8, s0
scvtf d0, w8
str d0, [sp, #16]
ldr w8, [sp, #8]
cbz w8, .LBB4_3
b .LBB4_2
.LBB4_2:
ldur x8, [x29, #-16]
ldr w9, [sp, #12]
add w9, w9, #1
ldr s0, [x8, w9, sxtw #2]
fmov w8, s0
scvtf d1, w8
ldr d0, [sp, #16]
fadd d0, d0, d1
str d0, [sp, #16]
ldr d0, [sp, #16]
fmov d1, #2.00000000
fdiv d0, d0, d1
str d0, [sp, #16]
b .LBB4_3
.LBB4_3:
ldr d0, [sp, #16]
stur d0, [x29, #-8]
b .LBB4_11
.LBB4_4:
ldur w8, [x29, #-20]
cbnz w8, .LBB4_8
b .LBB4_5
.LBB4_5:
ldr x8, [sp, #32]
ldrsw x9, [sp, #12]
ldr s0, [x8, x9, lsl #2]
fmov w8, s0
scvtf d0, w8
str d0, [sp, #16]
ldr w8, [sp, #8]
cbz w8, .LBB4_7
b .LBB4_6
.LBB4_6:
ldr x8, [sp, #32]
ldr w9, [sp, #12]
add w9, w9, #1
ldr s0, [x8, w9, sxtw #2]
fmov w8, s0
scvtf d1, w8
ldr d0, [sp, #16]
fadd d0, d0, d1
str d0, [sp, #16]
ldr d0, [sp, #16]
fmov d1, #2.00000000
fdiv d0, d0, d1
str d0, [sp, #16]
b .LBB4_7
.LBB4_7:
ldr d0, [sp, #16]
stur d0, [x29, #-8]
b .LBB4_11
.LBB4_8:
ldr x8, [sp, #32]
ldr w8, [x8]
ldur x9, [x29, #-16]
ldr w9, [x9]
subs w8, w8, w9
b.ge .LBB4_10
b .LBB4_9
.LBB4_9:
ldr x0, [sp, #32]
ldr w1, [sp, #28]
ldur x2, [x29, #-16]
ldur w3, [x29, #-20]
ldr w4, [sp, #12]
ldr w5, [sp, #8]
bl bs2
stur d0, [x29, #-8]
b .LBB4_11
.LBB4_10:
ldur x0, [x29, #-16]
ldur w1, [x29, #-20]
ldr x2, [sp, #32]
ldr w3, [sp, #28]
ldr w4, [sp, #12]
ldr w5, [sp, #8]
bl bs2
stur d0, [x29, #-8]
b .LBB4_11
.LBB4_11:
ldur d0, [x29, #-8]
ldp x29, x30, [sp, #64]
add sp, sp, #80
ret
|
101
| 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
|
armv8-a clang 21.1.0
|
bs:
subs w8, w1, #1
b.lt .LBB0_3
mov w9, wzr
.LBB0_2:
sub w10, w8, w9
add w10, w9, w10, lsr #1
ldr w11, [x0, w10, uxtw #2]
sub w12, w10, #1
cmp w11, w2
csel w8, w12, w8, gt
csinc w9, w9, w10, gt
cmp w9, w8
b.le .LBB0_2
.LBB0_3:
mov w0, w8
ret
bs2:
subs w9, w1, #1
b.lt .LBB1_4
.LBB1_1:
ldr w11, [x2]
mov w10, wzr
mov w8, w9
.LBB1_2:
sub w12, w8, w10
add w12, w10, w12, lsr #1
ldr w13, [x0, w12, uxtw #2]
sub w14, w12, #1
cmp w13, w11
csel w8, w14, w8, gt
csinc w10, w10, w12, gt
cmp w10, w8
b.le .LBB1_2
subs w10, w4, w8
b.gt .LBB1_5
b .LBB1_7
.LBB1_4:
mov w8, w9
subs w10, w4, w8
b.le .LBB1_7
.LBB1_5:
cmp w8, w9
b.eq .LBB1_9
add x9, x0, w8, sxtw #2
mvn w8, w8
mov x0, x2
add w4, w4, w8
add x2, x9, #4
add w9, w1, w8
mov w1, w3
mov w3, w9
subs w9, w1, #1
b.ge .LBB1_1
b .LBB1_4
.LBB1_7:
add x9, x0, w4, sxtw #2
ldr w10, [x9], #4
scvtf d0, w10
cbz w5, .LBB1_11
cmp w4, w8
csel x8, x9, x2, lt
b .LBB1_10
.LBB1_9:
add x8, x2, w10, sxtw #2
ldur w9, [x8, #-4]
scvtf d0, w9
cbz w5, .LBB1_11
.LBB1_10:
ldr w8, [x8]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
.LBB1_11:
ret
min:
cmp w0, w1
csel w0, w0, w1, lt
ret
max:
cmp w0, w1
csel w0, w0, w1, gt
ret
findMedianSortedArrays:
add w8, w3, w1
sub w9, w8, #1
and w8, w8, #0x1
add w9, w9, w9, lsr #31
asr w4, w9, #1
cbz w3, .LBB4_4
cbz w1, .LBB4_5
ldr w9, [x2]
ldr w10, [x0]
eor w5, w8, #0x1
cmp w9, w10
b.ge .LBB4_9
mov x8, x0
mov w9, w1
mov x0, x2
mov w1, w3
mov x2, x8
mov w3, w9
b bs2
.LBB4_4:
add x9, x0, w4, sxtw #2
b .LBB4_6
.LBB4_5:
add x9, x2, w4, sxtw #2
.LBB4_6:
ldr w10, [x9]
scvtf d0, w10
cbnz w8, .LBB4_8
ldr w8, [x9, #4]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
.LBB4_8:
ret
.LBB4_9:
b bs2
|
102
| 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
|
armv8-a clang 21.1.0
|
bs:
subs w8, w1, #1
b.lt .LBB0_3
mov w9, wzr
.LBB0_2:
sub w10, w8, w9
add w10, w9, w10, lsr #1
ldr w11, [x0, w10, uxtw #2]
sub w12, w10, #1
cmp w11, w2
csel w8, w12, w8, gt
csinc w9, w9, w10, gt
cmp w9, w8
b.le .LBB0_2
.LBB0_3:
mov w0, w8
ret
bs2:
subs w8, w1, #1
b.lt .LBB1_4
.LBB1_1:
ldr w11, [x2]
mov w10, wzr
mov w9, w8
.LBB1_2:
sub w12, w9, w10
add w12, w10, w12, lsr #1
ldr w13, [x0, w12, uxtw #2]
sub w14, w12, #1
cmp w13, w11
csel w9, w14, w9, gt
csinc w10, w10, w12, gt
cmp w10, w9
b.le .LBB1_2
cmp w9, w4
b.lt .LBB1_5
b .LBB1_7
.LBB1_4:
mov w9, w8
cmp w9, w4
b.ge .LBB1_7
.LBB1_5:
cmp w9, w8
b.eq .LBB1_9
add x8, x0, w9, sxtw #2
mvn w9, w9
mov x0, x2
add w4, w4, w9
add x2, x8, #4
add w8, w1, w9
mov w1, w3
mov w3, w8
subs w8, w1, #1
b.ge .LBB1_1
b .LBB1_4
.LBB1_7:
add x8, x0, w4, sxtw #2
ldr w10, [x8], #4
scvtf d0, w10
cbz w5, .LBB1_11
cmp w9, w4
csel x8, x8, x2, gt
b .LBB1_10
.LBB1_9:
sub w8, w4, w8
add x8, x2, w8, sxtw #2
ldur w9, [x8, #-4]
scvtf d0, w9
cbz w5, .LBB1_11
.LBB1_10:
ldr w8, [x8]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
.LBB1_11:
ret
min:
cmp w0, w1
csel w0, w0, w1, lt
ret
max:
cmp w0, w1
csel w0, w0, w1, gt
ret
findMedianSortedArrays:
add w8, w3, w1
sub w9, w8, #1
add w9, w9, w9, lsr #31
asr w9, w9, #1
cbz w3, .LBB4_17
cbz w1, .LBB4_18
ldr w10, [x2]
ldr w11, [x0]
cmp w10, w11
b.ge .LBB4_10
subs w12, w3, #1
b.lt .LBB4_7
.LBB4_4:
mov w13, wzr
mov w10, w12
.LBB4_5:
sub w14, w10, w13
add w14, w13, w14, lsr #1
ldr w15, [x2, w14, uxtw #2]
sub w16, w14, #1
cmp w15, w11
csel w10, w16, w10, gt
csinc w13, w13, w14, gt
cmp w13, w10
b.le .LBB4_5
cmp w10, w9
b.lt .LBB4_8
b .LBB4_21
.LBB4_7:
mov w10, w12
cmp w10, w9
b.ge .LBB4_21
.LBB4_8:
cmp w10, w12
b.eq .LBB4_25
add x12, x2, w10, sxtw #2
mvn w10, w10
mov x2, x0
add w13, w3, w10
add w9, w9, w10
mov w3, w1
ldr w11, [x12, #4]!
mov x0, x12
mov w1, w13
subs w12, w3, #1
b.ge .LBB4_4
b .LBB4_7
.LBB4_10:
subs w12, w1, #1
b.lt .LBB4_14
mov w13, wzr
mov w11, w12
.LBB4_12:
sub w14, w11, w13
add w14, w13, w14, lsr #1
ldr w15, [x0, w14, uxtw #2]
sub w16, w14, #1
cmp w15, w10
csel w11, w16, w11, gt
csinc w13, w13, w14, gt
cmp w13, w11
b.le .LBB4_12
cmp w11, w9
b.lt .LBB4_15
b .LBB4_23
.LBB4_14:
mov w11, w12
cmp w11, w9
b.ge .LBB4_23
.LBB4_15:
cmp w11, w12
b.eq .LBB4_26
add x12, x0, w11, sxtw #2
mvn w10, w11
mov x0, x2
add w11, w1, w10
add w9, w9, w10
mov w1, w3
ldr w10, [x12, #4]!
mov x2, x12
mov w3, w11
b .LBB4_10
.LBB4_17:
add x9, x0, w9, sxtw #2
b .LBB4_19
.LBB4_18:
add x9, x2, w9, sxtw #2
.LBB4_19:
ldr w10, [x9]
scvtf d0, w10
tbnz w8, #0, .LBB4_30
ldr w8, [x9, #4]
b .LBB4_29
.LBB4_21:
add x11, x2, w9, sxtw #2
ldr w12, [x11]
scvtf d0, w12
tbnz w8, #0, .LBB4_30
add x8, x11, #4
cmp w10, w9
csel x8, x8, x0, gt
ldr w8, [x8]
b .LBB4_29
.LBB4_23:
add x10, x0, w9, sxtw #2
ldr w12, [x10]
scvtf d0, w12
tbnz w8, #0, .LBB4_30
add x8, x10, #4
cmp w11, w9
csel x8, x8, x2, gt
ldr w8, [x8]
b .LBB4_29
.LBB4_25:
sub w9, w9, w12
add x9, x0, w9, sxtw #2
b .LBB4_27
.LBB4_26:
sub w9, w9, w12
add x9, x2, w9, sxtw #2
.LBB4_27:
ldur w10, [x9, #-4]
scvtf d0, w10
tbnz w8, #0, .LBB4_30
ldr w8, [x9]
.LBB4_29:
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
.LBB4_30:
ret
|
103
| 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
|
armv8-a clang 21.1.0
|
bs:
subs w8, w1, #1
b.lt .LBB0_3
mov w9, wzr
.LBB0_2:
sub w10, w8, w9
add w10, w9, w10, lsr #1
ldr w11, [x0, w10, uxtw #2]
sub w12, w10, #1
cmp w11, w2
csel w8, w8, w12, le
csinc w9, w9, w10, gt
cmp w9, w8
b.le .LBB0_2
.LBB0_3:
mov w0, w8
ret
bs2:
subs w8, w1, #1
b.lt .LBB1_4
.LBB1_1:
ldr w11, [x2]
mov w10, wzr
mov w9, w8
.LBB1_2:
sub w12, w9, w10
add w12, w10, w12, lsr #1
ldr w13, [x0, w12, uxtw #2]
sub w14, w12, #1
cmp w13, w11
csel w9, w9, w14, le
csinc w10, w10, w12, gt
cmp w10, w9
b.le .LBB1_2
cmp w9, w4
b.lt .LBB1_5
b .LBB1_7
.LBB1_4:
mov w9, w8
cmp w8, w4
b.ge .LBB1_7
.LBB1_5:
cmp w9, w8
b.eq .LBB1_9
add x8, x0, w9, sxtw #2
mvn w9, w9
mov x0, x2
add w4, w4, w9
add x2, x8, #4
add w8, w1, w9
mov w1, w3
mov w3, w8
subs w8, w1, #1
b.ge .LBB1_1
b .LBB1_4
.LBB1_7:
add x8, x0, w4, sxtw #2
ldr w10, [x8], #4
scvtf d0, w10
cbz w5, .LBB1_11
cmp w9, w4
csel x8, x8, x2, gt
b .LBB1_10
.LBB1_9:
sub w8, w4, w8
add x8, x2, w8, sxtw #2
ldur w9, [x8, #-4]
scvtf d0, w9
cbz w5, .LBB1_11
.LBB1_10:
ldr w8, [x8]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
.LBB1_11:
ret
min:
cmp w0, w1
csel w0, w0, w1, lt
ret
max:
cmp w0, w1
csel w0, w0, w1, gt
ret
findMedianSortedArrays:
add w8, w3, w1
sub w9, w8, #1
add w9, w9, w9, lsr #31
asr w9, w9, #1
cbz w3, .LBB4_17
cbz w1, .LBB4_19
ldr w10, [x2]
ldr w11, [x0]
cmp w10, w11
b.ge .LBB4_10
subs w12, w3, #1
b.lt .LBB4_7
.LBB4_4:
mov w13, wzr
mov w10, w12
.LBB4_5:
sub w14, w10, w13
add w14, w13, w14, lsr #1
ldr w15, [x2, w14, uxtw #2]
sub w16, w14, #1
cmp w15, w11
csel w10, w10, w16, le
csinc w13, w13, w14, gt
cmp w13, w10
b.le .LBB4_5
cmp w10, w9
b.lt .LBB4_8
b .LBB4_21
.LBB4_7:
mov w10, w12
cmp w12, w9
b.ge .LBB4_21
.LBB4_8:
cmp w10, w12
b.eq .LBB4_25
add x12, x2, w10, sxtw #2
mvn w10, w10
mov x2, x0
add w13, w3, w10
add w9, w9, w10
mov w3, w1
ldr w11, [x12, #4]!
mov x0, x12
mov w1, w13
subs w12, w3, #1
b.ge .LBB4_4
b .LBB4_7
.LBB4_10:
subs w12, w1, #1
b.lt .LBB4_14
mov w13, wzr
mov w11, w12
.LBB4_12:
sub w14, w11, w13
add w14, w13, w14, lsr #1
ldr w15, [x0, w14, uxtw #2]
sub w16, w14, #1
cmp w15, w10
csel w11, w11, w16, le
csinc w13, w13, w14, gt
cmp w13, w11
b.le .LBB4_12
cmp w11, w9
b.lt .LBB4_15
b .LBB4_23
.LBB4_14:
mov w11, w12
cmp w12, w9
b.ge .LBB4_23
.LBB4_15:
cmp w11, w12
b.eq .LBB4_26
add x12, x0, w11, sxtw #2
mvn w10, w11
mov x0, x2
add w11, w1, w10
add w9, w9, w10
mov w1, w3
ldr w10, [x12, #4]!
mov x2, x12
mov w3, w11
b .LBB4_10
.LBB4_17:
add x9, x0, w9, sxtw #2
ldr w10, [x9]
scvtf d0, w10
tbz w8, #0, .LBB4_20
.LBB4_18:
ret
.LBB4_19:
add x9, x2, w9, sxtw #2
ldr w10, [x9]
scvtf d0, w10
tbnz w8, #0, .LBB4_18
.LBB4_20:
ldr w8, [x9, #4]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
ret
.LBB4_21:
add x11, x2, w9, sxtw #2
ldr w12, [x11]
scvtf d0, w12
tbnz w8, #0, .LBB4_18
add x8, x11, #4
cmp w10, w9
csel x8, x8, x0, gt
ldr w8, [x8]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
ret
.LBB4_23:
add x10, x0, w9, sxtw #2
ldr w12, [x10]
scvtf d0, w12
tbnz w8, #0, .LBB4_18
add x8, x10, #4
cmp w11, w9
csel x8, x8, x2, gt
ldr w8, [x8]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
ret
.LBB4_25:
sub w9, w9, w12
add x9, x0, w9, sxtw #2
ldur w10, [x9, #-4]
scvtf d0, w10
tbnz w8, #0, .LBB4_18
b .LBB4_27
.LBB4_26:
sub w9, w9, w12
add x9, x2, w9, sxtw #2
ldur w10, [x9, #-4]
scvtf d0, w10
tbnz w8, #0, .LBB4_18
.LBB4_27:
ldr w8, [x9]
scvtf d1, w8
fadd d0, d0, d1
fmov d1, #0.50000000
fmul d0, d0, d1
ret
|
104
| 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
*/
|
mips64
|
-O0
|
mips64 clang 21.1.0
|
bs:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -48
sd $ra, 40($sp)
sd $fp, 32($sp)
move $fp, $sp
move $1, $6
move $2, $5
sd $4, 24($fp)
sw $2, 20($fp)
sw $1, 16($fp)
sw $zero, 12($fp)
lw $1, 20($fp)
addiu $1, $1, -1
sw $1, 8($fp)
b .LBB0_1
nop
.LBB0_1:
lw $2, 12($fp)
lw $1, 8($fp)
slt $1, $1, $2
bnez $1, .LBB0_8
nop
b .LBB0_3
nop
.LBB0_3:
lw $1, 12($fp)
lw $2, 8($fp)
subu $2, $2, $1
srl $3, $2, 31
addu $2, $2, $3
sra $2, $2, 1
addu $1, $1, $2
sw $1, 4($fp)
ld $1, 24($fp)
lw $2, 4($fp)
dsll $2, $2, 2
daddu $1, $1, $2
lw $2, 0($1)
lw $1, 16($fp)
slt $1, $1, $2
beqz $1, .LBB0_6
nop
b .LBB0_5
nop
.LBB0_5:
lw $1, 4($fp)
addiu $1, $1, -1
sw $1, 8($fp)
b .LBB0_7
nop
.LBB0_6:
lw $1, 4($fp)
addiu $1, $1, 1
sw $1, 12($fp)
b .LBB0_7
nop
.LBB0_7:
b .LBB0_1
nop
.LBB0_8:
lw $2, 8($fp)
move $sp, $fp
ld $fp, 32($sp)
ld $ra, 40($sp)
daddiu $sp, $sp, 48
jr $ra
nop
.LCPI1_0:
.8byte 0x3fe0000000000000
bs2:
.Lfunc_begin1 = .Ltmp8
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(bs2)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(bs2)))
sd $gp, 0($fp)
sd $5, 8($fp)
move $5, $4
ld $4, 8($fp)
move $1, $9
move $2, $8
move $3, $7
sd $5, 64($fp)
sw $4, 60($fp)
sd $6, 48($fp)
sw $3, 44($fp)
sw $2, 40($fp)
sw $1, 36($fp)
ld $4, 64($fp)
lw $5, 60($fp)
ld $1, 48($fp)
lw $6, 0($1)
ld $25, %call16(bs)($gp)
jalr $25
nop
sw $2, 20($fp)
lw $1, 20($fp)
lw $2, 40($fp)
slt $1, $1, $2
bnez $1, .LBB1_10
nop
b .LBB1_2
nop
.LBB1_2:
ld $1, 64($fp)
lw $2, 40($fp)
dsll $2, $2, 2
daddu $1, $1, $2
lw $1, 0($1)
mtc1 $1, $f0
cvt.d.w $f0, $f0
sdc1 $f0, 24($fp)
lw $1, 36($fp)
beqz $1, .LBB1_9
nop
b .LBB1_4
nop
.LBB1_4:
lw $2, 20($fp)
lw $1, 40($fp)
slt $1, $1, $2
beqz $1, .LBB1_7
nop
b .LBB1_6
nop
.LBB1_6:
ld $2, 64($fp)
lw $1, 40($fp)
dsll $1, $1, 2
daddu $1, $1, $2
lw $1, 4($1)
mtc1 $1, $f1
cvt.d.w $f1, $f1
ldc1 $f0, 24($fp)
add.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
b .LBB1_8
nop
.LBB1_7:
ld $1, 48($fp)
lw $1, 0($1)
mtc1 $1, $f1
cvt.d.w $f1, $f1
ldc1 $f0, 24($fp)
add.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
b .LBB1_8
nop
.LBB1_8:
ld $1, 0($fp)
ldc1 $f0, 24($fp)
ld $1, %got_page(.LCPI1_0)($1)
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
mul.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
b .LBB1_9
nop
.LBB1_9:
b .LBB1_18
nop
.LBB1_10:
lw $1, 20($fp)
lw $2, 60($fp)
addiu $2, $2, -1
bne $1, $2, .LBB1_16
nop
b .LBB1_12
nop
.LBB1_12:
ld $1, 48($fp)
lw $3, 40($fp)
lw $2, 20($fp)
not $2, $2
addu $3, $2, $3
move $2, $3
dsll $2, $2, 2
daddu $1, $1, $2
lw $1, 0($1)
mtc1 $1, $f0
cvt.d.w $f0, $f0
sdc1 $f0, 24($fp)
lw $1, 36($fp)
beqz $1, .LBB1_15
nop
b .LBB1_14
nop
.LBB1_14:
ld $1, 0($fp)
ld $2, 48($fp)
lw $3, 40($fp)
lw $4, 20($fp)
subu $4, $3, $4
move $3, $4
dsll $3, $3, 2
daddu $2, $2, $3
lw $2, 0($2)
mtc1 $2, $f1
cvt.d.w $f1, $f1
ldc1 $f0, 24($fp)
add.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
ldc1 $f0, 24($fp)
ld $1, %got_page(.LCPI1_0)($1)
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
mul.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
b .LBB1_15
nop
.LBB1_15:
b .LBB1_17
nop
.LBB1_16:
ld $gp, 0($fp)
ld $4, 48($fp)
lw $5, 44($fp)
ld $3, 64($fp)
lw $1, 20($fp)
sll $2, $1, 0
dsll $2, $2, 2
daddu $2, $2, $3
daddiu $6, $2, 4
lw $3, 60($fp)
lw $2, 40($fp)
not $1, $1
lw $9, 36($fp)
addu $3, $1, $3
move $7, $3
addu $1, $1, $2
move $8, $1
ld $25, %call16(bs2)($gp)
jalr $25
nop
sdc1 $f0, 24($fp)
b .LBB1_17
nop
.LBB1_17:
b .LBB1_18
nop
.LBB1_18:
ldc1 $f0, 24($fp)
move $sp, $fp
ld $gp, 72($sp)
ld $fp, 80($sp)
ld $ra, 88($sp)
daddiu $sp, $sp, 96
jr $ra
nop
min:
.Lfunc_begin2 = .Ltmp28
daddiu $sp, $sp, -32
sd $ra, 24($sp)
sd $fp, 16($sp)
move $fp, $sp
move $1, $5
move $2, $4
sw $2, 12($fp)
sw $1, 8($fp)
lw $1, 12($fp)
lw $2, 8($fp)
slt $1, $1, $2
beqz $1, .LBB2_3
nop
b .LBB2_2
nop
.LBB2_2:
lw $1, 12($fp)
sw $1, 4($fp)
b .LBB2_4
nop
.LBB2_3:
lw $1, 8($fp)
sw $1, 4($fp)
b .LBB2_4
nop
.LBB2_4:
lw $1, 4($fp)
sll $2, $1, 0
move $sp, $fp
ld $fp, 16($sp)
ld $ra, 24($sp)
daddiu $sp, $sp, 32
jr $ra
nop
max:
.Lfunc_begin3 = .Ltmp31
daddiu $sp, $sp, -32
sd $ra, 24($sp)
sd $fp, 16($sp)
move $fp, $sp
move $1, $5
move $2, $4
sw $2, 12($fp)
sw $1, 8($fp)
lw $2, 12($fp)
lw $1, 8($fp)
slt $1, $1, $2
beqz $1, .LBB3_3
nop
b .LBB3_2
nop
.LBB3_2:
lw $1, 12($fp)
sw $1, 4($fp)
b .LBB3_4
nop
.LBB3_3:
lw $1, 8($fp)
sw $1, 4($fp)
b .LBB3_4
nop
.LBB3_4:
lw $1, 4($fp)
sll $2, $1, 0
move $sp, $fp
ld $fp, 16($sp)
ld $ra, 24($sp)
daddiu $sp, $sp, 32
jr $ra
nop
.LCPI4_0:
.8byte 0x3fe0000000000000
findMedianSortedArrays:
.Lfunc_begin4 = .Ltmp34
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(findMedianSortedArrays)))
daddu $1, $1, $25
daddiu $1, $1, %lo(%neg(%gp_rel(findMedianSortedArrays)))
sd $1, 8($fp)
move $1, $7
move $2, $5
sd $4, 56($fp)
sw $2, 52($fp)
sd $6, 40($fp)
sw $1, 36($fp)
lw $1, 52($fp)
lw $2, 36($fp)
addu $1, $1, $2
addiu $1, $1, -1
srl $2, $1, 31
addu $1, $1, $2
sra $1, $1, 1
sw $1, 20($fp)
lw $1, 52($fp)
lw $2, 36($fp)
addu $1, $1, $2
srl $2, $1, 31
addu $2, $1, $2
addiu $3, $zero, -2
and $2, $2, $3
subu $1, $1, $2
sltiu $1, $1, 1
sw $1, 16($fp)
lw $1, 36($fp)
bnez $1, .LBB4_6
nop
b .LBB4_2
nop
.LBB4_2:
ld $1, 56($fp)
lw $2, 20($fp)
dsll $2, $2, 2
daddu $1, $1, $2
lw $1, 0($1)
mtc1 $1, $f0
cvt.d.w $f0, $f0
sdc1 $f0, 24($fp)
lw $1, 16($fp)
beqz $1, .LBB4_5
nop
b .LBB4_4
nop
.LBB4_4:
ld $1, 8($fp)
ld $3, 56($fp)
lw $2, 20($fp)
dsll $2, $2, 2
daddu $2, $2, $3
lw $2, 4($2)
mtc1 $2, $f1
cvt.d.w $f1, $f1
ldc1 $f0, 24($fp)
add.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
ldc1 $f0, 24($fp)
ld $1, %got_page(.LCPI4_0)($1)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
mul.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
b .LBB4_5
nop
.LBB4_5:
ldc1 $f0, 24($fp)
sdc1 $f0, 64($fp)
b .LBB4_16
nop
.LBB4_6:
lw $1, 52($fp)
bnez $1, .LBB4_12
nop
b .LBB4_8
nop
.LBB4_8:
ld $1, 40($fp)
lw $2, 20($fp)
dsll $2, $2, 2
daddu $1, $1, $2
lw $1, 0($1)
mtc1 $1, $f0
cvt.d.w $f0, $f0
sdc1 $f0, 24($fp)
lw $1, 16($fp)
beqz $1, .LBB4_11
nop
b .LBB4_10
nop
.LBB4_10:
ld $1, 8($fp)
ld $3, 40($fp)
lw $2, 20($fp)
dsll $2, $2, 2
daddu $2, $2, $3
lw $2, 4($2)
mtc1 $2, $f1
cvt.d.w $f1, $f1
ldc1 $f0, 24($fp)
add.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
ldc1 $f0, 24($fp)
ld $1, %got_page(.LCPI4_0)($1)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
mul.d $f0, $f0, $f1
sdc1 $f0, 24($fp)
b .LBB4_11
nop
.LBB4_11:
ldc1 $f0, 24($fp)
sdc1 $f0, 64($fp)
b .LBB4_16
nop
.LBB4_12:
ld $1, 40($fp)
lw $1, 0($1)
ld $2, 56($fp)
lw $2, 0($2)
slt $1, $1, $2
beqz $1, .LBB4_15
nop
b .LBB4_14
nop
.LBB4_14:
ld $gp, 8($fp)
ld $4, 40($fp)
lw $5, 36($fp)
ld $6, 56($fp)
lw $7, 52($fp)
lw $8, 20($fp)
lw $9, 16($fp)
ld $25, %call16(bs2)($gp)
jalr $25
nop
sdc1 $f0, 64($fp)
b .LBB4_16
nop
.LBB4_15:
ld $gp, 8($fp)
ld $4, 56($fp)
lw $5, 52($fp)
ld $6, 40($fp)
lw $7, 36($fp)
lw $8, 20($fp)
lw $9, 16($fp)
ld $25, %call16(bs2)($gp)
jalr $25
nop
sdc1 $f0, 64($fp)
b .LBB4_16
nop
.LBB4_16:
ldc1 $f0, 64($fp)
move $sp, $fp
ld $gp, 72($sp)
ld $fp, 80($sp)
ld $ra, 88($sp)
daddiu $sp, $sp, 96
jr $ra
nop
|
105
| 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
*/
|
mips64
|
-O1
|
mips64 clang 21.1.0
|
bs:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
blez $5, .LBB0_3
addiu $2, $5, -1
addiu $3, $zero, 0
.LBB0_2:
subu $1, $2, $3
srl $1, $1, 1
addu $1, $1, $3
dsll $5, $1, 2
daddu $5, $4, $5
lw $5, 0($5)
slt $5, $6, $5
addiu $7, $1, 1
movn $7, $3, $5
addiu $1, $1, -1
movn $2, $1, $5
slt $1, $2, $7
beqz $1, .LBB0_2
move $3, $7
.LBB0_3:
sll $2, $2, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
.LCPI1_0:
.8byte 0x3fe0000000000000
bs2:
.Lfunc_begin1 = .Ltmp13
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(bs2)))
daddu $1, $1, $25
daddiu $2, $1, %lo(%neg(%gp_rel(bs2)))
.LBB1_1:
addiu $10, $5, -1
blez $5, .LBB1_4
move $3, $10
lw $11, 0($6)
addiu $12, $zero, 0
move $3, $10
.LBB1_3:
subu $1, $3, $12
srl $1, $1, 1
addu $1, $1, $12
dsll $13, $1, 2
daddu $13, $4, $13
lw $13, 0($13)
slt $13, $11, $13
addiu $14, $1, 1
movn $14, $12, $13
addiu $1, $1, -1
movn $3, $1, $13
slt $1, $3, $14
beqz $1, .LBB1_3
move $12, $14
.LBB1_4:
slt $1, $3, $8
beqz $1, .LBB1_7
nop
beq $3, $10, .LBB1_9
nop
not $1, $3
addu $8, $8, $1
addu $1, $5, $1
sll $3, $3, 0
dsll $3, $3, 2
daddu $3, $4, $3
daddiu $3, $3, 4
move $4, $6
move $5, $7
move $6, $3
b .LBB1_1
move $7, $1
.LBB1_7:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, 0($4)
mtc1 $1, $f0
beqz $9, .LBB1_11
cvt.d.w $f0, $f0
slt $1, $8, $3
daddiu $3, $4, 4
movn $6, $3, $1
lw $1, 0($6)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI1_0)($2)
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
b .LBB1_11
mul.d $f0, $f0, $f1
.LBB1_9:
subu $1, $8, $3
dsll $1, $1, 2
daddu $3, $6, $1
lw $1, -4($3)
mtc1 $1, $f0
beqz $9, .LBB1_11
cvt.d.w $f0, $f0
lw $1, 0($3)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI1_0)($2)
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
mul.d $f0, $f0, $f1
.LBB1_11:
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
min:
.Lfunc_begin2 = .Ltmp48
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
slt $1, $4, $5
movn $5, $4, $1
sll $2, $5, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
max:
.Lfunc_begin3 = .Ltmp52
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
slt $1, $5, $4
movn $5, $4, $1
sll $2, $5, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
.LCPI4_0:
.8byte 0x3fe0000000000000
findMedianSortedArrays:
.Lfunc_begin4 = .Ltmp56
daddiu $sp, $sp, -32
sd $ra, 24($sp)
sd $fp, 16($sp)
sd $gp, 8($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(findMedianSortedArrays)))
daddu $1, $1, $25
daddiu $gp, $1, %lo(%neg(%gp_rel(findMedianSortedArrays)))
addu $1, $7, $5
addiu $2, $1, -1
srl $3, $2, 31
addu $2, $2, $3
sra $2, $2, 1
beqz $7, .LBB4_4
andi $3, $1, 1
beqz $5, .LBB4_6
nop
lw $1, 0($4)
lw $8, 0($6)
slt $1, $8, $1
beqz $1, .LBB4_8
xori $3, $3, 1
sll $1, $7, 0
sll $7, $5, 0
sll $8, $2, 0
sll $9, $3, 0
ld $25, %call16(bs2)($gp)
move $2, $4
move $4, $6
move $5, $1
jalr $25
move $6, $2
b .LBB4_9
nop
.LBB4_4:
sll $1, $2, 0
dsll $1, $1, 2
daddu $2, $4, $1
lw $1, 0($2)
mtc1 $1, $f0
bnez $3, .LBB4_9
cvt.d.w $f0, $f0
lw $1, 4($2)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($gp)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_9
mul.d $f0, $f0, $f1
.LBB4_6:
sll $1, $2, 0
dsll $1, $1, 2
daddu $2, $6, $1
lw $1, 0($2)
mtc1 $1, $f0
bnez $3, .LBB4_9
cvt.d.w $f0, $f0
lw $1, 4($2)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($gp)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_9
mul.d $f0, $f0, $f1
.LBB4_8:
sll $5, $5, 0
sll $7, $7, 0
sll $8, $2, 0
ld $25, %call16(bs2)($gp)
jalr $25
sll $9, $3, 0
.LBB4_9:
move $sp, $fp
ld $gp, 8($sp)
ld $fp, 16($sp)
ld $ra, 24($sp)
jr $ra
daddiu $sp, $sp, 32
|
106
| 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
*/
|
mips64
|
-O2
|
mips64 clang 21.1.0
|
bs:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
blez $5, .LBB0_3
addiu $2, $5, -1
addiu $3, $zero, 0
.LBB0_2:
subu $1, $2, $3
srl $1, $1, 1
addu $1, $1, $3
dsll $5, $1, 2
daddu $5, $4, $5
lw $5, 0($5)
slt $5, $6, $5
addiu $7, $1, 1
movn $7, $3, $5
addiu $1, $1, -1
movn $2, $1, $5
slt $1, $2, $7
beqz $1, .LBB0_2
move $3, $7
.LBB0_3:
sll $2, $2, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
.LCPI1_0:
.8byte 0x3fe0000000000000
bs2:
.Lfunc_begin1 = .Ltmp13
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(bs2)))
daddu $1, $1, $25
daddiu $2, $1, %lo(%neg(%gp_rel(bs2)))
.LBB1_1:
addiu $10, $5, -1
blez $5, .LBB1_4
move $3, $10
lw $11, 0($6)
addiu $12, $zero, 0
move $3, $10
.LBB1_3:
subu $1, $3, $12
srl $1, $1, 1
addu $1, $1, $12
dsll $13, $1, 2
daddu $13, $4, $13
lw $13, 0($13)
slt $13, $11, $13
addiu $14, $1, 1
movn $14, $12, $13
addiu $1, $1, -1
movn $3, $1, $13
slt $1, $3, $14
beqz $1, .LBB1_3
move $12, $14
.LBB1_4:
slt $1, $3, $8
beqz $1, .LBB1_7
nop
beq $3, $10, .LBB1_9
nop
not $1, $3
addu $8, $8, $1
addu $1, $5, $1
sll $3, $3, 0
dsll $3, $3, 2
daddu $3, $4, $3
daddiu $3, $3, 4
move $4, $6
move $5, $7
move $6, $3
b .LBB1_1
move $7, $1
.LBB1_7:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, 0($4)
mtc1 $1, $f0
beqz $9, .LBB1_11
cvt.d.w $f0, $f0
slt $1, $8, $3
daddiu $3, $4, 4
movn $6, $3, $1
lw $1, 0($6)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI1_0)($2)
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
b .LBB1_11
mul.d $f0, $f0, $f1
.LBB1_9:
subu $1, $8, $10
dsll $1, $1, 2
daddu $3, $6, $1
lw $1, -4($3)
mtc1 $1, $f0
beqz $9, .LBB1_11
cvt.d.w $f0, $f0
lw $1, 0($3)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI1_0)($2)
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
mul.d $f0, $f0, $f1
.LBB1_11:
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
min:
.Lfunc_begin2 = .Ltmp48
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
slt $1, $4, $5
movn $5, $4, $1
sll $2, $5, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
max:
.Lfunc_begin3 = .Ltmp52
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
slt $1, $5, $4
movn $5, $4, $1
sll $2, $5, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
.LCPI4_0:
.8byte 0x3fe0000000000000
findMedianSortedArrays:
.Lfunc_begin4 = .Ltmp56
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(findMedianSortedArrays)))
daddu $1, $1, $25
daddiu $2, $1, %lo(%neg(%gp_rel(findMedianSortedArrays)))
addu $1, $7, $5
addiu $3, $1, -1
srl $8, $3, 31
addu $3, $3, $8
sra $8, $3, 1
beqz $7, .LBB4_15
andi $3, $1, 1
beqz $5, .LBB4_17
nop
lw $9, 0($4)
lw $10, 0($6)
slt $1, $10, $9
beqz $1, .LBB4_9
nop
.LBB4_3:
addiu $11, $7, -1
blez $7, .LBB4_6
move $10, $11
addiu $12, $zero, 0
move $10, $11
.LBB4_5:
subu $1, $10, $12
srl $1, $1, 1
addu $1, $1, $12
dsll $13, $1, 2
daddu $13, $6, $13
lw $13, 0($13)
slt $13, $9, $13
addiu $14, $1, 1
movn $14, $12, $13
addiu $1, $1, -1
movn $10, $1, $13
slt $1, $10, $14
beqz $1, .LBB4_5
move $12, $14
.LBB4_6:
slt $1, $10, $8
beqz $1, .LBB4_19
nop
beq $10, $11, .LBB4_23
nop
not $1, $10
addu $8, $8, $1
addu $1, $7, $1
sll $7, $10, 0
dsll $7, $7, 2
daddu $6, $6, $7
lw $9, 4($6)
daddiu $10, $6, 4
move $6, $4
move $7, $5
move $4, $10
b .LBB4_3
move $5, $1
.LBB4_9:
addiu $11, $5, -1
blez $5, .LBB4_12
move $9, $11
addiu $12, $zero, 0
move $9, $11
.LBB4_11:
subu $1, $9, $12
srl $1, $1, 1
addu $1, $1, $12
dsll $13, $1, 2
daddu $13, $4, $13
lw $13, 0($13)
slt $13, $10, $13
addiu $14, $1, 1
movn $14, $12, $13
addiu $1, $1, -1
movn $9, $1, $13
slt $1, $9, $14
beqz $1, .LBB4_11
move $12, $14
.LBB4_12:
slt $1, $9, $8
beqz $1, .LBB4_21
nop
beq $9, $11, .LBB4_25
nop
not $1, $9
addu $8, $8, $1
addu $1, $5, $1
sll $5, $9, 0
dsll $5, $5, 2
daddu $4, $4, $5
lw $10, 4($4)
daddiu $9, $4, 4
move $4, $6
move $5, $7
move $6, $9
b .LBB4_9
move $7, $1
.LBB4_15:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, 0($4)
mtc1 $1, $f0
bnez $3, .LBB4_27
cvt.d.w $f0, $f0
lw $1, 4($4)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($2)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_27
mul.d $f0, $f0, $f1
.LBB4_17:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $6, $1
lw $1, 0($4)
mtc1 $1, $f0
bnez $3, .LBB4_27
cvt.d.w $f0, $f0
lw $1, 4($4)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($2)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_27
mul.d $f0, $f0, $f1
.LBB4_19:
sll $1, $8, 0
dsll $1, $1, 2
daddu $5, $6, $1
lw $1, 0($5)
mtc1 $1, $f0
bnez $3, .LBB4_27
cvt.d.w $f0, $f0
slt $1, $8, $10
daddiu $3, $5, 4
movn $4, $3, $1
lw $1, 0($4)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($2)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_27
mul.d $f0, $f0, $f1
.LBB4_21:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, 0($4)
mtc1 $1, $f0
bnez $3, .LBB4_27
cvt.d.w $f0, $f0
slt $1, $8, $9
daddiu $3, $4, 4
movn $6, $3, $1
lw $1, 0($6)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($2)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_27
mul.d $f0, $f0, $f1
.LBB4_23:
subu $1, $8, $11
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, -4($4)
mtc1 $1, $f0
bnez $3, .LBB4_27
cvt.d.w $f0, $f0
lw $1, 0($4)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($2)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_27
mul.d $f0, $f0, $f1
.LBB4_25:
subu $1, $8, $11
dsll $1, $1, 2
daddu $4, $6, $1
lw $1, -4($4)
mtc1 $1, $f0
bnez $3, .LBB4_27
cvt.d.w $f0, $f0
lw $1, 0($4)
mtc1 $1, $f1
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ld $1, %got_page(.LCPI4_0)($2)
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
mul.d $f0, $f0, $f1
.LBB4_27:
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
|
107
| 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
*/
|
mips64
|
-O3
|
mips64 clang 21.1.0
|
bs:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
blez $5, .LBB0_3
addiu $2, $5, -1
addiu $3, $zero, 0
.LBB0_2:
subu $1, $2, $3
srl $1, $1, 1
addu $1, $1, $3
dsll $5, $1, 2
addiu $7, $1, 1
addiu $1, $1, -1
daddu $5, $4, $5
lw $5, 0($5)
slt $5, $6, $5
movn $7, $3, $5
movn $2, $1, $5
slt $1, $2, $7
beqz $1, .LBB0_2
move $3, $7
.LBB0_3:
sll $2, $2, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
.LCPI1_0:
.8byte 0x3fe0000000000000
bs2:
.Lfunc_begin1 = .Ltmp13
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(bs2)))
daddu $1, $1, $25
daddiu $2, $1, %lo(%neg(%gp_rel(bs2)))
addiu $10, $5, -1
blez $5, .LBB1_3
move $3, $10
.LBB1_1:
lw $11, 0($6)
addiu $12, $zero, 0
move $3, $10
.LBB1_2:
subu $1, $3, $12
srl $1, $1, 1
addu $1, $1, $12
dsll $13, $1, 2
addiu $14, $1, 1
addiu $1, $1, -1
daddu $13, $4, $13
lw $13, 0($13)
slt $13, $11, $13
movn $14, $12, $13
movn $3, $1, $13
slt $1, $3, $14
beqz $1, .LBB1_2
move $12, $14
.LBB1_3:
slt $1, $3, $8
beqz $1, .LBB1_7
nop
beq $3, $10, .LBB1_9
nop
not $1, $3
sll $3, $3, 0
dsll $3, $3, 2
addu $8, $8, $1
addu $1, $5, $1
move $5, $7
daddu $3, $4, $3
move $4, $6
move $7, $1
daddiu $3, $3, 4
move $6, $3
addiu $10, $5, -1
bgtz $5, .LBB1_1
move $3, $10
b .LBB1_3
nop
.LBB1_7:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, 0($4)
mtc1 $1, $f0
beqz $9, .LBB1_11
cvt.d.w $f0, $f0
slt $1, $8, $3
daddiu $3, $4, 4
movn $6, $3, $1
lw $1, 0($6)
mtc1 $1, $f1
ld $1, %got_page(.LCPI1_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
b .LBB1_11
mul.d $f0, $f0, $f1
.LBB1_9:
subu $1, $8, $10
dsll $1, $1, 2
daddu $3, $6, $1
lw $1, -4($3)
mtc1 $1, $f0
beqz $9, .LBB1_11
cvt.d.w $f0, $f0
lw $1, 0($3)
mtc1 $1, $f1
ld $1, %got_page(.LCPI1_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI1_0)($1)
mul.d $f0, $f0, $f1
.LBB1_11:
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
min:
.Lfunc_begin2 = .Ltmp51
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
slt $1, $4, $5
movn $5, $4, $1
sll $2, $5, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
max:
.Lfunc_begin3 = .Ltmp55
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
slt $1, $5, $4
movn $5, $4, $1
sll $2, $5, 0
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
.LCPI4_0:
.8byte 0x3fe0000000000000
findMedianSortedArrays:
.Lfunc_begin4 = .Ltmp59
daddiu $sp, $sp, -16
sd $ra, 8($sp)
sd $fp, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(findMedianSortedArrays)))
daddu $1, $1, $25
daddiu $2, $1, %lo(%neg(%gp_rel(findMedianSortedArrays)))
addu $1, $7, $5
addiu $3, $1, -1
srl $8, $3, 31
addu $3, $3, $8
sra $8, $3, 1
beqz $7, .LBB4_16
andi $3, $1, 1
beqz $5, .LBB4_18
nop
lw $9, 0($4)
lw $10, 0($6)
slt $1, $10, $9
beqz $1, .LBB4_10
nop
addiu $11, $7, -1
blez $7, .LBB4_6
move $10, $11
.LBB4_4:
addiu $12, $zero, 0
move $10, $11
.LBB4_5:
subu $1, $10, $12
srl $1, $1, 1
addu $1, $1, $12
dsll $13, $1, 2
addiu $14, $1, 1
addiu $1, $1, -1
daddu $13, $6, $13
lw $13, 0($13)
slt $13, $9, $13
movn $14, $12, $13
movn $10, $1, $13
slt $1, $10, $14
beqz $1, .LBB4_5
move $12, $14
.LBB4_6:
slt $1, $10, $8
beqz $1, .LBB4_20
nop
beq $10, $11, .LBB4_24
nop
not $1, $10
addu $8, $8, $1
addu $1, $7, $1
sll $7, $10, 0
dsll $7, $7, 2
daddu $6, $6, $7
move $7, $5
move $5, $1
lw $9, 4($6)
daddiu $10, $6, 4
move $6, $4
move $4, $10
addiu $11, $7, -1
bgtz $7, .LBB4_4
move $10, $11
b .LBB4_6
nop
.LBB4_10:
addiu $11, $5, -1
blez $5, .LBB4_13
move $9, $11
addiu $12, $zero, 0
move $9, $11
.LBB4_12:
subu $1, $9, $12
srl $1, $1, 1
addu $1, $1, $12
dsll $13, $1, 2
addiu $14, $1, 1
addiu $1, $1, -1
daddu $13, $4, $13
lw $13, 0($13)
slt $13, $10, $13
movn $14, $12, $13
movn $9, $1, $13
slt $1, $9, $14
beqz $1, .LBB4_12
move $12, $14
.LBB4_13:
slt $1, $9, $8
beqz $1, .LBB4_22
nop
beq $9, $11, .LBB4_26
nop
not $1, $9
addu $8, $8, $1
addu $1, $5, $1
sll $5, $9, 0
dsll $5, $5, 2
daddu $4, $4, $5
move $5, $7
move $7, $1
lw $10, 4($4)
daddiu $9, $4, 4
move $4, $6
b .LBB4_10
move $6, $9
.LBB4_16:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, 0($4)
mtc1 $1, $f0
bnez $3, .LBB4_28
cvt.d.w $f0, $f0
lw $1, 4($4)
mtc1 $1, $f1
ld $1, %got_page(.LCPI4_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_28
mul.d $f0, $f0, $f1
.LBB4_18:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $6, $1
lw $1, 0($4)
mtc1 $1, $f0
bnez $3, .LBB4_28
cvt.d.w $f0, $f0
lw $1, 4($4)
mtc1 $1, $f1
ld $1, %got_page(.LCPI4_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_28
mul.d $f0, $f0, $f1
.LBB4_20:
sll $1, $8, 0
dsll $1, $1, 2
daddu $5, $6, $1
lw $1, 0($5)
mtc1 $1, $f0
bnez $3, .LBB4_28
cvt.d.w $f0, $f0
slt $1, $8, $10
daddiu $3, $5, 4
movn $4, $3, $1
lw $1, 0($4)
mtc1 $1, $f1
ld $1, %got_page(.LCPI4_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_28
mul.d $f0, $f0, $f1
.LBB4_22:
sll $1, $8, 0
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, 0($4)
mtc1 $1, $f0
bnez $3, .LBB4_28
cvt.d.w $f0, $f0
slt $1, $8, $9
daddiu $3, $4, 4
movn $6, $3, $1
lw $1, 0($6)
mtc1 $1, $f1
ld $1, %got_page(.LCPI4_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_28
mul.d $f0, $f0, $f1
.LBB4_24:
subu $1, $8, $11
dsll $1, $1, 2
daddu $4, $4, $1
lw $1, -4($4)
mtc1 $1, $f0
bnez $3, .LBB4_28
cvt.d.w $f0, $f0
lw $1, 0($4)
mtc1 $1, $f1
ld $1, %got_page(.LCPI4_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
b .LBB4_28
mul.d $f0, $f0, $f1
.LBB4_26:
subu $1, $8, $11
dsll $1, $1, 2
daddu $4, $6, $1
lw $1, -4($4)
mtc1 $1, $f0
bnez $3, .LBB4_28
cvt.d.w $f0, $f0
lw $1, 0($4)
mtc1 $1, $f1
ld $1, %got_page(.LCPI4_0)($2)
cvt.d.w $f1, $f1
add.d $f0, $f0, $f1
ldc1 $f1, %got_ofst(.LCPI4_0)($1)
mul.d $f0, $f0, $f1
.LBB4_28:
move $sp, $fp
ld $fp, 0($sp)
ld $ra, 8($sp)
jr $ra
daddiu $sp, $sp, 16
|
108
| 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
*/
|
mips64
|
-O0
|
mips64 gcc 15.2.0
|
bs:
daddiu $sp,$sp,-48
sd $fp,40($sp)
move $fp,$sp
sd $4,16($fp)
move $3,$5
move $2,$6
sll $3,$3,0
sw $3,24($fp)
sll $2,$2,0
sw $2,28($fp)
sw $0,0($fp)
lw $2,24($fp)
addiu $2,$2,-1
sw $2,4($fp)
b .L2
nop
.L4:
lw $3,4($fp)
lw $2,0($fp)
subu $2,$3,$2
srl $3,$2,31
addu $2,$3,$2
sra $2,$2,1
lw $3,0($fp)
addu $2,$3,$2
sw $2,8($fp)
lw $2,8($fp)
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $2,0($2)
lw $3,28($fp)
slt $2,$3,$2
beq $2,$0,.L3
nop
lw $2,8($fp)
addiu $2,$2,-1
sw $2,4($fp)
b .L2
nop
.L3:
lw $2,8($fp)
addiu $2,$2,1
sw $2,0($fp)
.L2:
lw $3,0($fp)
lw $2,4($fp)
slt $2,$2,$3
beq $2,$0,.L4
nop
lw $2,4($fp)
move $sp,$fp
ld $fp,40($sp)
daddiu $sp,$sp,48
jr $31
nop
bs2:
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(bs2)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(bs2)))
sd $4,16($fp)
sd $6,32($fp)
move $4,$7
move $3,$8
move $2,$9
sll $5,$5,0
sw $5,24($fp)
sll $4,$4,0
sw $4,28($fp)
sll $3,$3,0
sw $3,40($fp)
sll $2,$2,0
sw $2,44($fp)
ld $2,32($fp)
lw $3,0($2)
lw $2,24($fp)
move $6,$3
move $5,$2
ld $4,16($fp)
ld $2,%got_disp(bs)($28)
mtlo $2
mflo $25
jalr $25
nop
sw $2,8($fp)
lw $3,8($fp)
lw $2,40($fp)
slt $2,$3,$2
bne $2,$0,.L7
nop
lw $2,40($fp)
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
sdc1 $f0,0($fp)
lw $2,44($fp)
beq $2,$0,.L8
nop
lw $3,8($fp)
lw $2,40($fp)
slt $2,$2,$3
beq $2,$0,.L9
nop
lw $2,40($fp)
daddiu $2,$2,1
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
ldc1 $f1,0($fp)
add.d $f0,$f1,$f0
sdc1 $f0,0($fp)
b .L10
nop
.L9:
ld $2,32($fp)
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
ldc1 $f1,0($fp)
add.d $f0,$f1,$f0
sdc1 $f0,0($fp)
.L10:
ldc1 $f1,0($fp)
ld $2,%got_page(.LC0)($28)
ldc1 $f0,%got_ofst(.LC0)($2)
div.d $f0,$f1,$f0
sdc1 $f0,0($fp)
b .L8
nop
.L7:
lw $2,24($fp)
addiu $2,$2,-1
move $3,$2
lw $2,8($fp)
bne $2,$3,.L11
nop
lw $3,40($fp)
lw $2,8($fp)
subu $2,$3,$2
dsll $2,$2,2
daddiu $2,$2,-4
ld $3,32($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
sdc1 $f0,0($fp)
lw $2,44($fp)
beq $2,$0,.L8
nop
lw $3,40($fp)
lw $2,8($fp)
subu $2,$3,$2
dsll $2,$2,2
ld $3,32($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
ldc1 $f1,0($fp)
add.d $f0,$f1,$f0
sdc1 $f0,0($fp)
ldc1 $f1,0($fp)
ld $2,%got_page(.LC0)($28)
ldc1 $f0,%got_ofst(.LC0)($2)
div.d $f0,$f1,$f0
sdc1 $f0,0($fp)
b .L8
nop
.L11:
lw $2,8($fp)
daddiu $2,$2,1
dsll $2,$2,2
ld $3,16($fp)
daddu $4,$3,$2
lw $3,24($fp)
lw $2,8($fp)
subu $2,$3,$2
addiu $2,$2,-1
move $5,$2
lw $3,40($fp)
lw $2,8($fp)
subu $2,$3,$2
addiu $2,$2,-1
move $3,$2
lw $6,44($fp)
lw $2,28($fp)
move $9,$6
move $8,$3
move $7,$5
move $6,$4
move $5,$2
ld $4,32($fp)
ld $2,%got_disp(bs2)($28)
mtlo $2
mflo $25
jalr $25
nop
sdc1 $f0,0($fp)
.L8:
ldc1 $f0,0($fp)
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
daddiu $sp,$sp,80
jr $31
nop
min:
daddiu $sp,$sp,-32
sd $fp,24($sp)
move $fp,$sp
move $3,$4
move $2,$5
sll $3,$3,0
sw $3,0($fp)
sll $2,$2,0
sw $2,4($fp)
lw $3,0($fp)
lw $2,4($fp)
slt $4,$3,$2
beq $4,$0,.L14
nop
move $2,$3
.L14:
move $sp,$fp
ld $fp,24($sp)
daddiu $sp,$sp,32
jr $31
nop
max:
daddiu $sp,$sp,-32
sd $fp,24($sp)
move $fp,$sp
move $3,$4
move $2,$5
sll $3,$3,0
sw $3,0($fp)
sll $2,$2,0
sw $2,4($fp)
lw $3,0($fp)
lw $2,4($fp)
slt $4,$2,$3
beq $4,$0,.L17
nop
move $2,$3
.L17:
move $sp,$fp
ld $fp,24($sp)
daddiu $sp,$sp,32
jr $31
nop
findMedianSortedArrays:
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(findMedianSortedArrays)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(findMedianSortedArrays)))
sd $4,16($fp)
move $3,$5
sd $6,32($fp)
move $2,$7
sll $3,$3,0
sw $3,24($fp)
sll $2,$2,0
sw $2,28($fp)
lw $3,24($fp)
lw $2,28($fp)
addu $2,$3,$2
addiu $2,$2,-1
srl $3,$2,31
addu $2,$3,$2
sra $2,$2,1
sw $2,8($fp)
lw $3,24($fp)
lw $2,28($fp)
addu $2,$3,$2
andi $2,$2,0x1
sll $2,$2,0
andi $2,$2,0x1
xori $2,$2,0x1
andi $2,$2,0x00ff
sw $2,12($fp)
lw $2,28($fp)
bne $2,$0,.L20
nop
lw $2,8($fp)
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
sdc1 $f0,0($fp)
lw $2,12($fp)
beq $2,$0,.L21
nop
lw $2,8($fp)
daddiu $2,$2,1
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
ldc1 $f1,0($fp)
add.d $f0,$f1,$f0
sdc1 $f0,0($fp)
ldc1 $f1,0($fp)
ld $2,%got_page(.LC0)($28)
ldc1 $f0,%got_ofst(.LC0)($2)
div.d $f0,$f1,$f0
sdc1 $f0,0($fp)
.L21:
ldc1 $f0,0($fp)
b .L22
nop
.L20:
lw $2,24($fp)
bne $2,$0,.L23
nop
lw $2,8($fp)
dsll $2,$2,2
ld $3,32($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
sdc1 $f0,0($fp)
lw $2,12($fp)
beq $2,$0,.L24
nop
lw $2,8($fp)
daddiu $2,$2,1
dsll $2,$2,2
ld $3,32($fp)
daddu $2,$3,$2
lw $2,0($2)
dmtc1 $2,$f0
nop
cvt.d.w $f0,$f0
ldc1 $f1,0($fp)
add.d $f0,$f1,$f0
sdc1 $f0,0($fp)
ldc1 $f1,0($fp)
ld $2,%got_page(.LC0)($28)
ldc1 $f0,%got_ofst(.LC0)($2)
div.d $f0,$f1,$f0
sdc1 $f0,0($fp)
.L24:
ldc1 $f0,0($fp)
b .L22
nop
.L23:
ld $2,32($fp)
lw $3,0($2)
ld $2,16($fp)
lw $2,0($2)
slt $2,$3,$2
beq $2,$0,.L25
nop
lw $5,12($fp)
lw $4,8($fp)
lw $3,24($fp)
lw $2,28($fp)
move $9,$5
move $8,$4
move $7,$3
ld $6,16($fp)
move $5,$2
ld $4,32($fp)
ld $2,%got_disp(bs2)($28)
mtlo $2
mflo $25
jalr $25
nop
b .L22
nop
.L25:
lw $5,12($fp)
lw $4,8($fp)
lw $3,28($fp)
lw $2,24($fp)
move $9,$5
move $8,$4
move $7,$3
ld $6,32($fp)
move $5,$2
ld $4,16($fp)
ld $2,%got_disp(bs2)($28)
mtlo $2
mflo $25
jalr $25
nop
.L22:
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
daddiu $sp,$sp,80
jr $31
nop
.LC0:
.word 1073741824
.word 0
|
109
| 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
*/
|
mips64
|
-O1
|
mips64 gcc 15.2.0
|
bs:
addiu $2,$5,-1
bltz $2,.L8
move $8,$0
b .L9
subu $5,$2,$8
.L3:
.L4:
slt $3,$2,$8
bne $3,$0,.L8
subu $5,$2,$8
.L9:
srl $3,$5,31
addu $3,$3,$5
sra $3,$3,1
addu $3,$3,$8
dsll $7,$3,2
daddu $7,$4,$7
lw $7,0($7)
slt $7,$6,$7
beql $7,$0,.L3
addiu $8,$3,1
b .L4
addiu $2,$3,-1
.L8:
jr $31
nop
bs2:
daddiu $sp,$sp,-80
sd $31,72($sp)
sd $28,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)
lui $28,%hi(%neg(%gp_rel(bs2)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(bs2)))
move $17,$4
move $19,$5
move $20,$6
move $21,$7
move $16,$8
move $18,$9
lw $2,0($6)
move $22,$2
ld $25,%got_disp(bs)($28)
1: jalr $25
move $6,$2
slt $3,$2,$16
bnel $3,$0,.L11
addiu $3,$19,-1
dsll $4,$16,2
daddu $3,$17,$4
lw $3,0($3)
mtc1 $3,$f0
beq $18,$0,.L10
cvt.d.w $f0,$f0
slt $2,$16,$2
beq $2,$0,.L13
daddu $4,$17,$4
lw $2,4($4)
mtc1 $2,$f1
nop
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
.L14:
ld $2,%got_page(.LC0)($28)
ldc1 $f1,%got_ofst(.LC0)($2)
mul.d $f0,$f0,$f1
.L10:
ld $31,72($sp)
.L17:
ld $28,64($sp)
ld $22,56($sp)
ld $21,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,80
.L13:
dmtc1 $22,$f1
nop
cvt.d.w $f1,$f1
b .L14
add.d $f0,$f1,$f0
.L11:
bne $3,$2,.L15
subu $8,$16,$2
subu $2,$16,$2
daddiu $3,$2,-1
dsll $3,$3,2
daddu $3,$20,$3
lw $3,0($3)
mtc1 $3,$f0
beq $18,$0,.L10
cvt.d.w $f0,$f0
dsll $2,$2,2
daddu $2,$20,$2
lw $2,0($2)
mtc1 $2,$f1
nop
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ld $2,%got_page(.LC0)($28)
ldc1 $f1,%got_ofst(.LC0)($2)
b .L10
mul.d $f0,$f0,$f1
.L15:
subu $7,$19,$2
daddiu $2,$2,1
dsll $6,$2,2
move $9,$18
addiu $8,$8,-1
addiu $7,$7,-1
daddu $6,$17,$6
move $5,$21
ld $25,%got_disp(bs2)($28)
1: jalr $25
move $4,$20
b .L17
ld $31,72($sp)
min:
move $2,$5
slt $5,$4,$5
bnel $5,$0,.L21
move $2,$4
.L21:
jr $31
nop
max:
move $2,$5
slt $5,$5,$4
bnel $5,$0,.L25
move $2,$4
.L25:
jr $31
nop
findMedianSortedArrays:
daddiu $sp,$sp,-16
sd $31,8($sp)
sd $28,0($sp)
lui $28,%hi(%neg(%gp_rel(findMedianSortedArrays)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(findMedianSortedArrays)))
move $3,$6
addu $9,$5,$7
addiu $6,$9,-1
srl $8,$6,31
addu $8,$8,$6
sra $8,$8,1
beq $7,$0,.L32
andi $9,$9,0x1
beq $5,$0,.L33
move $2,$7
lw $6,0($3)
lw $7,0($4)
slt $6,$6,$7
bne $6,$0,.L34
xori $9,$9,0x1
move $7,$2
ld $25,%got_disp(bs2)($28)
1: jalr $25
move $6,$3
.L26:
ld $31,8($sp)
.L35:
ld $28,0($sp)
jr $31
daddiu $sp,$sp,16
.L32:
dsll $8,$8,2
daddu $2,$4,$8
lw $2,0($2)
mtc1 $2,$f0
bne $9,$0,.L26
cvt.d.w $f0,$f0
daddu $8,$4,$8
lw $2,4($8)
mtc1 $2,$f1
nop
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ld $2,%got_page(.LC0)($28)
ldc1 $f1,%got_ofst(.LC0)($2)
b .L26
mul.d $f0,$f0,$f1
.L33:
dsll $8,$8,2
daddu $2,$3,$8
lw $2,0($2)
mtc1 $2,$f0
bne $9,$0,.L26
cvt.d.w $f0,$f0
daddu $3,$3,$8
lw $2,4($3)
mtc1 $2,$f1
nop
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ld $2,%got_page(.LC0)($28)
ldc1 $f1,%got_ofst(.LC0)($2)
b .L26
mul.d $f0,$f0,$f1
.L34:
move $7,$5
move $6,$4
move $5,$2
ld $25,%got_disp(bs2)($28)
1: jalr $25
move $4,$3
b .L35
ld $31,8($sp)
.LC0:
.word 1071644672
.word 0
|
110
| 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
*/
|
mips64
|
-O2
|
mips64 gcc 15.2.0
|
bs:
addiu $2,$5,-1
bltz $2,.L11
move $8,$0
b .L10
subu $3,$2,$8
.L9:
slt $3,$2,$8
bne $3,$0,.L11
subu $3,$2,$8
.L10:
sra $3,$3,1
addu $3,$3,$8
dsll $7,$3,2
daddu $7,$4,$7
lw $7,0($7)
slt $7,$6,$7
bnel $7,$0,.L9
addiu $2,$3,-1
addiu $8,$3,1
slt $3,$2,$8
beq $3,$0,.L10
subu $3,$2,$8
.L11:
jr $31
nop
bs2:
lui $14,%hi(%neg(%gp_rel(bs2)))
daddu $14,$14,$25
addiu $13,$5,-1
lw $12,0($6)
daddiu $14,$14,%lo(%neg(%gp_rel(bs2)))
bltz $13,.L13
move $11,$13
.L36:
b .L16
move $10,$0
.L35:
slt $2,$11,$10
bne $2,$0,.L38
slt $2,$11,$8
.L16:
subu $2,$11,$10
.L39:
sra $2,$2,1
addu $2,$2,$10
dsll $3,$2,2
daddu $3,$4,$3
lw $3,0($3)
slt $3,$12,$3
bnel $3,$0,.L35
addiu $11,$2,-1
addiu $10,$2,1
slt $2,$11,$10
beql $2,$0,.L39
subu $2,$11,$10
slt $2,$11,$8
.L38:
beql $2,$0,.L40
dsll $2,$8,2
beq $11,$13,.L24
subu $3,$8,$11
addiu $2,$11,1
dsll $2,$2,2
subu $11,$5,$11
daddu $2,$4,$2
move $5,$7
move $4,$6
addiu $13,$5,-1
move $6,$2
addiu $7,$11,-1
lw $12,0($6)
addiu $8,$3,-1
bgez $13,.L36
move $11,$13
.L13:
slt $2,$13,$8
bne $2,$0,.L24
dsll $2,$8,2
.L40:
daddu $2,$4,$2
lwc1 $f0,0($2)
beq $9,$0,.L41
cvt.d.w $f0,$f0
slt $11,$8,$11
beq $11,$0,.L19
nop
lw $2,4($2)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L41:
jr $31
nop
.L24:
subu $11,$8,$11
addiu $2,$11,-1
dsll $2,$2,32
dsrl $2,$2,32
dsll $2,$2,2
daddu $2,$6,$2
lw $2,0($2)
mtc1 $2,$f0
beq $9,$0,.L41
cvt.d.w $f0,$f0
dsll $11,$11,32
dsrl $11,$11,32
dsll $11,$11,2
daddu $6,$6,$11
lw $2,0($6)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
.L37:
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L19:
dmtc1 $12,$f1
b .L37
ld $2,%got_page(.LC0)($14)
min:
slt $3,$4,$5
beq $3,$0,.L44
nop
jr $31
move $2,$4
.L44:
jr $31
move $2,$5
max:
slt $3,$5,$4
beq $3,$0,.L48
nop
jr $31
move $2,$4
.L48:
jr $31
move $2,$5
findMedianSortedArrays:
addu $9,$5,$7
addiu $8,$9,-1
srl $2,$8,31
lui $14,%hi(%neg(%gp_rel(findMedianSortedArrays)))
addu $2,$2,$8
daddu $14,$14,$25
sra $8,$2,1
daddiu $14,$14,%lo(%neg(%gp_rel(findMedianSortedArrays)))
sra $2,$2,1
beq $7,$0,.L57
andi $9,$9,0x1
beq $5,$0,.L58
move $12,$6
lw $2,0($6)
lw $13,0($4)
slt $2,$2,$13
bne $2,$0,.L59
xori $9,$9,0x1
ld $25,%got_disp(bs2)($14)
1: jr $25
nop
.L58:
dsll $2,$2,2
daddu $12,$6,$2
lwc1 $f0,0($12)
bne $9,$0,.L61
cvt.d.w $f0,$f0
lw $2,4($12)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L57:
dsll $2,$2,2
daddu $11,$4,$2
lwc1 $f0,0($11)
beq $9,$0,.L60
cvt.d.w $f0,$f0
.L61:
jr $31
nop
.L60:
lw $2,4($11)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L59:
ld $25,%got_disp(bs2)($14)
move $3,$7
move $6,$4
move $7,$5
move $4,$12
1: jr $25
move $5,$3
.LC0:
.word 1071644672
.word 0
|
111
| 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
*/
|
mips64
|
-O3
|
mips64 gcc 15.2.0
|
bs:
addiu $2,$5,-1
bltz $2,.L11
move $8,$0
b .L10
subu $3,$2,$8
.L9:
slt $3,$2,$8
bne $3,$0,.L11
subu $3,$2,$8
.L10:
sra $3,$3,1
addu $3,$3,$8
dsll $7,$3,2
daddu $7,$4,$7
lw $7,0($7)
slt $7,$6,$7
bnel $7,$0,.L9
addiu $2,$3,-1
addiu $8,$3,1
slt $3,$2,$8
beq $3,$0,.L10
subu $3,$2,$8
.L11:
jr $31
nop
bs2:
lui $14,%hi(%neg(%gp_rel(bs2)))
daddu $14,$14,$25
addiu $13,$5,-1
lw $12,0($6)
daddiu $14,$14,%lo(%neg(%gp_rel(bs2)))
bltz $13,.L13
move $11,$13
.L36:
b .L16
move $10,$0
.L35:
slt $2,$11,$10
bne $2,$0,.L38
slt $2,$11,$8
.L16:
subu $2,$11,$10
.L39:
sra $2,$2,1
addu $2,$2,$10
dsll $3,$2,2
daddu $3,$4,$3
lw $3,0($3)
slt $3,$12,$3
bnel $3,$0,.L35
addiu $11,$2,-1
addiu $10,$2,1
slt $2,$11,$10
beql $2,$0,.L39
subu $2,$11,$10
slt $2,$11,$8
.L38:
beql $2,$0,.L40
dsll $2,$8,2
beq $11,$13,.L24
subu $3,$8,$11
addiu $2,$11,1
dsll $2,$2,2
subu $11,$5,$11
daddu $2,$4,$2
move $5,$7
move $4,$6
addiu $13,$5,-1
move $6,$2
addiu $7,$11,-1
lw $12,0($6)
addiu $8,$3,-1
bgez $13,.L36
move $11,$13
.L13:
slt $2,$13,$8
bne $2,$0,.L24
dsll $2,$8,2
.L40:
daddu $2,$4,$2
lwc1 $f0,0($2)
beq $9,$0,.L41
cvt.d.w $f0,$f0
slt $11,$8,$11
beq $11,$0,.L19
nop
lw $2,4($2)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L41:
jr $31
nop
.L24:
subu $11,$8,$11
addiu $2,$11,-1
dsll $2,$2,32
dsrl $2,$2,32
dsll $2,$2,2
daddu $2,$6,$2
lw $2,0($2)
mtc1 $2,$f0
beq $9,$0,.L41
cvt.d.w $f0,$f0
dsll $11,$11,32
dsrl $11,$11,32
dsll $11,$11,2
daddu $6,$6,$11
lw $2,0($6)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
.L37:
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L19:
dmtc1 $12,$f1
b .L37
ld $2,%got_page(.LC0)($14)
min:
slt $3,$4,$5
beq $3,$0,.L44
nop
jr $31
move $2,$4
.L44:
jr $31
move $2,$5
max:
slt $3,$5,$4
beq $3,$0,.L48
nop
jr $31
move $2,$4
.L48:
jr $31
move $2,$5
findMedianSortedArrays:
addu $9,$5,$7
addiu $8,$9,-1
srl $2,$8,31
lui $14,%hi(%neg(%gp_rel(findMedianSortedArrays)))
addu $2,$2,$8
daddu $14,$14,$25
sra $8,$2,1
daddiu $14,$14,%lo(%neg(%gp_rel(findMedianSortedArrays)))
sra $2,$2,1
beq $7,$0,.L57
andi $9,$9,0x1
beq $5,$0,.L58
move $12,$6
lw $2,0($6)
lw $13,0($4)
slt $2,$2,$13
bne $2,$0,.L59
xori $9,$9,0x1
ld $25,%got_disp(bs2)($14)
1: jr $25
nop
.L58:
dsll $2,$2,2
daddu $12,$6,$2
lwc1 $f0,0($12)
bne $9,$0,.L61
cvt.d.w $f0,$f0
lw $2,4($12)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L57:
dsll $2,$2,2
daddu $11,$4,$2
lwc1 $f0,0($11)
beq $9,$0,.L60
cvt.d.w $f0,$f0
.L61:
jr $31
nop
.L60:
lw $2,4($11)
mtc1 $2,$f1
ld $2,%got_page(.LC0)($14)
cvt.d.w $f1,$f1
add.d $f0,$f1,$f0
ldc1 $f1,%got_ofst(.LC0)($2)
jr $31
mul.d $f0,$f0,$f1
.L59:
ld $25,%got_disp(bs2)($14)
move $3,$7
move $6,$4
move $7,$5
move $4,$12
1: jr $25
move $5,$3
.LC0:
.word 1071644672
.word 0
|
112
| 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
*/
|
riscv64
|
-O0
|
RISC-V 64 clang 21.1.0
|
bs:
addi sp, sp, -48
sd ra, 40(sp)
sd s0, 32(sp)
addi s0, sp, 48
sd a0, -24(s0)
sw a1, -28(s0)
sw a2, -32(s0)
li a0, 0
sw a0, -36(s0)
lw a0, -28(s0)
addiw a0, a0, -1
sw a0, -40(s0)
j .LBB0_1
.LBB0_1:
lw a1, -36(s0)
lw a0, -40(s0)
blt a0, a1, .LBB0_6
j .LBB0_2
.LBB0_2:
lw a0, -36(s0)
lw a1, -40(s0)
subw a1, a1, a0
srliw a2, a1, 31
addw a1, a1, a2
sraiw a1, a1, 1
addw a0, a0, a1
sw a0, -44(s0)
ld a0, -24(s0)
lw a1, -44(s0)
slli a1, a1, 2
add a0, a0, a1
lw a1, 0(a0)
lw a0, -32(s0)
bge a0, a1, .LBB0_4
j .LBB0_3
.LBB0_3:
lw a0, -44(s0)
addiw a0, a0, -1
sw a0, -40(s0)
j .LBB0_5
.LBB0_4:
lw a0, -44(s0)
addiw a0, a0, 1
sw a0, -36(s0)
j .LBB0_5
.LBB0_5:
j .LBB0_1
.LBB0_6:
lw a0, -40(s0)
ld ra, 40(sp)
ld s0, 32(sp)
addi sp, sp, 48
ret
.LCPI1_0:
.quad 0x3fe0000000000000
bs2:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
addi s0, sp, 80
sd a0, -24(s0)
sw a1, -28(s0)
sd a2, -40(s0)
sw a3, -44(s0)
sw a4, -48(s0)
sw a5, -52(s0)
ld a0, -24(s0)
lw a1, -28(s0)
ld a2, -40(s0)
lw a2, 0(a2)
call bs
sw a0, -68(s0)
lw a0, -68(s0)
lw a1, -48(s0)
blt a0, a1, .LBB1_7
j .LBB1_1
.LBB1_1:
ld a0, -24(s0)
lw a1, -48(s0)
slli a1, a1, 2
add a0, a0, a1
lw a0, 0(a0)
fcvt.d.w fa5, a0
fsd fa5, -64(s0)
lw a0, -52(s0)
beqz a0, .LBB1_6
j .LBB1_2
.LBB1_2:
lw a1, -68(s0)
lw a0, -48(s0)
bge a0, a1, .LBB1_4
j .LBB1_3
.LBB1_3:
ld a1, -24(s0)
lw a0, -48(s0)
slli a0, a0, 2
add a0, a0, a1
lw a0, 4(a0)
fcvt.d.w fa4, a0
fld fa5, -64(s0)
fadd.d fa5, fa5, fa4
fsd fa5, -64(s0)
j .LBB1_5
.LBB1_4:
ld a0, -40(s0)
lw a0, 0(a0)
fcvt.d.w fa4, a0
fld fa5, -64(s0)
fadd.d fa5, fa5, fa4
fsd fa5, -64(s0)
j .LBB1_5
.LBB1_5:
fld fa5, -64(s0)
.Lpcrel_hi0:
auipc a0, %pcrel_hi(.LCPI1_0)
addi a0, a0, %pcrel_lo(.Lpcrel_hi0)
fld fa4, 0(a0)
fmul.d fa5, fa5, fa4
fsd fa5, -64(s0)
j .LBB1_6
.LBB1_6:
j .LBB1_13
.LBB1_7:
lw a0, -68(s0)
lw a1, -28(s0)
addiw a1, a1, -1
bne a0, a1, .LBB1_11
j .LBB1_8
.LBB1_8:
ld a0, -40(s0)
lw a2, -48(s0)
lw a1, -68(s0)
not a1, a1
addw a1, a1, a2
slli a1, a1, 2
add a0, a0, a1
lw a0, 0(a0)
fcvt.d.w fa5, a0
fsd fa5, -64(s0)
lw a0, -52(s0)
beqz a0, .LBB1_10
j .LBB1_9
.LBB1_9:
ld a0, -40(s0)
lw a1, -48(s0)
lw a2, -68(s0)
subw a1, a1, a2
slli a1, a1, 2
add a0, a0, a1
lw a0, 0(a0)
fcvt.d.w fa4, a0
fld fa5, -64(s0)
fadd.d fa5, fa5, fa4
fsd fa5, -64(s0)
fld fa5, -64(s0)
.Lpcrel_hi1:
auipc a0, %pcrel_hi(.LCPI1_0)
addi a0, a0, %pcrel_lo(.Lpcrel_hi1)
fld fa4, 0(a0)
fmul.d fa5, fa5, fa4
fsd fa5, -64(s0)
j .LBB1_10
.LBB1_10:
j .LBB1_12
.LBB1_11:
ld a0, -40(s0)
lw a1, -44(s0)
ld a3, -24(s0)
lw a4, -68(s0)
slli a2, a4, 2
add a2, a2, a3
addi a2, a2, 4
lw a3, -28(s0)
not a4, a4
addw a3, a3, a4
lw a5, -48(s0)
addw a4, a4, a5
lw a5, -52(s0)
call bs2
fsd fa0, -64(s0)
j .LBB1_12
.LBB1_12:
j .LBB1_13
.LBB1_13:
fld fa0, -64(s0)
ld ra, 72(sp)
ld s0, 64(sp)
addi sp, sp, 80
ret
min:
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
addi s0, sp, 32
sw a0, -20(s0)
sw a1, -24(s0)
lw a0, -20(s0)
lw a1, -24(s0)
bge a0, a1, .LBB2_2
j .LBB2_1
.LBB2_1:
lw a0, -20(s0)
sd a0, -32(s0)
j .LBB2_3
.LBB2_2:
lw a0, -24(s0)
sd a0, -32(s0)
j .LBB2_3
.LBB2_3:
ld a0, -32(s0)
sext.w a0, a0
ld ra, 24(sp)
ld s0, 16(sp)
addi sp, sp, 32
ret
max:
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
addi s0, sp, 32
sw a0, -20(s0)
sw a1, -24(s0)
lw a1, -20(s0)
lw a0, -24(s0)
bge a0, a1, .LBB3_2
j .LBB3_1
.LBB3_1:
lw a0, -20(s0)
sd a0, -32(s0)
j .LBB3_3
.LBB3_2:
lw a0, -24(s0)
sd a0, -32(s0)
j .LBB3_3
.LBB3_3:
ld a0, -32(s0)
sext.w a0, a0
ld ra, 24(sp)
ld s0, 16(sp)
addi sp, sp, 32
ret
.LCPI4_0:
.quad 0x3fe0000000000000
findMedianSortedArrays:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
addi s0, sp, 80
sd a0, -32(s0)
sw a1, -36(s0)
sd a2, -48(s0)
sw a3, -52(s0)
lw a0, -36(s0)
lw a1, -52(s0)
addw a0, a0, a1
addiw a0, a0, -1
srliw a1, a0, 31
addw a0, a0, a1
sraiw a0, a0, 1
sw a0, -68(s0)
lw a0, -36(s0)
lw a1, -52(s0)
addw a0, a0, a1
srliw a1, a0, 31
addw a1, a1, a0
andi a1, a1, -2
subw a0, a0, a1
seqz a0, a0
sw a0, -72(s0)
lw a0, -52(s0)
bnez a0, .LBB4_4
j .LBB4_1
.LBB4_1:
ld a0, -32(s0)
lw a1, -68(s0)
slli a1, a1, 2
add a0, a0, a1
lw a0, 0(a0)
fcvt.d.w fa5, a0
fsd fa5, -64(s0)
lw a0, -72(s0)
beqz a0, .LBB4_3
j .LBB4_2
.LBB4_2:
ld a1, -32(s0)
lw a0, -68(s0)
slli a0, a0, 2
add a0, a0, a1
lw a0, 4(a0)
fcvt.d.w fa4, a0
fld fa5, -64(s0)
fadd.d fa5, fa5, fa4
fsd fa5, -64(s0)
fld fa5, -64(s0)
.Lpcrel_hi2:
auipc a0, %pcrel_hi(.LCPI4_0)
addi a0, a0, %pcrel_lo(.Lpcrel_hi2)
fld fa4, 0(a0)
fmul.d fa5, fa5, fa4
fsd fa5, -64(s0)
j .LBB4_3
.LBB4_3:
fld fa5, -64(s0)
fsd fa5, -24(s0)
j .LBB4_11
.LBB4_4:
lw a0, -36(s0)
bnez a0, .LBB4_8
j .LBB4_5
.LBB4_5:
ld a0, -48(s0)
lw a1, -68(s0)
slli a1, a1, 2
add a0, a0, a1
lw a0, 0(a0)
fcvt.d.w fa5, a0
fsd fa5, -64(s0)
lw a0, -72(s0)
beqz a0, .LBB4_7
j .LBB4_6
.LBB4_6:
ld a1, -48(s0)
lw a0, -68(s0)
slli a0, a0, 2
add a0, a0, a1
lw a0, 4(a0)
fcvt.d.w fa4, a0
fld fa5, -64(s0)
fadd.d fa5, fa5, fa4
fsd fa5, -64(s0)
fld fa5, -64(s0)
.Lpcrel_hi3:
auipc a0, %pcrel_hi(.LCPI4_0)
addi a0, a0, %pcrel_lo(.Lpcrel_hi3)
fld fa4, 0(a0)
fmul.d fa5, fa5, fa4
fsd fa5, -64(s0)
j .LBB4_7
.LBB4_7:
fld fa5, -64(s0)
fsd fa5, -24(s0)
j .LBB4_11
.LBB4_8:
ld a0, -48(s0)
lw a0, 0(a0)
ld a1, -32(s0)
lw a1, 0(a1)
bge a0, a1, .LBB4_10
j .LBB4_9
.LBB4_9:
ld a0, -48(s0)
lw a1, -52(s0)
ld a2, -32(s0)
lw a3, -36(s0)
lw a4, -68(s0)
lw a5, -72(s0)
call bs2
fsd fa0, -24(s0)
j .LBB4_11
.LBB4_10:
ld a0, -32(s0)
lw a1, -36(s0)
ld a2, -48(s0)
lw a3, -52(s0)
lw a4, -68(s0)
lw a5, -72(s0)
call bs2
fsd fa0, -24(s0)
j .LBB4_11
.LBB4_11:
fld fa0, -24(s0)
ld ra, 72(sp)
ld s0, 64(sp)
addi sp, sp, 80
ret
|
113
| 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
*/
|
riscv64
|
-O1
|
RISC-V 64 clang 21.1.0
|
bs:
mv a3, a0
addiw a0, a1, -1
blez a1, .LBB0_7
li a1, 0
j .LBB0_3
.LBB0_2:
blt a0, a1, .LBB0_7
.LBB0_3:
subw a4, a0, a1
srliw a4, a4, 1
addw a5, a4, a1
slli a5, a5, 2
add a5, a5, a3
lw a5, 0(a5)
add a4, a4, a1
blt a2, a5, .LBB0_5
blt a2, a5, .LBB0_2
j .LBB0_6
.LBB0_5:
addiw a0, a4, -1
blt a2, a5, .LBB0_2
.LBB0_6:
addiw a1, a4, 1
j .LBB0_2
.LBB0_7:
ret
.LCPI1_0:
.quad 0x3fe0000000000000
bs2:
.LBB1_1:
addiw a7, a1, -1
mv a6, a7
blez a1, .LBB1_8
li t1, 0
lw t0, 0(a2)
mv a6, a7
j .LBB1_4
.LBB1_3:
blt a6, t1, .LBB1_8
.LBB1_4:
subw t2, a6, t1
srliw t2, t2, 1
addw t3, t2, t1
slli t3, t3, 2
add t3, t3, a0
lw t3, 0(t3)
add t2, t2, t1
blt t0, t3, .LBB1_6
blt t0, t3, .LBB1_3
j .LBB1_7
.LBB1_6:
addiw a6, t2, -1
blt t0, t3, .LBB1_3
.LBB1_7:
addiw t1, t2, 1
j .LBB1_3
.LBB1_8:
bge a6, a4, .LBB1_11
beq a6, a7, .LBB1_15
slli a7, a6, 2
not a6, a6
add a7, a7, a0
mv a0, a2
addi a2, a7, 4
addw a7, a1, a6
addw a4, a4, a6
mv a1, a3
mv a3, a7
j .LBB1_1
.LBB1_11:
slli a1, a4, 2
add a0, a0, a1
lw a1, 0(a0)
fcvt.d.w fa0, a1
beqz a5, .LBB1_18
bge a4, a6, .LBB1_14
addi a2, a0, 4
.LBB1_14:
lw a0, 0(a2)
.Lpcrel_hi0:
auipc a1, %pcrel_hi(.LCPI1_0)
fld fa5, %pcrel_lo(.Lpcrel_hi0)(a1)
j .LBB1_17
.LBB1_15:
sub a0, a4, a6
slli a0, a0, 2
add a2, a2, a0
lw a0, -4(a2)
fcvt.d.w fa0, a0
beqz a5, .LBB1_18
lw a0, 0(a2)
.Lpcrel_hi1:
auipc a1, %pcrel_hi(.LCPI1_0)
fld fa5, %pcrel_lo(.Lpcrel_hi1)(a1)
.LBB1_17:
fcvt.d.w fa4, a0
fadd.d fa4, fa0, fa4
fmul.d fa0, fa4, fa5
.LBB1_18:
ret
min:
blt a0, a1, .LBB2_2
mv a0, a1
.LBB2_2:
ret
max:
blt a1, a0, .LBB3_2
mv a0, a1
.LBB3_2:
ret
.LCPI4_0:
.quad 0x3fe0000000000000
findMedianSortedArrays:
add a6, a3, a1
addi a4, a6, -1
srliw a5, a4, 31
add a4, a4, a5
sraiw a4, a4, 1
andi a5, a6, 1
beqz a3, .LBB4_5
beqz a1, .LBB4_7
lw a6, 0(a2)
lw a7, 0(a0)
xori a5, a5, 1
bge a6, a7, .LBB4_4
mv a6, a0
mv a0, a2
mv a7, a1
mv a1, a3
mv a2, a6
mv a3, a7
.LBB4_4:
tail bs2
.LBB4_5:
slli a4, a4, 2
add a0, a0, a4
lw a1, 0(a0)
fcvt.d.w fa0, a1
bnez a5, .LBB4_10
lw a0, 4(a0)
.Lpcrel_hi2:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi2)(a1)
j .LBB4_9
.LBB4_7:
slli a4, a4, 2
add a2, a2, a4
lw a0, 0(a2)
fcvt.d.w fa0, a0
bnez a5, .LBB4_10
lw a0, 4(a2)
.Lpcrel_hi3:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi3)(a1)
.LBB4_9:
fcvt.d.w fa4, a0
fadd.d fa4, fa0, fa4
fmul.d fa0, fa4, fa5
.LBB4_10:
ret
|
114
| 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
*/
|
riscv64
|
-O2
|
RISC-V 64 clang 21.1.0
|
bs:
mv a3, a0
addiw a0, a1, -1
blez a1, .LBB0_7
li a1, 0
j .LBB0_3
.LBB0_2:
blt a0, a1, .LBB0_7
.LBB0_3:
subw a4, a0, a1
srliw a4, a4, 1
addw a5, a4, a1
slli a5, a5, 2
add a5, a5, a3
lw a5, 0(a5)
add a4, a4, a1
blt a2, a5, .LBB0_5
blt a2, a5, .LBB0_2
j .LBB0_6
.LBB0_5:
addiw a0, a4, -1
blt a2, a5, .LBB0_2
.LBB0_6:
addiw a1, a4, 1
j .LBB0_2
.LBB0_7:
ret
.LCPI1_0:
.quad 0x3fe0000000000000
bs2:
.LBB1_1:
addiw a7, a1, -1
mv a6, a7
blez a1, .LBB1_8
li t1, 0
lw t0, 0(a2)
mv a6, a7
j .LBB1_4
.LBB1_3:
blt a6, t1, .LBB1_8
.LBB1_4:
subw t2, a6, t1
srliw t2, t2, 1
addw t3, t2, t1
slli t3, t3, 2
add t3, t3, a0
lw t3, 0(t3)
add t2, t2, t1
blt t0, t3, .LBB1_6
blt t0, t3, .LBB1_3
j .LBB1_7
.LBB1_6:
addiw a6, t2, -1
blt t0, t3, .LBB1_3
.LBB1_7:
addiw t1, t2, 1
j .LBB1_3
.LBB1_8:
bge a6, a4, .LBB1_11
beq a6, a7, .LBB1_15
slli a7, a6, 2
not a6, a6
add a7, a7, a0
mv a0, a2
addi a2, a7, 4
addw a7, a1, a6
addw a4, a4, a6
mv a1, a3
mv a3, a7
j .LBB1_1
.LBB1_11:
slli a1, a4, 2
add a0, a0, a1
lw a1, 0(a0)
fcvt.d.w fa0, a1
beqz a5, .LBB1_18
bge a4, a6, .LBB1_14
addi a2, a0, 4
.LBB1_14:
lw a0, 0(a2)
.Lpcrel_hi0:
auipc a1, %pcrel_hi(.LCPI1_0)
fld fa5, %pcrel_lo(.Lpcrel_hi0)(a1)
j .LBB1_17
.LBB1_15:
sub a0, a4, a7
slli a0, a0, 2
add a2, a2, a0
lw a0, -4(a2)
fcvt.d.w fa0, a0
beqz a5, .LBB1_18
lw a0, 0(a2)
.Lpcrel_hi1:
auipc a1, %pcrel_hi(.LCPI1_0)
fld fa5, %pcrel_lo(.Lpcrel_hi1)(a1)
.LBB1_17:
fcvt.d.w fa4, a0
fadd.d fa4, fa0, fa4
fmul.d fa0, fa4, fa5
.LBB1_18:
ret
min:
blt a0, a1, .LBB2_2
mv a0, a1
.LBB2_2:
ret
max:
blt a1, a0, .LBB3_2
mv a0, a1
.LBB3_2:
ret
.LCPI4_0:
.quad 0x3fe0000000000000
findMedianSortedArrays:
add a6, a3, a1
addi a5, a6, -1
srliw a4, a5, 31
add a4, a4, a5
sraiw a7, a4, 1
andi a6, a6, 1
beqz a3, .LBB4_23
beqz a1, .LBB4_25
lw t2, 0(a2)
lw t1, 0(a0)
bge t2, t1, .LBB4_13
.LBB4_3:
addiw t0, a3, -1
mv t2, t0
blez a3, .LBB4_10
li a5, 0
mv t2, t0
j .LBB4_6
.LBB4_5:
blt t2, a5, .LBB4_10
.LBB4_6:
subw a4, t2, a5
srliw t3, a4, 1
addw a4, t3, a5
slli a4, a4, 2
add a4, a4, a2
lw a4, 0(a4)
add t3, t3, a5
blt t1, a4, .LBB4_8
blt t1, a4, .LBB4_5
j .LBB4_9
.LBB4_8:
addiw t2, t3, -1
blt t1, a4, .LBB4_5
.LBB4_9:
addiw a5, t3, 1
j .LBB4_5
.LBB4_10:
bge t2, a7, .LBB4_27
beq t2, t0, .LBB4_35
slli a4, t2, 2
add a4, a4, a2
mv a2, a0
addi a0, a4, 4
lw t1, 4(a4)
not a4, t2
addw a5, a3, a4
addw a7, a7, a4
mv a3, a1
mv a1, a5
j .LBB4_3
.LBB4_13:
addiw t0, a1, -1
mv t1, t0
blez a1, .LBB4_20
li a4, 0
mv t1, t0
j .LBB4_16
.LBB4_15:
blt t1, a4, .LBB4_20
.LBB4_16:
subw a5, t1, a4
srliw t3, a5, 1
addw a5, t3, a4
slli a5, a5, 2
add a5, a5, a0
lw a5, 0(a5)
add t3, t3, a4
blt t2, a5, .LBB4_18
blt t2, a5, .LBB4_15
j .LBB4_19
.LBB4_18:
addiw t1, t3, -1
blt t2, a5, .LBB4_15
.LBB4_19:
addiw a4, t3, 1
j .LBB4_15
.LBB4_20:
bge t1, a7, .LBB4_31
beq t1, t0, .LBB4_37
slli a4, t1, 2
add a4, a4, a0
mv a0, a2
addi a2, a4, 4
lw t2, 4(a4)
not a4, t1
addw a5, a1, a4
addw a7, a7, a4
mv a1, a3
mv a3, a5
j .LBB4_13
.LBB4_23:
slli a7, a7, 2
add a0, a0, a7
lw a1, 0(a0)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
lw a0, 4(a0)
.Lpcrel_hi2:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi2)(a1)
j .LBB4_39
.LBB4_25:
slli a7, a7, 2
add a2, a2, a7
lw a0, 0(a2)
fcvt.d.w fa0, a0
bnez a6, .LBB4_40
lw a0, 4(a2)
.Lpcrel_hi3:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi3)(a1)
j .LBB4_39
.LBB4_27:
slli a1, a7, 2
add a2, a2, a1
lw a1, 0(a2)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
bge a7, t2, .LBB4_30
addi a0, a2, 4
.LBB4_30:
lw a0, 0(a0)
.Lpcrel_hi4:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi4)(a1)
j .LBB4_39
.LBB4_31:
slli a1, a7, 2
add a0, a0, a1
lw a1, 0(a0)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
bge a7, t1, .LBB4_34
addi a2, a0, 4
.LBB4_34:
lw a0, 0(a2)
.Lpcrel_hi6:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi6)(a1)
j .LBB4_39
.LBB4_35:
sub a1, a7, t0
slli a1, a1, 2
add a0, a0, a1
lw a1, -4(a0)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
lw a0, 0(a0)
.Lpcrel_hi5:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi5)(a1)
j .LBB4_39
.LBB4_37:
sub a0, a7, t0
slli a0, a0, 2
add a2, a2, a0
lw a0, -4(a2)
fcvt.d.w fa0, a0
bnez a6, .LBB4_40
lw a0, 0(a2)
.Lpcrel_hi7:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi7)(a1)
.LBB4_39:
fcvt.d.w fa4, a0
fadd.d fa4, fa0, fa4
fmul.d fa0, fa4, fa5
.LBB4_40:
ret
|
115
| 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
*/
|
riscv64
|
-O3
|
RISC-V 64 clang 21.1.0
|
bs:
mv a3, a0
addiw a0, a1, -1
blez a1, .LBB0_7
li a1, 0
j .LBB0_3
.LBB0_2:
blt a0, a1, .LBB0_7
.LBB0_3:
subw a4, a0, a1
srliw a4, a4, 1
addw a5, a4, a1
slli a5, a5, 2
add a5, a5, a3
lw a5, 0(a5)
add a4, a4, a1
blt a2, a5, .LBB0_5
blt a2, a5, .LBB0_2
j .LBB0_6
.LBB0_5:
addiw a0, a4, -1
blt a2, a5, .LBB0_2
.LBB0_6:
addiw a1, a4, 1
j .LBB0_2
.LBB0_7:
ret
.LCPI1_0:
.quad 0x3fe0000000000000
bs2:
addiw a7, a1, -1
mv a6, a7
blez a1, .LBB1_7
.LBB1_1:
li t1, 0
lw t0, 0(a2)
mv a6, a7
j .LBB1_3
.LBB1_2:
blt a6, t1, .LBB1_7
.LBB1_3:
subw t2, a6, t1
srliw t2, t2, 1
addw t3, t2, t1
slli t3, t3, 2
add t3, t3, a0
lw t3, 0(t3)
add t2, t2, t1
blt t0, t3, .LBB1_5
blt t0, t3, .LBB1_2
j .LBB1_6
.LBB1_5:
addiw a6, t2, -1
blt t0, t3, .LBB1_2
.LBB1_6:
addiw t1, t2, 1
j .LBB1_2
.LBB1_7:
bge a6, a4, .LBB1_10
beq a6, a7, .LBB1_14
slli a7, a6, 2
not a6, a6
add a7, a7, a0
mv a0, a2
addi a2, a7, 4
addw a7, a1, a6
addw a4, a4, a6
mv a1, a3
mv a3, a7
addiw a7, a1, -1
mv a6, a7
bgtz a1, .LBB1_1
j .LBB1_7
.LBB1_10:
slli a1, a4, 2
add a0, a0, a1
lw a1, 0(a0)
fcvt.d.w fa0, a1
beqz a5, .LBB1_17
bge a4, a6, .LBB1_13
addi a2, a0, 4
.LBB1_13:
lw a0, 0(a2)
.Lpcrel_hi0:
auipc a1, %pcrel_hi(.LCPI1_0)
fld fa5, %pcrel_lo(.Lpcrel_hi0)(a1)
j .LBB1_16
.LBB1_14:
sub a0, a4, a7
slli a0, a0, 2
add a2, a2, a0
lw a0, -4(a2)
fcvt.d.w fa0, a0
beqz a5, .LBB1_17
lw a0, 0(a2)
.Lpcrel_hi1:
auipc a1, %pcrel_hi(.LCPI1_0)
fld fa5, %pcrel_lo(.Lpcrel_hi1)(a1)
.LBB1_16:
fcvt.d.w fa4, a0
fadd.d fa4, fa0, fa4
fmul.d fa0, fa4, fa5
.LBB1_17:
ret
min:
blt a0, a1, .LBB2_2
mv a0, a1
.LBB2_2:
ret
max:
blt a1, a0, .LBB3_2
mv a0, a1
.LBB3_2:
ret
.LCPI4_0:
.quad 0x3fe0000000000000
findMedianSortedArrays:
add a6, a3, a1
addi a5, a6, -1
srliw a4, a5, 31
add a4, a4, a5
sraiw a7, a4, 1
andi a6, a6, 1
beqz a3, .LBB4_23
beqz a1, .LBB4_25
lw t2, 0(a2)
lw t1, 0(a0)
bge t2, t1, .LBB4_13
addiw t0, a3, -1
mv t2, t0
blez a3, .LBB4_10
.LBB4_4:
li a5, 0
mv t2, t0
j .LBB4_6
.LBB4_5:
blt t2, a5, .LBB4_10
.LBB4_6:
subw a4, t2, a5
srliw t3, a4, 1
addw a4, t3, a5
slli a4, a4, 2
add a4, a4, a2
lw a4, 0(a4)
add t3, t3, a5
blt t1, a4, .LBB4_8
blt t1, a4, .LBB4_5
j .LBB4_9
.LBB4_8:
addiw t2, t3, -1
blt t1, a4, .LBB4_5
.LBB4_9:
addiw a5, t3, 1
j .LBB4_5
.LBB4_10:
bge t2, a7, .LBB4_27
beq t2, t0, .LBB4_35
slli a4, t2, 2
add a4, a4, a2
mv a2, a0
addi a0, a4, 4
lw t1, 4(a4)
not a4, t2
addw a5, a3, a4
addw a7, a7, a4
mv a3, a1
mv a1, a5
addiw t0, a3, -1
mv t2, t0
bgtz a3, .LBB4_4
j .LBB4_10
.LBB4_13:
addiw t0, a1, -1
mv t1, t0
blez a1, .LBB4_20
li a4, 0
mv t1, t0
j .LBB4_16
.LBB4_15:
blt t1, a4, .LBB4_20
.LBB4_16:
subw a5, t1, a4
srliw t3, a5, 1
addw a5, t3, a4
slli a5, a5, 2
add a5, a5, a0
lw a5, 0(a5)
add t3, t3, a4
blt t2, a5, .LBB4_18
blt t2, a5, .LBB4_15
j .LBB4_19
.LBB4_18:
addiw t1, t3, -1
blt t2, a5, .LBB4_15
.LBB4_19:
addiw a4, t3, 1
j .LBB4_15
.LBB4_20:
bge t1, a7, .LBB4_31
beq t1, t0, .LBB4_37
slli a4, t1, 2
add a4, a4, a0
mv a0, a2
addi a2, a4, 4
lw t2, 4(a4)
not a4, t1
addw a5, a1, a4
addw a7, a7, a4
mv a1, a3
mv a3, a5
j .LBB4_13
.LBB4_23:
slli a7, a7, 2
add a0, a0, a7
lw a1, 0(a0)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
lw a0, 4(a0)
.Lpcrel_hi2:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi2)(a1)
j .LBB4_39
.LBB4_25:
slli a7, a7, 2
add a2, a2, a7
lw a0, 0(a2)
fcvt.d.w fa0, a0
bnez a6, .LBB4_40
lw a0, 4(a2)
.Lpcrel_hi3:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi3)(a1)
j .LBB4_39
.LBB4_27:
slli a1, a7, 2
add a2, a2, a1
lw a1, 0(a2)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
bge a7, t2, .LBB4_30
addi a0, a2, 4
.LBB4_30:
lw a0, 0(a0)
.Lpcrel_hi4:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi4)(a1)
j .LBB4_39
.LBB4_31:
slli a1, a7, 2
add a0, a0, a1
lw a1, 0(a0)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
bge a7, t1, .LBB4_34
addi a2, a0, 4
.LBB4_34:
lw a0, 0(a2)
.Lpcrel_hi6:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi6)(a1)
j .LBB4_39
.LBB4_35:
sub a1, a7, t0
slli a1, a1, 2
add a0, a0, a1
lw a1, -4(a0)
fcvt.d.w fa0, a1
bnez a6, .LBB4_40
lw a0, 0(a0)
.Lpcrel_hi5:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi5)(a1)
j .LBB4_39
.LBB4_37:
sub a0, a7, t0
slli a0, a0, 2
add a2, a2, a0
lw a0, -4(a2)
fcvt.d.w fa0, a0
bnez a6, .LBB4_40
lw a0, 0(a2)
.Lpcrel_hi7:
auipc a1, %pcrel_hi(.LCPI4_0)
fld fa5, %pcrel_lo(.Lpcrel_hi7)(a1)
.LBB4_39:
fcvt.d.w fa4, a0
fadd.d fa4, fa0, fa4
fmul.d fa0, fa4, fa5
.LBB4_40:
ret
|
116
| 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
*/
|
riscv64
|
-O0
|
RISC-V 64 gcc 15.2.0
|
bs:
addi sp,sp,-48
sd ra,40(sp)
sd s0,32(sp)
addi s0,sp,48
sd a0,-40(s0)
mv a5,a1
mv a4,a2
sw a5,-44(s0)
mv a5,a4
sw a5,-48(s0)
sw zero,-20(s0)
lw a5,-44(s0)
addiw a5,a5,-1
sw a5,-24(s0)
j .L2
.L4:
lw a5,-24(s0)
mv a4,a5
lw a5,-20(s0)
subw a5,a4,a5
sext.w a5,a5
srliw a4,a5,31
addw a5,a4,a5
sraiw a5,a5,1
sext.w a5,a5
lw a4,-20(s0)
addw a5,a4,a5
sw a5,-28(s0)
lw a5,-28(s0)
slli a5,a5,2
ld a4,-40(s0)
add a5,a4,a5
lw a5,0(a5)
lw a4,-48(s0)
sext.w a4,a4
bge a4,a5,.L3
lw a5,-28(s0)
addiw a5,a5,-1
sw a5,-24(s0)
j .L2
.L3:
lw a5,-28(s0)
addiw a5,a5,1
sw a5,-20(s0)
.L2:
lw a5,-20(s0)
mv a4,a5
lw a5,-24(s0)
sext.w a4,a4
sext.w a5,a5
ble a4,a5,.L4
lw a5,-24(s0)
mv a0,a5
ld ra,40(sp)
ld s0,32(sp)
addi sp,sp,48
jr ra
bs2:
addi sp,sp,-64
sd ra,56(sp)
sd s0,48(sp)
addi s0,sp,64
sd a0,-40(s0)
sd a2,-56(s0)
mv a2,a3
mv a3,a4
mv a4,a5
mv a5,a1
sw a5,-44(s0)
mv a5,a2
sw a5,-48(s0)
mv a5,a3
sw a5,-60(s0)
mv a5,a4
sw a5,-64(s0)
ld a5,-56(s0)
lw a4,0(a5)
lw a5,-44(s0)
mv a2,a4
mv a1,a5
ld a0,-40(s0)
call bs
mv a5,a0
sw a5,-28(s0)
lw a5,-28(s0)
mv a4,a5
lw a5,-60(s0)
sext.w a4,a4
sext.w a5,a5
blt a4,a5,.L7
lw a5,-60(s0)
slli a5,a5,2
ld a4,-40(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fsd fa5,-24(s0)
lw a5,-64(s0)
sext.w a5,a5
beq a5,zero,.L8
lw a5,-28(s0)
mv a4,a5
lw a5,-60(s0)
sext.w a4,a4
sext.w a5,a5
ble a4,a5,.L9
lw a5,-60(s0)
addi a5,a5,1
slli a5,a5,2
ld a4,-40(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fld fa4,-24(s0)
fadd.d fa5,fa4,fa5
fsd fa5,-24(s0)
j .L10
.L9:
ld a5,-56(s0)
lw a5,0(a5)
fcvt.d.w fa5,a5
fld fa4,-24(s0)
fadd.d fa5,fa4,fa5
fsd fa5,-24(s0)
.L10:
fld fa4,-24(s0)
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fdiv.d fa5,fa4,fa5
fsd fa5,-24(s0)
j .L8
.L7:
lw a5,-44(s0)
addiw a5,a5,-1
sext.w a5,a5
lw a4,-28(s0)
sext.w a4,a4
bne a4,a5,.L11
lw a5,-60(s0)
mv a4,a5
lw a5,-28(s0)
subw a5,a4,a5
sext.w a5,a5
slli a5,a5,2
addi a5,a5,-4
ld a4,-56(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fsd fa5,-24(s0)
lw a5,-64(s0)
sext.w a5,a5
beq a5,zero,.L8
lw a5,-60(s0)
mv a4,a5
lw a5,-28(s0)
subw a5,a4,a5
sext.w a5,a5
slli a5,a5,2
ld a4,-56(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fld fa4,-24(s0)
fadd.d fa5,fa4,fa5
fsd fa5,-24(s0)
fld fa4,-24(s0)
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fdiv.d fa5,fa4,fa5
fsd fa5,-24(s0)
j .L8
.L11:
lw a5,-28(s0)
addi a5,a5,1
slli a5,a5,2
ld a4,-40(s0)
add a2,a4,a5
lw a5,-44(s0)
mv a4,a5
lw a5,-28(s0)
subw a5,a4,a5
sext.w a5,a5
addiw a5,a5,-1
sext.w a3,a5
lw a5,-60(s0)
mv a4,a5
lw a5,-28(s0)
subw a5,a4,a5
sext.w a5,a5
addiw a5,a5,-1
sext.w a4,a5
lw a5,-64(s0)
lw a1,-48(s0)
ld a0,-56(s0)
call bs2
fsd fa0,-24(s0)
.L8:
fld fa5,-24(s0)
fmv.d fa0,fa5
ld ra,56(sp)
ld s0,48(sp)
addi sp,sp,64
jr ra
min:
addi sp,sp,-32
sd ra,24(sp)
sd s0,16(sp)
addi s0,sp,32
mv a5,a0
mv a4,a1
sw a5,-20(s0)
mv a5,a4
sw a5,-24(s0)
lw a5,-20(s0)
mv a2,a5
lw a5,-24(s0)
mv a3,a5
sext.w a4,a3
sext.w a5,a2
ble a4,a5,.L14
mv a3,a2
.L14:
sext.w a5,a3
mv a0,a5
ld ra,24(sp)
ld s0,16(sp)
addi sp,sp,32
jr ra
max:
addi sp,sp,-32
sd ra,24(sp)
sd s0,16(sp)
addi s0,sp,32
mv a5,a0
mv a4,a1
sw a5,-20(s0)
mv a5,a4
sw a5,-24(s0)
lw a5,-20(s0)
mv a2,a5
lw a5,-24(s0)
mv a3,a5
sext.w a4,a3
sext.w a5,a2
bge a4,a5,.L17
mv a3,a2
.L17:
sext.w a5,a3
mv a0,a5
ld ra,24(sp)
ld s0,16(sp)
addi sp,sp,32
jr ra
findMedianSortedArrays:
addi sp,sp,-64
sd ra,56(sp)
sd s0,48(sp)
addi s0,sp,64
sd a0,-40(s0)
mv a5,a1
sd a2,-56(s0)
mv a4,a3
sw a5,-44(s0)
mv a5,a4
sw a5,-48(s0)
lw a5,-44(s0)
mv a4,a5
lw a5,-48(s0)
addw a5,a4,a5
sext.w a5,a5
addiw a5,a5,-1
sext.w a5,a5
srliw a4,a5,31
addw a5,a4,a5
sraiw a5,a5,1
sw a5,-28(s0)
lw a5,-44(s0)
mv a4,a5
lw a5,-48(s0)
addw a5,a4,a5
sext.w a5,a5
andi a5,a5,1
sext.w a5,a5
andi a5,a5,1
xori a5,a5,1
andi a5,a5,0xff
sw a5,-32(s0)
lw a5,-48(s0)
sext.w a5,a5
bne a5,zero,.L20
lw a5,-28(s0)
slli a5,a5,2
ld a4,-40(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fsd fa5,-24(s0)
lw a5,-32(s0)
sext.w a5,a5
beq a5,zero,.L21
lw a5,-28(s0)
addi a5,a5,1
slli a5,a5,2
ld a4,-40(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fld fa4,-24(s0)
fadd.d fa5,fa4,fa5
fsd fa5,-24(s0)
fld fa4,-24(s0)
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fdiv.d fa5,fa4,fa5
fsd fa5,-24(s0)
.L21:
fld fa5,-24(s0)
j .L22
.L20:
lw a5,-44(s0)
sext.w a5,a5
bne a5,zero,.L23
lw a5,-28(s0)
slli a5,a5,2
ld a4,-56(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fsd fa5,-24(s0)
lw a5,-32(s0)
sext.w a5,a5
beq a5,zero,.L24
lw a5,-28(s0)
addi a5,a5,1
slli a5,a5,2
ld a4,-56(s0)
add a5,a4,a5
lw a5,0(a5)
fcvt.d.w fa5,a5
fld fa4,-24(s0)
fadd.d fa5,fa4,fa5
fsd fa5,-24(s0)
fld fa4,-24(s0)
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fdiv.d fa5,fa4,fa5
fsd fa5,-24(s0)
.L24:
fld fa5,-24(s0)
j .L22
.L23:
ld a5,-56(s0)
lw a4,0(a5)
ld a5,-40(s0)
lw a5,0(a5)
bge a4,a5,.L25
lw a5,-32(s0)
lw a4,-28(s0)
lw a3,-44(s0)
lw a1,-48(s0)
ld a2,-40(s0)
ld a0,-56(s0)
call bs2
fmv.d fa5,fa0
j .L22
.L25:
lw a5,-32(s0)
lw a4,-28(s0)
lw a3,-48(s0)
lw a1,-44(s0)
ld a2,-56(s0)
ld a0,-40(s0)
call bs2
fmv.d fa5,fa0
.L22:
fmv.d fa0,fa5
ld ra,56(sp)
ld s0,48(sp)
addi sp,sp,64
jr ra
.LC0:
.word 0
.word 1073741824
|
117
| 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
*/
|
riscv64
|
-O1
|
RISC-V 64 gcc 15.2.0
|
bs:
mv a6,a0
addiw a0,a1,-1
blt a0,zero,.L2
li a3,0
j .L5
.L3:
addiw a3,a5,1
.L4:
blt a0,a3,.L2
.L5:
subw a4,a0,a3
srliw a5,a4,31
addw a5,a5,a4
sraiw a5,a5,1
addw a5,a5,a3
slli a4,a5,2
add a4,a6,a4
lw a4,0(a4)
ble a4,a2,.L3
addiw a0,a5,-1
j .L4
.L2:
ret
bs2:
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)
sd s6,0(sp)
mv s1,a0
mv s3,a1
mv s4,a2
mv s5,a3
mv s0,a4
mv s2,a5
lw a4,0(a2)
mv s6,a4
mv a2,a4
call bs
blt a0,s0,.L8
slli a5,s0,2
add a4,s1,a5
lw a4,0(a4)
fcvt.d.w fa0,a4
beq s2,zero,.L7
ble a0,s0,.L10
add a5,s1,a5
lw a5,4(a5)
fcvt.d.w fa5,a5
fadd.d fa0,fa5,fa0
.L11:
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fmul.d fa0,fa0,fa5
.L7:
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)
ld s6,0(sp)
addi sp,sp,64
jr ra
.L10:
fcvt.d.w fa5,s6
fadd.d fa0,fa5,fa0
j .L11
.L8:
addiw a5,s3,-1
bne a5,a0,.L12
subw a0,s0,a0
slli a5,a0,2
addi a5,a5,-4
add a5,s4,a5
lw a5,0(a5)
fcvt.d.w fa0,a5
beq s2,zero,.L7
slli a0,a0,2
add a2,s4,a0
lw a5,0(a2)
fcvt.d.w fa5,a5
fadd.d fa0,fa5,fa0
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fmul.d fa0,fa0,fa5
j .L7
.L12:
subw a4,s0,a0
subw a3,s3,a0
slli a2,a0,2
addi a2,a2,4
mv a5,s2
addiw a4,a4,-1
addiw a3,a3,-1
add a2,s1,a2
mv a1,s5
mv a0,s4
call bs2
j .L7
min:
mv a5,a1
ble a1,a0,.L15
mv a5,a0
.L15:
sext.w a0,a5
ret
max:
mv a5,a1
bge a1,a0,.L17
mv a5,a0
.L17:
sext.w a0,a5
ret
findMedianSortedArrays:
mv a6,a3
addw a5,a1,a3
addiw a3,a5,-1
srliw a4,a3,31
addw a4,a4,a3
sraiw a4,a4,1
andi a5,a5,1
beq a6,zero,.L26
mv a7,a2
beq a1,zero,.L27
addi sp,sp,-16
sd ra,8(sp)
xori a5,a5,1
lw a2,0(a2)
lw a3,0(a0)
blt a2,a3,.L28
mv a3,a6
mv a2,a7
call bs2
.L18:
ld ra,8(sp)
addi sp,sp,16
jr ra
.L26:
slli a4,a4,2
add a3,a0,a4
lw a3,0(a3)
fcvt.d.w fa0,a3
bne a5,zero,.L24
add a0,a0,a4
lw a5,4(a0)
fcvt.d.w fa5,a5
fadd.d fa0,fa5,fa0
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fmul.d fa0,fa0,fa5
ret
.L27:
slli a4,a4,2
add a3,a2,a4
lw a3,0(a3)
fcvt.d.w fa0,a3
bne a5,zero,.L24
add a7,a2,a4
lw a5,4(a7)
fcvt.d.w fa5,a5
fadd.d fa0,fa5,fa0
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fmul.d fa0,fa0,fa5
ret
.L28:
mv a3,a1
mv a2,a0
mv a1,a6
mv a0,a7
call bs2
j .L18
.L24:
ret
.LC0:
.word 0
.word 1071644672
|
118
| 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
*/
|
riscv64
|
-O2
|
RISC-V 64 gcc 15.2.0
|
bs:
addiw a1,a1,-1
blt a1,zero,.L2
li a3,0
.L5:
subw a5,a1,a3
sraiw a5,a5,1
addw a5,a5,a3
slli a4,a5,2
add a4,a0,a4
lw a4,0(a4)
ble a4,a2,.L3
addiw a1,a5,-1
ble a3,a1,.L5
.L2:
mv a0,a1
ret
.L3:
addiw a3,a5,1
ble a3,a1,.L5
j .L2
bs2:
addiw t5,a1,-1
lw t4,0(a2)
mv t3,t5
blt t5,zero,.L9
.L28:
li t1,0
.L12:
subw a6,t3,t1
sraiw a6,a6,1
addw a6,a6,t1
slli a7,a6,2
add a7,a0,a7
lw a7,0(a7)
ble a7,t4,.L10
addiw t3,a6,-1
ble t1,t3,.L12
.L29:
ble a4,t3,.L19
subw a6,a4,t3
beq t5,t3,.L20
addiw a4,t3,1
slli a4,a4,2
subw t3,a1,t3
add a4,a0,a4
mv a1,a3
mv a0,a2
addiw t5,a1,-1
mv a2,a4
addiw a3,t3,-1
lw t4,0(a2)
addiw a4,a6,-1
mv t3,t5
bge t5,zero,.L28
.L9:
bgt a4,t5,.L20
.L19:
slli a3,a4,2
add a3,a0,a3
lw a2,0(a3)
fcvt.d.w fa0,a2
beq a5,zero,.L8
bge a4,t3,.L15
lw a5,4(a3)
fcvt.d.w fa5,a5
fadd.d fa0,fa5,fa0
.L16:
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fmul.d fa0,fa0,fa5
ret
.L10:
addiw t1,a6,1
ble t1,t3,.L12
j .L29
.L20:
subw a4,a4,t5
slli a3,a4,2
add a3,a2,a3
lw a3,-4(a3)
fcvt.d.w fa0,a3
bne a5,zero,.L30
.L8:
ret
.L30:
slli a4,a4,2
add a2,a2,a4
lw a5,0(a2)
lui a4,%hi(.LC0)
fld fa5,%lo(.LC0)(a4)
fcvt.d.w fa4,a5
fadd.d fa0,fa4,fa0
fmul.d fa0,fa0,fa5
ret
.L15:
fcvt.d.w fa5,t4
fadd.d fa0,fa5,fa0
j .L16
min:
mv a5,a1
ble a1,a0,.L32
mv a5,a0
.L32:
sext.w a0,a5
ret
max:
mv a5,a1
bge a1,a0,.L34
mv a5,a0
.L34:
sext.w a0,a5
ret
findMedianSortedArrays:
addw a5,a1,a3
addiw a7,a5,-1
srliw a4,a7,31
addw a4,a4,a7
mv a6,a3
andi a5,a5,1
sraiw a4,a4,1
mv t3,a2
beq a3,zero,.L41
beq a1,zero,.L42
lw t5,0(a2)
lw t4,0(a0)
xori a5,a5,1
blt t5,t4,.L43
tail bs2
.L42:
slli a4,a4,2
add t3,a2,a4
lw a4,0(t3)
fcvt.d.w fa0,a4
bne a5,zero,.L35
lw a5,4(t3)
lui a4,%hi(.LC0)
fld fa5,%lo(.LC0)(a4)
fcvt.d.w fa4,a5
fadd.d fa0,fa4,fa0
fmul.d fa0,fa0,fa5
ret
.L41:
slli a4,a4,2
add a7,a0,a4
lw a4,0(a7)
fcvt.d.w fa0,a4
beq a5,zero,.L44
.L35:
ret
.L44:
lw a5,4(a7)
lui a4,%hi(.LC0)
fld fa5,%lo(.LC0)(a4)
fcvt.d.w fa4,a5
fadd.d fa0,fa4,fa0
fmul.d fa0,fa0,fa5
ret
.L43:
mv a3,a1
mv a2,a0
mv a1,a6
mv a0,t3
tail bs2
.LC0:
.word 0
.word 1071644672
|
119
| 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
*/
|
riscv64
|
-O3
|
RISC-V 64 gcc 15.2.0
|
bs:
addiw a1,a1,-1
blt a1,zero,.L2
li a3,0
.L5:
subw a5,a1,a3
sraiw a5,a5,1
addw a5,a5,a3
slli a4,a5,2
add a4,a0,a4
lw a4,0(a4)
ble a4,a2,.L3
addiw a1,a5,-1
ble a3,a1,.L5
.L2:
mv a0,a1
ret
.L3:
addiw a3,a5,1
ble a3,a1,.L5
j .L2
bs2:
addiw t5,a1,-1
lw t4,0(a2)
mv t3,t5
blt t5,zero,.L9
.L28:
li t1,0
.L12:
subw a6,t3,t1
sraiw a6,a6,1
addw a6,a6,t1
slli a7,a6,2
add a7,a0,a7
lw a7,0(a7)
ble a7,t4,.L10
addiw t3,a6,-1
bge t3,t1,.L12
.L29:
ble a4,t3,.L19
subw a6,a4,t3
beq t5,t3,.L20
addiw a4,t3,1
slli a4,a4,2
subw t3,a1,t3
add a4,a0,a4
mv a1,a3
mv a0,a2
addiw t5,a1,-1
mv a2,a4
addiw a3,t3,-1
lw t4,0(a2)
addiw a4,a6,-1
mv t3,t5
bge t5,zero,.L28
.L9:
blt t5,a4,.L20
.L19:
slli a3,a4,2
add a3,a0,a3
lw a2,0(a3)
fcvt.d.w fa0,a2
beq a5,zero,.L8
bge a4,t3,.L15
lw a5,4(a3)
fcvt.d.w fa5,a5
fadd.d fa0,fa5,fa0
.L16:
lui a5,%hi(.LC0)
fld fa5,%lo(.LC0)(a5)
fmul.d fa0,fa0,fa5
ret
.L10:
addiw t1,a6,1
bge t3,t1,.L12
j .L29
.L20:
subw a4,a4,t5
slli a3,a4,2
add a3,a2,a3
lw a3,-4(a3)
fcvt.d.w fa0,a3
bne a5,zero,.L30
.L8:
ret
.L30:
slli a4,a4,2
add a2,a2,a4
lw a5,0(a2)
lui a4,%hi(.LC0)
fld fa5,%lo(.LC0)(a4)
fcvt.d.w fa4,a5
fadd.d fa0,fa4,fa0
fmul.d fa0,fa0,fa5
ret
.L15:
fcvt.d.w fa5,t4
fadd.d fa0,fa5,fa0
j .L16
min:
mv a5,a1
ble a1,a0,.L32
mv a5,a0
.L32:
sext.w a0,a5
ret
max:
mv a5,a1
bge a1,a0,.L34
mv a5,a0
.L34:
sext.w a0,a5
ret
findMedianSortedArrays:
addw a5,a1,a3
addiw a7,a5,-1
srliw a4,a7,31
addw a4,a4,a7
mv a6,a3
andi a5,a5,1
sraiw a4,a4,1
mv t3,a2
beq a3,zero,.L41
beq a1,zero,.L42
lw t5,0(a2)
lw t4,0(a0)
xori a5,a5,1
blt t5,t4,.L43
tail bs2
.L42:
slli a4,a4,2
add t3,a2,a4
lw a4,0(t3)
fcvt.d.w fa0,a4
bne a5,zero,.L35
lw a5,4(t3)
lui a4,%hi(.LC0)
fld fa5,%lo(.LC0)(a4)
fcvt.d.w fa4,a5
fadd.d fa0,fa4,fa0
fmul.d fa0,fa0,fa5
ret
.L41:
slli a4,a4,2
add a7,a0,a4
lw a4,0(a7)
fcvt.d.w fa0,a4
beq a5,zero,.L44
.L35:
ret
.L44:
lw a5,4(a7)
lui a4,%hi(.LC0)
fld fa5,%lo(.LC0)(a4)
fcvt.d.w fa4,a5
fadd.d fa0,fa4,fa0
fmul.d fa0,fa0,fa5
ret
.L43:
mv a3,a1
mv a2,a0
mv a1,a6
mv a0,t3
tail bs2
.LC0:
.word 0
.word 1071644672
|
120
| 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
*/
|
x86-64
|
-O0
|
x86-64 clang 21.1.0
|
bs:
push rbp
mov rbp, rsp
mov qword ptr [rbp - 8], rdi
mov dword ptr [rbp - 12], esi
mov dword ptr [rbp - 16], edx
mov dword ptr [rbp - 20], 0
mov eax, dword ptr [rbp - 12]
sub eax, 1
mov dword ptr [rbp - 24], eax
.LBB0_1:
mov eax, dword ptr [rbp - 20]
cmp eax, dword ptr [rbp - 24]
jg .LBB0_6
mov eax, dword ptr [rbp - 20]
mov dword ptr [rbp - 32], eax
mov eax, dword ptr [rbp - 24]
sub eax, dword ptr [rbp - 20]
mov ecx, 2
cdq
idiv ecx
mov ecx, eax
mov eax, dword ptr [rbp - 32]
add eax, ecx
mov dword ptr [rbp - 28], eax
mov rax, qword ptr [rbp - 8]
movsxd rcx, dword ptr [rbp - 28]
mov eax, dword ptr [rax + 4*rcx]
cmp eax, dword ptr [rbp - 16]
jle .LBB0_4
mov eax, dword ptr [rbp - 28]
sub eax, 1
mov dword ptr [rbp - 24], eax
jmp .LBB0_5
.LBB0_4:
mov eax, dword ptr [rbp - 28]
add eax, 1
mov dword ptr [rbp - 20], eax
.LBB0_5:
jmp .LBB0_1
.LBB0_6:
mov eax, dword ptr [rbp - 24]
pop rbp
ret
.LCPI1_0:
.quad 0x4000000000000000
bs2:
push rbp
mov rbp, rsp
sub rsp, 64
mov qword ptr [rbp - 8], rdi
mov dword ptr [rbp - 12], esi
mov qword ptr [rbp - 24], rdx
mov dword ptr [rbp - 28], ecx
mov dword ptr [rbp - 32], r8d
mov dword ptr [rbp - 36], r9d
mov rdi, qword ptr [rbp - 8]
mov esi, dword ptr [rbp - 12]
mov rax, qword ptr [rbp - 24]
mov edx, dword ptr [rax]
call bs
mov dword ptr [rbp - 52], eax
mov eax, dword ptr [rbp - 52]
cmp eax, dword ptr [rbp - 32]
jl .LBB1_7
mov rax, qword ptr [rbp - 8]
movsxd rcx, dword ptr [rbp - 32]
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
movsd qword ptr [rbp - 48], xmm0
cmp dword ptr [rbp - 36], 0
je .LBB1_6
mov eax, dword ptr [rbp - 52]
cmp eax, dword ptr [rbp - 32]
jle .LBB1_4
mov rax, qword ptr [rbp - 8]
mov ecx, dword ptr [rbp - 32]
add ecx, 1
movsxd rcx, ecx
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
addsd xmm0, qword ptr [rbp - 48]
movsd qword ptr [rbp - 48], xmm0
jmp .LBB1_5
.LBB1_4:
mov rax, qword ptr [rbp - 24]
cvtsi2sd xmm0, dword ptr [rax]
addsd xmm0, qword ptr [rbp - 48]
movsd qword ptr [rbp - 48], xmm0
.LBB1_5:
movsd xmm0, qword ptr [rbp - 48]
movsd xmm1, qword ptr [rip + .LCPI1_0]
divsd xmm0, xmm1
movsd qword ptr [rbp - 48], xmm0
.LBB1_6:
jmp .LBB1_13
.LBB1_7:
mov eax, dword ptr [rbp - 52]
mov ecx, dword ptr [rbp - 12]
sub ecx, 1
cmp eax, ecx
jne .LBB1_11
mov rax, qword ptr [rbp - 24]
mov ecx, dword ptr [rbp - 32]
sub ecx, dword ptr [rbp - 52]
sub ecx, 1
movsxd rcx, ecx
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
movsd qword ptr [rbp - 48], xmm0
cmp dword ptr [rbp - 36], 0
je .LBB1_10
mov rax, qword ptr [rbp - 24]
mov ecx, dword ptr [rbp - 32]
sub ecx, dword ptr [rbp - 52]
movsxd rcx, ecx
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
addsd xmm0, qword ptr [rbp - 48]
movsd qword ptr [rbp - 48], xmm0
movsd xmm0, qword ptr [rbp - 48]
movsd xmm1, qword ptr [rip + .LCPI1_0]
divsd xmm0, xmm1
movsd qword ptr [rbp - 48], xmm0
.LBB1_10:
jmp .LBB1_12
.LBB1_11:
mov rdi, qword ptr [rbp - 24]
mov esi, dword ptr [rbp - 28]
mov rdx, qword ptr [rbp - 8]
mov eax, dword ptr [rbp - 52]
add eax, 1
cdqe
shl rax, 2
add rdx, rax
mov ecx, dword ptr [rbp - 12]
sub ecx, dword ptr [rbp - 52]
sub ecx, 1
mov r8d, dword ptr [rbp - 32]
sub r8d, dword ptr [rbp - 52]
sub r8d, 1
mov r9d, dword ptr [rbp - 36]
call bs2
movsd qword ptr [rbp - 48], xmm0
.LBB1_12:
jmp .LBB1_13
.LBB1_13:
movsd xmm0, qword ptr [rbp - 48]
add rsp, 64
pop rbp
ret
min:
push rbp
mov rbp, rsp
mov dword ptr [rbp - 4], edi
mov dword ptr [rbp - 8], esi
mov eax, dword ptr [rbp - 4]
cmp eax, dword ptr [rbp - 8]
jge .LBB2_2
mov eax, dword ptr [rbp - 4]
mov dword ptr [rbp - 12], eax
jmp .LBB2_3
.LBB2_2:
mov eax, dword ptr [rbp - 8]
mov dword ptr [rbp - 12], eax
.LBB2_3:
mov eax, dword ptr [rbp - 12]
pop rbp
ret
max:
push rbp
mov rbp, rsp
mov dword ptr [rbp - 4], edi
mov dword ptr [rbp - 8], esi
mov eax, dword ptr [rbp - 4]
cmp eax, dword ptr [rbp - 8]
jle .LBB3_2
mov eax, dword ptr [rbp - 4]
mov dword ptr [rbp - 12], eax
jmp .LBB3_3
.LBB3_2:
mov eax, dword ptr [rbp - 8]
mov dword ptr [rbp - 12], eax
.LBB3_3:
mov eax, dword ptr [rbp - 12]
pop rbp
ret
.LCPI4_0:
.quad 0x4000000000000000
findMedianSortedArrays:
push rbp
mov rbp, rsp
sub rsp, 64
mov qword ptr [rbp - 16], rdi
mov dword ptr [rbp - 20], esi
mov qword ptr [rbp - 32], rdx
mov dword ptr [rbp - 36], ecx
mov eax, dword ptr [rbp - 20]
add eax, dword ptr [rbp - 36]
sub eax, 1
mov ecx, 2
cdq
idiv ecx
mov dword ptr [rbp - 52], eax
mov eax, dword ptr [rbp - 20]
add eax, dword ptr [rbp - 36]
mov ecx, 2
cdq
idiv ecx
mov eax, 1
xor ecx, ecx
cmp edx, 0
cmovne eax, ecx
mov dword ptr [rbp - 56], eax
cmp dword ptr [rbp - 36], 0
jne .LBB4_4
mov rax, qword ptr [rbp - 16]
movsxd rcx, dword ptr [rbp - 52]
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
movsd qword ptr [rbp - 48], xmm0
cmp dword ptr [rbp - 56], 0
je .LBB4_3
mov rax, qword ptr [rbp - 16]
mov ecx, dword ptr [rbp - 52]
add ecx, 1
movsxd rcx, ecx
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
addsd xmm0, qword ptr [rbp - 48]
movsd qword ptr [rbp - 48], xmm0
movsd xmm0, qword ptr [rbp - 48]
movsd xmm1, qword ptr [rip + .LCPI4_0]
divsd xmm0, xmm1
movsd qword ptr [rbp - 48], xmm0
.LBB4_3:
movsd xmm0, qword ptr [rbp - 48]
movsd qword ptr [rbp - 8], xmm0
jmp .LBB4_11
.LBB4_4:
cmp dword ptr [rbp - 20], 0
jne .LBB4_8
mov rax, qword ptr [rbp - 32]
movsxd rcx, dword ptr [rbp - 52]
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
movsd qword ptr [rbp - 48], xmm0
cmp dword ptr [rbp - 56], 0
je .LBB4_7
mov rax, qword ptr [rbp - 32]
mov ecx, dword ptr [rbp - 52]
add ecx, 1
movsxd rcx, ecx
cvtsi2sd xmm0, dword ptr [rax + 4*rcx]
addsd xmm0, qword ptr [rbp - 48]
movsd qword ptr [rbp - 48], xmm0
movsd xmm0, qword ptr [rbp - 48]
movsd xmm1, qword ptr [rip + .LCPI4_0]
divsd xmm0, xmm1
movsd qword ptr [rbp - 48], xmm0
.LBB4_7:
movsd xmm0, qword ptr [rbp - 48]
movsd qword ptr [rbp - 8], xmm0
jmp .LBB4_11
.LBB4_8:
mov rax, qword ptr [rbp - 32]
mov eax, dword ptr [rax]
mov rcx, qword ptr [rbp - 16]
cmp eax, dword ptr [rcx]
jge .LBB4_10
mov rdi, qword ptr [rbp - 32]
mov esi, dword ptr [rbp - 36]
mov rdx, qword ptr [rbp - 16]
mov ecx, dword ptr [rbp - 20]
mov r8d, dword ptr [rbp - 52]
mov r9d, dword ptr [rbp - 56]
call bs2
movsd qword ptr [rbp - 8], xmm0
jmp .LBB4_11
.LBB4_10:
mov rdi, qword ptr [rbp - 16]
mov esi, dword ptr [rbp - 20]
mov rdx, qword ptr [rbp - 32]
mov ecx, dword ptr [rbp - 36]
mov r8d, dword ptr [rbp - 52]
mov r9d, dword ptr [rbp - 56]
call bs2
movsd qword ptr [rbp - 8], xmm0
.LBB4_11:
movsd xmm0, qword ptr [rbp - 8]
add rsp, 64
pop rbp
ret
|
121
| 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
*/
|
x86-64
|
-O1
|
x86-64 clang 21.1.0
|
bs:
lea eax, [rsi - 1]
test esi, esi
jle .LBB0_6
xor ecx, ecx
jmp .LBB0_2
.LBB0_4:
add ecx, esi
inc ecx
cmp ecx, eax
jg .LBB0_6
.LBB0_2:
mov esi, eax
sub esi, ecx
shr esi
lea r8d, [rsi + rcx]
cmp dword ptr [rdi + 4*r8], edx
jle .LBB0_4
lea eax, [rsi + rcx]
dec eax
cmp ecx, eax
jle .LBB0_2
.LBB0_6:
ret
.LCPI1_0:
.quad 0x3fe0000000000000
bs2:
push r15
push r14
push rbx
.LBB1_1:
lea r10d, [rsi - 1]
mov eax, r10d
test esi, esi
jle .LBB1_7
mov r11d, dword ptr [rdx]
xor ebx, ebx
mov eax, r10d
jmp .LBB1_3
.LBB1_5:
add ebx, r14d
inc ebx
cmp ebx, eax
jg .LBB1_7
.LBB1_3:
mov r14d, eax
sub r14d, ebx
shr r14d
lea r15d, [r14 + rbx]
cmp dword ptr [rdi + 4*r15], r11d
jle .LBB1_5
lea eax, [r14 + rbx]
dec eax
cmp ebx, eax
jle .LBB1_3
.LBB1_7:
mov r11d, r8d
sub r11d, eax
jle .LBB1_8
cmp eax, r10d
je .LBB1_11
movsxd r10, eax
lea r10, [rdi + 4*r10]
add r10, 4
not eax
mov r11d, esi
add r11d, eax
add r8d, eax
mov rdi, rdx
mov esi, ecx
mov rdx, r10
mov ecx, r11d
jmp .LBB1_1
.LBB1_8:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx]
test r9d, r9d
je .LBB1_14
lea rcx, [rdi + 4*rcx]
add rcx, 4
cmp r8d, eax
cmovl rdx, rcx
cvtsi2sd xmm1, dword ptr [rdx]
jmp .LBB1_13
.LBB1_11:
movsxd rax, r11d
cvtsi2sd xmm0, dword ptr [rdx + 4*rax - 4]
test r9d, r9d
je .LBB1_14
cvtsi2sd xmm1, dword ptr [rdx + 4*rax]
.LBB1_13:
addsd xmm0, xmm1
mulsd xmm0, qword ptr [rip + .LCPI1_0]
.LBB1_14:
pop rbx
pop r14
pop r15
ret
min:
mov eax, esi
cmp edi, esi
cmovl eax, edi
ret
max:
mov eax, esi
cmp edi, esi
cmovg eax, edi
ret
.LCPI4_0:
.quad 0x3fe0000000000000
findMedianSortedArrays:
mov eax, esi
lea r9d, [rcx + rax]
lea esi, [rcx + rax]
dec esi
shr esi, 31
lea r8d, [rsi + r9]
dec r8d
sar r8d
and r9d, 1
test ecx, ecx
je .LBB4_1
test eax, eax
je .LBB4_6
xor r9d, 1
mov esi, dword ptr [rdx]
cmp esi, dword ptr [rdi]
jge .LBB4_9
mov r10, rdi
mov rdi, rdx
mov esi, ecx
mov rdx, r10
mov ecx, eax
jmp bs2
.LBB4_1:
movsxd rax, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rax]
test r9d, r9d
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdi + 4*rax + 4]
jmp .LBB4_3
.LBB4_6:
movsxd rax, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rax]
test r9d, r9d
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdx + 4*rax + 4]
.LBB4_3:
addsd xmm0, xmm1
mulsd xmm0, qword ptr [rip + .LCPI4_0]
.LBB4_4:
ret
.LBB4_9:
mov esi, eax
jmp bs2
|
122
| 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
*/
|
x86-64
|
-O2
|
x86-64 clang 21.1.0
|
bs:
lea eax, [rsi - 1]
test esi, esi
jle .LBB0_6
xor ecx, ecx
jmp .LBB0_2
.LBB0_4:
add ecx, esi
inc ecx
cmp ecx, eax
jg .LBB0_6
.LBB0_2:
mov esi, eax
sub esi, ecx
shr esi
lea r8d, [rsi + rcx]
cmp dword ptr [rdi + 4*r8], edx
jle .LBB0_4
lea eax, [rsi + rcx]
dec eax
cmp ecx, eax
jle .LBB0_2
.LBB0_6:
ret
.LCPI1_0:
.quad 0x3fe0000000000000
bs2:
push r15
push r14
push rbx
.LBB1_1:
lea r10d, [rsi - 1]
mov eax, r10d
test esi, esi
jle .LBB1_7
mov r11d, dword ptr [rdx]
xor ebx, ebx
mov eax, r10d
jmp .LBB1_3
.LBB1_5:
add ebx, r14d
inc ebx
cmp ebx, eax
jg .LBB1_7
.LBB1_3:
mov r14d, eax
sub r14d, ebx
shr r14d
lea r15d, [r14 + rbx]
cmp dword ptr [rdi + 4*r15], r11d
jle .LBB1_5
lea eax, [r14 + rbx]
dec eax
cmp ebx, eax
jle .LBB1_3
.LBB1_7:
cmp eax, r8d
jge .LBB1_8
cmp eax, r10d
je .LBB1_11
movsxd r10, eax
lea r10, [rdi + 4*r10]
add r10, 4
not eax
mov r11d, esi
add r11d, eax
add r8d, eax
mov rdi, rdx
mov esi, ecx
mov rdx, r10
mov ecx, r11d
jmp .LBB1_1
.LBB1_8:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx]
test r9d, r9d
je .LBB1_14
lea rcx, [rdi + 4*rcx]
add rcx, 4
cmp eax, r8d
cmovg rdx, rcx
cvtsi2sd xmm1, dword ptr [rdx]
jmp .LBB1_13
.LBB1_11:
sub r8d, r10d
movsxd rax, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rax - 4]
test r9d, r9d
je .LBB1_14
cvtsi2sd xmm1, dword ptr [rdx + 4*rax]
.LBB1_13:
addsd xmm0, xmm1
mulsd xmm0, qword ptr [rip + .LCPI1_0]
.LBB1_14:
pop rbx
pop r14
pop r15
ret
min:
mov eax, esi
cmp edi, esi
cmovl eax, edi
ret
max:
mov eax, esi
cmp edi, esi
cmovg eax, edi
ret
.LCPI4_0:
.quad 0x3fe0000000000000
findMedianSortedArrays:
push r15
push r14
push rbx
lea eax, [rcx + rsi]
lea r8d, [rcx + rsi]
dec r8d
shr r8d, 31
add r8d, eax
dec r8d
sar r8d
test ecx, ecx
je .LBB4_1
test esi, esi
je .LBB4_6
mov r9d, dword ptr [rdx]
mov r10d, dword ptr [rdi]
cmp r9d, r10d
jge .LBB4_23
.LBB4_9:
lea r11d, [rcx - 1]
mov r9d, r11d
test ecx, ecx
jle .LBB4_15
xor ebx, ebx
mov r9d, r11d
jmp .LBB4_11
.LBB4_13:
add ebx, r14d
inc ebx
cmp ebx, r9d
jg .LBB4_15
.LBB4_11:
mov r14d, r9d
sub r14d, ebx
shr r14d
lea r15d, [r14 + rbx]
cmp dword ptr [rdx + 4*r15], r10d
jle .LBB4_13
lea r9d, [r14 + rbx]
dec r9d
cmp ebx, r9d
jle .LBB4_11
.LBB4_15:
cmp r9d, r8d
jge .LBB4_16
cmp r9d, r11d
je .LBB4_19
movsxd r10, r9d
lea r11, [rdx + 4*r10]
add r11, 4
not r9d
mov ebx, ecx
add ebx, r9d
add r8d, r9d
mov r10d, dword ptr [rdx + 4*r10 + 4]
mov rdx, rdi
mov ecx, esi
mov rdi, r11
mov esi, ebx
jmp .LBB4_9
.LBB4_23:
lea r11d, [rsi - 1]
mov r10d, r11d
test esi, esi
jle .LBB4_29
xor ebx, ebx
mov r10d, r11d
jmp .LBB4_25
.LBB4_27:
add ebx, r14d
inc ebx
cmp ebx, r10d
jg .LBB4_29
.LBB4_25:
mov r14d, r10d
sub r14d, ebx
shr r14d
lea r15d, [r14 + rbx]
cmp dword ptr [rdi + 4*r15], r9d
jle .LBB4_27
lea r10d, [r14 + rbx]
dec r10d
cmp ebx, r10d
jle .LBB4_25
.LBB4_29:
cmp r10d, r8d
jge .LBB4_30
cmp r10d, r11d
je .LBB4_33
movsxd r9, r10d
lea r11, [rdi + 4*r9]
add r11, 4
not r10d
mov ebx, esi
add ebx, r10d
add r8d, r10d
mov r9d, dword ptr [rdi + 4*r9 + 4]
mov rdi, rdx
mov esi, ecx
mov rdx, r11
mov ecx, ebx
jmp .LBB4_23
.LBB4_1:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdi + 4*rcx + 4]
jmp .LBB4_3
.LBB4_6:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rcx]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdx + 4*rcx + 4]
jmp .LBB4_3
.LBB4_16:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rcx]
test al, 1
jne .LBB4_4
lea rax, [rdx + 4*rcx]
add rax, 4
cmp r9d, r8d
cmovg rdi, rax
cvtsi2sd xmm1, dword ptr [rdi]
jmp .LBB4_3
.LBB4_30:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx]
test al, 1
jne .LBB4_4
lea rax, [rdi + 4*rcx]
add rax, 4
cmp r10d, r8d
cmovg rdx, rax
cvtsi2sd xmm1, dword ptr [rdx]
jmp .LBB4_3
.LBB4_19:
sub r8d, r11d
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx - 4]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdi + 4*rcx]
jmp .LBB4_3
.LBB4_33:
sub r8d, r11d
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rcx - 4]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdx + 4*rcx]
.LBB4_3:
addsd xmm0, xmm1
mulsd xmm0, qword ptr [rip + .LCPI4_0]
.LBB4_4:
pop rbx
pop r14
pop r15
ret
|
123
| 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
*/
|
x86-64
|
-O3
|
x86-64 clang 21.1.0
|
bs:
lea eax, [rsi - 1]
test esi, esi
jle .LBB0_6
xor ecx, ecx
jmp .LBB0_2
.LBB0_4:
add ecx, esi
inc ecx
cmp ecx, eax
jg .LBB0_6
.LBB0_2:
mov esi, eax
sub esi, ecx
shr esi
lea r8d, [rsi + rcx]
cmp dword ptr [rdi + 4*r8], edx
jle .LBB0_4
lea eax, [rsi + rcx]
dec eax
cmp ecx, eax
jle .LBB0_2
.LBB0_6:
ret
.LCPI1_0:
.quad 0x3fe0000000000000
bs2:
push r15
push r14
push rbx
lea r10d, [rsi - 1]
mov eax, r10d
test esi, esi
jle .LBB1_7
.LBB1_2:
mov r11d, dword ptr [rdx]
xor ebx, ebx
mov eax, r10d
jmp .LBB1_3
.LBB1_5:
add ebx, r14d
inc ebx
cmp ebx, eax
jg .LBB1_7
.LBB1_3:
mov r14d, eax
sub r14d, ebx
shr r14d
lea r15d, [r14 + rbx]
cmp dword ptr [rdi + 4*r15], r11d
jle .LBB1_5
lea eax, [r14 + rbx]
dec eax
cmp ebx, eax
jle .LBB1_3
.LBB1_7:
cmp eax, r8d
jge .LBB1_8
cmp eax, r10d
je .LBB1_11
movsxd r10, eax
lea r10, [rdi + 4*r10]
add r10, 4
not eax
mov r11d, esi
add r11d, eax
add r8d, eax
mov rdi, rdx
mov esi, ecx
mov rdx, r10
mov ecx, r11d
lea r10d, [rsi - 1]
mov eax, r10d
test esi, esi
jg .LBB1_2
jmp .LBB1_7
.LBB1_8:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx]
test r9d, r9d
je .LBB1_14
lea rcx, [rdi + 4*rcx]
add rcx, 4
cmp eax, r8d
cmovg rdx, rcx
cvtsi2sd xmm1, dword ptr [rdx]
jmp .LBB1_13
.LBB1_11:
sub r8d, r10d
movsxd rax, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rax - 4]
test r9d, r9d
je .LBB1_14
cvtsi2sd xmm1, dword ptr [rdx + 4*rax]
.LBB1_13:
addsd xmm0, xmm1
mulsd xmm0, qword ptr [rip + .LCPI1_0]
.LBB1_14:
pop rbx
pop r14
pop r15
ret
min:
mov eax, esi
cmp edi, esi
cmovl eax, edi
ret
max:
mov eax, esi
cmp edi, esi
cmovg eax, edi
ret
.LCPI4_0:
.quad 0x3fe0000000000000
findMedianSortedArrays:
push r15
push r14
push rbx
lea eax, [rcx + rsi]
lea r8d, [rcx + rsi]
dec r8d
shr r8d, 31
add r8d, eax
dec r8d
sar r8d
test ecx, ecx
je .LBB4_1
test esi, esi
je .LBB4_6
mov r9d, dword ptr [rdx]
mov r10d, dword ptr [rdi]
cmp r9d, r10d
jge .LBB4_23
lea r11d, [rcx - 1]
mov r9d, r11d
test ecx, ecx
jle .LBB4_15
.LBB4_10:
xor ebx, ebx
mov r9d, r11d
jmp .LBB4_11
.LBB4_13:
add ebx, r14d
inc ebx
cmp ebx, r9d
jg .LBB4_15
.LBB4_11:
mov r14d, r9d
sub r14d, ebx
shr r14d
lea r15d, [r14 + rbx]
cmp dword ptr [rdx + 4*r15], r10d
jle .LBB4_13
lea r9d, [r14 + rbx]
dec r9d
cmp ebx, r9d
jle .LBB4_11
.LBB4_15:
cmp r9d, r8d
jge .LBB4_16
cmp r9d, r11d
je .LBB4_19
movsxd r10, r9d
lea r11, [rdx + 4*r10]
add r11, 4
not r9d
mov ebx, ecx
add ebx, r9d
add r8d, r9d
mov r10d, dword ptr [rdx + 4*r10 + 4]
mov rdx, rdi
mov ecx, esi
mov rdi, r11
mov esi, ebx
lea r11d, [rcx - 1]
mov r9d, r11d
test ecx, ecx
jg .LBB4_10
jmp .LBB4_15
.LBB4_23:
lea r11d, [rsi - 1]
mov r10d, r11d
test esi, esi
jle .LBB4_29
xor ebx, ebx
mov r10d, r11d
jmp .LBB4_25
.LBB4_27:
add ebx, r14d
inc ebx
cmp ebx, r10d
jg .LBB4_29
.LBB4_25:
mov r14d, r10d
sub r14d, ebx
shr r14d
lea r15d, [r14 + rbx]
cmp dword ptr [rdi + 4*r15], r9d
jle .LBB4_27
lea r10d, [r14 + rbx]
dec r10d
cmp ebx, r10d
jle .LBB4_25
.LBB4_29:
cmp r10d, r8d
jge .LBB4_30
cmp r10d, r11d
je .LBB4_33
movsxd r9, r10d
lea r11, [rdi + 4*r9]
add r11, 4
not r10d
mov ebx, esi
add ebx, r10d
add r8d, r10d
mov r9d, dword ptr [rdi + 4*r9 + 4]
mov rdi, rdx
mov esi, ecx
mov rdx, r11
mov ecx, ebx
jmp .LBB4_23
.LBB4_1:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdi + 4*rcx + 4]
jmp .LBB4_3
.LBB4_6:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rcx]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdx + 4*rcx + 4]
jmp .LBB4_3
.LBB4_16:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rcx]
test al, 1
jne .LBB4_4
lea rax, [rdx + 4*rcx]
add rax, 4
cmp r9d, r8d
cmovg rdi, rax
cvtsi2sd xmm1, dword ptr [rdi]
jmp .LBB4_3
.LBB4_30:
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx]
test al, 1
jne .LBB4_4
lea rax, [rdi + 4*rcx]
add rax, 4
cmp r10d, r8d
cmovg rdx, rax
cvtsi2sd xmm1, dword ptr [rdx]
jmp .LBB4_3
.LBB4_19:
sub r8d, r11d
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdi + 4*rcx - 4]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdi + 4*rcx]
jmp .LBB4_3
.LBB4_33:
sub r8d, r11d
movsxd rcx, r8d
cvtsi2sd xmm0, dword ptr [rdx + 4*rcx - 4]
test al, 1
jne .LBB4_4
cvtsi2sd xmm1, dword ptr [rdx + 4*rcx]
.LBB4_3:
addsd xmm0, xmm1
mulsd xmm0, qword ptr [rip + .LCPI4_0]
.LBB4_4:
pop rbx
pop r14
pop r15
ret
|
124
| 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
*/
|
x86-64
|
-O0
|
x86-64 gcc 15.2
|
bs:
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov DWORD PTR [rbp-32], edx
mov DWORD PTR [rbp-4], 0
mov eax, DWORD PTR [rbp-28]
sub eax, 1
mov DWORD PTR [rbp-8], eax
jmp .L2
.L4:
mov eax, DWORD PTR [rbp-8]
sub eax, DWORD PTR [rbp-4]
mov edx, eax
shr edx, 31
add eax, edx
sar eax
mov edx, eax
mov eax, DWORD PTR [rbp-4]
add eax, edx
mov DWORD PTR [rbp-12], eax
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
cmp DWORD PTR [rbp-32], eax
jge .L3
mov eax, DWORD PTR [rbp-12]
sub eax, 1
mov DWORD PTR [rbp-8], eax
jmp .L2
.L3:
mov eax, DWORD PTR [rbp-12]
add eax, 1
mov DWORD PTR [rbp-4], eax
.L2:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-8]
jle .L4
mov eax, DWORD PTR [rbp-8]
pop rbp
ret
bs2:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov QWORD PTR [rbp-40], rdx
mov DWORD PTR [rbp-32], ecx
mov DWORD PTR [rbp-44], r8d
mov DWORD PTR [rbp-48], r9d
mov rax, QWORD PTR [rbp-40]
mov edx, DWORD PTR [rax]
mov ecx, DWORD PTR [rbp-28]
mov rax, QWORD PTR [rbp-24]
mov esi, ecx
mov rdi, rax
call bs
mov DWORD PTR [rbp-12], eax
mov eax, DWORD PTR [rbp-12]
cmp eax, DWORD PTR [rbp-44]
jl .L7
mov eax, DWORD PTR [rbp-44]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd QWORD PTR [rbp-8], xmm0
cmp DWORD PTR [rbp-48], 0
je .L8
mov eax, DWORD PTR [rbp-12]
cmp eax, DWORD PTR [rbp-44]
jle .L9
mov eax, DWORD PTR [rbp-44]
cdqe
add rax, 1
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd xmm1, QWORD PTR [rbp-8]
addsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
jmp .L10
.L9:
mov rax, QWORD PTR [rbp-40]
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd xmm1, QWORD PTR [rbp-8]
addsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
.L10:
movsd xmm0, QWORD PTR [rbp-8]
movsd xmm1, QWORD PTR .LC0[rip]
divsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
jmp .L8
.L7:
mov eax, DWORD PTR [rbp-28]
sub eax, 1
cmp DWORD PTR [rbp-12], eax
jne .L11
mov eax, DWORD PTR [rbp-44]
sub eax, DWORD PTR [rbp-12]
cdqe
sal rax, 2
lea rdx, [rax-4]
mov rax, QWORD PTR [rbp-40]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd QWORD PTR [rbp-8], xmm0
cmp DWORD PTR [rbp-48], 0
je .L8
mov eax, DWORD PTR [rbp-44]
sub eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-40]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd xmm1, QWORD PTR [rbp-8]
addsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
movsd xmm0, QWORD PTR [rbp-8]
movsd xmm1, QWORD PTR .LC0[rip]
divsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
jmp .L8
.L11:
mov eax, DWORD PTR [rbp-44]
sub eax, DWORD PTR [rbp-12]
lea r8d, [rax-1]
mov eax, DWORD PTR [rbp-28]
sub eax, DWORD PTR [rbp-12]
lea ecx, [rax-1]
mov eax, DWORD PTR [rbp-12]
cdqe
add rax, 1
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rdx, rax
mov edi, DWORD PTR [rbp-48]
mov esi, DWORD PTR [rbp-32]
mov rax, QWORD PTR [rbp-40]
mov r9d, edi
mov rdi, rax
call bs2
movq rax, xmm0
mov QWORD PTR [rbp-8], rax
.L8:
movsd xmm0, QWORD PTR [rbp-8]
leave
ret
min:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-4]
cmp edx, eax
cmovle eax, edx
pop rbp
ret
max:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-4]
cmp edx, eax
cmovge eax, edx
pop rbp
ret
findMedianSortedArrays:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov QWORD PTR [rbp-40], rdx
mov DWORD PTR [rbp-32], ecx
mov edx, DWORD PTR [rbp-28]
mov eax, DWORD PTR [rbp-32]
add eax, edx
sub eax, 1
mov edx, eax
shr edx, 31
add eax, edx
sar eax
mov DWORD PTR [rbp-12], eax
mov edx, DWORD PTR [rbp-28]
mov eax, DWORD PTR [rbp-32]
add eax, edx
and eax, 1
and eax, 1
xor eax, 1
movzx eax, al
mov DWORD PTR [rbp-16], eax
cmp DWORD PTR [rbp-32], 0
jne .L18
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd QWORD PTR [rbp-8], xmm0
cmp DWORD PTR [rbp-16], 0
je .L19
mov eax, DWORD PTR [rbp-12]
cdqe
add rax, 1
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd xmm1, QWORD PTR [rbp-8]
addsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
movsd xmm0, QWORD PTR [rbp-8]
movsd xmm1, QWORD PTR .LC0[rip]
divsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
.L19:
mov rax, QWORD PTR [rbp-8]
jmp .L20
.L18:
cmp DWORD PTR [rbp-28], 0
jne .L21
mov eax, DWORD PTR [rbp-12]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-40]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd QWORD PTR [rbp-8], xmm0
cmp DWORD PTR [rbp-16], 0
je .L22
mov eax, DWORD PTR [rbp-12]
cdqe
add rax, 1
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-40]
add rax, rdx
mov eax, DWORD PTR [rax]
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd xmm1, QWORD PTR [rbp-8]
addsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
movsd xmm0, QWORD PTR [rbp-8]
movsd xmm1, QWORD PTR .LC0[rip]
divsd xmm0, xmm1
movsd QWORD PTR [rbp-8], xmm0
.L22:
mov rax, QWORD PTR [rbp-8]
jmp .L20
.L21:
mov rax, QWORD PTR [rbp-40]
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR [rbp-24]
mov eax, DWORD PTR [rax]
cmp edx, eax
jge .L23
mov r8d, DWORD PTR [rbp-16]
mov edi, DWORD PTR [rbp-12]
mov ecx, DWORD PTR [rbp-28]
mov rdx, QWORD PTR [rbp-24]
mov esi, DWORD PTR [rbp-32]
mov rax, QWORD PTR [rbp-40]
mov r9d, r8d
mov r8d, edi
mov rdi, rax
call bs2
movq rax, xmm0
jmp .L20
.L23:
mov r8d, DWORD PTR [rbp-16]
mov edi, DWORD PTR [rbp-12]
mov ecx, DWORD PTR [rbp-32]
mov rdx, QWORD PTR [rbp-40]
mov esi, DWORD PTR [rbp-28]
mov rax, QWORD PTR [rbp-24]
mov r9d, r8d
mov r8d, edi
mov rdi, rax
call bs2
movq rax, xmm0
.L20:
movq xmm0, rax
leave
ret
.LC0:
.long 0
.long 1073741824
|
125
| 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
*/
|
x86-64
|
-O1
|
x86-64 gcc 15.2
|
bs:
mov eax, esi
sub eax, 1
js .L1
mov r8d, 0
jmp .L5
.L3:
lea r8d, [rcx+1]
.L4:
cmp eax, r8d
jl .L1
.L5:
mov esi, eax
sub esi, r8d
mov ecx, esi
shr ecx, 31
add ecx, esi
sar ecx
add ecx, r8d
movsx rsi, ecx
cmp DWORD PTR [rdi+rsi*4], edx
jle .L3
lea eax, [rcx-1]
jmp .L4
.L1:
ret
bs2:
push r15
push r14
push r13
push r12
push rbp
push rbx
sub rsp, 24
mov r12, rdi
mov ebp, esi
mov r13, rdx
mov DWORD PTR [rsp+12], ecx
mov ebx, r8d
mov r15d, r9d
mov r14d, DWORD PTR [rdx]
mov edx, r14d
call bs
cmp eax, ebx
jl .L8
movsx rdx, ebx
lea rcx, [0+rdx*4]
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [r12+rdx*4]
test r15d, r15d
je .L7
cmp eax, ebx
jle .L10
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [r12+4+rcx]
addsd xmm0, xmm1
.L11:
mulsd xmm0, QWORD PTR .LC0[rip]
.L7:
add rsp, 24
pop rbx
pop rbp
pop r12
pop r13
pop r14
pop r15
ret
.L10:
pxor xmm1, xmm1
cvtsi2sd xmm1, r14d
addsd xmm0, xmm1
jmp .L11
.L8:
lea edx, [rbp-1]
cmp edx, eax
jne .L12
sub ebx, eax
movsx rbx, ebx
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [r13-4+rbx*4]
test r15d, r15d
je .L7
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [r13+0+rbx*4]
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
jmp .L7
.L12:
sub ebx, eax
sub ebp, eax
lea ecx, [rbp-1]
cdqe
lea rdx, [r12+4+rax*4]
mov r9d, r15d
lea r8d, [rbx-1]
mov esi, DWORD PTR [rsp+12]
mov rdi, r13
call bs2
jmp .L7
min:
cmp esi, edi
mov eax, edi
cmovle eax, esi
ret
max:
cmp esi, edi
mov eax, edi
cmovge eax, esi
ret
findMedianSortedArrays:
mov r10d, esi
mov rax, rdx
lea r9d, [rsi+rcx]
lea edx, [r9-1]
mov r8d, edx
shr r8d, 31
add r8d, edx
sar r8d
and r9d, 1
test ecx, ecx
je .L24
mov esi, ecx
test r10d, r10d
je .L25
sub rsp, 8
xor r9d, 1
mov ecx, DWORD PTR [rdi]
cmp DWORD PTR [rax], ecx
jl .L26
mov ecx, esi
mov rdx, rax
mov esi, r10d
call bs2
.L16:
add rsp, 8
ret
.L24:
movsx r8, r8d
lea rax, [0+r8*4]
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [rdi+r8*4]
test r9d, r9d
jne .L22
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rdi+4+rax]
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
ret
.L25:
movsx r8, r8d
lea rdx, [0+r8*4]
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [rax+r8*4]
test r9d, r9d
jne .L22
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rax+4+rdx]
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
ret
.L26:
mov ecx, r10d
mov rdx, rdi
mov rdi, rax
call bs2
jmp .L16
.L22:
ret
.LC0:
.long 0
.long 1071644672
|
126
| 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
*/
|
x86-64
|
-O2
|
x86-64 gcc 15.2
|
bs:
mov eax, esi
mov r8d, edx
sub eax, 1
js .L1
xor ecx, ecx
jmp .L5
.L8:
lea eax, [rdx-1]
cmp ecx, eax
jg .L1
.L5:
mov edx, eax
sub edx, ecx
sar edx
add edx, ecx
movsx rsi, edx
cmp r8d, DWORD PTR [rdi+rsi*4]
jl .L8
lea ecx, [rdx+1]
cmp ecx, eax
jle .L5
.L1:
ret
bs2:
mov r11d, esi
push r12
mov r10, rdi
push rbp
mov r12d, r11d
mov ebp, ecx
push rbx
mov rbx, rdx
mov edi, DWORD PTR [rbx]
sub r12d, 1
js .L10
.L33:
mov ecx, r12d
xor edx, edx
jmp .L13
.L31:
lea ecx, [rax-1]
cmp edx, ecx
jg .L30
.L13:
mov eax, ecx
sub eax, edx
sar eax
add eax, edx
movsx rsi, eax
cmp edi, DWORD PTR [r10+rsi*4]
jl .L31
lea edx, [rax+1]
cmp edx, ecx
jle .L13
.L30:
cmp r8d, ecx
jle .L32
mov eax, r8d
sub eax, ecx
cmp r12d, ecx
je .L21
mov edx, r11d
lea r8d, [rax-1]
mov r11d, ebp
sub edx, ecx
add ecx, 1
mov r12d, r11d
movsx rcx, ecx
lea ebp, [rdx-1]
lea rax, [r10+rcx*4]
mov r10, rbx
mov rbx, rax
mov edi, DWORD PTR [rbx]
sub r12d, 1
jns .L33
.L10:
cmp r8d, r12d
jg .L21
.L20:
movsx rax, r8d
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [r10+rax*4]
test r9d, r9d
je .L9
cmp r8d, r12d
jl .L34
pxor xmm1, xmm1
cvtsi2sd xmm1, edi
addsd xmm0, xmm1
.L17:
mulsd xmm0, QWORD PTR .LC0[rip]
.L9:
pop rbx
pop rbp
pop r12
ret
.L21:
sub r8d, r12d
pxor xmm0, xmm0
movsx rax, r8d
cvtsi2sd xmm0, DWORD PTR [rbx-4+rax*4]
test r9d, r9d
je .L9
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rbx+rax*4]
pop rbx
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
pop rbp
pop r12
ret
.L34:
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [r10+4+rax*4]
addsd xmm0, xmm1
jmp .L17
.L32:
mov r12d, ecx
jmp .L20
min:
cmp esi, edi
mov eax, edi
cmovle eax, esi
ret
max:
cmp esi, edi
mov eax, edi
cmovge eax, esi
ret
findMedianSortedArrays:
mov r10d, esi
mov rax, rdx
mov esi, ecx
lea r9d, [r10+rcx]
lea r8d, [r9-1]
shr r8d, 31
lea r8d, [r8-1+r9]
and r9d, 1
sar r8d
test ecx, ecx
je .L42
test r10d, r10d
je .L43
mov ecx, DWORD PTR [rdi]
xor r9d, 1
cmp DWORD PTR [rdx], ecx
jl .L44
mov ecx, esi
mov esi, r10d
jmp bs2
.L43:
movsx r8, r8d
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [rdx+r8*4]
test r9d, r9d
jne .L37
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rdx+4+r8*4]
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
ret
.L42:
movsx r8, r8d
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [rdi+r8*4]
test r9d, r9d
je .L45
.L37:
ret
.L45:
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rdi+4+r8*4]
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
ret
.L44:
mov rdx, rdi
mov ecx, r10d
mov rdi, rax
jmp bs2
.LC0:
.long 0
.long 1071644672
|
127
| 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
*/
|
x86-64
|
-O3
|
x86-64 gcc 15.2
|
bs:
mov eax, esi
mov r8d, edx
sub eax, 1
js .L1
xor ecx, ecx
jmp .L5
.L8:
lea eax, [rdx-1]
cmp ecx, eax
jg .L1
.L5:
mov edx, eax
sub edx, ecx
sar edx
add edx, ecx
movsx rsi, edx
cmp r8d, DWORD PTR [rdi+rsi*4]
jl .L8
lea ecx, [rdx+1]
cmp ecx, eax
jle .L5
.L1:
ret
bs2:
mov r11d, esi
push r12
mov r10, rdi
push rbp
mov r12d, r11d
mov ebp, ecx
push rbx
mov rbx, rdx
mov edi, DWORD PTR [rbx]
sub r12d, 1
js .L10
.L33:
mov ecx, r12d
xor edx, edx
jmp .L13
.L31:
lea ecx, [rax-1]
cmp ecx, edx
jl .L30
.L13:
mov eax, ecx
sub eax, edx
sar eax
add eax, edx
movsx rsi, eax
cmp edi, DWORD PTR [r10+rsi*4]
jl .L31
lea edx, [rax+1]
cmp ecx, edx
jge .L13
.L30:
cmp r8d, ecx
jle .L32
mov eax, r8d
sub eax, ecx
cmp r12d, ecx
je .L21
mov edx, r11d
lea r8d, [rax-1]
mov r11d, ebp
sub edx, ecx
add ecx, 1
mov r12d, r11d
movsx rcx, ecx
lea ebp, [rdx-1]
lea rax, [r10+rcx*4]
mov r10, rbx
mov rbx, rax
mov edi, DWORD PTR [rbx]
sub r12d, 1
jns .L33
.L10:
cmp r12d, r8d
jl .L21
.L20:
movsx rax, r8d
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [r10+rax*4]
test r9d, r9d
je .L9
cmp r8d, r12d
jl .L34
pxor xmm1, xmm1
cvtsi2sd xmm1, edi
addsd xmm0, xmm1
.L17:
mulsd xmm0, QWORD PTR .LC0[rip]
.L9:
pop rbx
pop rbp
pop r12
ret
.L21:
sub r8d, r12d
pxor xmm0, xmm0
movsx rax, r8d
cvtsi2sd xmm0, DWORD PTR [rbx-4+rax*4]
test r9d, r9d
je .L9
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rbx+rax*4]
pop rbx
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
pop rbp
pop r12
ret
.L34:
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [r10+4+rax*4]
addsd xmm0, xmm1
jmp .L17
.L32:
mov r12d, ecx
jmp .L20
min:
cmp esi, edi
mov eax, edi
cmovle eax, esi
ret
max:
cmp esi, edi
mov eax, edi
cmovge eax, esi
ret
findMedianSortedArrays:
mov r10d, esi
mov rax, rdx
mov esi, ecx
lea r9d, [r10+rcx]
lea r8d, [r9-1]
shr r8d, 31
lea r8d, [r8-1+r9]
and r9d, 1
sar r8d
test ecx, ecx
je .L42
test r10d, r10d
je .L43
mov ecx, DWORD PTR [rdi]
xor r9d, 1
cmp DWORD PTR [rdx], ecx
jl .L44
mov ecx, esi
mov esi, r10d
jmp bs2
.L43:
movsx r8, r8d
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [rdx+r8*4]
test r9d, r9d
jne .L37
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rdx+4+r8*4]
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
ret
.L42:
movsx r8, r8d
pxor xmm0, xmm0
cvtsi2sd xmm0, DWORD PTR [rdi+r8*4]
test r9d, r9d
je .L45
.L37:
ret
.L45:
pxor xmm1, xmm1
cvtsi2sd xmm1, DWORD PTR [rdi+4+r8*4]
addsd xmm0, xmm1
mulsd xmm0, QWORD PTR .LC0[rip]
ret
.L44:
mov rdx, rdi
mov ecx, r10d
mov rdi, rax
jmp bs2
.LC0:
.long 0
.long 1071644672
|
128
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O0
|
ARM64 gcc 15.2.0
|
longestPalindrome:
stp x29, x30, [sp, -64]!
mov x29, sp
str x0, [sp, 24]
str wzr, [sp, 48]
str wzr, [sp, 44]
ldr x0, [sp, 24]
cmp x0, 0
beq .L2
ldr x0, [sp, 24]
ldrb w0, [x0]
cmp w0, 0
bne .L3
.L2:
ldr x0, [sp, 24]
b .L4
.L3:
ldr x0, [sp, 24]
bl strlen
str w0, [sp, 40]
str wzr, [sp, 60]
str wzr, [sp, 56]
str wzr, [sp, 52]
b .L5
.L8:
ldr w0, [sp, 56]
add w0, w0, 1
str w0, [sp, 56]
.L6:
ldr w0, [sp, 56]
add w0, w0, 1
ldr w1, [sp, 40]
cmp w1, w0
ble .L7
ldrsw x0, [sp, 56]
ldr x1, [sp, 24]
add x0, x1, x0
ldrb w1, [x0]
ldrsw x0, [sp, 56]
add x0, x0, 1
ldr x2, [sp, 24]
add x0, x2, x0
ldrb w0, [x0]
cmp w1, w0
beq .L8
.L7:
ldr w0, [sp, 56]
add w0, w0, 1
str w0, [sp, 52]
b .L9
.L11:
ldr w0, [sp, 60]
sub w0, w0, #1
str w0, [sp, 60]
ldr w0, [sp, 56]
add w0, w0, 1
str w0, [sp, 56]
.L9:
ldr w0, [sp, 60]
cmp w0, 0
ble .L10
ldr w0, [sp, 56]
add w0, w0, 1
ldr w1, [sp, 40]
cmp w1, w0
ble .L10
ldrsw x0, [sp, 60]
sub x0, x0, #1
ldr x1, [sp, 24]
add x0, x1, x0
ldrb w1, [x0]
ldrsw x0, [sp, 56]
add x0, x0, 1
ldr x2, [sp, 24]
add x0, x2, x0
ldrb w0, [x0]
cmp w1, w0
beq .L11
.L10:
ldr w1, [sp, 56]
ldr w0, [sp, 60]
sub w0, w1, w0
add w0, w0, 1
str w0, [sp, 36]
ldr w1, [sp, 44]
ldr w0, [sp, 36]
cmp w1, w0
bge .L12
ldr w0, [sp, 36]
str w0, [sp, 44]
ldr w0, [sp, 60]
str w0, [sp, 48]
.L12:
ldr w0, [sp, 52]
str w0, [sp, 60]
ldr w0, [sp, 52]
str w0, [sp, 56]
.L5:
ldr w1, [sp, 40]
ldr w0, [sp, 52]
sub w0, w1, w0
lsl w0, w0, 1
ldr w1, [sp, 44]
cmp w1, w0
blt .L6
ldrsw x0, [sp, 48]
ldr x1, [sp, 24]
add x0, x1, x0
str x0, [sp, 24]
ldrsw x0, [sp, 44]
ldr x1, [sp, 24]
add x0, x1, x0
strb wzr, [x0]
ldr x0, [sp, 24]
.L4:
ldp x29, x30, [sp], 64
ret
|
129
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O1
|
ARM64 gcc 15.2.0
|
longestPalindrome:
stp x29, x30, [sp, -32]!
mov x29, sp
str x19, [sp, 16]
mov x19, x0
cbz x0, .L1
ldrb w1, [x0]
cbz w1, .L1
bl strlen
mov w7, w0
cmp w0, 0
ble .L13
lsl x13, x19, 1
mov w11, 0
mov w12, 0
mov w5, 0
b .L10
.L14:
mov w9, w4
.L6:
sub w9, w9, w5
add w9, w9, 1
cmp w9, w11
csel w0, w9, w11, gt
csel w12, w5, w12, gt
mov w11, w0
sub w0, w7, w1
cmp w11, w0, lsl 1
bge .L3
mov w5, w1
.L10:
sxtw x10, w5
add x3, x19, x10
mov x2, x3
mov w1, w5
.L5:
mov w4, w1
add w1, w1, 1
cmp w1, w7
bge .L4
ldrb w0, [x2]
ldrb w6, [x2, 1]!
cmp w6, w0
beq .L5
.L4:
cmp w5, 0
ble .L14
add x10, x13, x10
mov w2, w4
add x10, x10, 1
sxtw x6, w4
.L7:
mov w9, w2
add w2, w2, 1
cmp w2, w7
bge .L6
sub x4, x10, x3
ldrb w8, [x3, -1]
ldrb w0, [x4, x6]
cmp w8, w0
bne .L6
sub x3, x3, #1
subs w5, w5, #1
bne .L7
mov w9, w2
b .L6
.L13:
mov w11, 0
mov w12, 0
.L3:
add x0, x19, w12, sxtw
strb wzr, [x0, w11, sxtw]
.L1:
ldr x19, [sp, 16]
ldp x29, x30, [sp], 32
ret
|
130
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O2
|
ARM64 gcc 15.2.0
|
longestPalindrome:
cbz x0, .L11
ldrb w1, [x0]
mov x11, x0
cbz w1, .L21
stp x29, x30, [sp, -32]!
mov x29, sp
stp x0, x0, [sp, 16]
bl strlen
cmp w0, 0
ldr x11, [sp, 16]
mov x12, x11
ble .L3
lsl x13, x11, 1
mov w10, 0
mov w11, 0
mov w5, 0
.L10:
sxtw x8, w5
mov w1, w5
add x3, x12, x8
mov x4, x3
b .L5
.L25:
ldrb w6, [x4]
ldrb w7, [x4, 1]!
cmp w7, w6
bne .L4
.L5:
mov w2, w1
add w1, w1, 1
cmp w1, w0
blt .L25
.L4:
cmp w5, 0
ble .L15
add x8, x13, x8
sxtw x9, w2
add x8, x8, 1
b .L7
.L26:
sub x4, x8, x3
ldrb w7, [x3, -1]
ldrb w4, [x4, x9]
cmp w7, w4
bne .L6
sub x3, x3, #1
subs w5, w5, #1
beq .L15
.L7:
mov w6, w2
add w2, w2, 1
cmp w2, w0
blt .L26
.L6:
sub w6, w6, w5
sub w2, w0, w1
add w6, w6, 1
cmp w6, w10
csel w10, w6, w10, gt
csel w11, w5, w11, gt
cmp w10, w2, lsl 1
bge .L27
mov w5, w1
b .L10
.L27:
add x11, x12, w11, sxtw
add x12, x11, w10, sxtw
.L3:
strb wzr, [x12]
mov x0, x11
ldp x29, x30, [sp], 32
ret
.L15:
mov w6, w2
b .L6
.L11:
mov x11, 0
.L21:
mov x0, x11
ret
|
131
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O3
|
ARM64 gcc 15.2.0
|
longestPalindrome:
cbz x0, .L26
ldrb w1, [x0]
mov x11, x0
cbz w1, .L39
stp x29, x30, [sp, -32]!
mov x29, sp
stp x0, x0, [sp, 16]
bl strlen
cmp w0, 0
ldr x11, [sp, 16]
mov x8, x11
ble .L3
adrp x1, .LC0
dup v27.4s, w0
movi v19.4s, 0xd
lsl x12, x11, 1
ldr q7, [x1, #:lo12:.LC0]
adrp x1, .LC1
movi v20.4s, 0x1
mov w10, 0
ldr q16, [x1, #:lo12:.LC1]
adrp x1, .LC2
movi v21.4s, 0x5
mov w11, 0
movi v22.4s, 0x9
mov w3, 0
ldr q18, [x1, #:lo12:.LC2]
mvni v23.4s, 0xf
movi v17.4s, 0x10
.L25:
sxtw x13, w3
mov w2, w3
add x9, x8, x13
mov x1, x9
b .L5
.L43:
ldrb w4, [x1]
ldrb w5, [x1, 1]!
cmp w5, w4
bne .L4
.L5:
mov w6, w2
add w2, w2, 1
cmp w2, w0
blt .L43
.L4:
cmp w3, 0
ble .L29
cmp w3, 15
ble .L7
add w5, w6, 1
sub x7, x13, #16
add x7, x8, x7
add x5, x8, w5, sxtw
orr x1, x5, x7
tst x1, 15
bne .L7
dup v26.4s, w3
dup v30.4s, w6
and x9, x3, 4294967280
mov x1, 0
add v26.4s, v26.4s, v7.4s
add v30.4s, v30.4s, v16.4s
b .L9
.L45:
ldr q29, [x7, x4]
ldr q31, [x5, x1]
add x1, x1, 16
tbl v29.16b, {v29.16b}, v18.16b
cmeq v31.16b, v31.16b, v29.16b
not v31.16b, v31.16b
umaxp v31.4s, v31.4s, v31.4s
fmov x4, d31
cbnz x4, .L20
add v30.4s, v30.4s, v17.4s
cmp x9, x1
beq .L44
add v26.4s, v26.4s, v23.4s
.L9:
add v25.4s, v30.4s, v19.4s
neg x4, x1
add v29.4s, v30.4s, v22.4s
add v31.4s, v30.4s, v20.4s
add v28.4s, v30.4s, v21.4s
cmge v24.4s, v25.4s, v27.4s
cmge v29.4s, v29.4s, v27.4s
cmge v31.4s, v31.4s, v27.4s
cmge v28.4s, v28.4s, v27.4s
orr v29.16b, v29.16b, v24.16b
orr v31.16b, v31.16b, v28.16b
orr v31.16b, v31.16b, v29.16b
umaxp v31.4s, v31.4s, v31.4s
fmov x13, d31
cbz x13, .L45
.L20:
fmov w1, s30
fmov w3, s26
.L23:
sxtw x4, w3
sxtw x13, w1
add x9, x12, x4
add x4, x8, x4
add x9, x9, 1
b .L14
.L12:
ldrb w7, [x4, -1]
sub x4, x4, #1
ldrb w6, [x6, x13]
cmp w7, w6
bne .L6
subs w3, w3, #1
beq .L15
.L14:
mov w5, w1
add w1, w1, 1
sub x6, x9, x4
cmp w0, w1
bgt .L12
.L6:
sub w5, w5, w3
sub w1, w0, w2
add w5, w5, 1
cmp w5, w10
csel w10, w5, w10, gt
csel w11, w3, w11, gt
cmp w10, w1, lsl 1
bge .L46
mov w3, w2
b .L25
.L46:
add x11, x8, w11, sxtw
add x8, x11, w10, sxtw
.L3:
strb wzr, [x8]
mov x0, x11
ldp x29, x30, [sp], 32
ret
.L11:
mvni v31.4s, 0xc
umov w1, v25.s[3]
add v31.4s, v26.4s, v31.4s
umov w3, v31.s[3]
.L15:
mov w5, w1
b .L6
.L44:
and w4, w3, -16
cmp w3, w4
beq .L11
add w1, w6, w4
sub w3, w3, w4
b .L23
.L7:
add x1, x12, x13
sxtw x13, w6
add x1, x1, 1
b .L18
.L47:
ldrb w7, [x9, -1]
sub x9, x9, #1
ldrb w4, [x4, x13]
cmp w7, w4
bne .L6
subs w3, w3, #1
beq .L29
.L18:
mov w5, w6
add w6, w6, 1
sub x4, x1, x9
cmp w6, w0
blt .L47
b .L6
.L29:
mov w5, w6
b .L6
.L26:
mov x11, 0
.L39:
mov x0, x11
ret
.LC0:
.word 0
.word -1
.word -2
.word -3
.LC1:
.word 0
.word 1
.word 2
.word 3
.LC2:
.byte 15
.byte 14
.byte 13
.byte 12
.byte 11
.byte 10
.byte 9
.byte 8
.byte 7
.byte 6
.byte 5
.byte 4
.byte 3
.byte 2
.byte 1
.byte 0
|
132
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O0
|
armv8-a clang 21.1.0
|
longestPalindrome:
sub sp, sp, #80
stp x29, x30, [sp, #64]
add x29, sp, #64
stur x0, [x29, #-16]
str wzr, [sp, #24]
str wzr, [sp, #20]
ldur x8, [x29, #-16]
cbz x8, .LBB0_2
b .LBB0_1
.LBB0_1:
ldur x8, [x29, #-16]
ldrb w8, [x8]
cbnz w8, .LBB0_3
b .LBB0_2
.LBB0_2:
ldur x8, [x29, #-16]
stur x8, [x29, #-8]
b .LBB0_21
.LBB0_3:
ldur x0, [x29, #-16]
bl strlen
mov w8, w0
stur w8, [x29, #-20]
stur wzr, [x29, #-24]
stur wzr, [x29, #-28]
str wzr, [sp, #32]
b .LBB0_4
.LBB0_4:
ldr w8, [sp, #20]
ldur w9, [x29, #-20]
ldr w10, [sp, #32]
subs w9, w9, w10
subs w8, w8, w9, lsl #1
b.ge .LBB0_20
b .LBB0_5
.LBB0_5:
b .LBB0_6
.LBB0_6:
ldur w8, [x29, #-28]
add w9, w8, #1
ldur w10, [x29, #-20]
mov w8, #0
subs w9, w9, w10
str w8, [sp, #16]
b.ge .LBB0_8
b .LBB0_7
.LBB0_7:
ldur x8, [x29, #-16]
ldursw x9, [x29, #-28]
ldrb w8, [x8, x9]
ldur x9, [x29, #-16]
ldur w10, [x29, #-28]
add w10, w10, #1
ldrb w9, [x9, w10, sxtw]
subs w8, w8, w9
cset w8, eq
str w8, [sp, #16]
b .LBB0_8
.LBB0_8:
ldr w8, [sp, #16]
tbz w8, #0, .LBB0_10
b .LBB0_9
.LBB0_9:
ldur w8, [x29, #-28]
add w8, w8, #1
stur w8, [x29, #-28]
b .LBB0_6
.LBB0_10:
ldur w8, [x29, #-28]
add w8, w8, #1
str w8, [sp, #32]
b .LBB0_11
.LBB0_11:
ldur w9, [x29, #-24]
mov w8, #0
subs w9, w9, #0
str w8, [sp, #12]
b.le .LBB0_14
b .LBB0_12
.LBB0_12:
ldur w8, [x29, #-28]
add w9, w8, #1
ldur w10, [x29, #-20]
mov w8, #0
subs w9, w9, w10
str w8, [sp, #12]
b.ge .LBB0_14
b .LBB0_13
.LBB0_13:
ldur x8, [x29, #-16]
ldur w9, [x29, #-24]
subs w9, w9, #1
ldrb w8, [x8, w9, sxtw]
ldur x9, [x29, #-16]
ldur w10, [x29, #-28]
add w10, w10, #1
ldrb w9, [x9, w10, sxtw]
subs w8, w8, w9
cset w8, eq
str w8, [sp, #12]
b .LBB0_14
.LBB0_14:
ldr w8, [sp, #12]
tbz w8, #0, .LBB0_16
b .LBB0_15
.LBB0_15:
ldur w8, [x29, #-24]
subs w8, w8, #1
stur w8, [x29, #-24]
ldur w8, [x29, #-28]
add w8, w8, #1
stur w8, [x29, #-28]
b .LBB0_11
.LBB0_16:
ldur w8, [x29, #-28]
ldur w9, [x29, #-24]
subs w8, w8, w9
add w8, w8, #1
str w8, [sp, #28]
ldr w8, [sp, #20]
ldr w9, [sp, #28]
subs w8, w8, w9
b.ge .LBB0_18
b .LBB0_17
.LBB0_17:
ldr w8, [sp, #28]
str w8, [sp, #20]
ldur w8, [x29, #-24]
str w8, [sp, #24]
b .LBB0_18
.LBB0_18:
b .LBB0_19
.LBB0_19:
ldr w8, [sp, #32]
stur w8, [x29, #-24]
ldr w8, [sp, #32]
stur w8, [x29, #-28]
b .LBB0_4
.LBB0_20:
ldur x8, [x29, #-16]
ldrsw x9, [sp, #24]
add x8, x8, x9
stur x8, [x29, #-16]
ldur x8, [x29, #-16]
ldrsw x9, [sp, #20]
add x8, x8, x9
strb wzr, [x8]
ldur x8, [x29, #-16]
stur x8, [x29, #-8]
b .LBB0_21
.LBB0_21:
ldur x0, [x29, #-8]
ldp x29, x30, [sp, #64]
add sp, sp, #80
ret
|
133
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O1
|
armv8-a clang 21.1.0
|
longestPalindrome:
cbz x0, .LBB0_20
ldrb w8, [x0]
cbz w8, .LBB0_20
stp x29, x30, [sp, #-32]!
str x19, [sp, #16]
mov x29, sp
mov x19, x0
bl strlen
mov x8, x0
cmp w8, #1
b.lt .LBB0_18
mov w9, wzr
mov w10, wzr
mov w14, wzr
mov x0, x19
sub x11, x19, #1
and x12, x8, #0x7fffffff
add x13, x19, #1
b .LBB0_6
.LBB0_4:
mov w17, w16
.LBB0_5:
sub w16, w17, w15
add w17, w16, #1
cmp w9, w16
csel w10, w10, w15, gt
cmp w9, w17
sub w15, w8, w14
csinc w9, w9, w16, gt
cmp w9, w15, lsl #1
b.ge .LBB0_17
.LBB0_6:
mov w15, w14
add w14, w14, #1
cmp w14, w8
sxtw x14, w15
csinc w17, w8, w15, le
sub w16, w17, #1
add x18, x13, x14
add x1, x14, #1
mov w14, w15
.LBB0_7:
cmp x1, x12
b.ge .LBB0_10
ldurb w2, [x18, #-1]
ldrb w3, [x18], #1
add w14, w14, #1
add x1, x1, #1
cmp w2, w3
b.eq .LBB0_7
sub w16, w14, #1
cmp w15, #1
b.ge .LBB0_11
b .LBB0_4
.LBB0_10:
mov w14, w17
cmp w15, #1
b.lt .LBB0_4
.LBB0_11:
add w17, w16, #1
sxtw x1, w16
cmp w17, w8
add w17, w16, w15
csinc w18, w8, w16, le
add x1, x1, #1
sub w18, w18, #1
.LBB0_12:
cmp x1, x12
b.ge .LBB0_16
ldrb w2, [x11, w15, uxtw]
ldrb w3, [x0, x1]
cmp w2, w3
b.ne .LBB0_4
subs w15, w15, #1
add w16, w16, #1
add x1, x1, #1
b.gt .LBB0_12
mov w15, wzr
b .LBB0_5
.LBB0_16:
mov w17, w18
b .LBB0_5
.LBB0_17:
sxtw x8, w10
b .LBB0_19
.LBB0_18:
mov x8, xzr
mov x9, xzr
mov x0, x19
.LBB0_19:
add x0, x0, x8
strb wzr, [x0, x9]
ldr x19, [sp, #16]
ldp x29, x30, [sp], #32
.LBB0_20:
ret
|
134
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O2
|
armv8-a clang 21.1.0
|
longestPalindrome:
cbz x0, .LBB0_20
ldrb w8, [x0]
cbz w8, .LBB0_20
stp x29, x30, [sp, #-32]!
str x19, [sp, #16]
mov x29, sp
mov x19, x0
bl strlen
mov x8, x0
cmp w8, #1
b.lt .LBB0_18
mov w9, wzr
mov w10, wzr
mov w14, wzr
mov x0, x19
sub x11, x19, #1
and x12, x8, #0x7fffffff
add x13, x19, #1
b .LBB0_6
.LBB0_4:
mov w17, w16
.LBB0_5:
sub w16, w17, w15
add w17, w16, #1
cmp w9, w16
csel w10, w10, w15, gt
cmp w9, w17
sub w15, w8, w14
csinc w9, w9, w16, gt
cmp w9, w15, lsl #1
b.ge .LBB0_17
.LBB0_6:
mov w15, w14
add w14, w14, #1
cmp w14, w8
sxtw x14, w15
csinc w17, w8, w15, le
sub w16, w17, #1
add x18, x13, x14
add x1, x14, #1
mov w14, w15
.LBB0_7:
cmp x1, x12
b.ge .LBB0_10
ldurb w2, [x18, #-1]
ldrb w3, [x18], #1
add w14, w14, #1
add x1, x1, #1
cmp w2, w3
b.eq .LBB0_7
sub w16, w14, #1
cmp w15, #1
b.ge .LBB0_11
b .LBB0_4
.LBB0_10:
mov w14, w17
cmp w15, #1
b.lt .LBB0_4
.LBB0_11:
add w17, w16, #1
sxtw x1, w16
cmp w17, w8
add w17, w16, w15
csinc w18, w8, w16, le
add x1, x1, #1
sub w18, w18, #1
.LBB0_12:
cmp x1, x12
b.ge .LBB0_16
ldrb w2, [x11, w15, uxtw]
ldrb w3, [x0, x1]
cmp w2, w3
b.ne .LBB0_4
subs w15, w15, #1
add w16, w16, #1
add x1, x1, #1
b.gt .LBB0_12
mov w15, wzr
b .LBB0_5
.LBB0_16:
mov w17, w18
b .LBB0_5
.LBB0_17:
sxtw x8, w10
b .LBB0_19
.LBB0_18:
mov x8, xzr
mov x9, xzr
mov x0, x19
.LBB0_19:
add x0, x0, x8
strb wzr, [x0, x9]
ldr x19, [sp, #16]
ldp x29, x30, [sp], #32
.LBB0_20:
ret
|
135
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
aarch64
|
-O3
|
armv8-a clang 21.1.0
|
longestPalindrome:
cbz x0, .LBB0_20
ldrb w8, [x0]
cbz w8, .LBB0_20
stp x29, x30, [sp, #-32]!
str x19, [sp, #16]
mov x29, sp
mov x19, x0
bl strlen
mov x8, x0
cmp w8, #1
b.lt .LBB0_18
mov w9, wzr
mov w10, wzr
mov w14, wzr
mov x0, x19
sub x11, x19, #1
and x12, x8, #0x7fffffff
add x13, x19, #1
b .LBB0_6
.LBB0_4:
mov w17, w16
.LBB0_5:
sub w16, w17, w15
add w17, w16, #1
cmp w9, w16
csel w10, w10, w15, gt
cmp w9, w17
sub w15, w8, w14
csinc w9, w9, w16, gt
cmp w9, w15, lsl #1
b.ge .LBB0_17
.LBB0_6:
mov w15, w14
add w14, w14, #1
cmp w14, w8
sxtw x14, w15
csinc w17, w8, w15, le
sub w16, w17, #1
add x18, x13, x14
add x1, x14, #1
mov w14, w15
.LBB0_7:
cmp x1, x12
b.ge .LBB0_10
ldurb w2, [x18, #-1]
ldrb w3, [x18], #1
add w14, w14, #1
add x1, x1, #1
cmp w2, w3
b.eq .LBB0_7
sub w16, w14, #1
cmp w15, #1
b.ge .LBB0_11
b .LBB0_4
.LBB0_10:
mov w14, w17
cmp w15, #1
b.lt .LBB0_4
.LBB0_11:
add w17, w16, #1
sxtw x1, w16
cmp w17, w8
add w17, w16, w15
csinc w18, w8, w16, le
add x1, x1, #1
sub w18, w18, #1
.LBB0_12:
cmp x1, x12
b.ge .LBB0_16
ldrb w2, [x11, w15, uxtw]
ldrb w3, [x0, x1]
cmp w2, w3
b.ne .LBB0_4
subs w15, w15, #1
add w16, w16, #1
add x1, x1, #1
b.gt .LBB0_12
mov w15, wzr
b .LBB0_5
.LBB0_16:
mov w17, w18
b .LBB0_5
.LBB0_17:
sxtw x8, w10
b .LBB0_19
.LBB0_18:
mov x8, xzr
mov x9, xzr
mov x0, x19
.LBB0_19:
add x0, x0, x8
strb wzr, [x0, x9]
ldr x19, [sp, #16]
ldp x29, x30, [sp], #32
.LBB0_20:
ret
|
136
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O0
|
mips64 clang 21.1.0
|
longestPalindrome:
.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(longestPalindrome)))
daddu $1, $1, $25
daddiu $1, $1, %lo(%neg(%gp_rel(longestPalindrome)))
sd $1, 16($fp)
sd $4, 56($fp)
sw $zero, 32($fp)
sw $zero, 28($fp)
ld $1, 56($fp)
beqz $1, .LBB0_4
nop
b .LBB0_2
nop
.LBB0_2:
ld $1, 56($fp)
lbu $1, 0($1)
bnez $1, .LBB0_5
nop
b .LBB0_4
nop
.LBB0_4:
ld $1, 56($fp)
sd $1, 64($fp)
b .LBB0_30
nop
.LBB0_5:
ld $gp, 16($fp)
ld $4, 56($fp)
ld $25, %call16(strlen)($gp)
jalr $25
nop
sw $2, 52($fp)
sw $zero, 48($fp)
sw $zero, 44($fp)
sw $zero, 40($fp)
b .LBB0_6
nop
.LBB0_6:
lw $1, 28($fp)
lw $2, 52($fp)
lw $3, 40($fp)
subu $2, $2, $3
sll $2, $2, 1
slt $1, $1, $2
beqz $1, .LBB0_29
nop
b .LBB0_8
nop
.LBB0_8:
b .LBB0_9
nop
.LBB0_9:
lw $1, 44($fp)
addiu $1, $1, 1
lw $3, 52($fp)
addiu $2, $zero, 0
slt $1, $1, $3
sw $2, 12($fp)
beqz $1, .LBB0_12
nop
b .LBB0_11
nop
.LBB0_11:
ld $2, 56($fp)
lw $3, 44($fp)
sll $1, $3, 0
daddu $1, $2, $1
lb $1, 0($1)
addiu $4, $3, 1
move $3, $4
daddu $2, $2, $3
lb $2, 0($2)
xor $1, $1, $2
sltiu $1, $1, 1
sw $1, 12($fp)
b .LBB0_12
nop
.LBB0_12:
lw $1, 12($fp)
andi $1, $1, 1
beqz $1, .LBB0_15
nop
b .LBB0_14
nop
.LBB0_14:
lw $1, 44($fp)
addiu $1, $1, 1
sw $1, 44($fp)
b .LBB0_9
nop
.LBB0_15:
lw $1, 44($fp)
addiu $1, $1, 1
sw $1, 40($fp)
b .LBB0_16
nop
.LBB0_16:
lw $1, 48($fp)
addiu $2, $zero, 0
sw $2, 8($fp)
blez $1, .LBB0_21
nop
b .LBB0_18
nop
.LBB0_18:
lw $1, 44($fp)
addiu $1, $1, 1
lw $3, 52($fp)
addiu $2, $zero, 0
slt $1, $1, $3
sw $2, 8($fp)
beqz $1, .LBB0_21
nop
b .LBB0_20
nop
.LBB0_20:
ld $2, 56($fp)
lw $1, 48($fp)
addiu $3, $1, -1
move $1, $3
daddu $1, $2, $1
lb $1, 0($1)
lw $3, 44($fp)
addiu $4, $3, 1
move $3, $4
daddu $2, $2, $3
lb $2, 0($2)
xor $1, $1, $2
sltiu $1, $1, 1
sw $1, 8($fp)
b .LBB0_21
nop
.LBB0_21:
lw $1, 8($fp)
andi $1, $1, 1
beqz $1, .LBB0_24
nop
b .LBB0_23
nop
.LBB0_23:
lw $1, 48($fp)
addiu $1, $1, -1
sw $1, 48($fp)
lw $1, 44($fp)
addiu $1, $1, 1
sw $1, 44($fp)
b .LBB0_16
nop
.LBB0_24:
lw $1, 44($fp)
lw $2, 48($fp)
subu $1, $1, $2
addiu $1, $1, 1
sw $1, 36($fp)
lw $1, 28($fp)
lw $2, 36($fp)
slt $1, $1, $2
beqz $1, .LBB0_27
nop
b .LBB0_26
nop
.LBB0_26:
lw $1, 36($fp)
sw $1, 28($fp)
lw $1, 48($fp)
sw $1, 32($fp)
b .LBB0_27
nop
.LBB0_27:
b .LBB0_28
nop
.LBB0_28:
lw $1, 40($fp)
sw $1, 48($fp)
lw $1, 40($fp)
sw $1, 44($fp)
b .LBB0_6
nop
.LBB0_29:
ld $1, 56($fp)
lw $2, 32($fp)
daddu $1, $1, $2
sd $1, 56($fp)
ld $1, 56($fp)
lw $2, 28($fp)
daddu $1, $1, $2
addiu $2, $zero, 0
sb $zero, 0($1)
ld $1, 56($fp)
sd $1, 64($fp)
b .LBB0_30
nop
.LBB0_30:
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
|
137
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O1
|
mips64 clang 21.1.0
|
longestPalindrome:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -32
sd $ra, 24($sp)
sd $fp, 16($sp)
sd $gp, 8($sp)
sd $16, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(longestPalindrome)))
daddu $2, $1, $25
beqz $4, .LBB0_22
move $16, $4
lbu $1, 0($16)
beqz $1, .LBB0_22
nop
daddiu $gp, $2, %lo(%neg(%gp_rel(longestPalindrome)))
ld $25, %call16(strlen)($gp)
jalr $25
move $4, $16
sll $3, $2, 0
blez $3, .LBB0_20
nop
daddiu $4, $16, 1
dext $2, $2, 0, 31
daddiu $5, $16, -1
addiu $10, $zero, 0
addiu $12, $zero, 0
b .LBB0_6
addiu $6, $zero, 0
.LBB0_4:
move $12, $9
move $11, $10
.LBB0_5:
subu $1, $11, $12
addiu $10, $1, 1
slt $9, $10, $8
movn $10, $8, $9
subu $9, $3, $6
sll $9, $9, 1
slt $9, $10, $9
slt $1, $1, $8
beqz $9, .LBB0_19
movn $12, $7, $1
.LBB0_6:
move $9, $6
move $7, $12
move $8, $10
sll $1, $6, 0
daddu $12, $4, $1
addiu $10, $6, 1
slt $13, $3, $10
move $11, $3
movn $11, $10, $13
addiu $10, $11, -1
daddiu $13, $1, 1
.LBB0_7:
slt $1, $13, $2
beqz $1, .LBB0_11
nop
daddiu $13, $13, 1
daddiu $1, $12, 1
addiu $6, $6, 1
lbu $14, 0($12)
lbu $15, -1($12)
beq $15, $14, .LBB0_7
move $12, $1
bgtz $9, .LBB0_12
addiu $10, $6, -1
b .LBB0_4
nop
.LBB0_11:
blez $9, .LBB0_4
move $6, $11
.LBB0_12:
addu $11, $10, $9
addiu $1, $10, 1
slt $12, $3, $1
move $13, $3
movn $13, $1, $12
addiu $13, $13, -1
sll $1, $10, 0
daddiu $12, $1, 1
.LBB0_13:
slt $1, $12, $2
beqz $1, .LBB0_17
nop
daddu $1, $16, $12
lbu $1, 0($1)
sll $14, $9, 0
daddu $14, $5, $14
lbu $14, 0($14)
bne $14, $1, .LBB0_18
nop
daddiu $12, $12, 1
addiu $10, $10, 1
addiu $1, $9, -1
slti $14, $9, 2
beqz $14, .LBB0_13
move $9, $1
b .LBB0_5
addiu $12, $zero, 0
.LBB0_17:
move $12, $9
b .LBB0_5
move $11, $13
.LBB0_18:
move $12, $9
b .LBB0_5
move $11, $10
.LBB0_19:
sll $3, $10, 0
b .LBB0_21
sll $2, $12, 0
.LBB0_20:
daddiu $2, $zero, 0
daddiu $3, $zero, 0
.LBB0_21:
daddu $16, $16, $2
daddu $1, $16, $3
sb $zero, 0($1)
.LBB0_22:
move $2, $16
move $sp, $fp
ld $16, 0($sp)
ld $gp, 8($sp)
ld $fp, 16($sp)
ld $ra, 24($sp)
jr $ra
daddiu $sp, $sp, 32
|
138
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O2
|
mips64 clang 21.1.0
|
longestPalindrome:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -32
sd $ra, 24($sp)
sd $fp, 16($sp)
sd $gp, 8($sp)
sd $16, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(longestPalindrome)))
beqz $4, .LBB0_19
daddu $2, $1, $25
lbu $1, 0($4)
beqz $1, .LBB0_23
move $16, $4
daddiu $gp, $2, %lo(%neg(%gp_rel(longestPalindrome)))
ld $25, %call16(strlen)($gp)
jalr $25
move $4, $16
sll $3, $2, 0
blez $3, .LBB0_21
nop
daddiu $4, $16, 1
dext $2, $2, 0, 31
daddiu $5, $16, -1
addiu $10, $zero, 0
addiu $12, $zero, 0
b .LBB0_6
addiu $6, $zero, 0
.LBB0_4:
move $12, $9
move $11, $10
.LBB0_5:
subu $1, $11, $12
addiu $10, $1, 1
slt $9, $10, $8
movn $10, $8, $9
subu $9, $3, $6
sll $9, $9, 1
slt $9, $10, $9
slt $1, $1, $8
beqz $9, .LBB0_20
movn $12, $7, $1
.LBB0_6:
move $9, $6
move $7, $12
move $8, $10
sll $1, $6, 0
daddu $12, $4, $1
addiu $10, $6, 1
slt $13, $3, $10
move $11, $3
movn $11, $10, $13
addiu $10, $11, -1
daddiu $13, $1, 1
.LBB0_7:
slt $1, $13, $2
beqz $1, .LBB0_11
nop
daddiu $13, $13, 1
daddiu $1, $12, 1
addiu $6, $6, 1
lbu $14, 0($12)
lbu $15, -1($12)
beq $15, $14, .LBB0_7
move $12, $1
bgtz $9, .LBB0_12
addiu $10, $6, -1
b .LBB0_4
nop
.LBB0_11:
blez $9, .LBB0_4
move $6, $11
.LBB0_12:
addu $11, $10, $9
addiu $1, $10, 1
slt $12, $3, $1
move $13, $3
movn $13, $1, $12
addiu $13, $13, -1
sll $1, $10, 0
daddiu $12, $1, 1
.LBB0_13:
slt $1, $12, $2
beqz $1, .LBB0_17
nop
daddu $1, $16, $12
lbu $1, 0($1)
sll $14, $9, 0
daddu $14, $5, $14
lbu $14, 0($14)
bne $14, $1, .LBB0_18
nop
daddiu $12, $12, 1
addiu $10, $10, 1
addiu $1, $9, -1
slti $14, $9, 2
beqz $14, .LBB0_13
move $9, $1
b .LBB0_5
addiu $12, $zero, 0
.LBB0_17:
move $12, $9
b .LBB0_5
move $11, $13
.LBB0_18:
move $12, $9
b .LBB0_5
move $11, $10
.LBB0_19:
b .LBB0_23
daddiu $16, $zero, 0
.LBB0_20:
sll $3, $10, 0
b .LBB0_22
sll $2, $12, 0
.LBB0_21:
daddiu $2, $zero, 0
daddiu $3, $zero, 0
.LBB0_22:
daddu $16, $16, $2
daddu $1, $16, $3
sb $zero, 0($1)
.LBB0_23:
move $2, $16
move $sp, $fp
ld $16, 0($sp)
ld $gp, 8($sp)
ld $fp, 16($sp)
ld $ra, 24($sp)
jr $ra
daddiu $sp, $sp, 32
|
139
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O3
|
mips64 clang 21.1.0
|
longestPalindrome:
.Lfunc_begin0 = .Ltmp0
daddiu $sp, $sp, -32
sd $ra, 24($sp)
sd $fp, 16($sp)
sd $gp, 8($sp)
sd $16, 0($sp)
move $fp, $sp
lui $1, %hi(%neg(%gp_rel(longestPalindrome)))
beqz $4, .LBB0_19
daddu $2, $1, $25
lbu $1, 0($4)
beqz $1, .LBB0_23
move $16, $4
daddiu $gp, $2, %lo(%neg(%gp_rel(longestPalindrome)))
ld $25, %call16(strlen)($gp)
jalr $25
move $4, $16
sll $3, $2, 0
blez $3, .LBB0_21
nop
daddiu $4, $16, 1
dext $2, $2, 0, 31
daddiu $5, $16, -1
addiu $10, $zero, 0
addiu $12, $zero, 0
b .LBB0_6
addiu $6, $zero, 0
.LBB0_4:
move $12, $9
move $11, $10
.LBB0_5:
subu $1, $11, $12
addiu $10, $1, 1
slt $1, $1, $8
slt $9, $10, $8
movn $10, $8, $9
subu $9, $3, $6
sll $9, $9, 1
slt $9, $10, $9
beqz $9, .LBB0_20
movn $12, $7, $1
.LBB0_6:
move $8, $10
addiu $10, $6, 1
move $11, $3
sll $1, $6, 0
move $7, $12
move $9, $6
slt $13, $3, $10
daddu $12, $4, $1
movn $11, $10, $13
daddiu $13, $1, 1
addiu $10, $11, -1
.LBB0_7:
slt $1, $13, $2
beqz $1, .LBB0_11
nop
daddiu $1, $12, 1
lbu $14, 0($12)
lbu $15, -1($12)
daddiu $13, $13, 1
addiu $6, $6, 1
beq $15, $14, .LBB0_7
move $12, $1
bgtz $9, .LBB0_12
addiu $10, $6, -1
b .LBB0_4
nop
.LBB0_11:
blez $9, .LBB0_4
move $6, $11
.LBB0_12:
addiu $1, $10, 1
move $13, $3
addu $11, $10, $9
slt $12, $3, $1
movn $13, $1, $12
sll $1, $10, 0
addiu $13, $13, -1
daddiu $12, $1, 1
.LBB0_13:
slt $1, $12, $2
beqz $1, .LBB0_17
nop
sll $14, $9, 0
daddu $1, $16, $12
daddu $14, $5, $14
lbu $1, 0($1)
lbu $14, 0($14)
bne $14, $1, .LBB0_18
nop
addiu $1, $9, -1
slti $14, $9, 2
daddiu $12, $12, 1
addiu $10, $10, 1
beqz $14, .LBB0_13
move $9, $1
b .LBB0_5
addiu $12, $zero, 0
.LBB0_17:
move $12, $9
b .LBB0_5
move $11, $13
.LBB0_18:
move $12, $9
b .LBB0_5
move $11, $10
.LBB0_19:
b .LBB0_23
daddiu $16, $zero, 0
.LBB0_20:
sll $3, $10, 0
b .LBB0_22
sll $2, $12, 0
.LBB0_21:
daddiu $2, $zero, 0
daddiu $3, $zero, 0
.LBB0_22:
daddu $16, $16, $2
daddu $1, $16, $3
sb $zero, 0($1)
.LBB0_23:
move $2, $16
move $sp, $fp
ld $16, 0($sp)
ld $gp, 8($sp)
ld $fp, 16($sp)
ld $ra, 24($sp)
jr $ra
daddiu $sp, $sp, 32
|
140
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O0
|
mips64 gcc 15.2.0
|
longestPalindrome:
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(longestPalindrome)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(longestPalindrome)))
sd $4,32($fp)
sw $0,12($fp)
sw $0,16($fp)
ld $2,32($fp)
beq $2,$0,.L2
nop
ld $2,32($fp)
lb $2,0($2)
bne $2,$0,.L3
nop
.L2:
ld $2,32($fp)
b .L4
nop
.L3:
ld $4,32($fp)
ld $2,%call16(strlen)($28)
mtlo $2
mflo $25
jalr $25
nop
sw $2,20($fp)
sw $0,0($fp)
sw $0,4($fp)
sw $0,8($fp)
b .L5
nop
.L8:
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
.L6:
lw $2,4($fp)
addiu $2,$2,1
lw $3,20($fp)
slt $2,$2,$3
beq $2,$0,.L7
nop
lw $2,4($fp)
ld $3,32($fp)
daddu $2,$3,$2
lb $2,0($2)
lw $3,4($fp)
daddiu $3,$3,1
ld $4,32($fp)
daddu $3,$4,$3
lb $3,0($3)
beq $2,$3,.L8
nop
.L7:
lw $2,4($fp)
addiu $2,$2,1
sw $2,8($fp)
b .L9
nop
.L11:
lw $2,0($fp)
addiu $2,$2,-1
sw $2,0($fp)
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
.L9:
lw $2,0($fp)
blez $2,.L10
nop
lw $2,4($fp)
addiu $2,$2,1
lw $3,20($fp)
slt $2,$2,$3
beq $2,$0,.L10
nop
lw $2,0($fp)
daddiu $2,$2,-1
ld $3,32($fp)
daddu $2,$3,$2
lb $2,0($2)
lw $3,4($fp)
daddiu $3,$3,1
ld $4,32($fp)
daddu $3,$4,$3
lb $3,0($3)
beq $2,$3,.L11
nop
.L10:
lw $3,4($fp)
lw $2,0($fp)
subu $2,$3,$2
addiu $2,$2,1
sw $2,24($fp)
lw $3,16($fp)
lw $2,24($fp)
slt $2,$3,$2
beq $2,$0,.L12
nop
lw $2,24($fp)
sw $2,16($fp)
lw $2,0($fp)
sw $2,12($fp)
.L12:
lw $2,8($fp)
sw $2,0($fp)
lw $2,8($fp)
sw $2,4($fp)
.L5:
lw $3,20($fp)
lw $2,8($fp)
subu $2,$3,$2
sll $2,$2,1
lw $3,16($fp)
slt $2,$3,$2
bne $2,$0,.L6
nop
lw $2,12($fp)
ld $3,32($fp)
daddu $2,$3,$2
sd $2,32($fp)
lw $2,16($fp)
ld $3,32($fp)
daddu $2,$3,$2
sb $0,0($2)
ld $2,32($fp)
.L4:
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
daddiu $sp,$sp,80
jr $31
nop
|
141
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O1
|
mips64 gcc 15.2.0
|
longestPalindrome:
daddiu $sp,$sp,-32
sd $31,24($sp)
sd $28,16($sp)
sd $16,8($sp)
lui $28,%hi(%neg(%gp_rel(longestPalindrome)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(longestPalindrome)))
beq $4,$0,.L11
move $16,$4
lb $2,0($4)
beq $2,$0,.L12
ld $25,%call16(strlen)($28)
1: jalr $25
nop
sll $2,$2,0
blez $2,.L13
move $9,$2
move $11,$0
move $13,$0
move $7,$0
daddiu $14,$16,-1
b .L10
daddiu $12,$16,1
.L14:
move $10,$3
.L6:
subu $10,$10,$7
.L22:
addiu $10,$10,1
slt $2,$11,$10
beq $2,$0,.L20
subu $2,$9,$5
move $11,$10
move $13,$7
.L20:
sll $2,$2,1
slt $2,$11,$2
beq $2,$0,.L3
move $7,$5
.L10:
move $8,$7
daddu $6,$7,$16
move $5,$7
move $3,$5
.L21:
addiu $5,$5,1
slt $2,$5,$9
beq $2,$0,.L4
nop
lb $2,0($6)
daddiu $6,$6,1
lb $4,0($6)
beql $4,$2,.L21
move $3,$5
.L4:
blez $7,.L14
daddu $6,$14,$8
daddu $8,$12,$3
.L7:
move $10,$3
addiu $3,$3,1
slt $2,$3,$9
beql $2,$0,.L22
subu $10,$10,$7
lb $4,0($6)
lb $2,0($8)
bne $4,$2,.L6
daddiu $6,$6,-1
addiu $7,$7,-1
bne $7,$0,.L7
daddiu $8,$8,1
b .L6
move $10,$3
.L13:
move $11,$0
move $13,$0
.L3:
daddu $2,$16,$13
daddu $11,$2,$11
sb $0,0($11)
.L1:
ld $31,24($sp)
ld $28,16($sp)
ld $16,8($sp)
jr $31
daddiu $sp,$sp,32
.L11:
b .L1
move $2,$4
.L12:
b .L1
move $2,$4
|
142
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O2
|
mips64 gcc 15.2.0
|
longestPalindrome:
beq $4,$0,.L11
nop
daddiu $sp,$sp,-32
sd $28,16($sp)
sd $16,8($sp)
sd $31,24($sp)
lb $2,0($4)
lui $28,%hi(%neg(%gp_rel(longestPalindrome)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(longestPalindrome)))
beq $2,$0,.L12
move $16,$4
ld $25,%call16(strlen)($28)
1: jalr $25
nop
sll $2,$2,0
blez $2,.L13
move $4,$0
move $12,$0
move $7,$0
daddiu $14,$16,-1
daddiu $13,$16,1
.L10:
daddu $6,$7,$16
b .L5
move $3,$7
.L24:
daddiu $6,$6,1
lb $9,0($6)
bne $9,$8,.L4
nop
.L5:
move $5,$3
addiu $3,$3,1
slt $8,$3,$2
bnel $8,$0,.L24
lb $8,0($6)
.L4:
blez $7,.L14
daddu $8,$14,$7
b .L7
daddu $6,$13,$5
.L25:
lb $10,0($6)
daddiu $8,$8,-1
bne $11,$10,.L6
daddiu $6,$6,1
addiu $7,$7,-1
beql $7,$0,.L6
addiu $9,$9,1
.L7:
move $9,$5
addiu $5,$5,1
slt $10,$5,$2
bnel $10,$0,.L25
lb $11,0($8)
.L6:
subu $9,$9,$7
addiu $5,$9,1
slt $5,$4,$5
beq $5,$0,.L27
subu $5,$2,$3
addiu $4,$9,1
move $12,$7
.L27:
sll $5,$5,1
slt $5,$4,$5
beql $5,$0,.L26
daddu $2,$16,$12
b .L10
move $7,$3
.L26:
daddu $16,$2,$4
.L3:
sb $0,0($16)
ld $31,24($sp)
ld $28,16($sp)
ld $16,8($sp)
jr $31
daddiu $sp,$sp,32
.L12:
ld $31,24($sp)
ld $28,16($sp)
ld $16,8($sp)
move $2,$4
jr $31
daddiu $sp,$sp,32
.L13:
b .L3
move $2,$16
.L11:
jr $31
move $2,$0
.L14:
b .L6
move $9,$5
|
143
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
mips64
|
-O3
|
mips64 gcc 15.2.0
|
longestPalindrome:
beq $4,$0,.L11
nop
daddiu $sp,$sp,-32
sd $28,16($sp)
sd $16,8($sp)
sd $31,24($sp)
lb $2,0($4)
lui $28,%hi(%neg(%gp_rel(longestPalindrome)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(longestPalindrome)))
beq $2,$0,.L12
move $16,$4
ld $25,%call16(strlen)($28)
1: jalr $25
nop
sll $2,$2,0
blez $2,.L13
move $4,$0
move $12,$0
move $7,$0
daddiu $14,$16,-1
daddiu $13,$16,1
.L10:
daddu $6,$16,$7
b .L5
move $3,$7
.L24:
daddiu $6,$6,1
lb $9,0($6)
bne $9,$8,.L4
nop
.L5:
move $5,$3
addiu $3,$3,1
slt $8,$3,$2
bnel $8,$0,.L24
lb $8,0($6)
.L4:
blez $7,.L14
daddu $8,$14,$7
b .L7
daddu $6,$13,$5
.L25:
lb $10,0($6)
daddiu $8,$8,-1
bne $11,$10,.L6
daddiu $6,$6,1
addiu $7,$7,-1
beql $7,$0,.L6
addiu $9,$9,1
.L7:
move $9,$5
addiu $5,$5,1
slt $10,$5,$2
bnel $10,$0,.L25
lb $11,0($8)
.L6:
subu $9,$9,$7
addiu $5,$9,1
slt $5,$4,$5
beq $5,$0,.L27
subu $5,$2,$3
addiu $4,$9,1
move $12,$7
.L27:
sll $5,$5,1
slt $5,$4,$5
beql $5,$0,.L26
daddu $2,$16,$12
b .L10
move $7,$3
.L26:
daddu $16,$2,$4
.L3:
sb $0,0($16)
ld $31,24($sp)
ld $28,16($sp)
ld $16,8($sp)
jr $31
daddiu $sp,$sp,32
.L12:
ld $31,24($sp)
ld $28,16($sp)
ld $16,8($sp)
move $2,$4
jr $31
daddiu $sp,$sp,32
.L13:
b .L3
move $2,$16
.L11:
jr $31
move $2,$0
.L14:
b .L6
move $9,$5
|
144
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O0
|
RISC-V 64 clang 21.1.0
|
longestPalindrome:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
addi s0, sp, 80
sd a0, -32(s0)
li a0, 0
sw a0, -56(s0)
sw a0, -60(s0)
ld a0, -32(s0)
beqz a0, .LBB0_2
j .LBB0_1
.LBB0_1:
ld a0, -32(s0)
lbu a0, 0(a0)
bnez a0, .LBB0_3
j .LBB0_2
.LBB0_2:
ld a0, -32(s0)
sd a0, -24(s0)
j .LBB0_21
.LBB0_3:
ld a0, -32(s0)
call strlen
sw a0, -36(s0)
li a0, 0
sw a0, -40(s0)
sw a0, -44(s0)
sw a0, -48(s0)
j .LBB0_4
.LBB0_4:
lw a0, -60(s0)
lw a1, -36(s0)
lw a2, -48(s0)
subw a1, a1, a2
slliw a1, a1, 1
bge a0, a1, .LBB0_20
j .LBB0_5
.LBB0_5:
j .LBB0_6
.LBB0_6:
lw a0, -44(s0)
addiw a0, a0, 1
lw a1, -36(s0)
li a2, 0
sd a2, -72(s0)
bge a0, a1, .LBB0_8
j .LBB0_7
.LBB0_7:
ld a1, -32(s0)
lw a2, -44(s0)
add a0, a1, a2
lbu a0, 0(a0)
addiw a2, a2, 1
add a1, a1, a2
lbu a1, 0(a1)
xor a0, a0, a1
seqz a0, a0
sd a0, -72(s0)
j .LBB0_8
.LBB0_8:
ld a0, -72(s0)
andi a0, a0, 1
beqz a0, .LBB0_10
j .LBB0_9
.LBB0_9:
lw a0, -44(s0)
addiw a0, a0, 1
sw a0, -44(s0)
j .LBB0_6
.LBB0_10:
lw a0, -44(s0)
addiw a0, a0, 1
sw a0, -48(s0)
j .LBB0_11
.LBB0_11:
lw a1, -40(s0)
li a0, 0
mv a2, a0
sd a2, -80(s0)
bge a0, a1, .LBB0_14
j .LBB0_12
.LBB0_12:
lw a0, -44(s0)
addiw a0, a0, 1
lw a1, -36(s0)
li a2, 0
sd a2, -80(s0)
bge a0, a1, .LBB0_14
j .LBB0_13
.LBB0_13:
ld a1, -32(s0)
lw a0, -40(s0)
addiw a0, a0, -1
add a0, a0, a1
lbu a0, 0(a0)
lw a2, -44(s0)
addiw a2, a2, 1
add a1, a1, a2
lbu a1, 0(a1)
xor a0, a0, a1
seqz a0, a0
sd a0, -80(s0)
j .LBB0_14
.LBB0_14:
ld a0, -80(s0)
andi a0, a0, 1
beqz a0, .LBB0_16
j .LBB0_15
.LBB0_15:
lw a0, -40(s0)
addiw a0, a0, -1
sw a0, -40(s0)
lw a0, -44(s0)
addiw a0, a0, 1
sw a0, -44(s0)
j .LBB0_11
.LBB0_16:
lw a0, -44(s0)
lw a1, -40(s0)
subw a0, a0, a1
addiw a0, a0, 1
sw a0, -52(s0)
lw a0, -60(s0)
lw a1, -52(s0)
bge a0, a1, .LBB0_18
j .LBB0_17
.LBB0_17:
lw a0, -52(s0)
sw a0, -60(s0)
lw a0, -40(s0)
sw a0, -56(s0)
j .LBB0_18
.LBB0_18:
j .LBB0_19
.LBB0_19:
lw a0, -48(s0)
sw a0, -40(s0)
lw a0, -48(s0)
sw a0, -44(s0)
j .LBB0_4
.LBB0_20:
ld a0, -32(s0)
lw a1, -56(s0)
add a0, a0, a1
sd a0, -32(s0)
ld a0, -32(s0)
lw a1, -60(s0)
add a1, a1, a0
li a0, 0
sb a0, 0(a1)
ld a0, -32(s0)
sd a0, -24(s0)
j .LBB0_21
.LBB0_21:
ld a0, -24(s0)
ld ra, 72(sp)
ld s0, 64(sp)
addi sp, sp, 80
ret
|
145
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O1
|
RISC-V 64 clang 21.1.0
|
longestPalindrome:
beqz a0, .LBB0_27
lbu a1, 0(a0)
beqz a1, .LBB0_27
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
sd s1, 8(sp)
mv s1, a0
call strlen
sext.w t1, a0
blez t1, .LBB0_25
mv t0, a0
li t2, 0
li a6, 0
li s0, 0
slli a1, a0, 33
mv a0, s1
addi a7, s1, 1
srli t6, a1, 33
li t3, 1
j .LBB0_5
.LBB0_4:
subw a2, t0, a1
slliw a2, a2, 1
mv s0, a1
bge t2, a2, .LBB0_26
.LBB0_5:
addiw t4, s0, 1
blt t1, t4, .LBB0_7
mv t4, t1
.LBB0_7:
addiw a5, t4, -1
add a4, a7, s0
addi a3, s0, 1
mv a1, s0
.LBB0_8:
bge a3, t6, .LBB0_11
lbu s1, -1(a4)
lbu a2, 0(a4)
addiw a1, a1, 1
addi a4, a4, 1
addi a3, a3, 1
beq s1, a2, .LBB0_8
addiw a5, a1, -1
bgtz s0, .LBB0_12
j .LBB0_21
.LBB0_11:
mv a1, t4
blez s0, .LBB0_21
.LBB0_12:
addiw t4, a5, 1
blt t1, t4, .LBB0_14
mv t4, t1
.LBB0_14:
add t5, a5, s0
addi t4, t4, -1
addi a2, a5, 1
.LBB0_15:
mv a4, s0
bge a2, t6, .LBB0_19
add a3, a0, a4
add s0, a0, a2
lbu a3, -1(a3)
lbu s0, 0(s0)
bne a3, s0, .LBB0_20
addiw s0, a4, -1
addi a5, a5, 1
addi a2, a2, 1
blt t3, a4, .LBB0_15
li s0, 0
subw a2, t5, s0
bge a2, t2, .LBB0_22
j .LBB0_23
.LBB0_19:
mv s0, a4
mv t5, t4
subw a2, t5, s0
blt a2, t2, .LBB0_23
j .LBB0_22
.LBB0_20:
mv s0, a4
.LBB0_21:
mv t5, a5
subw a2, t5, s0
blt a2, t2, .LBB0_23
.LBB0_22:
mv a6, s0
.LBB0_23:
addiw a2, a2, 1
blt a2, t2, .LBB0_4
mv t2, a2
j .LBB0_4
.LBB0_25:
li a6, 0
li t2, 0
mv a0, s1
.LBB0_26:
add a0, a0, a6
add t2, t2, a0
sb zero, 0(t2)
ld ra, 24(sp)
ld s0, 16(sp)
ld s1, 8(sp)
addi sp, sp, 32
.LBB0_27:
ret
|
146
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O2
|
RISC-V 64 clang 21.1.0
|
longestPalindrome:
beqz a0, .LBB0_27
lbu a1, 0(a0)
beqz a1, .LBB0_27
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
sd s1, 8(sp)
mv s1, a0
call strlen
sext.w t1, a0
blez t1, .LBB0_25
mv t0, a0
li t3, 0
li a6, 0
li s0, 0
slli a1, a0, 33
mv a0, s1
addi a7, s1, 1
srli t6, a1, 33
li t2, 1
j .LBB0_5
.LBB0_4:
subw a2, t0, a1
slliw a2, a2, 1
mv s0, a1
bge t3, a2, .LBB0_26
.LBB0_5:
addiw t4, s0, 1
blt t1, t4, .LBB0_7
mv t4, t1
.LBB0_7:
addiw a2, t4, -1
add a4, a7, s0
addi a3, s0, 1
mv a1, s0
.LBB0_8:
bge a3, t6, .LBB0_11
lbu s1, -1(a4)
lbu a5, 0(a4)
addiw a1, a1, 1
addi a4, a4, 1
addi a3, a3, 1
beq s1, a5, .LBB0_8
addiw a2, a1, -1
bgtz s0, .LBB0_12
j .LBB0_21
.LBB0_11:
mv a1, t4
blez s0, .LBB0_21
.LBB0_12:
addiw t4, a2, 1
blt t1, t4, .LBB0_14
mv t4, t1
.LBB0_14:
add t5, a2, s0
addi t4, t4, -1
addi a5, a2, 1
.LBB0_15:
mv a4, s0
bge a5, t6, .LBB0_19
add a3, a0, a4
add s0, a0, a5
lbu a3, -1(a3)
lbu s0, 0(s0)
bne a3, s0, .LBB0_20
addiw s0, a4, -1
addi a2, a2, 1
addi a5, a5, 1
blt t2, a4, .LBB0_15
li s0, 0
sext.w a2, t5
bge a2, t3, .LBB0_22
j .LBB0_23
.LBB0_19:
mv s0, a4
subw a2, t4, a4
blt a2, t3, .LBB0_23
j .LBB0_22
.LBB0_20:
mv s0, a4
.LBB0_21:
subw a2, a2, s0
blt a2, t3, .LBB0_23
.LBB0_22:
mv a6, s0
.LBB0_23:
addiw a2, a2, 1
blt a2, t3, .LBB0_4
mv t3, a2
j .LBB0_4
.LBB0_25:
li a6, 0
li t3, 0
mv a0, s1
.LBB0_26:
add a0, a0, a6
add t3, t3, a0
sb zero, 0(t3)
ld ra, 24(sp)
ld s0, 16(sp)
ld s1, 8(sp)
addi sp, sp, 32
.LBB0_27:
ret
|
147
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O3
|
RISC-V 64 clang 21.1.0
|
longestPalindrome:
beqz a0, .LBB0_27
lbu a1, 0(a0)
beqz a1, .LBB0_27
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
sd s1, 8(sp)
mv s1, a0
call strlen
sext.w t1, a0
blez t1, .LBB0_25
mv t0, a0
li t3, 0
li a6, 0
li s0, 0
slli a1, a0, 33
mv a0, s1
addi a7, s1, 1
srli t6, a1, 33
li t2, 1
j .LBB0_5
.LBB0_4:
subw a2, t0, a1
slliw a2, a2, 1
mv s0, a1
bge t3, a2, .LBB0_26
.LBB0_5:
addiw t4, s0, 1
blt t1, t4, .LBB0_7
mv t4, t1
.LBB0_7:
addiw a2, t4, -1
add a4, a7, s0
addi a3, s0, 1
mv a1, s0
.LBB0_8:
bge a3, t6, .LBB0_12
lbu s1, -1(a4)
lbu a5, 0(a4)
addiw a1, a1, 1
addi a4, a4, 1
addi a3, a3, 1
beq s1, a5, .LBB0_8
addiw a2, a1, -1
bgtz s0, .LBB0_13
.LBB0_11:
subw a2, a2, s0
bge a2, t3, .LBB0_21
j .LBB0_22
.LBB0_12:
mv a1, t4
blez s0, .LBB0_11
.LBB0_13:
addiw t4, a2, 1
blt t1, t4, .LBB0_15
mv t4, t1
.LBB0_15:
add t5, a2, s0
addi t4, t4, -1
addi a5, a2, 1
.LBB0_16:
mv a4, s0
bge a5, t6, .LBB0_20
add a3, a0, a4
add s0, a0, a5
lbu a3, -1(a3)
lbu s0, 0(s0)
bne a3, s0, .LBB0_24
addiw s0, a4, -1
addi a2, a2, 1
addi a5, a5, 1
blt t2, a4, .LBB0_16
li s0, 0
sext.w a2, t5
bge a2, t3, .LBB0_21
j .LBB0_22
.LBB0_20:
mv s0, a4
subw a2, t4, a4
blt a2, t3, .LBB0_22
.LBB0_21:
mv a6, s0
.LBB0_22:
addiw a2, a2, 1
blt a2, t3, .LBB0_4
mv t3, a2
j .LBB0_4
.LBB0_24:
mv s0, a4
subw a2, a2, a4
bge a2, t3, .LBB0_21
j .LBB0_22
.LBB0_25:
li a6, 0
li t3, 0
mv a0, s1
.LBB0_26:
add a0, a0, a6
add t3, t3, a0
sb zero, 0(t3)
ld ra, 24(sp)
ld s0, 16(sp)
ld s1, 8(sp)
addi sp, sp, 32
.LBB0_27:
ret
|
148
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O0
|
RISC-V 64 gcc 15.2.0
|
longestPalindrome:
addi sp,sp,-64
sd ra,56(sp)
sd s0,48(sp)
addi s0,sp,64
sd a0,-56(s0)
sw zero,-32(s0)
sw zero,-36(s0)
ld a5,-56(s0)
beq a5,zero,.L2
ld a5,-56(s0)
lbu a5,0(a5)
bne a5,zero,.L3
.L2:
ld a5,-56(s0)
j .L4
.L3:
ld a0,-56(s0)
call strlen
mv a5,a0
sw a5,-40(s0)
sw zero,-20(s0)
sw zero,-24(s0)
sw zero,-28(s0)
j .L5
.L8:
lw a5,-24(s0)
addiw a5,a5,1
sw a5,-24(s0)
.L6:
lw a5,-24(s0)
addiw a5,a5,1
sext.w a5,a5
lw a4,-40(s0)
sext.w a4,a4
ble a4,a5,.L7
lw a5,-24(s0)
ld a4,-56(s0)
add a5,a4,a5
lbu a3,0(a5)
lw a5,-24(s0)
addi a5,a5,1
ld a4,-56(s0)
add a5,a4,a5
lbu a5,0(a5)
mv a4,a3
beq a4,a5,.L8
.L7:
lw a5,-24(s0)
addiw a5,a5,1
sw a5,-28(s0)
j .L9
.L11:
lw a5,-20(s0)
addiw a5,a5,-1
sw a5,-20(s0)
lw a5,-24(s0)
addiw a5,a5,1
sw a5,-24(s0)
.L9:
lw a5,-20(s0)
sext.w a5,a5
ble a5,zero,.L10
lw a5,-24(s0)
addiw a5,a5,1
sext.w a5,a5
lw a4,-40(s0)
sext.w a4,a4
ble a4,a5,.L10
lw a5,-20(s0)
addi a5,a5,-1
ld a4,-56(s0)
add a5,a4,a5
lbu a3,0(a5)
lw a5,-24(s0)
addi a5,a5,1
ld a4,-56(s0)
add a5,a4,a5
lbu a5,0(a5)
mv a4,a3
beq a4,a5,.L11
.L10:
lw a5,-24(s0)
mv a4,a5
lw a5,-20(s0)
subw a5,a4,a5
sext.w a5,a5
addiw a5,a5,1
sw a5,-44(s0)
lw a5,-36(s0)
mv a4,a5
lw a5,-44(s0)
sext.w a4,a4
sext.w a5,a5
bge a4,a5,.L12
lw a5,-44(s0)
sw a5,-36(s0)
lw a5,-20(s0)
sw a5,-32(s0)
.L12:
lw a5,-28(s0)
sw a5,-20(s0)
lw a5,-28(s0)
sw a5,-24(s0)
.L5:
lw a5,-40(s0)
mv a4,a5
lw a5,-28(s0)
subw a5,a4,a5
sext.w a5,a5
slliw a5,a5,1
sext.w a5,a5
lw a4,-36(s0)
sext.w a4,a4
blt a4,a5,.L6
lw a5,-32(s0)
ld a4,-56(s0)
add a5,a4,a5
sd a5,-56(s0)
lw a5,-36(s0)
ld a4,-56(s0)
add a5,a4,a5
sb zero,0(a5)
ld a5,-56(s0)
.L4:
mv a0,a5
ld ra,56(sp)
ld s0,48(sp)
addi sp,sp,64
jr ra
|
149
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O1
|
RISC-V 64 gcc 15.2.0
|
longestPalindrome:
addi sp,sp,-16
sd ra,8(sp)
sd s0,0(sp)
mv s0,a0
beq a0,zero,.L1
lbu a5,0(a0)
beq a5,zero,.L1
call strlen
sext.w a7,a0
ble a7,zero,.L13
li t4,0
li t5,0
li a2,0
addi t0,s0,-1
addi t6,s0,1
j .L10
.L14:
mv t3,a5
.L6:
subw t3,t3,a2
addiw t3,t3,1
ble t3,t4,.L9
mv t4,t3
mv t5,a2
.L9:
subw a5,a7,a6
slliw a5,a5,1
ble a5,t4,.L3
mv a2,a6
.L10:
mv a1,a2
add a3,a2,s0
mv a4,a2
.L5:
mv a5,a4
addiw a6,a4,1
mv a4,a6
bge a6,a7,.L4
lbu a0,0(a3)
addi a3,a3,1
lbu t1,0(a3)
beq t1,a0,.L5
.L4:
ble a2,zero,.L14
add a4,t0,a1
add a3,t6,a5
.L7:
mv t3,a5
addiw a1,a5,1
mv a5,a1
bge a1,a7,.L6
lbu t1,0(a4)
lbu a0,0(a3)
bne t1,a0,.L6
addiw a2,a2,-1
addi a4,a4,-1
addi a3,a3,1
bne a2,zero,.L7
mv t3,a1
j .L6
.L13:
li t4,0
li t5,0
.L3:
add a0,s0,t5
add t4,a0,t4
sb zero,0(t4)
.L1:
ld ra,8(sp)
ld s0,0(sp)
addi sp,sp,16
jr ra
|
150
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O2
|
RISC-V 64 gcc 15.2.0
|
longestPalindrome:
beq a0,zero,.L11
lbu a5,0(a0)
mv t5,a0
beq a5,zero,.L21
addi sp,sp,-32
sd ra,24(sp)
sd a0,0(sp)
sd a0,8(sp)
call strlen
ld t5,0(sp)
sext.w a7,a0
mv t4,t5
ble a7,zero,.L3
li a0,0
li t5,0
li a2,0
addi t0,t4,-1
addi t6,t4,1
.L10:
add a3,a2,t4
mv a5,a2
j .L5
.L25:
lbu a1,0(a3)
addi a3,a3,1
lbu a6,0(a3)
bne a6,a1,.L4
.L5:
mv a4,a5
addiw a5,a5,1
blt a5,a7,.L25
.L4:
ble a2,zero,.L15
add a1,t0,a2
add a3,t6,a4
j .L7
.L26:
lbu t3,0(a1)
lbu t1,0(a3)
addi a1,a1,-1
addi a3,a3,1
bne t3,t1,.L6
addiw a2,a2,-1
beq a2,zero,.L15
.L7:
mv a6,a4
addiw a4,a4,1
blt a4,a7,.L26
.L6:
subw a6,a6,a2
addiw a6,a6,1
ble a6,a0,.L9
mv a0,a6
mv t5,a2
.L9:
subw a4,a7,a5
slliw a4,a4,1
ble a4,a0,.L27
mv a2,a5
j .L10
.L27:
add t5,t4,t5
add t4,t5,a0
.L3:
sb zero,0(t4)
ld ra,24(sp)
mv a0,t5
addi sp,sp,32
jr ra
.L15:
mv a6,a4
j .L6
.L11:
li t5,0
.L21:
mv a0,t5
ret
|
151
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
riscv64
|
-O3
|
RISC-V 64 gcc 15.2.0
|
longestPalindrome:
beq a0,zero,.L11
lbu a5,0(a0)
mv t5,a0
beq a5,zero,.L21
addi sp,sp,-32
sd ra,24(sp)
sd a0,0(sp)
sd a0,8(sp)
call strlen
ld t5,0(sp)
sext.w a7,a0
mv t4,t5
ble a7,zero,.L3
li a0,0
li t5,0
li a2,0
addi t0,t4,-1
addi t6,t4,1
.L10:
add a3,t4,a2
mv a5,a2
j .L5
.L25:
lbu a1,0(a3)
addi a3,a3,1
lbu a6,0(a3)
bne a6,a1,.L4
.L5:
mv a4,a5
addiw a5,a5,1
blt a5,a7,.L25
.L4:
ble a2,zero,.L15
add a1,t0,a2
add a3,t6,a4
j .L7
.L26:
lbu t3,0(a1)
lbu t1,0(a3)
addi a1,a1,-1
addi a3,a3,1
bne t3,t1,.L6
addiw a2,a2,-1
beq a2,zero,.L15
.L7:
mv a6,a4
addiw a4,a4,1
blt a4,a7,.L26
.L6:
subw a6,a6,a2
addiw a6,a6,1
ble a6,a0,.L9
mv a0,a6
mv t5,a2
.L9:
subw a4,a7,a5
slliw a4,a4,1
ble a4,a0,.L27
mv a2,a5
j .L10
.L27:
add t5,t4,t5
add t4,t5,a0
.L3:
sb zero,0(t4)
ld ra,24(sp)
mv a0,t5
addi sp,sp,32
jr ra
.L15:
mv a6,a4
j .L6
.L11:
li t5,0
.L21:
mv a0,t5
ret
|
152
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O0
|
x86-64 clang 21.1.0
|
longestPalindrome:
push rbp
mov rbp, rsp
sub rsp, 48
mov qword ptr [rbp - 16], rdi
mov dword ptr [rbp - 40], 0
mov dword ptr [rbp - 44], 0
cmp qword ptr [rbp - 16], 0
je .LBB0_2
mov rax, qword ptr [rbp - 16]
cmp byte ptr [rax], 0
jne .LBB0_3
.LBB0_2:
mov rax, qword ptr [rbp - 16]
mov qword ptr [rbp - 8], rax
jmp .LBB0_21
.LBB0_3:
mov rdi, qword ptr [rbp - 16]
call strlen@PLT
mov dword ptr [rbp - 20], eax
mov dword ptr [rbp - 24], 0
mov dword ptr [rbp - 28], 0
mov dword ptr [rbp - 32], 0
.LBB0_4:
mov eax, dword ptr [rbp - 44]
mov ecx, dword ptr [rbp - 20]
sub ecx, dword ptr [rbp - 32]
shl ecx
cmp eax, ecx
jge .LBB0_20
jmp .LBB0_6
.LBB0_6:
mov ecx, dword ptr [rbp - 28]
add ecx, 1
xor eax, eax
cmp ecx, dword ptr [rbp - 20]
mov byte ptr [rbp - 45], al
jge .LBB0_8
mov rax, qword ptr [rbp - 16]
movsxd rcx, dword ptr [rbp - 28]
movsx eax, byte ptr [rax + rcx]
mov rcx, qword ptr [rbp - 16]
mov edx, dword ptr [rbp - 28]
add edx, 1
movsxd rdx, edx
movsx ecx, byte ptr [rcx + rdx]
cmp eax, ecx
sete al
mov byte ptr [rbp - 45], al
.LBB0_8:
mov al, byte ptr [rbp - 45]
test al, 1
jne .LBB0_9
jmp .LBB0_10
.LBB0_9:
mov eax, dword ptr [rbp - 28]
add eax, 1
mov dword ptr [rbp - 28], eax
jmp .LBB0_6
.LBB0_10:
mov eax, dword ptr [rbp - 28]
add eax, 1
mov dword ptr [rbp - 32], eax
.LBB0_11:
xor eax, eax
cmp dword ptr [rbp - 24], 0
mov byte ptr [rbp - 46], al
jle .LBB0_14
mov ecx, dword ptr [rbp - 28]
add ecx, 1
xor eax, eax
cmp ecx, dword ptr [rbp - 20]
mov byte ptr [rbp - 46], al
jge .LBB0_14
mov rax, qword ptr [rbp - 16]
mov ecx, dword ptr [rbp - 24]
sub ecx, 1
movsxd rcx, ecx
movsx eax, byte ptr [rax + rcx]
mov rcx, qword ptr [rbp - 16]
mov edx, dword ptr [rbp - 28]
add edx, 1
movsxd rdx, edx
movsx ecx, byte ptr [rcx + rdx]
cmp eax, ecx
sete al
mov byte ptr [rbp - 46], al
.LBB0_14:
mov al, byte ptr [rbp - 46]
test al, 1
jne .LBB0_15
jmp .LBB0_16
.LBB0_15:
mov eax, dword ptr [rbp - 24]
add eax, -1
mov dword ptr [rbp - 24], eax
mov eax, dword ptr [rbp - 28]
add eax, 1
mov dword ptr [rbp - 28], eax
jmp .LBB0_11
.LBB0_16:
mov eax, dword ptr [rbp - 28]
sub eax, dword ptr [rbp - 24]
add eax, 1
mov dword ptr [rbp - 36], eax
mov eax, dword ptr [rbp - 44]
cmp eax, dword ptr [rbp - 36]
jge .LBB0_18
mov eax, dword ptr [rbp - 36]
mov dword ptr [rbp - 44], eax
mov eax, dword ptr [rbp - 24]
mov dword ptr [rbp - 40], eax
.LBB0_18:
jmp .LBB0_19
.LBB0_19:
mov eax, dword ptr [rbp - 32]
mov dword ptr [rbp - 24], eax
mov eax, dword ptr [rbp - 32]
mov dword ptr [rbp - 28], eax
jmp .LBB0_4
.LBB0_20:
mov rax, qword ptr [rbp - 16]
movsxd rcx, dword ptr [rbp - 40]
add rax, rcx
mov qword ptr [rbp - 16], rax
mov rax, qword ptr [rbp - 16]
movsxd rcx, dword ptr [rbp - 44]
mov byte ptr [rax + rcx], 0
mov rax, qword ptr [rbp - 16]
mov qword ptr [rbp - 8], rax
.LBB0_21:
mov rax, qword ptr [rbp - 8]
add rsp, 48
pop rbp
ret
|
153
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O1
|
x86-64 clang 21.1.0
|
longestPalindrome:
push rbp
push r14
push rbx
mov rbx, rdi
test rdi, rdi
je .LBB0_21
cmp byte ptr [rbx], 0
je .LBB0_21
mov rdi, rbx
call strlen@PLT
xor r9d, r9d
test eax, eax
jle .LBB0_19
mov ecx, eax
and ecx, 2147483647
xor edx, edx
xor esi, esi
jmp .LBB0_6
.LBB0_4:
mov r10d, r9d
.LBB0_5:
sub r10d, edi
cmp r8d, r10d
cmovle edx, edi
lea r9d, [r10 + 1]
cmp r8d, r9d
cmovg r9d, r8d
mov edi, eax
sub edi, esi
add edi, edi
cmp r9d, edi
jge .LBB0_18
.LBB0_6:
mov edi, esi
movsxd r11, esi
lea r10d, [r11 + 1]
cmp r10d, eax
cmovle r10d, eax
mov r8d, r9d
lea r9d, [r10 - 1]
inc r11
.LBB0_7:
cmp r11, rcx
jge .LBB0_10
movzx ebp, byte ptr [rbx + r11 - 1]
inc esi
cmp bpl, byte ptr [rbx + r11]
lea r11, [r11 + 1]
je .LBB0_7
lea r9d, [rsi - 1]
test edi, edi
jg .LBB0_11
jmp .LBB0_4
.LBB0_10:
mov esi, r10d
test edi, edi
jle .LBB0_4
.LBB0_11:
movsxd r11, r9d
lea r10d, [r11 + rdi]
lea ebp, [r11 + 1]
cmp ebp, eax
cmovle ebp, eax
dec ebp
inc r11
.LBB0_12:
cmp r11, rcx
jge .LBB0_16
mov r14d, edi
movzx r14d, byte ptr [rbx + r14 - 1]
cmp r14b, byte ptr [rbx + r11]
jne .LBB0_4
inc r9d
inc r11
cmp edi, 1
lea edi, [rdi - 1]
jg .LBB0_12
xor edi, edi
jmp .LBB0_5
.LBB0_16:
mov r10d, ebp
jmp .LBB0_5
.LBB0_18:
mov eax, r9d
movsxd r9, edx
jmp .LBB0_20
.LBB0_19:
xor eax, eax
.LBB0_20:
add rbx, r9
mov byte ptr [rax + rbx], 0
.LBB0_21:
mov rax, rbx
pop rbx
pop r14
pop rbp
ret
|
154
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O2
|
x86-64 clang 21.1.0
|
longestPalindrome:
push rbp
push r14
push rbx
test rdi, rdi
je .LBB0_18
mov rbx, rdi
cmp byte ptr [rdi], 0
je .LBB0_22
mov rdi, rbx
call strlen@PLT
xor r9d, r9d
test eax, eax
jle .LBB0_20
mov ecx, eax
and ecx, 2147483647
xor edx, edx
xor esi, esi
jmp .LBB0_6
.LBB0_4:
mov r10d, r9d
.LBB0_5:
sub r10d, edi
cmp r8d, r10d
cmovle edx, edi
lea r9d, [r10 + 1]
cmp r8d, r9d
cmovg r9d, r8d
mov edi, eax
sub edi, esi
add edi, edi
cmp r9d, edi
jge .LBB0_19
.LBB0_6:
mov edi, esi
movsxd r11, esi
lea r10d, [r11 + 1]
cmp r10d, eax
cmovle r10d, eax
mov r8d, r9d
lea r9d, [r10 - 1]
inc r11
.LBB0_7:
cmp r11, rcx
jge .LBB0_10
movzx ebp, byte ptr [rbx + r11 - 1]
inc esi
cmp bpl, byte ptr [rbx + r11]
lea r11, [r11 + 1]
je .LBB0_7
lea r9d, [rsi - 1]
test edi, edi
jg .LBB0_11
jmp .LBB0_4
.LBB0_10:
mov esi, r10d
test edi, edi
jle .LBB0_4
.LBB0_11:
movsxd r11, r9d
lea r10d, [r11 + rdi]
lea ebp, [r11 + 1]
cmp ebp, eax
cmovle ebp, eax
dec ebp
inc r11
.LBB0_12:
cmp r11, rcx
jge .LBB0_16
mov r14d, edi
movzx r14d, byte ptr [rbx + r14 - 1]
cmp r14b, byte ptr [rbx + r11]
jne .LBB0_4
inc r9d
inc r11
cmp edi, 1
lea edi, [rdi - 1]
jg .LBB0_12
xor edi, edi
jmp .LBB0_5
.LBB0_16:
mov r10d, ebp
jmp .LBB0_5
.LBB0_18:
xor ebx, ebx
jmp .LBB0_22
.LBB0_19:
mov eax, r9d
movsxd r9, edx
jmp .LBB0_21
.LBB0_20:
xor eax, eax
.LBB0_21:
add rbx, r9
mov byte ptr [rax + rbx], 0
.LBB0_22:
mov rax, rbx
pop rbx
pop r14
pop rbp
ret
|
155
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O3
|
x86-64 clang 21.1.0
|
longestPalindrome:
push rbp
push r14
push rbx
test rdi, rdi
je .LBB0_18
mov rbx, rdi
cmp byte ptr [rdi], 0
je .LBB0_22
mov rdi, rbx
call strlen@PLT
xor r9d, r9d
test eax, eax
jle .LBB0_20
mov ecx, eax
and ecx, 2147483647
xor edx, edx
xor esi, esi
jmp .LBB0_6
.LBB0_4:
mov r10d, r9d
.LBB0_5:
sub r10d, edi
cmp r8d, r10d
cmovle edx, edi
lea r9d, [r10 + 1]
cmp r8d, r9d
cmovg r9d, r8d
mov edi, eax
sub edi, esi
add edi, edi
cmp r9d, edi
jge .LBB0_19
.LBB0_6:
mov edi, esi
movsxd r11, esi
lea r10d, [r11 + 1]
cmp r10d, eax
cmovle r10d, eax
mov r8d, r9d
lea r9d, [r10 - 1]
inc r11
.LBB0_7:
cmp r11, rcx
jge .LBB0_10
movzx ebp, byte ptr [rbx + r11 - 1]
inc esi
cmp bpl, byte ptr [rbx + r11]
lea r11, [r11 + 1]
je .LBB0_7
lea r9d, [rsi - 1]
test edi, edi
jg .LBB0_11
jmp .LBB0_4
.LBB0_10:
mov esi, r10d
test edi, edi
jle .LBB0_4
.LBB0_11:
movsxd r11, r9d
lea r10d, [r11 + rdi]
lea ebp, [r11 + 1]
cmp ebp, eax
cmovle ebp, eax
dec ebp
inc r11
.LBB0_12:
cmp r11, rcx
jge .LBB0_16
mov r14d, edi
movzx r14d, byte ptr [rbx + r14 - 1]
cmp r14b, byte ptr [rbx + r11]
jne .LBB0_4
inc r9d
inc r11
cmp edi, 1
lea edi, [rdi - 1]
jg .LBB0_12
xor edi, edi
jmp .LBB0_5
.LBB0_16:
mov r10d, ebp
jmp .LBB0_5
.LBB0_18:
xor ebx, ebx
jmp .LBB0_22
.LBB0_19:
mov eax, r9d
movsxd r9, edx
jmp .LBB0_21
.LBB0_20:
xor eax, eax
.LBB0_21:
add rbx, r9
mov byte ptr [rax + rbx], 0
.LBB0_22:
mov rax, rbx
pop rbx
pop r14
pop rbp
ret
|
156
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O0
|
x86-64 gcc 15.2
|
longestPalindrome:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-40], rdi
mov DWORD PTR [rbp-16], 0
mov DWORD PTR [rbp-20], 0
cmp QWORD PTR [rbp-40], 0
je .L2
mov rax, QWORD PTR [rbp-40]
movzx eax, BYTE PTR [rax]
test al, al
jne .L3
.L2:
mov rax, QWORD PTR [rbp-40]
jmp .L4
.L3:
mov rax, QWORD PTR [rbp-40]
mov rdi, rax
call strlen
mov DWORD PTR [rbp-24], eax
mov DWORD PTR [rbp-4], 0
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-12], 0
jmp .L5
.L8:
add DWORD PTR [rbp-8], 1
.L6:
mov eax, DWORD PTR [rbp-8]
add eax, 1
cmp DWORD PTR [rbp-24], eax
jle .L7
mov eax, DWORD PTR [rbp-8]
movsx rdx, eax
mov rax, QWORD PTR [rbp-40]
add rax, rdx
movzx edx, BYTE PTR [rax]
mov eax, DWORD PTR [rbp-8]
cdqe
lea rcx, [rax+1]
mov rax, QWORD PTR [rbp-40]
add rax, rcx
movzx eax, BYTE PTR [rax]
cmp dl, al
je .L8
.L7:
mov eax, DWORD PTR [rbp-8]
add eax, 1
mov DWORD PTR [rbp-12], eax
jmp .L9
.L11:
sub DWORD PTR [rbp-4], 1
add DWORD PTR [rbp-8], 1
.L9:
cmp DWORD PTR [rbp-4], 0
jle .L10
mov eax, DWORD PTR [rbp-8]
add eax, 1
cmp DWORD PTR [rbp-24], eax
jle .L10
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [rax-1]
mov rax, QWORD PTR [rbp-40]
add rax, rdx
movzx edx, BYTE PTR [rax]
mov eax, DWORD PTR [rbp-8]
cdqe
lea rcx, [rax+1]
mov rax, QWORD PTR [rbp-40]
add rax, rcx
movzx eax, BYTE PTR [rax]
cmp dl, al
je .L11
.L10:
mov eax, DWORD PTR [rbp-8]
sub eax, DWORD PTR [rbp-4]
add eax, 1
mov DWORD PTR [rbp-28], eax
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-28]
jge .L12
mov eax, DWORD PTR [rbp-28]
mov DWORD PTR [rbp-20], eax
mov eax, DWORD PTR [rbp-4]
mov DWORD PTR [rbp-16], eax
.L12:
mov eax, DWORD PTR [rbp-12]
mov DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-12]
mov DWORD PTR [rbp-8], eax
.L5:
mov eax, DWORD PTR [rbp-24]
sub eax, DWORD PTR [rbp-12]
add eax, eax
cmp DWORD PTR [rbp-20], eax
jl .L6
mov eax, DWORD PTR [rbp-16]
cdqe
add QWORD PTR [rbp-40], rax
mov eax, DWORD PTR [rbp-20]
movsx rdx, eax
mov rax, QWORD PTR [rbp-40]
add rax, rdx
mov BYTE PTR [rax], 0
mov rax, QWORD PTR [rbp-40]
.L4:
leave
ret
|
157
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O1
|
x86-64 gcc 15.2
|
longestPalindrome:
push r14
push r12
push rbp
push rbx
sub rsp, 8
mov rbx, rdi
test rdi, rdi
je .L11
mov rax, rdi
cmp BYTE PTR [rdi], 0
je .L1
call strlen
mov r8d, eax
test eax, eax
jle .L13
mov r10d, 0
mov r11d, 0
mov esi, 0
lea r12, [rbx-1]
lea rbp, [rbx+1]
jmp .L10
.L14:
mov r9d, eax
.L6:
sub r9d, esi
add r9d, 1
cmp r9d, r10d
cmovg r10d, r9d
cmovg r11d, esi
mov eax, r8d
sub eax, edx
add eax, eax
cmp eax, r10d
jle .L3
mov esi, edx
.L10:
movsx rdi, esi
lea rcx, [rdi+rbx]
mov edx, esi
.L5:
mov eax, edx
add edx, 1
cmp edx, r8d
jge .L4
movzx r9d, BYTE PTR [rcx]
add rcx, 1
cmp r9b, BYTE PTR [rcx]
je .L5
.L4:
test esi, esi
jle .L14
lea rcx, [r12+rdi]
movsx rdi, eax
add rdi, rbp
.L7:
mov r9d, eax
add eax, 1
cmp eax, r8d
jge .L6
movzx r14d, BYTE PTR [rdi]
cmp BYTE PTR [rcx], r14b
jne .L6
sub rcx, 1
add rdi, 1
sub esi, 1
jne .L7
mov r9d, eax
jmp .L6
.L13:
mov r10d, 0
mov r11d, 0
.L3:
movsx rax, r11d
add rax, rbx
movsx r10, r10d
mov BYTE PTR [rax+r10], 0
.L1:
add rsp, 8
pop rbx
pop rbp
pop r12
pop r14
ret
.L11:
mov rax, rdi
jmp .L1
|
158
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O2
|
x86-64 gcc 15.2
|
longestPalindrome:
push r14
push r12
push rbp
push rbx
sub rsp, 8
test rdi, rdi
je .L11
mov rbx, rdi
mov rbp, rdi
cmp BYTE PTR [rdi], 0
je .L1
call strlen
mov r9, rax
test eax, eax
jle .L3
xor r10d, r10d
xor r11d, r11d
lea r12, [rbx-1]
xor esi, esi
lea rbp, [rbx+1]
.L10:
movsx rdi, esi
mov eax, esi
lea rcx, [rdi+rbx]
jmp .L5
.L21:
movzx r8d, BYTE PTR [rcx]
add rcx, 1
cmp r8b, BYTE PTR [rcx]
jne .L4
.L5:
mov edx, eax
add eax, 1
cmp eax, r9d
jl .L21
.L4:
test esi, esi
jle .L15
lea rcx, [r12+rdi]
movsx rdi, edx
add rdi, rbp
jmp .L7
.L22:
movzx r14d, BYTE PTR [rdi]
cmp BYTE PTR [rcx], r14b
jne .L6
sub rcx, 1
add rdi, 1
sub esi, 1
je .L15
.L7:
mov r8d, edx
lea edx, [rdx+1]
cmp edx, r9d
jl .L22
.L6:
sub r8d, esi
mov edx, r9d
add r8d, 1
cmp r8d, r10d
cmovg r10d, r8d
cmovg r11d, esi
sub edx, eax
add edx, edx
cmp edx, r10d
jle .L23
mov esi, eax
jmp .L10
.L23:
movsx r11, r11d
movsx r10, r10d
lea rbp, [rbx+r11]
lea rbx, [rbp+0+r10]
.L3:
mov BYTE PTR [rbx], 0
.L1:
add rsp, 8
mov rax, rbp
pop rbx
pop rbp
pop r12
pop r14
ret
.L15:
mov r8d, edx
jmp .L6
.L11:
add rsp, 8
xor ebp, ebp
pop rbx
mov rax, rbp
pop rbp
pop r12
pop r14
ret
|
159
| 5
|
Longest Palindromic Substring
|
Medium
|
/*
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
char* longestPalindrome(char* s) {
int sz;
int i, j, k, l;
int where = 0, len = 0;
if (!s || !*s) return s;
sz = strlen(s);
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
while (j + 1 < sz && s[j] == s[j + 1]) {
j ++; // skip all repeating characters
}
k = j + 1; // where to start next try
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
i --; // expand dual direction
j ++;
}
l = j - i + 1; // what we have fond so far
if (len < l) {
len = l;
where = i;
}
}
s = s + where;
s[len] = 0;
return s;
}
/*
Difficulty:Medium
Total Accepted:222.3K
Total Submissions:883.5K
Companies Amazon Microsoft Bloomberg
Related Topics String
Similar Questions
Shortest Palindrome
Palindrome Permutation
Palindrome Pairs
Longest Palindromic Subsequence
Palindromic Substrings
*/
|
x86-64
|
-O3
|
x86-64 gcc 15.2
|
longestPalindrome:
push r14
push r12
push rbp
push rbx
sub rsp, 8
test rdi, rdi
je .L11
mov rbx, rdi
mov rbp, rdi
cmp BYTE PTR [rdi], 0
je .L1
call strlen
mov r9, rax
test eax, eax
jle .L3
xor r10d, r10d
xor r11d, r11d
lea r12, [rbx-1]
xor esi, esi
lea rbp, [rbx+1]
.L10:
movsx rdi, esi
mov eax, esi
lea rcx, [rbx+rdi]
jmp .L5
.L21:
movzx r8d, BYTE PTR [rcx]
add rcx, 1
cmp r8b, BYTE PTR [rcx]
jne .L4
.L5:
mov edx, eax
add eax, 1
cmp eax, r9d
jl .L21
.L4:
test esi, esi
jle .L15
lea rcx, [r12+rdi]
movsx rdi, edx
add rdi, rbp
jmp .L7
.L22:
movzx r14d, BYTE PTR [rdi]
cmp BYTE PTR [rcx], r14b
jne .L6
sub rcx, 1
add rdi, 1
sub esi, 1
je .L15
.L7:
mov r8d, edx
lea edx, [rdx+1]
cmp edx, r9d
jl .L22
.L6:
sub r8d, esi
mov edx, r9d
add r8d, 1
cmp r8d, r10d
cmovg r10d, r8d
cmovg r11d, esi
sub edx, eax
add edx, edx
cmp edx, r10d
jle .L23
mov esi, eax
jmp .L10
.L23:
movsx r11, r11d
movsx r10, r10d
lea rbp, [rbx+r11]
lea rbx, [rbp+0+r10]
.L3:
mov BYTE PTR [rbx], 0
.L1:
add rsp, 8
mov rax, rbp
pop rbx
pop rbp
pop r12
pop r14
ret
.L15:
mov r8d, edx
jmp .L6
.L11:
add rsp, 8
xor ebp, ebp
pop rbx
mov rax, rbp
pop rbp
pop r12
pop r14
ret
|
160
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O0
|
ARM64 gcc 15.2.0
|
convert:
stp x29, x30, [sp, -80]!
mov x29, sp
str x0, [sp, 24]
str w1, [sp, 20]
str wzr, [sp, 68]
ldr x0, [sp, 24]
cmp x0, 0
beq .L2
ldr x0, [sp, 24]
ldrb w0, [x0]
cmp w0, 0
beq .L2
ldr w0, [sp, 20]
cmp w0, 1
bne .L3
.L2:
ldr x0, [sp, 24]
b .L4
.L3:
ldr x0, [sp, 24]
bl strlen
str w0, [sp, 60]
ldr w0, [sp, 60]
add w0, w0, 1
sxtw x0, w0
bl malloc
str x0, [sp, 48]
ldr w0, [sp, 20]
sub w0, w0, #1
lsl w0, w0, 1
str w0, [sp, 44]
str wzr, [sp, 76]
b .L5
.L11:
ldr w0, [sp, 76]
str w0, [sp, 72]
mov w0, 1
str w0, [sp, 64]
b .L6
.L10:
ldrsw x0, [sp, 72]
ldr x1, [sp, 24]
add x1, x1, x0
ldr w0, [sp, 68]
add w2, w0, 1
str w2, [sp, 68]
sxtw x0, w0
ldr x2, [sp, 48]
add x0, x2, x0
ldrb w1, [x1]
strb w1, [x0]
ldr w0, [sp, 76]
cmp w0, 0
beq .L7
ldr w0, [sp, 20]
sub w0, w0, #1
ldr w1, [sp, 76]
cmp w1, w0
bne .L8
.L7:
ldr w1, [sp, 72]
ldr w0, [sp, 44]
add w0, w1, w0
str w0, [sp, 72]
b .L6
.L8:
ldr w0, [sp, 64]
cmp w0, 0
beq .L9
ldr w0, [sp, 76]
lsl w0, w0, 1
ldr w1, [sp, 44]
sub w0, w1, w0
ldr w1, [sp, 72]
add w0, w1, w0
str w0, [sp, 72]
str wzr, [sp, 64]
b .L6
.L9:
ldr w0, [sp, 76]
lsl w0, w0, 1
ldr w1, [sp, 72]
add w0, w1, w0
str w0, [sp, 72]
mov w0, 1
str w0, [sp, 64]
.L6:
ldr w1, [sp, 72]
ldr w0, [sp, 60]
cmp w1, w0
blt .L10
ldr w0, [sp, 76]
add w0, w0, 1
str w0, [sp, 76]
.L5:
ldr w1, [sp, 76]
ldr w0, [sp, 20]
cmp w1, w0
blt .L11
ldrsw x0, [sp, 68]
ldr x1, [sp, 48]
add x0, x1, x0
strb wzr, [x0]
ldr x0, [sp, 48]
.L4:
ldp x29, x30, [sp], 80
ret
|
161
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O1
|
ARM64 gcc 15.2.0
|
convert:
stp x29, x30, [sp, -48]!
mov x29, sp
stp x19, x20, [sp, 16]
mov x19, x0
cbz x0, .L2
str x21, [sp, 32]
mov w21, w1
ldrb w0, [x0]
cmp w0, 0
ccmp w1, 1, 4, ne
bne .L15
ldr x21, [sp, 32]
.L2:
mov x0, x19
ldp x19, x20, [sp, 16]
ldp x29, x30, [sp], 48
ret
.L15:
mov x0, x19
bl strlen
mov w20, w0
add w0, w0, 1
sxtw x0, w0
bl malloc
cmp w21, 0
ble .L11
sub w12, w21, #1
lsl w7, w12, 1
mov w8, w7
mov w2, 0
mov w6, 0
mov w9, 1
mov w13, 0
b .L4
.L5:
cbz w5, .L7
add w1, w1, w10
mov w5, w13
.L6:
add x3, x2, 1
cmp w20, w1
ble .L16
mov x2, x3
.L8:
ldrb w3, [x19, w1, sxtw]
strb w3, [x0, x2]
cbz w4, .L5
add w1, w1, w7
b .L6
.L7:
add w1, w1, w11
mov w5, w9
b .L6
.L16:
add w2, w2, 1
.L10:
add w6, w6, 1
sub w8, w8, #2
cmn w8, #2
beq .L3
.L4:
cmp w20, w6
ble .L10
cmp w6, 0
ccmp w12, w6, 4, ne
lsl w11, w6, 1
mov w10, w8
sxtw x2, w2
mov w1, w6
mov w5, w9
cset w4, eq
b .L8
.L11:
mov w2, 0
.L3:
strb wzr, [x0, w2, sxtw]
mov x19, x0
ldr x21, [sp, 32]
b .L2
|
162
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O2
|
ARM64 gcc 15.2.0
|
convert:
mov x5, x0
cbz x0, .L23
ldrb w2, [x0]
cmp w2, 0
ccmp w1, 1, 4, ne
bne .L27
.L23:
mov x0, x5
ret
.L27:
stp x29, x30, [sp, -48]!
mov x29, sp
str w1, [sp, 36]
str x0, [sp, 40]
bl strlen
mov x6, x0
add w0, w0, 1
str x6, [sp, 24]
sxtw x0, w0
bl malloc
mov x4, x0
ldr w1, [sp, 36]
ldr x6, [sp, 24]
cmp w1, 0
ldr x5, [sp, 40]
ble .L4
sub w9, w1, #1
mov w4, 0
mov w7, 0
lsl w10, w9, 1
.L12:
cmp w6, w7
ble .L8
sxtw x2, w4
ldrb w3, [x5, w7, sxtw]
cmp w7, 0
add w4, w4, 1
ccmp w9, w7, 4, ne
lsl w11, w7, 1
strb w3, [x0, x2]
mov w3, w7
beq .L5
b .L20
.L10:
add x2, x2, 1
ldrb w8, [x5, w3, sxtw]
add w3, w3, w11
add w4, w2, 1
strb w8, [x0, x2]
cmp w6, w3
ble .L8
add x2, x2, 1
ldrb w8, [x5, w3, sxtw]
add w4, w2, 1
strb w8, [x0, x2]
.L20:
add w3, w3, w10
cmp w6, w3
bgt .L10
.L8:
add w7, w7, 1
sub w10, w10, #2
cmp w1, w7
bne .L12
.L28:
add x4, x0, w4, sxtw
.L4:
strb wzr, [x4]
ldp x29, x30, [sp], 48
ret
.L7:
add x2, x2, 1
ldrb w8, [x5, w3, sxtw]
add w4, w2, 1
strb w8, [x0, x2]
.L5:
add w3, w3, w9, lsl 1
cmp w6, w3
bgt .L7
add w7, w7, 1
sub w10, w10, #2
cmp w1, w7
bne .L12
b .L28
|
163
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O3
|
ARM64 gcc 15.2.0
|
convert:
mov x3, x0
cbz x0, .L23
ldrb w2, [x0]
cmp w2, 0
ccmp w1, 1, 4, ne
bne .L27
.L23:
mov x0, x3
ret
.L27:
stp x29, x30, [sp, -48]!
mov x29, sp
str w1, [sp, 36]
str x0, [sp, 40]
bl strlen
mov x5, x0
add w0, w0, 1
str x5, [sp, 24]
sxtw x0, w0
bl malloc
mov x4, x0
ldr w1, [sp, 36]
ldr x5, [sp, 24]
cmp w1, 0
ldr x3, [sp, 40]
ble .L4
cmp w5, 0
ble .L15
sub w13, w1, #1
cmp w1, w5
csel w10, w1, w5, le
mov x6, 0
lsl w9, w13, 1
mov w4, 0
sxtw x7, w9
.L9:
cmp x6, 0
ccmp w6, w13, 4, ne
beq .L13
sxtw x1, w4
ldrb w8, [x3, x6]
add w4, w4, 1
mov w2, w6
lsl w12, w6, 1
strb w8, [x0, x1]
b .L7
.L29:
ldrb w8, [x3, w8, sxtw]
add w4, w1, 1
strb w8, [x0, x11]
cmp w5, w2
ble .L28
ldrb w8, [x3, w2, sxtw]
strb w8, [x0, x1]
.L7:
add w8, w9, w2
add x11, x1, 1
add w2, w12, w8
add x1, x1, 2
cmp w5, w8
bgt .L29
add x6, x6, 1
sub w9, w9, #2
cmp w10, w6
bgt .L9
.L11:
add x4, x0, w4, sxtw
.L4:
strb wzr, [x4]
ldp x29, x30, [sp], 48
ret
.L13:
sxtw x2, w4
mov x1, x6
.L8:
ldrb w4, [x3, x1]
add x1, x1, x7
strb w4, [x0, x2]
mov x4, x2
add x2, x2, 1
cmp w5, w1
bgt .L8
add x6, x6, 1
add w4, w4, 1
sub w9, w9, #2
cmp w10, w6
bgt .L9
b .L11
.L28:
add x6, x6, 1
add w4, w11, 1
sub w9, w9, #2
cmp w10, w6
bgt .L9
b .L11
.L15:
mov w4, 0
b .L11
|
164
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O0
|
armv8-a clang 21.1.0
|
convert:
sub sp, sp, #80
stp x29, x30, [sp, #64]
add x29, sp, #64
stur x0, [x29, #-16]
stur w1, [x29, #-20]
str wzr, [sp, #28]
ldur x8, [x29, #-16]
cbz x8, .LBB0_3
b .LBB0_1
.LBB0_1:
ldur x8, [x29, #-16]
ldrb w8, [x8]
cbz w8, .LBB0_3
b .LBB0_2
.LBB0_2:
ldur w8, [x29, #-20]
subs w8, w8, #1
b.ne .LBB0_4
b .LBB0_3
.LBB0_3:
ldur x8, [x29, #-16]
stur x8, [x29, #-8]
b .LBB0_19
.LBB0_4:
ldur x0, [x29, #-16]
bl strlen
mov w8, w0
stur w8, [x29, #-24]
ldur w8, [x29, #-24]
add w9, w8, #1
mov w8, w9
sxtw x8, w8
lsr x0, x8, #0
bl malloc
str x0, [sp, #16]
ldur w8, [x29, #-20]
subs w8, w8, #1
lsl w8, w8, #1
str w8, [sp, #12]
stur wzr, [x29, #-28]
b .LBB0_5
.LBB0_5:
ldur w8, [x29, #-28]
ldur w9, [x29, #-20]
subs w8, w8, w9
b.ge .LBB0_18
b .LBB0_6
.LBB0_6:
ldur w8, [x29, #-28]
str w8, [sp, #32]
mov w8, #1
str w8, [sp, #8]
b .LBB0_7
.LBB0_7:
ldr w8, [sp, #32]
ldur w9, [x29, #-24]
subs w8, w8, w9
b.ge .LBB0_16
b .LBB0_8
.LBB0_8:
ldur x8, [x29, #-16]
ldrsw x9, [sp, #32]
add x8, x8, x9
ldrb w8, [x8]
ldr x9, [sp, #16]
ldrsw x10, [sp, #28]
mov w11, w10
add w11, w11, #1
str w11, [sp, #28]
add x9, x9, x10
strb w8, [x9]
ldur w8, [x29, #-28]
cbz w8, .LBB0_10
b .LBB0_9
.LBB0_9:
ldur w8, [x29, #-28]
ldur w9, [x29, #-20]
subs w9, w9, #1
subs w8, w8, w9
b.ne .LBB0_11
b .LBB0_10
.LBB0_10:
ldr w9, [sp, #12]
ldr w8, [sp, #32]
add w8, w8, w9
str w8, [sp, #32]
b .LBB0_15
.LBB0_11:
ldr w8, [sp, #8]
cbz w8, .LBB0_13
b .LBB0_12
.LBB0_12:
ldr w8, [sp, #12]
ldur w9, [x29, #-28]
subs w9, w8, w9, lsl #1
ldr w8, [sp, #32]
add w8, w8, w9
str w8, [sp, #32]
str wzr, [sp, #8]
b .LBB0_14
.LBB0_13:
ldur w9, [x29, #-28]
ldr w8, [sp, #32]
add w8, w8, w9, lsl #1
str w8, [sp, #32]
mov w8, #1
str w8, [sp, #8]
b .LBB0_14
.LBB0_14:
b .LBB0_15
.LBB0_15:
b .LBB0_7
.LBB0_16:
b .LBB0_17
.LBB0_17:
ldur w8, [x29, #-28]
add w8, w8, #1
stur w8, [x29, #-28]
b .LBB0_5
.LBB0_18:
ldr x8, [sp, #16]
ldrsw x9, [sp, #28]
add x8, x8, x9
strb wzr, [x8]
ldr x8, [sp, #16]
stur x8, [x29, #-8]
b .LBB0_19
.LBB0_19:
ldur x0, [x29, #-8]
ldp x29, x30, [sp, #64]
add sp, sp, #80
ret
|
165
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O1
|
armv8-a clang 21.1.0
|
convert:
cbz x0, .LBB0_12
cmp w1, #1
b.eq .LBB0_12
ldrb w8, [x0]
cbz w8, .LBB0_12
stp x29, x30, [sp, #-48]!
str x21, [sp, #16]
stp x20, x19, [sp, #32]
mov x29, sp
mov x20, x0
mov w21, w1
bl strlen
mov x19, x0
add w8, w19, #1
sxtw x0, w8
bl malloc
subs w9, w21, #1
b.lt .LBB0_10
mov w8, w21
mov w11, wzr
mov w10, wzr
mov x12, x20
b .LBB0_6
.LBB0_5:
add w11, w11, #1
cmp w11, w8
b.eq .LBB0_9
.LBB0_6:
cmp w11, w19
b.ge .LBB0_5
cmp w11, #0
add x13, x0, w10, sxtw
sub w15, w9, w11
ccmp w9, w11, #4, ne
mov w16, #1
mov w17, w11
cset w14, ne
.LBB0_8:
cmp w16, #0
ldrb w1, [x12, w17, sxtw]
eor w16, w16, w14
csel w18, w11, w15, eq
cmp w14, #0
add w10, w10, #1
csel w18, w18, w9, ne
strb w1, [x13], #1
add w17, w17, w18, lsl #1
cmp w17, w19
b.lt .LBB0_8
b .LBB0_5
.LBB0_9:
sxtw x8, w10
b .LBB0_11
.LBB0_10:
mov x8, xzr
.LBB0_11:
strb wzr, [x0, x8]
ldp x20, x19, [sp, #32]
ldr x21, [sp, #16]
ldp x29, x30, [sp], #48
.LBB0_12:
ret
|
166
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O2
|
armv8-a clang 21.1.0
|
convert:
cbz x0, .LBB0_12
cmp w1, #1
b.eq .LBB0_12
ldrb w8, [x0]
cbz w8, .LBB0_12
stp x29, x30, [sp, #-48]!
str x21, [sp, #16]
stp x20, x19, [sp, #32]
mov x29, sp
mov x20, x0
mov w21, w1
bl strlen
mov x19, x0
add w8, w19, #1
sxtw x0, w8
bl malloc
subs w9, w21, #1
b.lt .LBB0_10
mov w8, w21
mov w11, wzr
mov w10, wzr
mov x12, x20
b .LBB0_6
.LBB0_5:
add w11, w11, #1
cmp w11, w8
b.eq .LBB0_9
.LBB0_6:
cmp w11, w19
b.ge .LBB0_5
cmp w11, #0
add x13, x0, w10, sxtw
sub w15, w9, w11
ccmp w9, w11, #4, ne
mov w16, #1
mov w17, w11
cset w14, ne
.LBB0_8:
cmp w16, #0
ldrb w1, [x12, w17, sxtw]
eor w16, w16, w14
csel w18, w11, w15, eq
cmp w14, #0
add w10, w10, #1
csel w18, w18, w9, ne
strb w1, [x13], #1
add w17, w17, w18, lsl #1
cmp w17, w19
b.lt .LBB0_8
b .LBB0_5
.LBB0_9:
sxtw x8, w10
b .LBB0_11
.LBB0_10:
mov x8, xzr
.LBB0_11:
strb wzr, [x0, x8]
ldp x20, x19, [sp, #32]
ldr x21, [sp, #16]
ldp x29, x30, [sp], #48
.LBB0_12:
ret
|
167
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
aarch64
|
-O3
|
armv8-a clang 21.1.0
|
convert:
cbz x0, .LBB0_15
cmp w1, #1
b.eq .LBB0_15
ldrb w8, [x0]
cbz w8, .LBB0_15
stp x29, x30, [sp, #-48]!
str x21, [sp, #16]
stp x20, x19, [sp, #32]
mov x29, sp
mov x20, x0
mov w21, w1
bl strlen
mov x19, x0
add w8, w19, #1
sxtw x0, w8
bl malloc
subs w8, w21, #1
b.lt .LBB0_13
lsl w11, w8, #1
sxtw x12, w19
mov x10, xzr
mov w9, wzr
mov w13, w21
mov x14, x20
sxtw x11, w11
b .LBB0_6
.LBB0_5:
add x10, x10, #1
cmp x10, x13
b.eq .LBB0_12
.LBB0_6:
cmp w10, w19
b.ge .LBB0_5
cmp w10, #0
sxtw x17, w9
ccmp w8, w10, #4, ne
cset w15, ne
b.eq .LBB0_10
sub w16, w8, w10
add x17, x0, x17
mov w18, #1
mov w1, w10
.LBB0_9:
cmp w18, #0
ldrb w3, [x14, w1, sxtw]
eor w18, w18, w15
csel w2, w10, w16, eq
add w9, w9, #1
add w1, w1, w2, lsl #1
strb w3, [x17], #1
cmp w1, w19
b.lt .LBB0_9
b .LBB0_5
.LBB0_10:
add x15, x0, x17
mov x16, x10
.LBB0_11:
ldrb w17, [x14, x16]
add x16, x16, x11
add w9, w9, #1
cmp x16, x12
strb w17, [x15], #1
b.lt .LBB0_11
b .LBB0_5
.LBB0_12:
sxtw x8, w9
b .LBB0_14
.LBB0_13:
mov x8, xzr
.LBB0_14:
strb wzr, [x0, x8]
ldp x20, x19, [sp, #32]
ldr x21, [sp, #16]
ldp x29, x30, [sp], #48
.LBB0_15:
ret
|
168
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O0
|
mips64 clang 21.1.0
|
convert:
.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(convert)))
daddu $1, $1, $25
daddiu $1, $1, %lo(%neg(%gp_rel(convert)))
sd $1, 8($fp)
move $1, $5
sd $4, 56($fp)
sw $1, 52($fp)
sw $zero, 36($fp)
ld $1, 56($fp)
beqz $1, .LBB0_6
nop
b .LBB0_2
nop
.LBB0_2:
ld $1, 56($fp)
lbu $1, 0($1)
beqz $1, .LBB0_6
nop
b .LBB0_4
nop
.LBB0_4:
lw $1, 52($fp)
addiu $2, $zero, 1
bne $1, $2, .LBB0_7
nop
b .LBB0_6
nop
.LBB0_6:
ld $1, 56($fp)
sd $1, 64($fp)
b .LBB0_27
nop
.LBB0_7:
ld $gp, 8($fp)
ld $4, 56($fp)
ld $25, %call16(strlen)($gp)
jalr $25
nop
ld $gp, 8($fp)
sw $2, 48($fp)
lw $1, 48($fp)
addiu $1, $1, 1
move $4, $1
ld $25, %call16(malloc)($gp)
jalr $25
nop
sd $2, 24($fp)
lw $1, 52($fp)
sll $1, $1, 1
addiu $1, $1, -2
sw $1, 20($fp)
sw $zero, 44($fp)
b .LBB0_8
nop
.LBB0_8:
lw $1, 44($fp)
lw $2, 52($fp)
slt $1, $1, $2
beqz $1, .LBB0_26
nop
b .LBB0_10
nop
.LBB0_10:
lw $1, 44($fp)
sw $1, 40($fp)
addiu $1, $zero, 1
sw $1, 16($fp)
b .LBB0_11
nop
.LBB0_11:
lw $1, 40($fp)
lw $2, 48($fp)
slt $1, $1, $2
beqz $1, .LBB0_24
nop
b .LBB0_13
nop
.LBB0_13:
ld $1, 56($fp)
lw $2, 40($fp)
daddu $1, $1, $2
lbu $1, 0($1)
ld $2, 24($fp)
lw $3, 36($fp)
addiu $4, $3, 1
sw $4, 36($fp)
sll $3, $3, 0
daddu $2, $2, $3
sb $1, 0($2)
lw $1, 44($fp)
beqz $1, .LBB0_17
nop
b .LBB0_15
nop
.LBB0_15:
lw $1, 44($fp)
lw $2, 52($fp)
addiu $2, $2, -1
bne $1, $2, .LBB0_18
nop
b .LBB0_17
nop
.LBB0_17:
lw $2, 20($fp)
lw $1, 40($fp)
addu $1, $1, $2
sw $1, 40($fp)
b .LBB0_23
nop
.LBB0_18:
lw $1, 16($fp)
beqz $1, .LBB0_21
nop
b .LBB0_20
nop
.LBB0_20:
lw $1, 20($fp)
lw $2, 44($fp)
sll $2, $2, 1
subu $2, $1, $2
lw $1, 40($fp)
addu $1, $1, $2
sw $1, 40($fp)
sw $zero, 16($fp)
b .LBB0_22
nop
.LBB0_21:
lw $1, 44($fp)
sll $2, $1, 1
lw $1, 40($fp)
addu $1, $1, $2
sw $1, 40($fp)
addiu $1, $zero, 1
sw $1, 16($fp)
b .LBB0_22
nop
.LBB0_22:
b .LBB0_23
nop
.LBB0_23:
b .LBB0_11
nop
.LBB0_24:
b .LBB0_25
nop
.LBB0_25:
lw $1, 44($fp)
addiu $1, $1, 1
sw $1, 44($fp)
b .LBB0_8
nop
.LBB0_26:
ld $1, 24($fp)
lw $2, 36($fp)
daddu $1, $1, $2
addiu $2, $zero, 0
sb $zero, 0($1)
ld $1, 24($fp)
sd $1, 64($fp)
b .LBB0_27
nop
.LBB0_27:
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
|
169
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O1
|
mips64 clang 21.1.0
|
convert:
.Lfunc_begin0 = .Ltmp0
lui $1, %hi(%neg(%gp_rel(convert)))
beqz $4, .LBB0_10
daddu $2, $1, $25
addiu $1, $zero, 1
beq $5, $1, .LBB0_13
nop
lbu $1, 0($4)
andi $1, $1, 255
beqz $1, .LBB0_12
nop
daddiu $sp, $sp, -48
sd $ra, 40($sp)
sd $fp, 32($sp)
sd $gp, 24($sp)
sd $18, 16($sp)
sd $17, 8($sp)
sd $16, 0($sp)
move $fp, $sp
daddiu $gp, $2, %lo(%neg(%gp_rel(convert)))
ld $25, %call16(strlen)($gp)
move $17, $4
jalr $25
move $18, $5
move $16, $2
dsll $1, $2, 32
daddiu $2, $zero, 1
dsll $2, $2, 32
daddu $1, $1, $2
ld $25, %call16(malloc)($gp)
jalr $25
dsra $4, $1, 32
move $3, $18
blez $3, .LBB0_14
nop
sll $4, $16, 0
addiu $5, $3, -1
addiu $6, $zero, 0
addiu $7, $zero, 0
b .LBB0_6
move $8, $17
.LBB0_5:
addiu $6, $6, 1
beq $6, $3, .LBB0_11
nop
.LBB0_6:
slt $1, $6, $4
beqz $1, .LBB0_5
nop
sll $1, $7, 0
subu $9, $5, $6
daddu $10, $2, $1
xor $1, $5, $6
sltu $1, $zero, $1
sltu $11, $zero, $6
and $11, $11, $1
addiu $12, $zero, 1
move $13, $6
.LBB0_8:
move $1, $9
movz $1, $6, $12
move $14, $5
movn $14, $1, $11
sll $1, $14, 1
addu $1, $1, $13
sll $13, $13, 0
daddu $13, $8, $13
lbu $13, 0($13)
sb $13, 0($10)
slt $14, $1, $4
xor $12, $12, $11
addiu $7, $7, 1
daddiu $10, $10, 1
bnez $14, .LBB0_8
move $13, $1
b .LBB0_5
nop
.LBB0_10:
jr $ra
move $2, $4
.LBB0_11:
b .LBB0_15
sll $3, $7, 0
.LBB0_12:
jr $ra
move $2, $4
.LBB0_13:
jr $ra
move $2, $4
.LBB0_14:
daddiu $3, $zero, 0
.LBB0_15:
daddu $1, $2, $3
sb $zero, 0($1)
move $sp, $fp
ld $16, 0($sp)
ld $17, 8($sp)
ld $18, 16($sp)
ld $gp, 24($sp)
ld $fp, 32($sp)
ld $ra, 40($sp)
jr $ra
daddiu $sp, $sp, 48
|
170
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O2
|
mips64 clang 21.1.0
|
convert:
.Lfunc_begin0 = .Ltmp0
lui $1, %hi(%neg(%gp_rel(convert)))
beqz $4, .LBB0_10
daddu $2, $1, $25
addiu $1, $zero, 1
beq $5, $1, .LBB0_13
nop
lbu $1, 0($4)
andi $1, $1, 255
beqz $1, .LBB0_12
nop
daddiu $sp, $sp, -48
sd $ra, 40($sp)
sd $fp, 32($sp)
sd $gp, 24($sp)
sd $18, 16($sp)
sd $17, 8($sp)
sd $16, 0($sp)
move $fp, $sp
daddiu $gp, $2, %lo(%neg(%gp_rel(convert)))
ld $25, %call16(strlen)($gp)
move $17, $4
jalr $25
move $18, $5
move $16, $2
dsll $1, $2, 32
daddiu $2, $zero, 1
dsll $2, $2, 32
daddu $1, $1, $2
ld $25, %call16(malloc)($gp)
jalr $25
dsra $4, $1, 32
move $3, $18
blez $3, .LBB0_14
nop
sll $4, $16, 0
addiu $5, $3, -1
addiu $6, $zero, 0
addiu $7, $zero, 0
b .LBB0_6
move $8, $17
.LBB0_5:
addiu $6, $6, 1
beq $6, $3, .LBB0_11
nop
.LBB0_6:
slt $1, $6, $4
beqz $1, .LBB0_5
nop
sll $1, $7, 0
subu $9, $5, $6
daddu $10, $2, $1
xor $1, $5, $6
sltu $1, $zero, $1
sltu $11, $zero, $6
and $11, $11, $1
addiu $12, $zero, 1
move $13, $6
.LBB0_8:
move $1, $9
movz $1, $6, $12
move $14, $5
movn $14, $1, $11
sll $1, $14, 1
addu $1, $1, $13
sll $13, $13, 0
daddu $13, $8, $13
lbu $13, 0($13)
sb $13, 0($10)
slt $14, $1, $4
xor $12, $12, $11
addiu $7, $7, 1
daddiu $10, $10, 1
bnez $14, .LBB0_8
move $13, $1
b .LBB0_5
nop
.LBB0_10:
jr $ra
daddiu $2, $zero, 0
.LBB0_11:
b .LBB0_15
sll $3, $7, 0
.LBB0_12:
jr $ra
move $2, $4
.LBB0_13:
jr $ra
move $2, $4
.LBB0_14:
daddiu $3, $zero, 0
.LBB0_15:
daddu $1, $2, $3
sb $zero, 0($1)
move $sp, $fp
ld $16, 0($sp)
ld $17, 8($sp)
ld $18, 16($sp)
ld $gp, 24($sp)
ld $fp, 32($sp)
ld $ra, 40($sp)
jr $ra
daddiu $sp, $sp, 48
|
171
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O3
|
mips64 clang 21.1.0
|
convert:
.Lfunc_begin0 = .Ltmp0
lui $1, %hi(%neg(%gp_rel(convert)))
beqz $4, .LBB0_14
daddu $2, $1, $25
addiu $1, $zero, 1
beq $5, $1, .LBB0_17
nop
lbu $1, 0($4)
andi $1, $1, 255
beqz $1, .LBB0_16
nop
daddiu $sp, $sp, -48
sd $ra, 40($sp)
sd $fp, 32($sp)
sd $gp, 24($sp)
sd $18, 16($sp)
sd $17, 8($sp)
sd $16, 0($sp)
move $fp, $sp
daddiu $gp, $2, %lo(%neg(%gp_rel(convert)))
move $17, $4
ld $25, %call16(strlen)($gp)
jalr $25
move $18, $5
move $16, $2
dsll $1, $2, 32
daddiu $2, $zero, 1
ld $25, %call16(malloc)($gp)
dsll $2, $2, 32
daddu $1, $1, $2
jalr $25
dsra $4, $1, 32
blez $18, .LBB0_18
nop
addiu $4, $18, -1
sll $3, $16, 0
sll $6, $18, 0
sll $16, $16, 0
addiu $7, $zero, 0
daddiu $8, $zero, 0
addiu $9, $zero, 0
move $10, $17
b .LBB0_6
sll $5, $4, 1
.LBB0_5:
daddiu $8, $8, 1
beq $8, $6, .LBB0_15
addiu $7, $7, 1
.LBB0_6:
slt $1, $7, $3
beqz $1, .LBB0_5
nop
xor $1, $4, $7
sltu $11, $zero, $7
sltu $1, $zero, $1
and $11, $11, $1
beqz $11, .LBB0_11
sll $13, $9, 0
subu $12, $4, $7
daddu $13, $2, $13
addiu $14, $zero, 1
move $15, $7
.LBB0_9:
move $1, $12
addiu $9, $9, 1
movz $1, $7, $14
xor $14, $14, $11
sll $1, $1, 1
addu $1, $1, $15
sll $15, $15, 0
daddu $15, $10, $15
slt $24, $1, $3
lbu $15, 0($15)
sb $15, 0($13)
daddiu $13, $13, 1
bnez $24, .LBB0_9
move $15, $1
b .LBB0_5
nop
.LBB0_11:
daddu $11, $2, $13
move $12, $8
.LBB0_12:
daddu $1, $12, $5
daddu $12, $10, $12
addiu $9, $9, 1
lbu $12, 0($12)
slt $13, $1, $16
sb $12, 0($11)
daddiu $11, $11, 1
bnez $13, .LBB0_12
move $12, $1
b .LBB0_5
nop
.LBB0_14:
jr $ra
daddiu $2, $zero, 0
.LBB0_15:
b .LBB0_19
sll $3, $9, 0
.LBB0_16:
jr $ra
move $2, $4
.LBB0_17:
jr $ra
move $2, $4
.LBB0_18:
daddiu $3, $zero, 0
.LBB0_19:
daddu $1, $2, $3
sb $zero, 0($1)
move $sp, $fp
ld $16, 0($sp)
ld $17, 8($sp)
ld $18, 16($sp)
ld $gp, 24($sp)
ld $fp, 32($sp)
ld $ra, 40($sp)
jr $ra
daddiu $sp, $sp, 48
|
172
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O0
|
mips64 gcc 15.2.0
|
convert:
daddiu $sp,$sp,-96
sd $31,88($sp)
sd $fp,80($sp)
sd $28,72($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(convert)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(convert)))
sd $4,48($fp)
move $2,$5
sll $2,$2,0
sw $2,56($fp)
sw $0,8($fp)
ld $2,48($fp)
beq $2,$0,.L2
nop
ld $2,48($fp)
lb $2,0($2)
beq $2,$0,.L2
nop
lw $2,56($fp)
li $3,1 # 0x1
bne $2,$3,.L3
nop
.L2:
ld $2,48($fp)
b .L4
nop
.L3:
ld $4,48($fp)
ld $2,%call16(strlen)($28)
mtlo $2
mflo $25
jalr $25
nop
sw $2,16($fp)
lw $2,16($fp)
addiu $2,$2,1
move $4,$2
ld $2,%call16(malloc)($28)
mtlo $2
mflo $25
jalr $25
nop
sd $2,24($fp)
lw $2,56($fp)
addiu $2,$2,-1
sll $2,$2,1
sw $2,32($fp)
sw $0,0($fp)
b .L5
nop
.L11:
lw $2,0($fp)
sw $2,4($fp)
li $2,1 # 0x1
sw $2,12($fp)
b .L6
nop
.L10:
lw $2,4($fp)
ld $3,48($fp)
daddu $3,$3,$2
lw $2,8($fp)
addiu $4,$2,1
sw $4,8($fp)
move $4,$2
ld $2,24($fp)
daddu $2,$2,$4
lb $3,0($3)
sb $3,0($2)
lw $2,0($fp)
beq $2,$0,.L7
nop
lw $2,56($fp)
addiu $2,$2,-1
move $3,$2
lw $2,0($fp)
bne $2,$3,.L8
nop
.L7:
lw $3,4($fp)
lw $2,32($fp)
addu $2,$3,$2
sw $2,4($fp)
b .L6
nop
.L8:
lw $2,12($fp)
beq $2,$0,.L9
nop
lw $2,0($fp)
sll $2,$2,1
lw $3,32($fp)
subu $2,$3,$2
lw $3,4($fp)
addu $2,$3,$2
sw $2,4($fp)
sw $0,12($fp)
b .L6
nop
.L9:
lw $2,0($fp)
sll $2,$2,1
lw $3,4($fp)
addu $2,$3,$2
sw $2,4($fp)
li $2,1 # 0x1
sw $2,12($fp)
.L6:
lw $3,4($fp)
lw $2,16($fp)
slt $2,$3,$2
bne $2,$0,.L10
nop
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
.L5:
lw $3,0($fp)
lw $2,56($fp)
slt $2,$3,$2
bne $2,$0,.L11
nop
lw $2,8($fp)
ld $3,24($fp)
daddu $2,$3,$2
sb $0,0($2)
ld $2,24($fp)
.L4:
move $sp,$fp
ld $31,88($sp)
ld $fp,80($sp)
ld $28,72($sp)
daddiu $sp,$sp,96
jr $31
nop
|
173
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O1
|
mips64 gcc 15.2.0
|
convert:
daddiu $sp,$sp,-48
sd $31,40($sp)
sd $28,32($sp)
sd $18,24($sp)
sd $17,16($sp)
sd $16,8($sp)
lui $28,%hi(%neg(%gp_rel(convert)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(convert)))
beq $4,$0,.L2
move $16,$4
lb $2,0($4)
beq $2,$0,.L2
move $18,$5
li $2,1 # 0x1
beq $5,$2,.L2
ld $25,%call16(strlen)($28)
1: jalr $25
nop
sll $4,$2,0
move $17,$4
ld $25,%call16(malloc)($28)
1: jalr $25
addiu $4,$4,1
blez $18,.L12
addiu $11,$18,-1
addiu $5,$18,-1
sll $12,$5,1
sll $11,$11,1
move $7,$0
move $9,$0
li $13,1 # 0x1
move $24,$0
b .L4
li $25,-2 # 0xfffffffffffffffe
.L5:
addu $3,$12,$3
.L7:
.L15:
slt $8,$3,$17
beq $8,$0,.L11
daddiu $6,$6,1
.L9:
addiu $7,$7,1
daddu $8,$16,$3
lbu $8,0($8)
beq $9,$0,.L5
sb $8,0($6)
beql $5,$10,.L15
addu $3,$12,$3
beql $4,$0,.L8
addu $3,$15,$3
addu $3,$14,$3
b .L7
move $4,$24
.L8:
b .L7
move $4,$13
.L11:
addiu $11,$11,-2
beq $11,$25,.L3
addiu $9,$9,1
.L4:
slt $3,$9,$17
beq $3,$0,.L11
sll $15,$9,1
move $14,$11
daddu $6,$2,$7
move $3,$9
move $4,$13
b .L9
move $10,$9
.L12:
move $7,$0
.L3:
daddu $7,$2,$7
sb $0,0($7)
move $16,$2
.L2:
move $2,$16
ld $31,40($sp)
ld $28,32($sp)
ld $18,24($sp)
ld $17,16($sp)
ld $16,8($sp)
jr $31
daddiu $sp,$sp,48
|
174
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O2
|
mips64 gcc 15.2.0
|
convert:
beq $4,$0,.L25
move $9,$4
lb $2,0($4)
beq $2,$0,.L25
nop
li $2,1 # 0x1
beq $5,$2,.L25
nop
daddiu $sp,$sp,-48
sd $28,32($sp)
lui $28,%hi(%neg(%gp_rel(convert)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(convert)))
ld $25,%call16(strlen)($28)
sd $31,40($sp)
sd $5,8($sp)
1: jalr $25
sd $4,16($sp)
ld $25,%call16(malloc)($28)
sll $10,$2,0
addiu $4,$10,1
1: jalr $25
sw $10,0($sp)
ld $5,8($sp)
lw $10,0($sp)
ld $9,16($sp)
blez $5,.L4
move $7,$2
addiu $14,$5,-1
addiu $13,$5,-1
sll $15,$13,1
sll $14,$14,1
move $7,$0
move $8,$0
.L11:
slt $3,$8,$10
beq $3,$0,.L8
sll $24,$8,1
daddu $6,$2,$7
move $3,$8
li $11,1 # 0x1
b .L10
move $12,$8
.L5:
addu $3,$3,$15
.L30:
slt $4,$3,$10
beq $4,$0,.L8
daddiu $6,$6,1
.L10:
daddu $4,$9,$3
lbu $4,0($4)
addiu $7,$7,1
beq $8,$0,.L5
sb $4,0($6)
.L6:
beql $13,$12,.L30
addu $3,$3,$15
beql $11,$0,.L31
addu $3,$24,$3
addu $3,$14,$3
.L32:
slt $4,$3,$10
daddu $11,$9,$3
beq $4,$0,.L8
daddiu $6,$6,1
lbu $4,0($11)
addiu $7,$7,1
move $11,$0
beq $13,$12,.L5
sb $4,0($6)
bnel $11,$0,.L32
addu $3,$14,$3
addu $3,$24,$3
.L31:
slt $4,$3,$10
daddu $11,$9,$3
beq $4,$0,.L8
daddiu $6,$6,1
lbu $4,0($11)
addiu $7,$7,1
li $11,1 # 0x1
b .L6
sb $4,0($6)
.L25:
jr $31
move $2,$9
.L8:
addiu $8,$8,1
bne $5,$8,.L11
addiu $14,$14,-2
daddu $7,$2,$7
.L4:
sb $0,0($7)
ld $31,40($sp)
ld $28,32($sp)
jr $31
daddiu $sp,$sp,48
|
175
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
mips64
|
-O3
|
mips64 gcc 15.2.0
|
convert:
beq $4,$0,.L31
move $8,$4
lb $2,0($4)
beq $2,$0,.L31
nop
li $2,1 # 0x1
beq $5,$2,.L31
nop
daddiu $sp,$sp,-48
sd $28,32($sp)
lui $28,%hi(%neg(%gp_rel(convert)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(convert)))
ld $25,%call16(strlen)($28)
sd $31,40($sp)
sd $5,8($sp)
1: jalr $25
sd $4,16($sp)
ld $25,%call16(malloc)($28)
sll $9,$2,0
addiu $4,$9,1
1: jalr $25
sw $9,0($sp)
ld $5,8($sp)
lw $9,0($sp)
ld $8,16($sp)
blez $5,.L4
move $3,$2
blez $9,.L19
addiu $14,$5,-1
slt $3,$9,$5
addiu $12,$5,-1
bne $3,$0,.L35
sll $11,$14,1
.L16:
move $13,$5
sll $12,$12,1
move $10,$0
move $3,$0
.L12:
beq $10,$0,.L36
sll $4,$10,0
beq $4,$14,.L5
daddu $5,$8,$10
lbu $15,0($5)
daddu $5,$2,$3
move $6,$4
sll $7,$4,1
addiu $3,$3,1
b .L8
sb $15,0($5)
.L6:
lbu $4,0($24)
daddiu $5,$5,2
beq $15,$0,.L37
sb $4,-1($5)
lbu $4,0($25)
addiu $3,$3,2
sb $4,0($5)
.L8:
addu $4,$12,$6
addu $6,$7,$4
daddu $24,$8,$4
slt $4,$4,$9
daddu $25,$8,$6
bne $4,$0,.L6
slt $15,$6,$9
daddiu $10,$10,1
sll $4,$10,0
slt $4,$4,$13
bne $4,$0,.L12
addiu $12,$12,-2
.L14:
daddu $3,$2,$3
.L4:
sb $0,0($3)
ld $31,40($sp)
ld $28,32($sp)
jr $31
daddiu $sp,$sp,48
.L31:
jr $31
move $2,$8
.L36:
daddu $7,$2,$3
move $5,$0
.L11:
daddu $4,$8,$5
lbu $4,0($4)
daddu $5,$5,$11
sll $6,$5,0
slt $6,$6,$9
sb $4,0($7)
addiu $3,$3,1
bne $6,$0,.L11
daddiu $7,$7,1
daddiu $10,$10,1
sll $4,$10,0
slt $4,$4,$13
bne $4,$0,.L12
addiu $12,$12,-2
b .L4
daddu $3,$2,$3
.L5:
daddu $6,$2,$3
move $4,$10
.L10:
daddu $5,$8,$4
lbu $7,0($5)
daddu $4,$4,$11
sll $5,$4,0
slt $5,$5,$9
sb $7,0($6)
addiu $3,$3,1
bne $5,$0,.L10
daddiu $6,$6,1
daddiu $10,$10,1
sll $4,$10,0
slt $4,$4,$13
bne $4,$0,.L12
addiu $12,$12,-2
b .L4
daddu $3,$2,$3
.L37:
daddiu $10,$10,1
sll $4,$10,0
slt $4,$4,$13
addiu $3,$3,1
bne $4,$0,.L12
addiu $12,$12,-2
b .L4
daddu $3,$2,$3
.L35:
b .L16
move $5,$9
.L19:
b .L14
move $3,$0
|
176
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O0
|
RISC-V 64 clang 21.1.0
|
convert:
addi sp, sp, -80
sd ra, 72(sp)
sd s0, 64(sp)
addi s0, sp, 80
sd a0, -32(s0)
sw a1, -36(s0)
li a0, 0
sw a0, -52(s0)
ld a0, -32(s0)
beqz a0, .LBB0_3
j .LBB0_1
.LBB0_1:
ld a0, -32(s0)
lbu a0, 0(a0)
beqz a0, .LBB0_3
j .LBB0_2
.LBB0_2:
lw a0, -36(s0)
li a1, 1
bne a0, a1, .LBB0_4
j .LBB0_3
.LBB0_3:
ld a0, -32(s0)
sd a0, -24(s0)
j .LBB0_19
.LBB0_4:
ld a0, -32(s0)
call strlen
sw a0, -40(s0)
lw a0, -40(s0)
addiw a0, a0, 1
call malloc
sd a0, -64(s0)
lw a0, -36(s0)
slliw a0, a0, 1
addiw a0, a0, -2
sw a0, -68(s0)
li a0, 0
sw a0, -44(s0)
j .LBB0_5
.LBB0_5:
lw a0, -44(s0)
lw a1, -36(s0)
bge a0, a1, .LBB0_18
j .LBB0_6
.LBB0_6:
lw a0, -44(s0)
sw a0, -48(s0)
li a0, 1
sw a0, -72(s0)
j .LBB0_7
.LBB0_7:
lw a0, -48(s0)
lw a1, -40(s0)
bge a0, a1, .LBB0_16
j .LBB0_8
.LBB0_8:
ld a0, -32(s0)
lw a1, -48(s0)
add a0, a0, a1
lbu a0, 0(a0)
ld a1, -64(s0)
lw a2, -52(s0)
addiw a3, a2, 1
sw a3, -52(s0)
add a1, a1, a2
sb a0, 0(a1)
lw a0, -44(s0)
beqz a0, .LBB0_10
j .LBB0_9
.LBB0_9:
lw a0, -44(s0)
lw a1, -36(s0)
addiw a1, a1, -1
bne a0, a1, .LBB0_11
j .LBB0_10
.LBB0_10:
lw a1, -68(s0)
lw a0, -48(s0)
addw a0, a0, a1
sw a0, -48(s0)
j .LBB0_15
.LBB0_11:
lw a0, -72(s0)
beqz a0, .LBB0_13
j .LBB0_12
.LBB0_12:
lw a0, -68(s0)
lw a1, -44(s0)
slliw a1, a1, 1
subw a1, a0, a1
lw a0, -48(s0)
addw a0, a0, a1
sw a0, -48(s0)
li a0, 0
sw a0, -72(s0)
j .LBB0_14
.LBB0_13:
lw a0, -44(s0)
slliw a1, a0, 1
lw a0, -48(s0)
addw a0, a0, a1
sw a0, -48(s0)
li a0, 1
sw a0, -72(s0)
j .LBB0_14
.LBB0_14:
j .LBB0_15
.LBB0_15:
j .LBB0_7
.LBB0_16:
j .LBB0_17
.LBB0_17:
lw a0, -44(s0)
addiw a0, a0, 1
sw a0, -44(s0)
j .LBB0_5
.LBB0_18:
ld a0, -64(s0)
lw a1, -52(s0)
add a1, a1, a0
li a0, 0
sb a0, 0(a1)
ld a0, -64(s0)
sd a0, -24(s0)
j .LBB0_19
.LBB0_19:
ld a0, -24(s0)
ld ra, 72(sp)
ld s0, 64(sp)
addi sp, sp, 80
ret
|
177
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O1
|
RISC-V 64 clang 21.1.0
|
convert:
beqz a0, .LBB0_15
li a2, 1
beq a1, a2, .LBB0_15
lbu a2, 0(a0)
beqz a2, .LBB0_15
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
sd s1, 8(sp)
sd s2, 0(sp)
mv s2, a0
mv s1, a1
call strlen
mv s0, a0
addiw a0, a0, 1
call malloc
blez s1, .LBB0_13
mv a6, s1
li t3, 0
li a2, 0
addiw a7, s1, -1
sext.w t2, s0
mv t1, s2
j .LBB0_6
.LBB0_5:
addiw t3, t3, 1
beq t3, a6, .LBB0_14
.LBB0_6:
bge t3, t2, .LBB0_5
snez a3, t3
xor a4, a7, t3
subw t0, a7, t3
add a1, a0, a2
snez a4, a4
and a4, a4, a3
li a5, 1
mv s1, t3
j .LBB0_9
.LBB0_8:
xor a5, a5, a4
slli s0, s0, 1
addw s1, s1, s0
addi a1, a1, 1
addiw a2, a2, 1
bge s1, t2, .LBB0_5
.LBB0_9:
add a3, t1, s1
lbu a3, 0(a3)
sb a3, 0(a1)
mv s0, t3
beqz a5, .LBB0_11
mv s0, t0
.LBB0_11:
bnez a4, .LBB0_8
mv s0, a7
j .LBB0_8
.LBB0_13:
li a2, 0
.LBB0_14:
add a2, a2, a0
sb zero, 0(a2)
ld ra, 24(sp)
ld s0, 16(sp)
ld s1, 8(sp)
ld s2, 0(sp)
addi sp, sp, 32
.LBB0_15:
ret
|
178
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O2
|
RISC-V 64 clang 21.1.0
|
convert:
beqz a0, .LBB0_15
li a2, 1
beq a1, a2, .LBB0_15
lbu a2, 0(a0)
beqz a2, .LBB0_15
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
sd s1, 8(sp)
sd s2, 0(sp)
mv s2, a0
mv s1, a1
call strlen
mv s0, a0
addiw a0, a0, 1
call malloc
blez s1, .LBB0_13
mv a6, s1
li t3, 0
li a2, 0
addiw a7, s1, -1
sext.w t2, s0
mv t1, s2
j .LBB0_6
.LBB0_5:
addiw t3, t3, 1
beq t3, a6, .LBB0_14
.LBB0_6:
bge t3, t2, .LBB0_5
snez a3, t3
xor a4, a7, t3
subw t0, a7, t3
add a1, a0, a2
snez a4, a4
and a4, a4, a3
li a5, 1
mv s1, t3
j .LBB0_9
.LBB0_8:
xor a5, a5, a4
slli s0, s0, 1
addw s1, s1, s0
addi a1, a1, 1
addiw a2, a2, 1
bge s1, t2, .LBB0_5
.LBB0_9:
add a3, t1, s1
lbu a3, 0(a3)
sb a3, 0(a1)
mv s0, t3
beqz a5, .LBB0_11
mv s0, t0
.LBB0_11:
bnez a4, .LBB0_8
mv s0, a7
j .LBB0_8
.LBB0_13:
li a2, 0
.LBB0_14:
add a2, a2, a0
sb zero, 0(a2)
ld ra, 24(sp)
ld s0, 16(sp)
ld s1, 8(sp)
ld s2, 0(sp)
addi sp, sp, 32
.LBB0_15:
ret
|
179
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O3
|
RISC-V 64 clang 21.1.0
|
convert:
beqz a0, .LBB0_16
li a2, 1
beq a1, a2, .LBB0_16
lbu a2, 0(a0)
beqz a2, .LBB0_16
addi sp, sp, -32
sd ra, 24(sp)
sd s0, 16(sp)
sd s1, 8(sp)
sd s2, 0(sp)
mv s2, a0
mv s0, a1
call strlen
mv s1, a0
addiw a0, a0, 1
call malloc
blez s0, .LBB0_14
mv a7, s0
li t0, 0
li t3, 0
li s0, 0
addiw a6, a7, -1
slliw t1, a6, 1
sext.w s1, s1
mv a5, s2
j .LBB0_6
.LBB0_5:
addi t0, t0, 1
addiw t3, t3, 1
beq t0, a7, .LBB0_15
.LBB0_6:
bge t3, s1, .LBB0_5
snez a1, t3
xor a2, a6, t3
snez a2, a2
and t4, a1, a2
beqz t4, .LBB0_12
subw t2, a6, t3
add a2, a0, s0
li a3, 1
mv a4, t3
j .LBB0_10
.LBB0_9:
xor a3, a3, t4
slli a1, a1, 1
addw a4, a4, a1
addi a2, a2, 1
addiw s0, s0, 1
bge a4, s1, .LBB0_5
.LBB0_10:
add a1, a5, a4
lbu a1, 0(a1)
sb a1, 0(a2)
mv a1, t3
beqz a3, .LBB0_9
mv a1, t2
j .LBB0_9
.LBB0_12:
add a1, a0, s0
mv a2, t0
.LBB0_13:
add a3, a5, a2
lbu a3, 0(a3)
add a2, a2, t1
sb a3, 0(a1)
addi a1, a1, 1
addiw s0, s0, 1
blt a2, s1, .LBB0_13
j .LBB0_5
.LBB0_14:
li s0, 0
.LBB0_15:
add s0, s0, a0
sb zero, 0(s0)
ld ra, 24(sp)
ld s0, 16(sp)
ld s1, 8(sp)
ld s2, 0(sp)
addi sp, sp, 32
.LBB0_16:
ret
|
180
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O0
|
RISC-V 64 gcc 15.2.0
|
convert:
addi sp,sp,-80
sd ra,72(sp)
sd s0,64(sp)
addi s0,sp,80
sd a0,-72(s0)
mv a5,a1
sw a5,-76(s0)
sw zero,-28(s0)
ld a5,-72(s0)
beq a5,zero,.L2
ld a5,-72(s0)
lbu a5,0(a5)
beq a5,zero,.L2
lw a5,-76(s0)
sext.w a4,a5
li a5,1
bne a4,a5,.L3
.L2:
ld a5,-72(s0)
j .L4
.L3:
ld a0,-72(s0)
call strlen
mv a5,a0
sw a5,-36(s0)
lw a5,-36(s0)
addiw a5,a5,1
sext.w a5,a5
mv a0,a5
call malloc
mv a5,a0
sd a5,-48(s0)
lw a5,-76(s0)
addiw a5,a5,-1
sext.w a5,a5
slliw a5,a5,1
sw a5,-52(s0)
sw zero,-20(s0)
j .L5
.L11:
lw a5,-20(s0)
sw a5,-24(s0)
li a5,1
sw a5,-32(s0)
j .L6
.L10:
lw a5,-24(s0)
ld a4,-72(s0)
add a4,a4,a5
lw a5,-28(s0)
addiw a3,a5,1
sw a3,-28(s0)
mv a3,a5
ld a5,-48(s0)
add a5,a5,a3
lbu a4,0(a4)
sb a4,0(a5)
lw a5,-20(s0)
sext.w a5,a5
beq a5,zero,.L7
lw a5,-76(s0)
addiw a5,a5,-1
sext.w a5,a5
lw a4,-20(s0)
sext.w a4,a4
bne a4,a5,.L8
.L7:
lw a5,-24(s0)
mv a4,a5
lw a5,-52(s0)
addw a5,a4,a5
sw a5,-24(s0)
j .L6
.L8:
lw a5,-32(s0)
sext.w a5,a5
beq a5,zero,.L9
lw a5,-20(s0)
slliw a5,a5,1
sext.w a5,a5
lw a4,-52(s0)
subw a5,a4,a5
sext.w a5,a5
lw a4,-24(s0)
addw a5,a4,a5
sw a5,-24(s0)
sw zero,-32(s0)
j .L6
.L9:
lw a5,-20(s0)
slliw a5,a5,1
sext.w a5,a5
lw a4,-24(s0)
addw a5,a4,a5
sw a5,-24(s0)
li a5,1
sw a5,-32(s0)
.L6:
lw a5,-24(s0)
mv a4,a5
lw a5,-36(s0)
sext.w a4,a4
sext.w a5,a5
blt a4,a5,.L10
lw a5,-20(s0)
addiw a5,a5,1
sw a5,-20(s0)
.L5:
lw a5,-20(s0)
mv a4,a5
lw a5,-76(s0)
sext.w a4,a4
sext.w a5,a5
blt a4,a5,.L11
lw a5,-28(s0)
ld a4,-48(s0)
add a5,a4,a5
sb zero,0(a5)
ld a5,-48(s0)
.L4:
mv a0,a5
ld ra,72(sp)
ld s0,64(sp)
addi sp,sp,80
jr ra
|
181
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O1
|
RISC-V 64 gcc 15.2.0
|
convert:
addi sp,sp,-32
sd ra,24(sp)
sd s0,16(sp)
mv s0,a0
beq a0,zero,.L2
sd s2,0(sp)
mv s2,a1
lbu a5,0(a0)
beq a5,zero,.L14
addi a5,a1,-1
beq a5,zero,.L15
sd s1,8(sp)
call strlen
sext.w s1,a0
addiw a0,a0,1
call malloc
ble s2,zero,.L11
addiw t0,s2,-1
slliw t1,t0,1
mv t3,t1
li a3,0
li a7,0
li t4,1
li t2,-2
j .L4
.L5:
beq a6,zero,.L7
addw a5,t5,a5
li a6,0
.L6:
addi a4,a4,1
ble s1,a5,.L10
.L8:
addiw a3,a3,1
add a2,s0,a5
lbu a2,0(a2)
sb a2,0(a4)
beq a1,zero,.L5
addw a5,t1,a5
j .L6
.L7:
addw a5,t6,a5
mv a6,t4
j .L6
.L10:
addiw a7,a7,1
addiw t3,t3,-2
beq t3,t2,.L3
.L4:
ble s1,a7,.L10
sub a1,t0,a7
seqz a1,a1
seqz a5,a7
or a1,a1,a5
slliw t6,a7,1
mv t5,t3
add a4,a0,a3
mv a5,a7
mv a6,t4
j .L8
.L11:
li a3,0
.L3:
add a3,a0,a3
sb zero,0(a3)
mv s0,a0
ld s1,8(sp)
ld s2,0(sp)
.L2:
mv a0,s0
ld ra,24(sp)
ld s0,16(sp)
addi sp,sp,32
jr ra
.L14:
ld s2,0(sp)
j .L2
.L15:
ld s2,0(sp)
j .L2
|
182
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O2
|
RISC-V 64 gcc 15.2.0
|
convert:
mv a2,a0
beq a0,zero,.L22
lbu a5,0(a0)
beq a5,zero,.L22
addi a5,a1,-1
beq a5,zero,.L22
addi sp,sp,-48
sd a1,16(sp)
sd ra,40(sp)
sd a0,24(sp)
call strlen
mv a6,a0
sext.w a6,a6
addiw a0,a0,1
sd a6,8(sp)
call malloc
ld a1,16(sp)
ld a6,8(sp)
ld a2,24(sp)
mv a4,a0
ble a1,zero,.L4
addiw t0,a1,-1
slliw t3,t0,1
mv t4,t3
li a4,0
li a7,0
.L12:
ble a6,a7,.L8
add a5,a2,a7
lbu a3,0(a5)
add a5,a0,a4
addiw a4,a4,1
sb a3,0(a5)
beq t0,a7,.L14
beq a7,zero,.L14
slliw t6,a7,1
mv a3,a7
j .L19
.L10:
lbu t1,0(t1)
addw a3,t6,a3
add t5,a2,a3
sb t1,1(a5)
addiw a4,a4,1
addi a5,a5,2
ble a6,a3,.L8
lbu t1,0(t5)
addiw a4,a4,1
sb t1,0(a5)
.L19:
addw a3,t4,a3
add t1,a2,a3
bgt a6,a3,.L10
.L8:
addiw a7,a7,1
addiw t4,t4,-2
bne a1,a7,.L12
.L26:
add a4,a0,a4
.L4:
sb zero,0(a4)
ld ra,40(sp)
addi sp,sp,48
jr ra
.L22:
mv a0,a2
ret
.L14:
mv a3,a7
j .L5
.L7:
lbu t1,0(t1)
addiw a4,a4,1
sb t1,0(a5)
.L5:
addw a3,t3,a3
add t1,a2,a3
addi a5,a5,1
bgt a6,a3,.L7
addiw a7,a7,1
addiw t4,t4,-2
bne a1,a7,.L12
j .L26
|
183
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
riscv64
|
-O3
|
RISC-V 64 gcc 15.2.0
|
convert:
mv a2,a0
beq a0,zero,.L24
lbu a5,0(a0)
beq a5,zero,.L24
addi a5,a1,-1
beq a5,zero,.L24
addi sp,sp,-48
sd a1,16(sp)
sd ra,40(sp)
sd a0,24(sp)
call strlen
mv a7,a0
sext.w a7,a7
addiw a0,a0,1
sd a7,8(sp)
call malloc
ld a1,16(sp)
ld a7,8(sp)
ld a2,24(sp)
mv a3,a0
ble a1,zero,.L4
ble a7,zero,.L16
addiw t2,a1,-1
slliw t3,t2,1
sext.w t0,a1
bgt a1,a7,.L28
slliw t4,t2,1
li t1,0
li a3,0
li a5,0
.L9:
beq a5,t2,.L14
beq t1,zero,.L14
add a4,a2,t1
lbu a1,0(a4)
add a4,a0,a3
slliw t6,t1,1
sb a1,0(a4)
addiw a3,a3,1
j .L7
.L30:
lbu a1,0(a6)
addi a4,a4,2
sb a1,-1(a4)
ble a7,a5,.L29
lbu a1,0(t5)
addiw a3,a3,2
sb a1,0(a4)
.L7:
addw a1,a5,t4
addw a5,t6,a1
add a6,a2,a1
add t5,a2,a5
bgt a7,a1,.L30
addi t1,t1,1
sext.w a5,t1
addiw t4,t4,-2
blt a5,t0,.L9
.L11:
add a3,a0,a3
.L4:
sb zero,0(a3)
ld ra,40(sp)
addi sp,sp,48
jr ra
.L24:
mv a0,a2
ret
.L14:
add a4,a0,a3
mv a5,t1
.L8:
add a1,a2,a5
lbu a6,0(a1)
add a5,a5,t3
sext.w a1,a5
sb a6,0(a4)
addiw a3,a3,1
addi a4,a4,1
bgt a7,a1,.L8
addi t1,t1,1
sext.w a5,t1
addiw t4,t4,-2
blt a5,t0,.L9
j .L11
.L29:
addi t1,t1,1
sext.w a5,t1
addiw a3,a3,1
addiw t4,t4,-2
blt a5,t0,.L9
j .L11
.L28:
sext.w t0,a7
slliw t4,t2,1
li t1,0
li a3,0
li a5,0
j .L9
.L16:
li a3,0
j .L11
|
184
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O0
|
x86-64 clang 21.1.0
|
convert:
push rbp
mov rbp, rsp
sub rsp, 64
mov qword ptr [rbp - 16], rdi
mov dword ptr [rbp - 20], esi
mov dword ptr [rbp - 36], 0
cmp qword ptr [rbp - 16], 0
je .LBB0_3
mov rax, qword ptr [rbp - 16]
cmp byte ptr [rax], 0
je .LBB0_3
cmp dword ptr [rbp - 20], 1
jne .LBB0_4
.LBB0_3:
mov rax, qword ptr [rbp - 16]
mov qword ptr [rbp - 8], rax
jmp .LBB0_19
.LBB0_4:
mov rdi, qword ptr [rbp - 16]
call strlen@PLT
mov dword ptr [rbp - 24], eax
mov eax, dword ptr [rbp - 24]
add eax, 1
movsxd rdi, eax
shl rdi, 0
call malloc@PLT
mov qword ptr [rbp - 48], rax
mov eax, dword ptr [rbp - 20]
sub eax, 1
shl eax
mov dword ptr [rbp - 52], eax
mov dword ptr [rbp - 28], 0
.LBB0_5:
mov eax, dword ptr [rbp - 28]
cmp eax, dword ptr [rbp - 20]
jge .LBB0_18
mov eax, dword ptr [rbp - 28]
mov dword ptr [rbp - 32], eax
mov dword ptr [rbp - 56], 1
.LBB0_7:
mov eax, dword ptr [rbp - 32]
cmp eax, dword ptr [rbp - 24]
jge .LBB0_16
mov rax, qword ptr [rbp - 16]
movsxd rcx, dword ptr [rbp - 32]
mov dl, byte ptr [rax + rcx]
mov rax, qword ptr [rbp - 48]
mov ecx, dword ptr [rbp - 36]
mov esi, ecx
add esi, 1
mov dword ptr [rbp - 36], esi
movsxd rcx, ecx
mov byte ptr [rax + rcx], dl
cmp dword ptr [rbp - 28], 0
je .LBB0_10
mov eax, dword ptr [rbp - 28]
mov ecx, dword ptr [rbp - 20]
sub ecx, 1
cmp eax, ecx
jne .LBB0_11
.LBB0_10:
mov eax, dword ptr [rbp - 52]
add eax, dword ptr [rbp - 32]
mov dword ptr [rbp - 32], eax
jmp .LBB0_15
.LBB0_11:
cmp dword ptr [rbp - 56], 0
je .LBB0_13
mov eax, dword ptr [rbp - 52]
mov ecx, dword ptr [rbp - 28]
shl ecx
sub eax, ecx
add eax, dword ptr [rbp - 32]
mov dword ptr [rbp - 32], eax
mov dword ptr [rbp - 56], 0
jmp .LBB0_14
.LBB0_13:
mov eax, dword ptr [rbp - 28]
shl eax
add eax, dword ptr [rbp - 32]
mov dword ptr [rbp - 32], eax
mov dword ptr [rbp - 56], 1
.LBB0_14:
jmp .LBB0_15
.LBB0_15:
jmp .LBB0_7
.LBB0_16:
jmp .LBB0_17
.LBB0_17:
mov eax, dword ptr [rbp - 28]
add eax, 1
mov dword ptr [rbp - 28], eax
jmp .LBB0_5
.LBB0_18:
mov rax, qword ptr [rbp - 48]
movsxd rcx, dword ptr [rbp - 36]
mov byte ptr [rax + rcx], 0
mov rax, qword ptr [rbp - 48]
mov qword ptr [rbp - 8], rax
.LBB0_19:
mov rax, qword ptr [rbp - 8]
add rsp, 64
pop rbp
ret
|
185
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O1
|
x86-64 clang 21.1.0
|
convert:
test rdi, rdi
je .LBB0_9
push rbp
push r15
push r14
push r12
push rbx
mov ebx, esi
cmp esi, 1
je .LBB0_13
cmp byte ptr [rdi], 0
je .LBB0_13
mov r15, rdi
call strlen@PLT
mov r14, rax
inc eax
movsxd rdi, eax
call malloc@PLT
xor ecx, ecx
test ebx, ebx
jle .LBB0_11
lea edx, [rbx - 1]
xor esi, esi
mov rdi, r15
jmp .LBB0_6
.LBB0_5:
inc ecx
cmp ecx, ebx
je .LBB0_10
.LBB0_6:
cmp ecx, r14d
jge .LBB0_5
test ecx, ecx
setne r9b
mov r8d, edx
sub r8d, ecx
setne r10b
and r10b, r9b
movzx r9d, r10b
movsxd r10, esi
add r10, rax
mov r11d, 1
mov ebp, ecx
.LBB0_8:
movsxd r15, ebp
movzx ebp, byte ptr [rdi + r15]
mov byte ptr [r10], bpl
test r11d, r11d
mov r12d, r8d
cmove r12d, ecx
test r9d, r9d
cmove r12d, edx
xor r11d, r9d
lea ebp, [r15 + 2*r12]
inc r10
inc esi
cmp ebp, r14d
jl .LBB0_8
jmp .LBB0_5
.LBB0_9:
mov rax, rdi
ret
.LBB0_13:
mov rax, rdi
jmp .LBB0_14
.LBB0_10:
movsxd rcx, esi
.LBB0_11:
mov byte ptr [rax + rcx], 0
.LBB0_14:
pop rbx
pop r12
pop r14
pop r15
pop rbp
ret
|
186
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O2
|
x86-64 clang 21.1.0
|
convert:
test rdi, rdi
je .LBB0_9
push rbp
push r15
push r14
push r12
push rbx
mov ebx, esi
cmp esi, 1
je .LBB0_13
cmp byte ptr [rdi], 0
je .LBB0_13
mov r15, rdi
call strlen@PLT
mov r14, rax
inc eax
movsxd rdi, eax
call malloc@PLT
xor ecx, ecx
test ebx, ebx
jle .LBB0_11
lea edx, [rbx - 1]
xor esi, esi
mov rdi, r15
jmp .LBB0_6
.LBB0_5:
inc ecx
cmp ecx, ebx
je .LBB0_10
.LBB0_6:
cmp ecx, r14d
jge .LBB0_5
test ecx, ecx
setne r9b
mov r8d, edx
sub r8d, ecx
setne r10b
and r10b, r9b
movzx r9d, r10b
movsxd r10, esi
add r10, rax
mov r11d, 1
mov ebp, ecx
.LBB0_8:
movsxd r15, ebp
movzx ebp, byte ptr [rdi + r15]
mov byte ptr [r10], bpl
test r11d, r11d
mov r12d, r8d
cmove r12d, ecx
test r9d, r9d
cmove r12d, edx
xor r11d, r9d
lea ebp, [r15 + 2*r12]
inc r10
inc esi
cmp ebp, r14d
jl .LBB0_8
jmp .LBB0_5
.LBB0_9:
xor eax, eax
ret
.LBB0_13:
mov rax, rdi
jmp .LBB0_14
.LBB0_10:
movsxd rcx, esi
.LBB0_11:
mov byte ptr [rax + rcx], 0
.LBB0_14:
pop rbx
pop r12
pop r14
pop r15
pop rbp
ret
|
187
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O3
|
x86-64 clang 21.1.0
|
convert:
test rdi, rdi
je .LBB0_12
push rbp
push r15
push r14
push r13
push r12
push rbx
push rax
mov r14d, esi
cmp esi, 1
je .LBB0_16
cmp byte ptr [rdi], 0
je .LBB0_16
mov r15, rdi
call strlen@PLT
mov rbx, rax
inc eax
movsxd rdi, eax
call malloc@PLT
xor ecx, ecx
test r14d, r14d
jle .LBB0_14
lea edx, [r14 - 1]
lea esi, [rdx + rdx]
movsxd rsi, esi
movsxd rdi, ebx
mov r8d, r14d
xor r9d, r9d
mov r10, r15
jmp .LBB0_6
.LBB0_5:
inc r9
cmp r9, r8
je .LBB0_13
.LBB0_6:
cmp r9d, ebx
jge .LBB0_5
test r9d, r9d
setne r15b
mov ebp, edx
sub ebp, r9d
setne r14b
movsxd r11, ecx
and r14b, r15b
je .LBB0_10
movzx r14d, r14b
add r11, rax
mov r15d, 1
mov r12d, r9d
.LBB0_9:
movsxd r12, r12d
movzx r13d, byte ptr [r10 + r12]
mov byte ptr [r11], r13b
test r15d, r15d
mov r13d, ebp
cmove r13d, r9d
xor r15d, r14d
lea r12d, [r12 + 2*r13]
inc r11
inc ecx
cmp r12d, ebx
jl .LBB0_9
jmp .LBB0_5
.LBB0_10:
add r11, rax
mov r14, r9
.LBB0_11:
movzx ebp, byte ptr [r10 + r14]
mov byte ptr [r11], bpl
add r14, rsi
inc r11
inc ecx
cmp r14, rdi
jl .LBB0_11
jmp .LBB0_5
.LBB0_12:
xor eax, eax
ret
.LBB0_16:
mov rax, rdi
jmp .LBB0_17
.LBB0_13:
movsxd rcx, ecx
.LBB0_14:
mov byte ptr [rax + rcx], 0
.LBB0_17:
add rsp, 8
pop rbx
pop r12
pop r13
pop r14
pop r15
pop rbp
ret
|
188
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O0
|
x86-64 gcc 15.2
|
convert:
push rbp
mov rbp, rsp
sub rsp, 64
mov QWORD PTR [rbp-56], rdi
mov DWORD PTR [rbp-60], esi
mov DWORD PTR [rbp-12], 0
cmp QWORD PTR [rbp-56], 0
je .L2
mov rax, QWORD PTR [rbp-56]
movzx eax, BYTE PTR [rax]
test al, al
je .L2
cmp DWORD PTR [rbp-60], 1
jne .L3
.L2:
mov rax, QWORD PTR [rbp-56]
jmp .L4
.L3:
mov rax, QWORD PTR [rbp-56]
mov rdi, rax
call strlen
mov DWORD PTR [rbp-20], eax
mov eax, DWORD PTR [rbp-20]
add eax, 1
cdqe
mov rdi, rax
call malloc
mov QWORD PTR [rbp-32], rax
mov eax, DWORD PTR [rbp-60]
sub eax, 1
add eax, eax
mov DWORD PTR [rbp-36], eax
mov DWORD PTR [rbp-4], 0
jmp .L5
.L11:
mov eax, DWORD PTR [rbp-4]
mov DWORD PTR [rbp-8], eax
mov DWORD PTR [rbp-16], 1
jmp .L6
.L10:
mov eax, DWORD PTR [rbp-8]
movsx rdx, eax
mov rax, QWORD PTR [rbp-56]
lea rcx, [rdx+rax]
mov eax, DWORD PTR [rbp-12]
lea edx, [rax+1]
mov DWORD PTR [rbp-12], edx
movsx rdx, eax
mov rax, QWORD PTR [rbp-32]
add rdx, rax
movzx eax, BYTE PTR [rcx]
mov BYTE PTR [rdx], al
cmp DWORD PTR [rbp-4], 0
je .L7
mov eax, DWORD PTR [rbp-60]
sub eax, 1
cmp DWORD PTR [rbp-4], eax
jne .L8
.L7:
mov eax, DWORD PTR [rbp-36]
add DWORD PTR [rbp-8], eax
jmp .L6
.L8:
cmp DWORD PTR [rbp-16], 0
je .L9
mov eax, DWORD PTR [rbp-4]
lea edx, [rax+rax]
mov eax, DWORD PTR [rbp-36]
sub eax, edx
add DWORD PTR [rbp-8], eax
mov DWORD PTR [rbp-16], 0
jmp .L6
.L9:
mov eax, DWORD PTR [rbp-4]
add eax, eax
add DWORD PTR [rbp-8], eax
mov DWORD PTR [rbp-16], 1
.L6:
mov eax, DWORD PTR [rbp-8]
cmp eax, DWORD PTR [rbp-20]
jl .L10
add DWORD PTR [rbp-4], 1
.L5:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-60]
jl .L11
mov eax, DWORD PTR [rbp-12]
movsx rdx, eax
mov rax, QWORD PTR [rbp-32]
add rax, rdx
mov BYTE PTR [rax], 0
mov rax, QWORD PTR [rbp-32]
.L4:
leave
ret
|
189
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O1
|
x86-64 gcc 15.2
|
convert:
push r15
push r14
push r13
push r12
push rbp
push rbx
sub rsp, 24
mov rbx, rdi
test rdi, rdi
je .L2
mov r12d, esi
cmp BYTE PTR [rdi], 0
je .L2
cmp esi, 1
je .L2
call strlen
mov ebp, eax
add eax, 1
movsx rdi, eax
call malloc
mov QWORD PTR [rsp], rax
test r12d, r12d
jle .L11
lea eax, [r12-1]
mov DWORD PTR [rsp+12], eax
lea r11d, [rax+rax]
mov r12d, r11d
mov r8d, 0
mov r9d, 0
mov r13d, 1
jmp .L4
.L5:
test edi, edi
je .L7
add eax, r14d
mov edi, 0
.L6:
lea rcx, [rdx+1]
cmp ebp, eax
jle .L15
mov rdx, rcx
.L8:
movsx rcx, eax
movzx ecx, BYTE PTR [rbx+rcx]
mov BYTE PTR [rdx], cl
test sil, sil
je .L5
add eax, r11d
jmp .L6
.L7:
add eax, r15d
mov edi, r13d
jmp .L6
.L15:
add r8d, 1
sub r8d, r10d
add r8d, edx
.L10:
add r9d, 1
sub r12d, 2
cmp r12d, -2
je .L3
.L4:
cmp ebp, r9d
jle .L10
cmp DWORD PTR [rsp+12], r9d
sete sil
test r9d, r9d
sete al
or esi, eax
lea r15d, [r9+r9]
mov r14d, r12d
movsx r10, r8d
mov rax, QWORD PTR [rsp]
add r10, rax
mov rdx, r10
mov eax, r9d
mov edi, r13d
jmp .L8
.L11:
mov r8d, 0
.L3:
movsx r8, r8d
mov rax, QWORD PTR [rsp]
mov BYTE PTR [rax+r8], 0
mov rbx, rax
.L2:
mov rax, rbx
add rsp, 24
pop rbx
pop rbp
pop r12
pop r13
pop r14
pop r15
ret
|
190
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O2
|
x86-64 gcc 15.2
|
convert:
mov r8, rdi
test rdi, rdi
je .L18
cmp BYTE PTR [rdi], 0
je .L18
cmp esi, 1
je .L18
push r15
push r14
push r13
mov r13d, esi
push r12
push rbp
push rbx
sub rsp, 24
mov QWORD PTR [rsp+8], rdi
call strlen
lea edi, [rax+1]
mov QWORD PTR [rsp], rax
movsx rdi, edi
call malloc
test r13d, r13d
mov r9, QWORD PTR [rsp]
mov r8, QWORD PTR [rsp+8]
mov r15, rax
jle .L4
lea eax, [r13-1]
xor r11d, r11d
xor r10d, r10d
mov DWORD PTR [rsp], eax
lea ebp, [rax+rax]
mov r12d, ebp
.L9:
cmp r9d, r10d
jle .L11
cmp DWORD PTR [rsp], r10d
movsx rbx, r11d
lea r14d, [r10+r10]
mov edi, 1
sete sil
test r10d, r10d
sete al
add rbx, r15
or esi, eax
mov rdx, rbx
mov eax, r10d
jmp .L8
.L23:
add eax, ebp
.L6:
cmp r9d, eax
jle .L22
.L12:
add rdx, 1
.L8:
movsx rcx, eax
movzx ecx, BYTE PTR [r8+rcx]
mov BYTE PTR [rdx], cl
test sil, sil
jne .L23
test edi, edi
je .L7
add eax, r12d
xor edi, edi
cmp r9d, eax
jg .L12
.L22:
add r11d, 1
sub r11d, ebx
add r11d, edx
.L11:
add r10d, 1
sub r12d, 2
cmp r13d, r10d
jne .L9
movsx rax, r11d
add rax, r15
.L4:
mov BYTE PTR [rax], 0
add rsp, 24
mov rax, r15
pop rbx
pop rbp
pop r12
pop r13
pop r14
pop r15
ret
.L18:
mov rax, r8
ret
.L7:
add eax, r14d
mov edi, 1
jmp .L6
|
191
| 6
|
Zigzag Conversion
|
Medium
|
/*
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
char* convert(char* s, int numRows) {
int len;
int i, j, k = 0;
char *p;
int step, up;
if (!s || !*s || numRows == 1) return s;
len = strlen(s);
p = malloc((len + 1) * sizeof(char));
//assert(p);
step = (numRows - 1) * 2; // max span
for (i = 0; i < numRows; i ++) {
j = i; // first letter of each row
up = 1;
while (j < len) {
p[k ++] = s[j];
if (i == 0 || i == numRows - 1) {
j += step; // full span
} else if (up) {
j += step - i * 2; // full span - offset
up = 0;
} else {
j += i * 2; // offset
up = 1;
}
}
}
p[k] = 0;
return p;
}
/*
Difficulty:Medium
Total Accepted:163.3K
Total Submissions:610.5K
Related Topics String
*/
|
x86-64
|
-O3
|
x86-64 gcc 15.2
|
convert:
mov r8, rdi
test rdi, rdi
je .L27
cmp BYTE PTR [rdi], 0
je .L27
cmp esi, 1
je .L27
push r15
push r14
push r13
push r12
push rbp
push rbx
mov ebx, esi
sub rsp, 24
mov QWORD PTR [rsp+8], rdi
call strlen
lea edi, [rax+1]
mov QWORD PTR [rsp], rax
movsx rdi, edi
call malloc
test ebx, ebx
mov r9, QWORD PTR [rsp]
mov r8, QWORD PTR [rsp+8]
mov rbp, rax
jle .L4
test r9d, r9d
jle .L18
lea r12d, [rbx-1]
cmp ebx, r9d
lea r11d, [r12+r12]
cmovg ebx, r9d
xor edi, edi
xor ecx, ecx
movsx r10, r11d
.L11:
cmp r12d, edi
je .L15
test rdi, rdi
je .L15
movsx r14, ecx
mov eax, edi
lea r15d, [rdi+rdi]
mov r13d, 1
add r14, rbp
mov rdx, r14
jmp .L7
.L32:
add eax, r15d
mov r13d, 1
cmp r9d, eax
jle .L31
.L17:
add rdx, 1
.L7:
movsx rsi, eax
movzx esi, BYTE PTR [r8+rsi]
mov BYTE PTR [rdx], sil
test r13d, r13d
je .L32
add eax, r11d
xor r13d, r13d
cmp r9d, eax
jg .L17
.L31:
add ecx, 1
add rdi, 1
sub r11d, 2
sub ecx, r14d
add ecx, edx
cmp ebx, edi
jg .L11
.L13:
movsx rax, ecx
add rax, rbp
.L4:
mov BYTE PTR [rax], 0
add rsp, 24
mov rax, rbp
pop rbx
pop rbp
pop r12
pop r13
pop r14
pop r15
ret
.L27:
mov rax, r8
ret
.L15:
movsx rdx, ecx
mov rax, rdi
add rdx, rbp
.L9:
movzx esi, BYTE PTR [r8+rax]
add rax, r10
add ecx, 1
add rdx, 1
mov BYTE PTR [rdx-1], sil
cmp r9d, eax
jg .L9
add rdi, 1
sub r11d, 2
cmp ebx, edi
jg .L11
jmp .L13
.L18:
xor ecx, ecx
jmp .L13
|
192
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O0
|
ARM64 gcc 15.2.0
|
reverse:
sub sp, sp, #32
str w0, [sp, 12]
str wzr, [sp, 28]
b .L2
.L7:
ldr w1, [sp, 12]
mov w0, 10
sdiv w2, w1, w0
mov w0, w2
lsl w0, w0, 2
add w0, w0, w2
lsl w0, w0, 1
sub w0, w1, w0
str w0, [sp, 24]
ldr w0, [sp, 12]
cmp w0, 0
ble .L3
mov w1, 2147483647
ldr w0, [sp, 24]
sub w0, w1, w0
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
ldr w1, [sp, 28]
cmp w1, w0
bgt .L4
.L3:
ldr w0, [sp, 12]
cmp w0, 0
bge .L5
mov w1, -2147483648
ldr w0, [sp, 24]
sub w0, w1, w0
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
ldr w1, [sp, 28]
cmp w1, w0
bge .L5
.L4:
mov w0, 0
b .L6
.L5:
ldr w1, [sp, 28]
mov w0, w1
lsl w0, w0, 2
add w0, w0, w1
lsl w0, w0, 1
mov w1, w0
ldr w0, [sp, 24]
add w0, w0, w1
str w0, [sp, 28]
ldr w0, [sp, 12]
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, 12]
.L2:
ldr w0, [sp, 12]
cmp w0, 0
bne .L7
ldr w0, [sp, 28]
.L6:
add sp, sp, 32
ret
|
193
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O1
|
ARM64 gcc 15.2.0
|
reverse:
mov w2, w0
cbz w0, .L1
mov w0, 0
mov w6, 10
mov w8, -2147483648
mov w5, 26215
movk w5, 0x6666, lsl 16
mov w7, 2147483647
b .L5
.L3:
sub w3, w8, w1
smull x4, w3, w5
asr x4, x4, 34
sub w3, w4, w3, asr 31
cmp w3, w0
bgt .L8
.L4:
add w0, w0, w0, lsl 2
add w0, w1, w0, lsl 1
smull x1, w2, w5
asr x1, x1, 34
subs w2, w1, w2, asr 31
beq .L1
.L5:
sdiv w1, w2, w6
add w1, w1, w1, lsl 2
sub w1, w2, w1, lsl 1
cmp w2, 0
ble .L3
sub w3, w7, w1
smull x4, w3, w5
asr x4, x4, 34
sub w3, w4, w3, asr 31
cmp w3, w0
bge .L4
mov w0, 0
.L1:
ret
.L8:
mov w0, 0
b .L1
|
194
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O2
|
ARM64 gcc 15.2.0
|
reverse:
mov w2, w0
mov w0, 0
cbz w2, .L1
mov w5, 26215
mov w7, 52429
mov w6, 10
mov w9, -2147483648
movk w5, 0x6666, lsl 16
mov w8, 2147483647
movk w7, 0xcccc, lsl 16
b .L5
.L11:
sub w3, w8, w1
umull x3, w3, w7
lsr x3, x3, 35
cmp w3, w0
blt .L8
.L4:
add w0, w0, w0, lsl 2
add w0, w1, w0, lsl 1
smull x1, w2, w5
asr x1, x1, 34
subs w2, w1, w2, asr 31
beq .L1
.L5:
sdiv w1, w2, w6
add w1, w1, w1, lsl 2
sub w1, w2, w1, lsl 1
cmp w2, 0
bgt .L11
sub w3, w9, w1
smull x4, w3, w5
asr x4, x4, 34
sub w3, w4, w3, asr 31
cmp w3, w0
ble .L4
.L8:
mov w0, 0
.L1:
ret
|
195
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O3
|
ARM64 gcc 15.2.0
|
reverse:
mov w2, w0
mov w0, 0
cbz w2, .L1
mov w7, 26215
mov w9, 52429
mov w8, 10
mov w11, -2147483648
movk w7, 0x6666, lsl 16
mov w10, 2147483647
movk w9, 0xcccc, lsl 16
b .L5
.L11:
sub w4, w10, w1
umull x4, w4, w9
lsr x4, x4, 35
cmp w4, w0
blt .L8
.L4:
add w0, w1, w5, lsl 1
subs w2, w3, w2, asr 31
beq .L1
.L5:
sdiv w1, w2, w8
smull x3, w2, w7
add w5, w0, w0, lsl 2
asr x3, x3, 34
add w1, w1, w1, lsl 2
sub w1, w2, w1, lsl 1
cmp w2, 0
bgt .L11
sub w4, w11, w1
smull x6, w4, w7
asr x6, x6, 34
sub w4, w6, w4, asr 31
cmp w4, w0
ble .L4
.L8:
mov w0, 0
.L1:
ret
|
196
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O0
|
armv8-a clang 21.1.0
|
reverse:
sub sp, sp, #16
str w0, [sp, #8]
str wzr, [sp]
b .LBB0_1
.LBB0_1:
ldr w8, [sp, #8]
cbz w8, .LBB0_8
b .LBB0_2
.LBB0_2:
ldr w8, [sp, #8]
mov w10, #10
sdiv w9, w8, w10
mul w9, w9, w10
subs w8, w8, w9
str w8, [sp, #4]
ldr w8, [sp, #8]
subs w8, w8, #0
b.le .LBB0_4
b .LBB0_3
.LBB0_3:
ldr w8, [sp]
ldr w10, [sp, #4]
mov w9, #2147483647
subs w9, w9, w10
mov w10, #10
sdiv w9, w9, w10
subs w8, w8, w9
b.gt .LBB0_6
b .LBB0_4
.LBB0_4:
ldr w8, [sp, #8]
tbz w8, #31, .LBB0_7
b .LBB0_5
.LBB0_5:
ldr w8, [sp]
ldr w10, [sp, #4]
mov w9, #-2147483648
subs w9, w9, w10
mov w10, #10
sdiv w9, w9, w10
subs w8, w8, w9
b.ge .LBB0_7
b .LBB0_6
.LBB0_6:
str wzr, [sp, #12]
b .LBB0_9
.LBB0_7:
ldr w8, [sp]
mov w9, #10
mul w8, w8, w9
ldr w10, [sp, #4]
add w8, w8, w10
str w8, [sp]
ldr w8, [sp, #8]
sdiv w8, w8, w9
str w8, [sp, #8]
b .LBB0_1
.LBB0_8:
ldr w8, [sp]
str w8, [sp, #12]
b .LBB0_9
.LBB0_9:
ldr w0, [sp, #12]
add sp, sp, #16
ret
|
197
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O1
|
armv8-a clang 21.1.0
|
reverse:
cbz w0, .LBB0_8
mov w9, #26215
mov w8, w0
mov w0, wzr
movk w9, #26214, lsl #16
mov w10, #10
mov w11, #-2147483648
b .LBB0_3
.LBB0_2:
madd w0, w0, w10, w13
add w8, w8, #9
cmp w8, #18
mov w8, w12
b.ls .LBB0_8
.LBB0_3:
smull x12, w8, w9
cmp w8, #1
asr x12, x12, #34
add w12, w12, w12, lsr #31
msub w13, w12, w10, w8
b.lt .LBB0_5
eor w14, w13, #0x7fffffff
umull x14, w14, w9
lsr x14, x14, #34
cmp w0, w14
b.gt .LBB0_7
.LBB0_5:
tbz w8, #31, .LBB0_2
sub w14, w11, w13
smull x14, w14, w9
asr x14, x14, #34
add w14, w14, w14, lsr #31
cmp w0, w14
b.ge .LBB0_2
.LBB0_7:
mov w0, wzr
.LBB0_8:
ret
|
198
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O2
|
armv8-a clang 21.1.0
|
reverse:
cbz w0, .LBB0_7
mov w9, #26215
mov w11, #52429
mov w8, w0
mov w0, wzr
movk w9, #26214, lsl #16
mov w10, #10
movk w11, #52428, lsl #16
b .LBB0_4
.LBB0_2:
eor w14, w13, #0x80000000
umull x14, w14, w11
lsr x14, x14, #35
cmn w0, w14
b.lt .LBB0_6
.LBB0_3:
madd w0, w0, w10, w13
add w8, w8, #9
cmp w8, #18
mov w8, w12
b.ls .LBB0_7
.LBB0_4:
smull x12, w8, w9
cmp w8, #1
asr x12, x12, #34
add w12, w12, w12, lsr #31
msub w13, w12, w10, w8
b.lt .LBB0_2
eor w14, w13, #0x7fffffff
umull x14, w14, w9
lsr x14, x14, #34
cmp w0, w14
b.le .LBB0_3
.LBB0_6:
mov w0, wzr
.LBB0_7:
ret
|
199
| 7
|
Reverse Integer
|
Medium
|
/*
7. Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
*/
int reverse(int x) {
int d, k = 0;
// 2147483647
//-2147483648
while (x) {
d = x % 10;
if ((x > 0 && k > (0x7fffffff - d) / 10) ||
(x < 0 && k < ((signed)0x80000000 - d) / 10)) {
return 0; // overflow
}
k = k * 10 + d;
x = x / 10;
}
return k; //(int)k == k ? (int)k : 0;
}
/*
Difficulty:Easy
Total Accepted:275.6K
Total Submissions:1.1M
Companies Bloomberg Apple
Related Topics Math
Similar Questions
String to Integer (atoi)
*/
|
aarch64
|
-O3
|
armv8-a clang 21.1.0
|
reverse:
cbz w0, .LBB0_7
mov w9, #26215
mov w11, #52429
mov w8, w0
mov w0, wzr
movk w9, #26214, lsl #16
mov w10, #10
movk w11, #52428, lsl #16
b .LBB0_4
.LBB0_2:
eor w14, w13, #0x80000000
umull x14, w14, w11
lsr x14, x14, #35
cmn w0, w14
b.lt .LBB0_6
.LBB0_3:
madd w0, w0, w10, w13
add w8, w8, #9
cmp w8, #18
mov w8, w12
b.ls .LBB0_7
.LBB0_4:
smull x12, w8, w9
cmp w8, #1
asr x12, x12, #34
add w12, w12, w12, lsr #31
msub w13, w12, w10, w8
b.lt .LBB0_2
eor w14, w13, #0x7fffffff
umull x14, w14, w9
lsr x14, x14, #34
cmp w0, w14
b.le .LBB0_3
.LBB0_6:
mov w0, wzr
.LBB0_7:
ret
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.