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
300
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O0
mips64 gcc 15.2.0
match_recursive: daddiu $sp,$sp,-64 sd $31,56($sp) sd $fp,48($sp) sd $28,40($sp) move $fp,$sp lui $28,%hi(%neg(%gp_rel(match_recursive))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(match_recursive))) sd $4,0($fp) sd $5,8($fp) sd $6,16($fp) ld $2,8($fp) lb $2,0($2) bne $2,$0,.L2 nop ld $2,0($fp) lb $2,0($2) sltu $2,$2,1 andi $2,$2,0x00ff b .L3 nop .L2: ld $2,8($fp) daddiu $2,$2,1 lb $2,0($2) li $3,42 # 0x2a bne $2,$3,.L4 nop b .L5 nop .L10: ld $2,8($fp) daddiu $2,$2,2 ld $6,16($fp) move $5,$2 ld $4,0($fp) ld $2,%got_disp(match_recursive)($28) mtlo $2 mflo $25 jalr $25 nop beq $2,$0,.L6 nop li $2,1 # 0x1 b .L3 nop .L6: ld $2,8($fp) lb $2,0($2) li $3,46 # 0x2e bne $2,$3,.L7 nop ld $2,0($fp) lb $2,0($2) beq $2,$0,.L8 nop .L7: ld $2,8($fp) lb $2,0($2) li $3,46 # 0x2e beq $2,$3,.L9 nop ld $2,0($fp) lb $2,0($2) ld $3,8($fp) lb $3,0($3) beq $2,$3,.L9 nop .L8: move $2,$0 b .L3 nop .L9: ld $2,0($fp) daddiu $2,$2,1 sd $2,0($fp) .L5: ld $2,16($fp) lw $2,0($2) bne $2,$0,.L10 nop ld $2,16($fp) sw $0,0($2) move $2,$0 b .L3 nop .L4: ld $2,8($fp) lb $2,0($2) li $3,46 # 0x2e bne $2,$3,.L11 nop ld $2,0($fp) lb $2,0($2) beq $2,$0,.L12 nop .L11: ld $2,8($fp) lb $2,0($2) li $3,46 # 0x2e beq $2,$3,.L13 nop ld $2,0($fp) lb $2,0($2) ld $3,8($fp) lb $3,0($3) beq $2,$3,.L13 nop .L12: move $2,$0 b .L3 nop .L13: ld $2,0($fp) daddiu $3,$2,1 ld $2,8($fp) daddiu $2,$2,1 ld $6,16($fp) move $5,$2 move $4,$3 ld $2,%got_disp(match_recursive)($28) mtlo $2 mflo $25 jalr $25 nop .L3: move $sp,$fp ld $31,56($sp) ld $fp,48($sp) ld $28,40($sp) daddiu $sp,$sp,64 jr $31 nop isMatch: 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(isMatch))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(isMatch))) sd $4,32($fp) sd $5,40($fp) ld $4,32($fp) ld $2,%call16(strlen)($28) mtlo $2 mflo $25 jalr $25 nop sw $2,8($fp) ld $4,40($fp) ld $2,%call16(strlen)($28) mtlo $2 mflo $25 jalr $25 nop sw $2,12($fp) lw $2,8($fp) addiu $2,$2,1 move $3,$2 lw $2,12($fp) addiu $2,$2,1 mult $3,$2 mflo $2 li $5,4 # 0x4 move $4,$2 ld $2,%call16(calloc)($28) mtlo $2 mflo $25 jalr $25 nop sd $2,16($fp) ld $2,16($fp) li $3,1 # 0x1 sw $3,0($2) sw $0,4($fp) b .L15 nop .L17: lw $2,4($fp) ld $3,40($fp) daddu $2,$3,$2 lb $2,0($2) li $3,42 # 0x2a bne $2,$3,.L16 nop lw $2,4($fp) addiu $2,$2,-2 daddiu $2,$2,1 dsll $2,$2,2 ld $3,16($fp) daddu $3,$3,$2 lw $2,4($fp) daddiu $2,$2,1 dsll $2,$2,2 ld $4,16($fp) daddu $2,$4,$2 lw $3,0($3) sw $3,0($2) .L16: lw $2,4($fp) addiu $2,$2,1 sw $2,4($fp) .L15: lw $3,4($fp) lw $2,12($fp) slt $2,$3,$2 bne $2,$0,.L17 nop sw $0,0($fp) b .L18 nop .L30: sw $0,4($fp) b .L19 nop .L29: lw $2,4($fp) ld $3,40($fp) daddu $2,$3,$2 lb $2,0($2) li $3,42 # 0x2a beq $2,$3,.L20 nop lw $2,12($fp) addiu $2,$2,1 lw $3,0($fp) mult $3,$2 mflo $3 lw $2,4($fp) addiu $2,$2,-1 addu $2,$3,$2 daddiu $2,$2,1 dsll $2,$2,2 ld $3,16($fp) daddu $2,$3,$2 lw $2,0($2) beq $2,$0,.L21 nop lw $2,0($fp) ld $3,32($fp) daddu $2,$3,$2 lb $2,0($2) lw $3,4($fp) ld $4,40($fp) daddu $3,$4,$3 lb $3,0($3) beq $2,$3,.L22 nop lw $2,4($fp) ld $3,40($fp) daddu $2,$3,$2 lb $2,0($2) li $3,46 # 0x2e bne $2,$3,.L21 nop .L22: li $3,1 # 0x1 b .L23 nop .L21: move $3,$0 .L23: lw $2,0($fp) addiu $2,$2,1 move $4,$2 lw $2,12($fp) addiu $2,$2,1 mult $4,$2 mflo $2 lw $4,4($fp) addu $2,$4,$2 daddiu $2,$2,1 dsll $2,$2,2 ld $4,16($fp) daddu $2,$4,$2 sw $3,0($2) b .L24 nop .L20: lw $2,0($fp) addiu $2,$2,1 move $3,$2 lw $2,12($fp) addiu $2,$2,1 mult $3,$2 mflo $3 lw $2,4($fp) addiu $2,$2,-2 addu $2,$3,$2 daddiu $2,$2,1 dsll $2,$2,2 ld $3,16($fp) daddu $2,$3,$2 lw $2,0($2) bne $2,$0,.L25 nop lw $2,0($fp) addiu $2,$2,1 move $3,$2 lw $2,12($fp) addiu $2,$2,1 mult $3,$2 mflo $3 lw $2,4($fp) addiu $2,$2,-1 addu $2,$3,$2 daddiu $2,$2,1 dsll $2,$2,2 ld $3,16($fp) daddu $2,$3,$2 lw $2,0($2) bne $2,$0,.L25 nop lw $2,0($fp) ld $3,32($fp) daddu $2,$3,$2 lb $2,0($2) lw $3,4($fp) daddiu $3,$3,-1 ld $4,40($fp) daddu $3,$4,$3 lb $3,0($3) beq $2,$3,.L26 nop lw $2,4($fp) daddiu $2,$2,-1 ld $3,40($fp) daddu $2,$3,$2 lb $2,0($2) li $3,46 # 0x2e bne $2,$3,.L27 nop .L26: lw $2,12($fp) addiu $2,$2,1 lw $3,0($fp) mult $3,$2 mflo $2 lw $3,4($fp) addu $2,$3,$2 daddiu $2,$2,1 dsll $2,$2,2 ld $3,16($fp) daddu $2,$3,$2 lw $2,0($2) beq $2,$0,.L27 nop .L25: li $3,1 # 0x1 b .L28 nop .L27: move $3,$0 .L28: lw $2,0($fp) addiu $2,$2,1 move $4,$2 lw $2,12($fp) addiu $2,$2,1 mult $4,$2 mflo $2 lw $4,4($fp) addu $2,$4,$2 daddiu $2,$2,1 dsll $2,$2,2 ld $4,16($fp) daddu $2,$4,$2 sw $3,0($2) .L24: lw $2,4($fp) addiu $2,$2,1 sw $2,4($fp) .L19: lw $3,4($fp) lw $2,12($fp) slt $2,$3,$2 bne $2,$0,.L29 nop lw $2,0($fp) addiu $2,$2,1 sw $2,0($fp) .L18: lw $3,0($fp) lw $2,8($fp) slt $2,$3,$2 bne $2,$0,.L30 nop lw $2,12($fp) addiu $2,$2,1 lw $3,8($fp) mult $3,$2 mflo $3 lw $2,12($fp) addiu $2,$2,-1 addu $2,$3,$2 daddiu $2,$2,1 dsll $2,$2,2 ld $3,16($fp) daddu $2,$3,$2 lw $2,0($2) sw $2,0($fp) ld $4,16($fp) ld $2,%call16(free)($28) mtlo $2 mflo $25 jalr $25 nop lw $2,0($fp) sltu $2,$0,$2 andi $2,$2,0x00ff move $sp,$fp ld $31,72($sp) ld $fp,64($sp) ld $28,56($sp) daddiu $sp,$sp,80 jr $31 nop
301
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O1
mips64 gcc 15.2.0
match_recursive: daddiu $sp,$sp,-64 sd $31,56($sp) sd $28,48($sp) sd $20,40($sp) sd $19,32($sp) sd $18,24($sp) sd $17,16($sp) sd $16,8($sp) lui $28,%hi(%neg(%gp_rel(match_recursive))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(match_recursive))) lb $3,0($5) beq $3,$0,.L15 move $16,$4 move $17,$5 lb $4,1($5) li $2,42 # 0x2a beq $4,$2,.L16 move $18,$6 li $2,46 # 0x2e beql $3,$2,.L17 lb $2,0($16) lb $4,0($16) bne $4,$3,.L3 move $2,$0 move $6,$18 .L21: daddiu $5,$17,1 ld $25,%got_disp(match_recursive)($28) 1: jalr $25 daddiu $4,$16,1 b .L19 ld $31,56($sp) .L15: lb $2,0($4) sltu $2,$2,1 .L3: ld $31,56($sp) .L19: ld $28,48($sp) ld $20,40($sp) ld $19,32($sp) ld $18,24($sp) ld $17,16($sp) ld $16,8($sp) jr $31 daddiu $sp,$sp,64 .L16: lw $2,0($6) beq $2,$0,.L5 daddiu $2,$5,2 move $19,$2 ld $2,%got_disp(match_recursive)($28) b .L8 move $20,$2 .L18: beq $3,$0,.L19 ld $31,56($sp) lw $2,0($18) .L20: beq $2,$0,.L5 daddiu $16,$16,1 .L8: move $6,$18 move $5,$19 move $25,$20 jalr $25 move $4,$16 bne $2,$0,.L3 li $4,46 # 0x2e lb $3,0($17) beql $3,$4,.L18 lb $3,0($16) lb $4,0($16) beql $4,$3,.L20 lw $2,0($18) b .L19 ld $31,56($sp) .L5: sw $0,0($18) b .L3 move $2,$0 .L17: bne $2,$0,.L21 move $6,$18 b .L3 move $2,$0 isMatch: daddiu $sp,$sp,-80 sd $31,72($sp) sd $28,64($sp) sd $23,56($sp) sd $22,48($sp) sd $21,40($sp) sd $20,32($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) sd $16,0($sp) lui $28,%hi(%neg(%gp_rel(isMatch))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(isMatch))) move $23,$4 ld $25,%call16(strlen)($28) 1: jalr $25 move $18,$5 move $20,$2 sll $22,$2,0 ld $25,%call16(strlen)($28) 1: jalr $25 move $4,$18 move $21,$2 sll $16,$2,0 move $17,$16 addiu $16,$16,1 addiu $2,$22,1 mult $2,$16 mflo $4 move $19,$4 ld $25,%call16(calloc)($28) 1: jalr $25 li $5,4 # 0x4 move $4,$2 li $2,1 # 0x1 blez $17,.L23 sw $2,0($4) move $3,$18 daddiu $2,$4,4 sll $5,$21,0 addiu $5,$5,-1 dsll $6,$5,32 dsrl $6,$6,32 dsll $6,$6,2 daddiu $5,$4,8 daddu $6,$6,$5 b .L25 li $7,42 # 0x2a .L24: daddiu $2,$2,4 .L42: beq $2,$6,.L23 daddiu $3,$3,1 .L25: lb $5,0($3) bnel $5,$7,.L42 daddiu $2,$2,4 lw $5,-8($2) b .L24 sw $5,0($2) .L23: blez $22,.L26 sll $15,$21,0 addu $13,$15,$16 sll $2,$18,0 move $24,$2 subu $7,$16,$2 addiu $15,$15,1 subu $12,$0,$2 move $14,$12 move $9,$23 daddiu $31,$23,1 sll $2,$20,0 addiu $2,$2,-1 dsll $2,$2,32 dsrl $2,$2,32 daddu $31,$31,$2 li $8,42 # 0x2a li $11,1 # 0x1 b .L27 li $25,46 # 0x2e .L36: move $22,$11 .L29: addu $3,$20,$7 .L43: daddiu $3,$3,1 dsll $3,$3,2 daddu $3,$4,$3 sw $22,0($3) .L30: addiu $6,$6,1 beq $6,$2,.L35 daddiu $5,$5,1 .L33: lb $21,0($5) beq $21,$8,.L28 sll $3,$5,0 move $20,$3 addu $3,$10,$3 daddiu $3,$3,1 dsll $3,$3,2 daddu $3,$4,$3 lw $22,0($3) beql $22,$0,.L43 addu $3,$20,$7 lb $3,0($9) beq $3,$21,.L36 xori $21,$21,0x2e b .L29 sltu $22,$21,1 .L28: addiu $3,$6,-2 daddiu $3,$3,1 dsll $3,$3,2 daddu $3,$4,$3 lw $3,0($3) bne $3,$0,.L31 move $21,$11 addiu $3,$6,-1 daddiu $3,$3,1 dsll $3,$3,2 daddu $3,$4,$3 lw $21,0($3) bnel $21,$0,.L38 move $21,$11 lb $3,-1($5) lb $20,0($9) beql $20,$3,.L44 sll $3,$5,0 bnel $3,$25,.L45 daddiu $3,$6,1 sll $3,$5,0 .L44: addu $3,$3,$14 daddiu $3,$3,1 dsll $3,$3,2 daddu $3,$4,$3 lw $21,0($3) sltu $21,$0,$21 .L38: .L31: daddiu $3,$6,1 .L45: dsll $3,$3,2 daddu $3,$4,$3 b .L30 sw $21,0($3) .L35: addu $13,$13,$16 addu $7,$16,$7 addu $14,$14,$15 daddiu $9,$9,1 beq $9,$31,.L26 addu $12,$16,$12 .L27: blez $17,.L35 addu $6,$7,$24 move $5,$18 addiu $10,$12,-1 b .L33 move $2,$13 .L26: subu $2,$19,$16 addiu $17,$17,-1 addu $2,$2,$17 daddiu $2,$2,1 dsll $2,$2,2 daddu $2,$4,$2 ld $25,%call16(free)($28) 1: jalr $25 lw $16,0($2) sltu $2,$0,$16 ld $31,72($sp) ld $28,64($sp) ld $23,56($sp) ld $22,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) jr $31 daddiu $sp,$sp,80
302
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O2
mips64 gcc 15.2.0
match_recursive: daddiu $sp,$sp,-64 sd $28,48($sp) sd $16,0($sp) sd $31,56($sp) sd $21,40($sp) sd $20,32($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) lb $2,0($5) lui $28,%hi(%neg(%gp_rel(match_recursive))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(match_recursive))) beq $2,$0,.L14 move $16,$4 move $17,$5 li $7,46 # 0x2e b .L2 li $5,42 # 0x2a .L12: bnel $4,$3,.L3 move $2,$0 daddiu $16,$16,1 .L28: beq $2,$0,.L14 daddiu $17,$17,1 .L2: move $3,$2 lb $2,1($17) beql $2,$5,.L27 lw $2,0($6) bne $3,$7,.L12 lb $4,0($16) bnel $4,$0,.L28 daddiu $16,$16,1 .L8: move $2,$0 .L3: ld $31,56($sp) .L29: ld $28,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) jr $31 daddiu $sp,$sp,64 .L14: lb $2,0($16) ld $31,56($sp) ld $28,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) sltu $2,$2,1 jr $31 daddiu $sp,$sp,64 .L27: move $18,$6 daddiu $20,$17,2 bne $2,$0,.L10 li $19,46 # 0x2e b .L8 sw $0,0($18) .L6: bne $2,$3,.L3 move $2,$0 lw $2,0($18) .L30: beql $2,$0,.L8 sw $0,0($18) .L10: ld $25,%got_disp(match_recursive)($28) move $6,$18 move $5,$20 1: jalr $25 move $4,$16 bne $2,$0,.L29 ld $31,56($sp) lb $2,0($17) lb $3,0($16) bne $2,$19,.L6 daddiu $16,$16,1 bnel $3,$0,.L30 lw $2,0($18) b .L29 move $2,$0 isMatch: daddiu $sp,$sp,-64 sd $28,48($sp) lui $28,%hi(%neg(%gp_rel(isMatch))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(isMatch))) ld $25,%call16(strlen)($28) sd $31,56($sp) sd $21,40($sp) sd $20,32($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) sd $16,0($sp) move $19,$5 1: jalr $25 move $21,$4 ld $25,%call16(strlen)($28) move $4,$19 1: jalr $25 sll $20,$2,0 sll $18,$2,0 addiu $17,$18,1 addiu $2,$20,1 mult $2,$17 ld $25,%call16(calloc)($28) li $5,4 # 0x4 mflo $16 1: jalr $25 move $4,$16 li $3,1 # 0x1 blez $18,.L38 sw $3,0($2) addiu $6,$18,-1 dsll $6,$6,32 dsrl $6,$6,32 dsll $6,$6,2 daddiu $5,$2,8 move $4,$19 daddiu $3,$2,4 daddu $6,$6,$5 li $7,42 # 0x2a lb $5,0($4) .L60: bne $5,$7,.L36 daddiu $4,$4,1 lw $5,-8($3) sw $5,0($3) .L36: daddiu $3,$3,4 bnel $3,$6,.L60 lb $5,0($4) .L38: blez $20,.L33 addiu $20,$20,-1 dsll $20,$20,32 daddiu $12,$21,1 dsrl $20,$20,32 move $4,$21 blez $18,.L35 daddu $12,$12,$20 sll $13,$19,0 subu $10,$0,$13 addu $7,$18,$17 addiu $14,$18,1 move $11,$10 addu $13,$13,$17 li $8,42 # 0x2a li $15,46 # 0x2e .L45: addu $5,$13,$10 move $6,$19 b .L44 addiu $9,$10,-1 .L58: addu $3,$9,$3 addiu $3,$3,1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $25,0($3) beql $25,$0,.L61 addiu $3,$5,1 lb $3,0($4) beq $3,$24,.L46 xori $24,$24,0x2e sltu $25,$24,1 .L40: addiu $3,$5,1 .L61: dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 addiu $5,$5,1 sw $25,0($3) beq $7,$5,.L57 daddiu $6,$6,1 .L44: lb $24,0($6) bne $24,$8,.L58 sll $3,$6,0 addiu $3,$5,-1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $3,0($3) bne $3,$0,.L42 li $24,1 # 0x1 dsll $3,$5,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $24,0($3) bnel $24,$0,.L42 li $24,1 # 0x1 lb $3,-1($6) lb $25,0($4) beql $25,$3,.L62 sll $3,$6,0 beq $3,$15,.L43 sll $3,$6,0 .L42: addiu $3,$5,1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 addiu $5,$5,1 sw $24,0($3) bne $7,$5,.L44 daddiu $6,$6,1 .L57: daddiu $4,$4,1 addu $7,$7,$17 addu $11,$11,$14 bne $4,$12,.L45 addu $10,$10,$17 .L33: subu $3,$16,$17 .L59: addiu $18,$18,-1 ld $25,%call16(free)($28) addu $3,$3,$18 dsll $3,$3,2 daddu $3,$2,$3 move $4,$2 1: jalr $25 lw $16,4($3) ld $31,56($sp) ld $28,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) sltu $2,$0,$16 ld $16,0($sp) jr $31 daddiu $sp,$sp,64 .L35: daddiu $3,$4,1 .L63: beq $3,$12,.L33 daddiu $4,$4,2 bnel $4,$12,.L63 daddiu $3,$4,1 b .L59 subu $3,$16,$17 .L46: b .L40 li $25,1 # 0x1 .L43: .L62: addu $3,$3,$11 addiu $3,$3,1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $24,0($3) b .L42 sltu $24,$0,$24
303
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
mips64
-O3
mips64 gcc 15.2.0
match_recursive: daddiu $sp,$sp,-64 sd $28,48($sp) sd $16,0($sp) sd $31,56($sp) sd $21,40($sp) sd $20,32($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) lb $2,0($5) lui $28,%hi(%neg(%gp_rel(match_recursive))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(match_recursive))) beq $2,$0,.L14 move $16,$4 move $17,$5 li $7,46 # 0x2e b .L2 li $5,42 # 0x2a .L12: bnel $4,$3,.L3 move $2,$0 daddiu $16,$16,1 .L28: beq $2,$0,.L14 daddiu $17,$17,1 .L2: move $3,$2 lb $2,1($17) beql $2,$5,.L27 lw $2,0($6) bne $3,$7,.L12 lb $4,0($16) bnel $4,$0,.L28 daddiu $16,$16,1 .L8: move $2,$0 .L3: ld $31,56($sp) .L29: ld $28,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) jr $31 daddiu $sp,$sp,64 .L14: lb $2,0($16) ld $31,56($sp) ld $28,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) sltu $2,$2,1 jr $31 daddiu $sp,$sp,64 .L27: move $18,$6 daddiu $20,$17,2 bne $2,$0,.L10 li $19,46 # 0x2e b .L8 sw $0,0($18) .L6: bne $2,$3,.L3 move $2,$0 lw $2,0($18) .L30: beql $2,$0,.L8 sw $0,0($18) .L10: ld $25,%got_disp(match_recursive)($28) move $6,$18 move $5,$20 1: jalr $25 move $4,$16 bne $2,$0,.L29 ld $31,56($sp) lb $2,0($17) lb $3,0($16) bne $2,$19,.L6 daddiu $16,$16,1 bnel $3,$0,.L30 lw $2,0($18) b .L29 move $2,$0 isMatch: daddiu $sp,$sp,-64 sd $28,48($sp) lui $28,%hi(%neg(%gp_rel(isMatch))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(isMatch))) ld $25,%call16(strlen)($28) sd $31,56($sp) sd $21,40($sp) sd $20,32($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) sd $16,0($sp) move $18,$5 1: jalr $25 move $21,$4 ld $25,%call16(strlen)($28) move $4,$18 1: jalr $25 sll $19,$2,0 sll $20,$2,0 addiu $17,$20,1 addiu $2,$19,1 mult $2,$17 ld $25,%call16(calloc)($28) li $5,4 # 0x4 mflo $16 1: jalr $25 move $4,$16 li $3,1 # 0x1 sw $3,0($2) blez $20,.L33 addiu $25,$20,-1 dsll $6,$25,32 dsrl $6,$6,32 daddiu $4,$2,8 dsll $6,$6,2 daddu $6,$6,$4 daddiu $3,$2,4 move $4,$18 li $7,42 # 0x2a lb $5,0($4) .L56: bne $5,$7,.L35 daddiu $4,$4,1 lw $5,-8($3) sw $5,0($3) .L35: daddiu $3,$3,4 bnel $6,$3,.L56 lb $5,0($4) blez $19,.L33 addiu $19,$19,-1 sll $14,$18,0 dsll $19,$19,32 subu $10,$0,$14 daddiu $15,$21,1 dsrl $19,$19,32 addu $11,$20,$17 move $4,$21 addiu $20,$20,1 move $12,$10 daddu $15,$15,$19 addu $14,$14,$17 li $8,42 # 0x2a li $24,46 # 0x2e .L34: addu $5,$14,$10 move $6,$18 addiu $9,$10,-1 b .L43 move $7,$11 .L55: addu $3,$9,$3 addiu $3,$3,1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $19,0($3) beql $19,$0,.L57 addiu $3,$5,1 lb $3,0($4) beq $3,$31,.L44 xori $31,$31,0x2e sltu $19,$31,1 .L39: addiu $3,$5,1 .L57: dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 addiu $5,$5,1 sw $19,0($3) beq $5,$7,.L54 daddiu $6,$6,1 .L43: lb $31,0($6) bne $31,$8,.L55 sll $3,$6,0 addiu $3,$5,-1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $3,0($3) bne $3,$0,.L41 li $31,1 # 0x1 dsll $3,$5,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $31,0($3) bnel $31,$0,.L41 li $31,1 # 0x1 lb $3,-1($6) lb $19,0($4) beql $19,$3,.L58 sll $3,$6,0 beq $3,$24,.L42 sll $3,$6,0 .L41: addiu $3,$5,1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 addiu $5,$5,1 sw $31,0($3) bne $5,$7,.L43 daddiu $6,$6,1 .L54: daddiu $4,$4,1 addu $11,$11,$17 addu $12,$20,$12 bne $15,$4,.L34 addu $10,$10,$17 .L33: subu $3,$16,$17 addu $3,$3,$25 ld $25,%call16(free)($28) dsll $3,$3,2 daddu $3,$2,$3 move $4,$2 1: jalr $25 lw $16,4($3) ld $31,56($sp) ld $28,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) sltu $2,$0,$16 ld $16,0($sp) jr $31 daddiu $sp,$sp,64 .L44: b .L39 li $19,1 # 0x1 .L42: .L58: addu $3,$3,$12 addiu $3,$3,1 dsll $3,$3,32 dsrl $3,$3,32 dsll $3,$3,2 daddu $3,$2,$3 lw $31,0($3) b .L41 sltu $31,$0,$31
304
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O0
RISC-V 64 clang 21.1.0
match_recursive: addi sp, sp, -48 sd ra, 40(sp) sd s0, 32(sp) addi s0, sp, 48 sd a0, -32(s0) sd a1, -40(s0) sd a2, -48(s0) ld a0, -40(s0) lbu a0, 0(a0) bnez a0, .LBB0_2 j .LBB0_1 .LBB0_1: ld a0, -32(s0) lbu a0, 0(a0) seqz a0, a0 sb a0, -17(s0) j .LBB0_20 .LBB0_2: ld a0, -40(s0) lbu a0, 1(a0) li a1, 42 bne a0, a1, .LBB0_14 j .LBB0_3 .LBB0_3: j .LBB0_4 .LBB0_4: ld a0, -48(s0) lw a0, 0(a0) beqz a0, .LBB0_13 j .LBB0_5 .LBB0_5: ld a0, -32(s0) ld a1, -40(s0) addi a1, a1, 2 ld a2, -48(s0) call match_recursive beqz a0, .LBB0_7 j .LBB0_6 .LBB0_6: li a0, 1 sb a0, -17(s0) j .LBB0_20 .LBB0_7: ld a0, -40(s0) lbu a0, 0(a0) li a1, 46 bne a0, a1, .LBB0_9 j .LBB0_8 .LBB0_8: ld a0, -32(s0) lbu a0, 0(a0) beqz a0, .LBB0_11 j .LBB0_9 .LBB0_9: ld a0, -40(s0) lbu a0, 0(a0) li a1, 46 beq a0, a1, .LBB0_12 j .LBB0_10 .LBB0_10: ld a0, -32(s0) lbu a0, 0(a0) ld a1, -40(s0) lbu a1, 0(a1) beq a0, a1, .LBB0_12 j .LBB0_11 .LBB0_11: li a0, 0 sb a0, -17(s0) j .LBB0_20 .LBB0_12: ld a0, -32(s0) addi a0, a0, 1 sd a0, -32(s0) j .LBB0_4 .LBB0_13: ld a1, -48(s0) li a0, 0 sw a0, 0(a1) sb a0, -17(s0) j .LBB0_20 .LBB0_14: ld a0, -40(s0) lbu a0, 0(a0) li a1, 46 bne a0, a1, .LBB0_16 j .LBB0_15 .LBB0_15: ld a0, -32(s0) lbu a0, 0(a0) beqz a0, .LBB0_18 j .LBB0_16 .LBB0_16: ld a0, -40(s0) lbu a0, 0(a0) li a1, 46 beq a0, a1, .LBB0_19 j .LBB0_17 .LBB0_17: ld a0, -32(s0) lbu a0, 0(a0) ld a1, -40(s0) lbu a1, 0(a1) beq a0, a1, .LBB0_19 j .LBB0_18 .LBB0_18: li a0, 0 sb a0, -17(s0) j .LBB0_20 .LBB0_19: ld a0, -32(s0) addi a0, a0, 1 ld a1, -40(s0) addi a1, a1, 1 ld a2, -48(s0) call match_recursive sb a0, -17(s0) j .LBB0_20 .LBB0_20: lbu a0, -17(s0) ld ra, 40(sp) ld s0, 32(sp) addi sp, sp, 48 ret isMatch: addi sp, sp, -96 sd ra, 88(sp) sd s0, 80(sp) addi s0, sp, 96 sd a0, -24(s0) sd a1, -32(s0) ld a0, -24(s0) call strlen sw a0, -52(s0) ld a0, -32(s0) call strlen sw a0, -56(s0) lw a0, -52(s0) addiw a0, a0, 1 lw a1, -56(s0) addiw a1, a1, 1 mulw a0, a0, a1 li a1, 4 call calloc sd a0, -40(s0) ld a1, -40(s0) li a0, 1 sw a0, 0(a1) li a0, 0 sw a0, -48(s0) j .LBB1_1 .LBB1_1: lw a0, -48(s0) lw a1, -56(s0) bge a0, a1, .LBB1_6 j .LBB1_2 .LBB1_2: ld a0, -32(s0) lw a1, -48(s0) add a0, a0, a1 lbu a0, 0(a0) li a1, 42 bne a0, a1, .LBB1_4 j .LBB1_3 .LBB1_3: ld a1, -40(s0) lw a2, -48(s0) addiw a0, a2, -2 slli a0, a0, 2 add a0, a0, a1 lw a0, 4(a0) addiw a2, a2, 1 slli a2, a2, 2 add a1, a1, a2 sw a0, 0(a1) j .LBB1_4 .LBB1_4: j .LBB1_5 .LBB1_5: lw a0, -48(s0) addiw a0, a0, 1 sw a0, -48(s0) j .LBB1_1 .LBB1_6: li a0, 0 sw a0, -44(s0) j .LBB1_7 .LBB1_7: lw a0, -44(s0) lw a1, -52(s0) bge a0, a1, .LBB1_27 j .LBB1_8 .LBB1_8: li a0, 0 sw a0, -48(s0) j .LBB1_9 .LBB1_9: lw a0, -48(s0) lw a1, -56(s0) bge a0, a1, .LBB1_25 j .LBB1_10 .LBB1_10: ld a0, -32(s0) lw a1, -48(s0) add a0, a0, a1 lbu a0, 0(a0) li a1, 42 beq a0, a1, .LBB1_16 j .LBB1_11 .LBB1_11: ld a1, -40(s0) lw a0, -44(s0) lw a2, -56(s0) addiw a2, a2, 1 mulw a2, a0, a2 lw a0, -48(s0) addw a0, a0, a2 addiw a0, a0, -1 slli a0, a0, 2 add a0, a0, a1 lw a0, 4(a0) li a1, 0 sd a1, -64(s0) beqz a0, .LBB1_15 j .LBB1_12 .LBB1_12: ld a0, -24(s0) lw a1, -44(s0) add a0, a0, a1 lbu a0, 0(a0) ld a1, -32(s0) lw a2, -48(s0) add a1, a1, a2 lbu a1, 0(a1) li a2, 1 sd a2, -72(s0) beq a0, a1, .LBB1_14 j .LBB1_13 .LBB1_13: ld a0, -32(s0) lw a1, -48(s0) add a0, a0, a1 lbu a0, 0(a0) addi a0, a0, -46 seqz a0, a0 sd a0, -72(s0) j .LBB1_14 .LBB1_14: ld a0, -72(s0) sd a0, -64(s0) j .LBB1_15 .LBB1_15: ld a0, -64(s0) andi a0, a0, 1 ld a2, -40(s0) lw a1, -44(s0) addiw a1, a1, 1 lw a3, -56(s0) addiw a3, a3, 1 mulw a1, a1, a3 lw a3, -48(s0) addw a1, a1, a3 slli a1, a1, 2 add a1, a1, a2 sw a0, 4(a1) j .LBB1_23 .LBB1_16: ld a1, -40(s0) lw a0, -44(s0) addiw a0, a0, 1 lw a2, -56(s0) addiw a2, a2, 1 mulw a2, a0, a2 lw a0, -48(s0) addw a0, a0, a2 addiw a0, a0, -2 slli a0, a0, 2 add a0, a0, a1 lw a0, 4(a0) li a1, 1 sd a1, -80(s0) bnez a0, .LBB1_22 j .LBB1_17 .LBB1_17: ld a1, -40(s0) lw a0, -44(s0) addiw a0, a0, 1 lw a2, -56(s0) addiw a2, a2, 1 mulw a2, a0, a2 lw a0, -48(s0) addw a0, a0, a2 addiw a0, a0, -1 slli a0, a0, 2 add a0, a0, a1 lw a0, 4(a0) li a1, 1 sd a1, -80(s0) bnez a0, .LBB1_22 j .LBB1_18 .LBB1_18: ld a0, -24(s0) lw a1, -44(s0) add a0, a0, a1 lbu a0, 0(a0) ld a1, -32(s0) lw a2, -48(s0) addiw a2, a2, -1 add a1, a1, a2 lbu a1, 0(a1) beq a0, a1, .LBB1_20 j .LBB1_19 .LBB1_19: ld a0, -32(s0) lw a1, -48(s0) addiw a1, a1, -1 add a0, a0, a1 lbu a0, 0(a0) li a2, 0 li a1, 46 sd a2, -88(s0) bne a0, a1, .LBB1_21 j .LBB1_20 .LBB1_20: ld a1, -40(s0) lw a0, -44(s0) lw a2, -56(s0) addiw a2, a2, 1 mulw a0, a0, a2 lw a2, -48(s0) addw a0, a0, a2 slli a0, a0, 2 add a0, a0, a1 lw a0, 4(a0) snez a0, a0 sd a0, -88(s0) j .LBB1_21 .LBB1_21: ld a0, -88(s0) sd a0, -80(s0) j .LBB1_22 .LBB1_22: ld a0, -80(s0) andi a0, a0, 1 ld a2, -40(s0) lw a1, -44(s0) addiw a1, a1, 1 lw a3, -56(s0) addiw a3, a3, 1 mulw a1, a1, a3 lw a3, -48(s0) addw a1, a1, a3 slli a1, a1, 2 add a1, a1, a2 sw a0, 4(a1) j .LBB1_23 .LBB1_23: j .LBB1_24 .LBB1_24: lw a0, -48(s0) addiw a0, a0, 1 sw a0, -48(s0) j .LBB1_9 .LBB1_25: j .LBB1_26 .LBB1_26: lw a0, -44(s0) addiw a0, a0, 1 sw a0, -44(s0) j .LBB1_7 .LBB1_27: ld a1, -40(s0) lw a2, -52(s0) lw a0, -56(s0) addiw a3, a0, 1 mulw a2, a2, a3 addw a0, a0, a2 addiw a0, a0, -1 slli a0, a0, 2 add a0, a0, a1 lw a0, 4(a0) sw a0, -44(s0) ld a0, -40(s0) call free lw a0, -44(s0) snez a0, a0 ld ra, 88(sp) ld s0, 80(sp) addi sp, sp, 96 ret
305
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O1
RISC-V 64 clang 21.1.0
match_recursive: addi sp, sp, -48 sd ra, 40(sp) sd s0, 32(sp) sd s1, 24(sp) sd s2, 16(sp) sd s3, 8(sp) mv s2, a2 mv s1, a0 addi s0, a1, 2 li a0, 42 li a1, 46 j .LBB0_3 .LBB0_1: beqz a3, .LBB0_7 .LBB0_2: addi s1, s1, 1 addi s0, s0, 1 .LBB0_3: lbu a2, -2(s0) beqz a2, .LBB0_8 lbu a3, -1(s0) beq a3, a0, .LBB0_9 lbu a3, 0(s1) beq a2, a1, .LBB0_1 beq a3, a2, .LBB0_2 .LBB0_7: li a0, 0 j .LBB0_17 .LBB0_8: lbu a0, 0(s1) seqz a0, a0 j .LBB0_17 .LBB0_9: lw a0, 0(s2) beqz a0, .LBB0_16 li s3, 46 j .LBB0_13 .LBB0_11: bne a1, a2, .LBB0_17 .LBB0_12: lw a0, 0(s2) addi s1, s1, 1 beqz a0, .LBB0_16 .LBB0_13: mv a0, s1 mv a1, s0 mv a2, s2 call match_recursive bnez a0, .LBB0_17 lbu a2, -2(s0) lbu a1, 0(s1) bne a2, s3, .LBB0_11 bnez a1, .LBB0_12 j .LBB0_17 .LBB0_16: li a0, 0 sw zero, 0(s2) .LBB0_17: ld ra, 40(sp) ld s0, 32(sp) ld s1, 24(sp) ld s2, 16(sp) ld s3, 8(sp) addi sp, sp, 48 ret isMatch: addi sp, sp, -112 sd ra, 104(sp) sd s0, 96(sp) sd s1, 88(sp) sd s2, 80(sp) sd s3, 72(sp) sd s4, 64(sp) sd s5, 56(sp) sd s6, 48(sp) sd s7, 40(sp) sd s8, 32(sp) sd s9, 24(sp) sd s10, 16(sp) sd s11, 8(sp) mv s5, a1 mv s4, a0 lui s0, 524288 addiw s0, s0, -1 call strlen mv s1, a0 mv a0, s5 call strlen mv s3, a0 sext.w s6, a0 addi a0, s1, 1 addiw s7, s3, 1 mulw a0, s7, a0 li a1, 4 call calloc li a1, 1 sw a1, 0(a0) and s11, s3, s0 blez s6, .LBB1_5 addi a1, a0, 4 add a2, s5, s11 li a3, 42 mv a4, s5 j .LBB1_3 .LBB1_2: addi a4, a4, 1 addi a1, a1, 4 beq a4, a2, .LBB1_5 .LBB1_3: lbu a5, 0(a4) bne a5, a3, .LBB1_2 lw a5, -8(a1) sw a5, 0(a1) j .LBB1_2 .LBB1_5: sd s1, 0(sp) sext.w a1, s1 blez a1, .LBB1_20 li s2, 0 li t3, 0 li t6, 0 addi s8, a0, 4 li a3, 1 ld a1, 0(sp) and a6, a1, s0 slli a7, s6, 2 slli t0, s3, 32 li a1, -1 add s11, s11, s5 slli a3, a3, 32 addi a7, a7, 4 slli a1, a1, 32 add t4, t0, a1 add t0, t0, a3 li s9, 42 li t2, 46 mv t5, t0 j .LBB1_8 .LBB1_7: addi t6, t6, 1 add t3, t3, a7 add t4, t4, t0 addw s2, s2, s7 add t5, t5, t0 beq t6, a6, .LBB1_20 .LBB1_8: blez s6, .LBB1_7 add s10, s4, t6 addi a1, t6, 1 mul ra, a1, s7 mv a5, t5 mv a4, s5 mv t1, s2 mv s0, t4 mv s1, s8 j .LBB1_12 .LBB1_10: li a2, 0 .LBB1_11: slli a1, ra, 2 add s0, s0, a3 addiw t1, t1, 1 addi a4, a4, 1 add a1, a1, s1 addi s1, s1, 4 sw a2, 0(a1) add a5, a5, a3 beq a4, s11, .LBB1_7 .LBB1_12: lbu a2, 0(a4) bne a2, s9, .LBB1_18 srai a1, s0, 30 add a1, a1, s8 lw a1, 0(a1) li a2, 1 bnez a1, .LBB1_11 srai a1, a5, 30 add a1, a1, a0 lw a1, 0(a1) bnez a1, .LBB1_11 lbu a1, 0(s10) lbu a2, -1(a4) beq a1, a2, .LBB1_17 bne a2, t2, .LBB1_10 .LBB1_17: add a1, s1, t3 lw a1, 0(a1) snez a2, a1 j .LBB1_11 .LBB1_18: slli a1, t1, 2 add a1, a1, a0 lw a1, 0(a1) beqz a1, .LBB1_10 lbu a1, 0(s10) xor a1, a1, a2 addi a2, a2, -46 seqz a1, a1 seqz a2, a2 or a2, a2, a1 j .LBB1_11 .LBB1_20: ld a1, 0(sp) mul a1, s7, a1 addw a1, a1, s3 slli a1, a1, 2 add a1, a1, a0 lw s0, 0(a1) call free snez a0, s0 ld ra, 104(sp) ld s0, 96(sp) ld s1, 88(sp) ld s2, 80(sp) ld s3, 72(sp) ld s4, 64(sp) ld s5, 56(sp) ld s6, 48(sp) ld s7, 40(sp) ld s8, 32(sp) ld s9, 24(sp) ld s10, 16(sp) ld s11, 8(sp) addi sp, sp, 112 ret
306
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O2
RISC-V 64 clang 21.1.0
match_recursive: addi sp, sp, -48 sd ra, 40(sp) sd s0, 32(sp) sd s1, 24(sp) sd s2, 16(sp) sd s3, 8(sp) mv s2, a2 lbu a2, 0(a1) mv s1, a0 beqz a2, .LBB0_8 addi s0, a1, 2 li a0, 42 li a1, 46 j .LBB0_4 .LBB0_2: bne a4, a2, .LBB0_7 .LBB0_3: addi s1, s1, 1 addi s0, s0, 1 mv a2, a3 beqz a3, .LBB0_8 .LBB0_4: lbu a3, -1(s0) beq a3, a0, .LBB0_10 lbu a4, 0(s1) bne a2, a1, .LBB0_2 bnez a4, .LBB0_3 .LBB0_7: li a0, 0 j .LBB0_9 .LBB0_8: lbu a0, 0(s1) seqz a0, a0 .LBB0_9: ld ra, 40(sp) ld s0, 32(sp) ld s1, 24(sp) ld s2, 16(sp) ld s3, 8(sp) addi sp, sp, 48 ret .LBB0_10: lw a0, 0(s2) beqz a0, .LBB0_17 li s3, 46 j .LBB0_14 .LBB0_12: bne a1, a2, .LBB0_9 .LBB0_13: lw a0, 0(s2) addi s1, s1, 1 beqz a0, .LBB0_17 .LBB0_14: mv a0, s1 mv a1, s0 mv a2, s2 call match_recursive bnez a0, .LBB0_9 lbu a2, -2(s0) lbu a1, 0(s1) bne a2, s3, .LBB0_12 bnez a1, .LBB0_13 j .LBB0_9 .LBB0_17: li a0, 0 sw zero, 0(s2) j .LBB0_9 isMatch: addi sp, sp, -128 sd ra, 120(sp) sd s0, 112(sp) sd s1, 104(sp) sd s2, 96(sp) sd s3, 88(sp) sd s4, 80(sp) sd s5, 72(sp) sd s6, 64(sp) sd s7, 56(sp) sd s8, 48(sp) sd s9, 40(sp) sd s10, 32(sp) sd s11, 24(sp) mv s6, a1 mv s4, a0 lui s0, 524288 addiw s3, s0, -1 call strlen mv s1, a0 mv a0, s6 call strlen mv s0, a0 sext.w s7, a0 addi a0, s1, 1 addi a1, s0, 1 sd a1, 0(sp) mulw a0, a1, a0 li a1, 4 call calloc li a1, 1 sw a1, 0(a0) sd s0, 8(sp) and ra, s0, s3 blez s7, .LBB1_5 addi a1, a0, 4 add a2, s6, ra li a3, 42 mv a4, s6 j .LBB1_3 .LBB1_2: addi a4, a4, 1 addi a1, a1, 4 beq a4, a2, .LBB1_5 .LBB1_3: lbu a5, 0(a4) bne a5, a3, .LBB1_2 lw a5, -8(a1) sw a5, 0(a1) j .LBB1_2 .LBB1_5: sd s1, 16(sp) sext.w a1, s1 blez a1, .LBB1_20 li s2, 0 li s11, 0 addi s9, a0, 4 li t0, 1 ld a1, 16(sp) and a6, a1, s3 slli a7, s7, 2 ld s3, 8(sp) slli s3, s3, 32 li a1, -1 add ra, ra, s6 slli t0, t0, 32 add t3, a7, a0 addi a7, a7, 4 slli a1, a1, 32 addi t3, t3, 8 add t5, s3, a1 add s3, s3, t0 li s10, 42 li t1, 46 mv t6, s3 mv s8, s9 j .LBB1_8 .LBB1_7: add t3, t3, a7 add s8, s8, a7 add t5, t5, s3 add t6, t6, s3 add s2, s2, s3 mv s11, t4 beq t4, a6, .LBB1_20 .LBB1_8: addi t4, s11, 1 blez s7, .LBB1_7 add s11, s11, s4 mv t2, s2 mv a1, t6 mv a2, s6 mv s0, t5 mv a3, s8 mv a4, t3 j .LBB1_12 .LBB1_10: li s5, 0 .LBB1_11: sw s5, 0(a4) addi a4, a4, 4 addi a3, a3, 4 add s0, s0, t0 addi a2, a2, 1 add a1, a1, t0 add t2, t2, t0 beq a2, ra, .LBB1_7 .LBB1_12: lbu s1, 0(a2) bne s1, s10, .LBB1_18 srai a5, s0, 30 add a5, a5, s9 lw a5, 0(a5) li s5, 1 bnez a5, .LBB1_11 srai a5, a1, 30 add a5, a5, a0 lw a5, 0(a5) bnez a5, .LBB1_11 lbu a5, 0(s11) lbu s1, -1(a2) beq a5, s1, .LBB1_17 bne s1, t1, .LBB1_10 .LBB1_17: lw a5, 0(a3) snez s5, a5 j .LBB1_11 .LBB1_18: srai a5, t2, 30 add a5, a5, a0 lw a5, 0(a5) beqz a5, .LBB1_10 lbu a5, 0(s11) xor a5, a5, s1 addi s1, s1, -46 seqz a5, a5 seqz s1, s1 or s5, a5, s1 j .LBB1_11 .LBB1_20: ld a1, 16(sp) ld a2, 0(sp) mul a1, a2, a1 ld a2, 8(sp) addw a1, a1, a2 slli a1, a1, 2 add a1, a1, a0 lw s0, 0(a1) call free snez a0, s0 ld ra, 120(sp) ld s0, 112(sp) ld s1, 104(sp) ld s2, 96(sp) ld s3, 88(sp) ld s4, 80(sp) ld s5, 72(sp) ld s6, 64(sp) ld s7, 56(sp) ld s8, 48(sp) ld s9, 40(sp) ld s10, 32(sp) ld s11, 24(sp) addi sp, sp, 128 ret
307
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O3
RISC-V 64 clang 21.1.0
match_recursive: addi sp, sp, -48 sd ra, 40(sp) sd s0, 32(sp) sd s1, 24(sp) sd s2, 16(sp) sd s3, 8(sp) mv s2, a2 lbu a2, 0(a1) mv s1, a0 beqz a2, .LBB0_8 addi s0, a1, 2 li a0, 42 li a1, 46 j .LBB0_4 .LBB0_2: bne a4, a2, .LBB0_7 .LBB0_3: addi s1, s1, 1 addi s0, s0, 1 mv a2, a3 beqz a3, .LBB0_8 .LBB0_4: lbu a3, -1(s0) beq a3, a0, .LBB0_10 lbu a4, 0(s1) bne a2, a1, .LBB0_2 bnez a4, .LBB0_3 .LBB0_7: li a0, 0 j .LBB0_9 .LBB0_8: lbu a0, 0(s1) seqz a0, a0 .LBB0_9: ld ra, 40(sp) ld s0, 32(sp) ld s1, 24(sp) ld s2, 16(sp) ld s3, 8(sp) addi sp, sp, 48 ret .LBB0_10: lw a0, 0(s2) beqz a0, .LBB0_17 li s3, 46 j .LBB0_14 .LBB0_12: bne a1, a2, .LBB0_9 .LBB0_13: lw a0, 0(s2) addi s1, s1, 1 beqz a0, .LBB0_17 .LBB0_14: mv a0, s1 mv a1, s0 mv a2, s2 call match_recursive bnez a0, .LBB0_9 lbu a2, -2(s0) lbu a1, 0(s1) bne a2, s3, .LBB0_12 bnez a1, .LBB0_13 j .LBB0_9 .LBB0_17: li a0, 0 sw zero, 0(s2) j .LBB0_9 isMatch: addi sp, sp, -112 sd ra, 104(sp) sd s0, 96(sp) sd s1, 88(sp) sd s2, 80(sp) sd s3, 72(sp) sd s4, 64(sp) sd s5, 56(sp) sd s6, 48(sp) sd s7, 40(sp) sd s8, 32(sp) sd s9, 24(sp) sd s10, 16(sp) sd s11, 8(sp) mv s6, a1 mv s5, a0 call strlen mv s2, a0 mv a0, s6 call strlen mv s3, a0 sext.w s0, a0 addi a0, s2, 1 addi a1, s3, 1 sd a1, 0(sp) mulw a0, a1, a0 li a1, 4 call calloc li a1, 1 sw a1, 0(a0) blez s0, .LBB1_19 lui a1, 524288 addi a2, a0, 4 addiw a1, a1, -1 and s11, s3, a1 add s11, s11, s6 li a3, 42 mv a4, s6 j .LBB1_3 .LBB1_2: addi a4, a4, 1 addi a2, a2, 4 beq a4, s11, .LBB1_5 .LBB1_3: lbu a5, 0(a4) bne a5, a3, .LBB1_2 lw a5, -8(a2) sw a5, 0(a2) j .LBB1_2 .LBB1_5: sext.w a2, s2 blez a2, .LBB1_19 li t0, 0 li t1, 0 li ra, 1 addi s8, a0, 4 and a6, s2, a1 ld s4, 0(sp) slli s4, s4, 32 li a1, -1 li s9, 42 slli ra, ra, 32 srli a7, s4, 30 slli a1, a1, 33 add t4, s8, a7 add t5, s4, a1 li t2, 46 mv t6, s4 mv s7, s8 j .LBB1_8 .LBB1_7: add t4, t4, a7 add s7, s7, a7 add t5, t5, s4 add t6, t6, s4 add t0, t0, s4 beq t1, a6, .LBB1_19 .LBB1_8: add s10, s5, t1 addi t1, t1, 1 mv a2, s6 mv t3, t0 mv s1, t6 mv a4, t5 mv a3, s7 mv a1, t4 j .LBB1_11 .LBB1_9: li s0, 0 .LBB1_10: sw s0, 0(a1) addi a1, a1, 4 addi a3, a3, 4 add a4, a4, ra add s1, s1, ra addi a2, a2, 1 add t3, t3, ra beq a2, s11, .LBB1_7 .LBB1_11: lbu s0, 0(a2) bne s0, s9, .LBB1_17 srai a5, a4, 30 add a5, a5, s8 lw a5, 0(a5) li s0, 1 bnez a5, .LBB1_10 srai a5, s1, 30 add a5, a5, a0 lw a5, 0(a5) bnez a5, .LBB1_10 lbu a5, 0(s10) lbu s0, -1(a2) beq a5, s0, .LBB1_16 bne s0, t2, .LBB1_9 .LBB1_16: lw a5, 0(a3) snez s0, a5 j .LBB1_10 .LBB1_17: srai a5, t3, 30 add a5, a5, a0 lw a5, 0(a5) beqz a5, .LBB1_9 lbu a5, 0(s10) xor a5, a5, s0 addi s0, s0, -46 seqz a5, a5 seqz s0, s0 or s0, s0, a5 j .LBB1_10 .LBB1_19: ld a1, 0(sp) mul a1, a1, s2 addw a1, a1, s3 slli a1, a1, 2 add a1, a1, a0 lw s0, 0(a1) call free snez a0, s0 ld ra, 104(sp) ld s0, 96(sp) ld s1, 88(sp) ld s2, 80(sp) ld s3, 72(sp) ld s4, 64(sp) ld s5, 56(sp) ld s6, 48(sp) ld s7, 40(sp) ld s8, 32(sp) ld s9, 24(sp) ld s10, 16(sp) ld s11, 8(sp) addi sp, sp, 112 ret
308
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O0
RISC-V 64 gcc 15.2.0
match_recursive: addi sp,sp,-48 sd ra,40(sp) sd s0,32(sp) addi s0,sp,48 sd a0,-24(s0) sd a1,-32(s0) sd a2,-40(s0) ld a5,-32(s0) lbu a5,0(a5) bne a5,zero,.L2 ld a5,-24(s0) lbu a5,0(a5) sext.w a5,a5 seqz a5,a5 andi a5,a5,0xff j .L3 .L2: ld a5,-32(s0) addi a5,a5,1 lbu a5,0(a5) mv a4,a5 li a5,42 bne a4,a5,.L4 j .L5 .L10: ld a5,-32(s0) addi a5,a5,2 ld a2,-40(s0) mv a1,a5 ld a0,-24(s0) call match_recursive mv a5,a0 beq a5,zero,.L6 li a5,1 j .L3 .L6: ld a5,-32(s0) lbu a5,0(a5) mv a4,a5 li a5,46 bne a4,a5,.L7 ld a5,-24(s0) lbu a5,0(a5) beq a5,zero,.L8 .L7: ld a5,-32(s0) lbu a5,0(a5) mv a4,a5 li a5,46 beq a4,a5,.L9 ld a5,-24(s0) lbu a4,0(a5) ld a5,-32(s0) lbu a5,0(a5) beq a4,a5,.L9 .L8: li a5,0 j .L3 .L9: ld a5,-24(s0) addi a5,a5,1 sd a5,-24(s0) .L5: ld a5,-40(s0) lw a5,0(a5) bne a5,zero,.L10 ld a5,-40(s0) sw zero,0(a5) li a5,0 j .L3 .L4: ld a5,-32(s0) lbu a5,0(a5) mv a4,a5 li a5,46 bne a4,a5,.L11 ld a5,-24(s0) lbu a5,0(a5) beq a5,zero,.L12 .L11: ld a5,-32(s0) lbu a5,0(a5) mv a4,a5 li a5,46 beq a4,a5,.L13 ld a5,-24(s0) lbu a4,0(a5) ld a5,-32(s0) lbu a5,0(a5) beq a4,a5,.L13 .L12: li a5,0 j .L3 .L13: ld a5,-24(s0) addi a4,a5,1 ld a5,-32(s0) addi a5,a5,1 ld a2,-40(s0) mv a1,a5 mv a0,a4 call match_recursive mv a5,a0 .L3: mv a0,a5 ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra isMatch: addi sp,sp,-64 sd ra,56(sp) sd s0,48(sp) addi s0,sp,64 sd a0,-56(s0) sd a1,-64(s0) ld a0,-56(s0) call strlen mv a5,a0 sw a5,-28(s0) ld a0,-64(s0) call strlen mv a5,a0 sw a5,-32(s0) lw a5,-28(s0) addiw a5,a5,1 sext.w a4,a5 lw a5,-32(s0) addiw a5,a5,1 sext.w a5,a5 mulw a5,a4,a5 sext.w a5,a5 li a1,4 mv a0,a5 call calloc mv a5,a0 sd a5,-40(s0) ld a5,-40(s0) li a4,1 sw a4,0(a5) sw zero,-24(s0) j .L15 .L17: lw a5,-24(s0) ld a4,-64(s0) add a5,a4,a5 lbu a5,0(a5) mv a4,a5 li a5,42 bne a4,a5,.L16 lw a5,-24(s0) addiw a5,a5,-2 sext.w a5,a5 addi a5,a5,1 slli a5,a5,2 ld a4,-40(s0) add a4,a4,a5 lw a5,-24(s0) addi a5,a5,1 slli a5,a5,2 ld a3,-40(s0) add a5,a3,a5 lw a4,0(a4) sw a4,0(a5) .L16: lw a5,-24(s0) addiw a5,a5,1 sw a5,-24(s0) .L15: lw a5,-24(s0) mv a4,a5 lw a5,-32(s0) sext.w a4,a4 sext.w a5,a5 blt a4,a5,.L17 sw zero,-20(s0) j .L18 .L30: sw zero,-24(s0) j .L19 .L29: lw a5,-24(s0) ld a4,-64(s0) add a5,a4,a5 lbu a5,0(a5) mv a4,a5 li a5,42 beq a4,a5,.L20 lw a5,-32(s0) addiw a5,a5,1 sext.w a5,a5 lw a4,-20(s0) mulw a5,a4,a5 sext.w a4,a5 lw a5,-24(s0) addiw a5,a5,-1 sext.w a5,a5 addw a5,a4,a5 sext.w a5,a5 addi a5,a5,1 slli a5,a5,2 ld a4,-40(s0) add a5,a4,a5 lw a5,0(a5) beq a5,zero,.L21 lw a5,-20(s0) ld a4,-56(s0) add a5,a4,a5 lbu a3,0(a5) lw a5,-24(s0) ld a4,-64(s0) add a5,a4,a5 lbu a5,0(a5) mv a4,a3 beq a4,a5,.L22 lw a5,-24(s0) ld a4,-64(s0) add a5,a4,a5 lbu a5,0(a5) mv a4,a5 li a5,46 bne a4,a5,.L21 .L22: li a5,1 j .L23 .L21: li a5,0 .L23: lw a4,-20(s0) addiw a4,a4,1 sext.w a3,a4 lw a4,-32(s0) addiw a4,a4,1 sext.w a4,a4 mulw a4,a3,a4 sext.w a4,a4 lw a3,-24(s0) addw a4,a3,a4 sext.w a4,a4 addi a4,a4,1 slli a4,a4,2 ld a3,-40(s0) add a4,a3,a4 sw a5,0(a4) j .L24 .L20: lw a5,-20(s0) addiw a5,a5,1 sext.w a4,a5 lw a5,-32(s0) addiw a5,a5,1 sext.w a5,a5 mulw a5,a4,a5 sext.w a4,a5 lw a5,-24(s0) addiw a5,a5,-2 sext.w a5,a5 addw a5,a4,a5 sext.w a5,a5 addi a5,a5,1 slli a5,a5,2 ld a4,-40(s0) add a5,a4,a5 lw a5,0(a5) bne a5,zero,.L25 lw a5,-20(s0) addiw a5,a5,1 sext.w a4,a5 lw a5,-32(s0) addiw a5,a5,1 sext.w a5,a5 mulw a5,a4,a5 sext.w a4,a5 lw a5,-24(s0) addiw a5,a5,-1 sext.w a5,a5 addw a5,a4,a5 sext.w a5,a5 addi a5,a5,1 slli a5,a5,2 ld a4,-40(s0) add a5,a4,a5 lw a5,0(a5) bne a5,zero,.L25 lw a5,-20(s0) ld a4,-56(s0) add a5,a4,a5 lbu a3,0(a5) lw a5,-24(s0) addi a5,a5,-1 ld a4,-64(s0) add a5,a4,a5 lbu a5,0(a5) mv a4,a3 beq a4,a5,.L26 lw a5,-24(s0) addi a5,a5,-1 ld a4,-64(s0) add a5,a4,a5 lbu a5,0(a5) mv a4,a5 li a5,46 bne a4,a5,.L27 .L26: lw a5,-32(s0) addiw a5,a5,1 sext.w a5,a5 lw a4,-20(s0) mulw a5,a4,a5 sext.w a5,a5 lw a4,-24(s0) addw a5,a4,a5 sext.w a5,a5 addi a5,a5,1 slli a5,a5,2 ld a4,-40(s0) add a5,a4,a5 lw a5,0(a5) beq a5,zero,.L27 .L25: li a5,1 j .L28 .L27: li a5,0 .L28: lw a4,-20(s0) addiw a4,a4,1 sext.w a3,a4 lw a4,-32(s0) addiw a4,a4,1 sext.w a4,a4 mulw a4,a3,a4 sext.w a4,a4 lw a3,-24(s0) addw a4,a3,a4 sext.w a4,a4 addi a4,a4,1 slli a4,a4,2 ld a3,-40(s0) add a4,a3,a4 sw a5,0(a4) .L24: lw a5,-24(s0) addiw a5,a5,1 sw a5,-24(s0) .L19: lw a5,-24(s0) mv a4,a5 lw a5,-32(s0) sext.w a4,a4 sext.w a5,a5 blt a4,a5,.L29 lw a5,-20(s0) addiw a5,a5,1 sw a5,-20(s0) .L18: lw a5,-20(s0) mv a4,a5 lw a5,-28(s0) sext.w a4,a4 sext.w a5,a5 blt a4,a5,.L30 lw a5,-32(s0) addiw a5,a5,1 sext.w a5,a5 lw a4,-28(s0) mulw a5,a4,a5 sext.w a4,a5 lw a5,-32(s0) addiw a5,a5,-1 sext.w a5,a5 addw a5,a4,a5 sext.w a5,a5 addi a5,a5,1 slli a5,a5,2 ld a4,-40(s0) add a5,a4,a5 lw a5,0(a5) sw a5,-20(s0) ld a0,-40(s0) call free lw a5,-20(s0) sext.w a5,a5 snez a5,a5 andi a5,a5,0xff mv a0,a5 ld ra,56(sp) ld s0,48(sp) addi sp,sp,64 jr ra
309
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O1
RISC-V 64 gcc 15.2.0
match_recursive: addi sp,sp,-48 sd ra,40(sp) sd s0,32(sp) mv s0,a0 lbu a5,0(a1) beq a5,zero,.L18 sd s1,24(sp) sd s2,16(sp) mv s1,a1 mv s2,a2 lbu a3,1(a1) li a4,42 beq a3,a4,.L19 li a4,46 beq a5,a4,.L20 lbu a4,0(a0) li a0,0 bne a4,a5,.L17 .L10: mv a2,s2 addi a1,s1,1 addi a0,s0,1 call match_recursive ld s1,24(sp) ld s2,16(sp) j .L3 .L18: lbu a0,0(a0) seqz a0,a0 .L3: ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra .L19: lw a5,0(a2) beq a5,zero,.L5 sd s3,8(sp) addi a5,a1,2 mv s3,a5 j .L8 .L23: lbu a5,0(s0) beq a5,zero,.L21 .L7: addi s0,s0,1 lw a5,0(s2) beq a5,zero,.L22 .L8: mv a2,s2 mv a1,s3 mv a0,s0 call match_recursive bne a0,zero,.L15 lbu a5,0(s1) li a4,46 beq a5,a4,.L23 lbu a4,0(s0) beq a4,a5,.L7 ld s1,24(sp) ld s2,16(sp) ld s3,8(sp) j .L3 .L21: ld s1,24(sp) ld s2,16(sp) ld s3,8(sp) j .L3 .L22: ld s3,8(sp) .L5: sw zero,0(s2) li a0,0 ld s1,24(sp) ld s2,16(sp) j .L3 .L20: lbu a5,0(a0) li a0,0 bne a5,zero,.L10 ld s1,24(sp) ld s2,16(sp) j .L3 .L15: ld s1,24(sp) ld s2,16(sp) ld s3,8(sp) j .L3 .L17: ld s1,24(sp) ld s2,16(sp) j .L3 isMatch: addi sp,sp,-80 sd ra,72(sp) sd s0,64(sp) sd s1,56(sp) sd s2,48(sp) sd s3,40(sp) sd s4,32(sp) sd s5,24(sp) sd s6,16(sp) sd s7,8(sp) mv s7,a0 mv s2,a1 call strlen mv s6,a0 sext.w s5,a0 mv a0,s2 call strlen mv s4,a0 sext.w s1,a0 addiw s0,a0,1 addiw s3,s5,1 mulw s3,s3,s0 li a1,4 mv a0,s3 call calloc li a5,1 sw a5,0(a0) ble s1,zero,.L25 mv a4,s2 addi a5,a0,4 addiw a2,s4,-1 slli a3,a2,32 srli a2,a3,30 addi a3,a0,8 add a2,a2,a3 li a1,42 j .L27 .L26: addi a4,a4,1 addi a5,a5,4 beq a5,a2,.L25 .L27: lbu a3,0(a4) bne a3,a1,.L26 lw a3,-8(a5) sw a3,0(a5) j .L26 .L25: ble s5,zero,.L28 addw a6,s0,s4 sext.w t2,s2 subw a7,s0,s2 addiw t0,s4,1 negw s4,s2 mv t6,s4 mv t3,s7 addi s5,s7,1 addiw a5,s6,-1 slli a5,a5,32 srli a5,a5,32 add s5,s5,a5 li t1,42 li t5,1 j .L29 .L31: addw a4,a4,a7 slli a4,a4,2 addi a4,a4,4 add a4,a0,a4 sw a3,0(a4) .L32: addi a5,a5,1 addiw a1,a1,1 beq a1,a6,.L37 .L35: lbu a2,0(a5) beq a2,t1,.L30 sext.w a4,a5 addw a3,t4,a5 slli a3,a3,2 addi a3,a3,4 add a3,a0,a3 lw a3,0(a3) beq a3,zero,.L31 lbu a3,0(t3) sub a3,a3,a2 seqz a3,a3 addi a2,a2,-46 seqz a2,a2 or a3,a3,a2 j .L31 .L30: addiw a4,a1,-2 slli a4,a4,2 addi a4,a4,4 add a4,a0,a4 lw a4,0(a4) mv a3,t5 bne a4,zero,.L33 addiw a4,a1,-1 slli a4,a4,2 addi a4,a4,4 add a4,a0,a4 lw a3,0(a4) bne a3,zero,.L39 lbu a4,-1(a5) lbu a2,0(t3) beq a2,a4,.L40 addi a4,a4,-46 bne a4,zero,.L33 .L40: addw a4,a5,t6 slli a4,a4,2 addi a4,a4,4 add a4,a0,a4 lw a3,0(a4) snez a3,a3 j .L33 .L39: mv a3,t5 .L33: slli a4,a1,2 addi a4,a4,4 add a4,a0,a4 sw a3,0(a4) j .L32 .L37: addw a6,a6,s0 addw a7,s0,a7 addw t6,t6,t0 addi t3,t3,1 addw s4,s0,s4 beq t3,s5,.L28 .L29: ble s1,zero,.L37 addw a1,a7,t2 mv a5,s2 addiw t4,s4,-1 j .L35 .L28: subw s3,s3,s0 addiw a5,s1,-1 addw a5,a5,s3 slli a5,a5,2 addi a5,a5,4 add a5,a0,a5 lw s0,0(a5) call free snez a0,s0 ld ra,72(sp) ld s0,64(sp) ld s1,56(sp) ld s2,48(sp) ld s3,40(sp) ld s4,32(sp) ld s5,24(sp) ld s6,16(sp) ld s7,8(sp) addi sp,sp,80 jr ra
310
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O2
RISC-V 64 gcc 15.2.0
match_recursive: lbu a5,0(a1) mv a4,a0 mv a3,a1 li a6,42 li a7,46 bne a5,zero,.L2 j .L14 .L12: bne a0,a1,.L27 .L13: addi a4,a4,1 addi a3,a3,1 beq a5,zero,.L14 .L2: mv a1,a5 lbu a5,1(a3) beq a5,a6,.L31 lbu a0,0(a4) bne a1,a7,.L12 bne a0,zero,.L13 .L27: li a0,0 ret .L14: lbu a0,0(a4) seqz a0,a0 ret .L31: lw a5,0(a2) addi sp,sp,-48 sd s0,32(sp) sd ra,40(sp) addi s0,a3,2 bne a5,zero,.L10 j .L11 .L6: bne a5,a1,.L8 .L7: lw a5,0(a2) beq a5,zero,.L11 .L10: mv a1,s0 mv a0,a4 sd a3,24(sp) sd a2,16(sp) sd a4,8(sp) call match_recursive bne a0,zero,.L3 ld a3,24(sp) ld a4,8(sp) li a6,46 lbu a5,0(a3) lbu a1,0(a4) ld a2,16(sp) addi a4,a4,1 bne a5,a6,.L6 bne a1,zero,.L7 .L8: li a0,0 .L3: ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra .L11: sw zero,0(a2) j .L8 isMatch: addi sp,sp,-80 sd ra,72(sp) sd s0,64(sp) sd s1,56(sp) sd s2,48(sp) sd s3,40(sp) mv s2,a1 sd s4,32(sp) sd s5,24(sp) sd s6,16(sp) sd s7,8(sp) mv s7,a0 call strlen mv s6,a0 mv a0,s2 call strlen sext.w s5,s6 addiw s0,a0,1 addiw s3,s5,1 mulw s3,s3,s0 mv s4,a0 li a1,4 sext.w s1,a0 mv a0,s3 call calloc li a5,1 sw a5,0(a0) ble s1,zero,.L39 addiw a3,s4,-1 slli a5,a3,32 srli a3,a5,30 addi a5,a0,8 add a3,a3,a5 mv a4,s2 addi a5,a0,4 li a1,42 .L38: lbu a2,0(a4) addi a4,a4,1 bne a2,a1,.L37 lw a2,-8(a5) sw a2,0(a5) .L37: addi a5,a5,4 bne a5,a3,.L38 .L39: ble s5,zero,.L34 addiw a5,s6,-1 slli a5,a5,32 srli a5,a5,32 addi t5,s7,1 mv a7,s7 add t5,t5,a5 ble s1,zero,.L36 negw t4,s2 mv t3,t4 addw a1,s1,s0 addiw t0,s1,1 addw t6,s2,s0 li a6,42 .L46: addw a5,t6,t3 mv a3,s2 addiw t1,t3,-1 j .L45 .L62: addw a4,t1,a3 slli a4,a4,2 add a4,a0,a4 lw a4,4(a4) beq a4,zero,.L41 lbu a4,0(a7) addi t2,a2,-46 seqz t2,t2 sub a4,a4,a2 seqz a4,a4 or a4,a4,t2 .L41: slli a2,a5,2 add a2,a0,a2 sw a4,4(a2) addiw a5,a5,1 addi a3,a3,1 beq a1,a5,.L61 .L45: lbu a2,0(a3) bne a2,a6,.L62 addiw a4,a5,-2 slli a4,a4,2 add a4,a0,a4 lw a4,4(a4) li a2,1 bne a4,zero,.L43 addiw a4,a5,-1 slli a4,a4,2 add a4,a0,a4 lw a2,4(a4) bne a2,zero,.L48 lbu t2,0(a7) lbu a4,-1(a3) beq t2,a4,.L49 addi a4,a4,-46 beq a4,zero,.L49 .L43: slli a4,a5,2 add a4,a0,a4 sw a2,4(a4) addiw a5,a5,1 addi a3,a3,1 bne a1,a5,.L45 .L61: addi a7,a7,1 addw a1,a1,s0 addw t4,t4,t0 addw t3,t3,s0 bne a7,t5,.L46 .L34: subw s3,s3,s0 addiw a5,s1,-1 addw a5,a5,s3 slli a5,a5,2 add a5,a0,a5 lw s0,4(a5) call free ld ra,72(sp) snez a0,s0 ld s0,64(sp) ld s1,56(sp) ld s2,48(sp) ld s3,40(sp) ld s4,32(sp) ld s5,24(sp) ld s6,16(sp) ld s7,8(sp) addi sp,sp,80 jr ra .L36: addi a5,a7,1 addi a7,a7,2 beq a5,t5,.L34 bne a7,t5,.L36 j .L34 .L48: li a2,1 j .L43 .L49: addw a4,a3,t4 slli a4,a4,2 add a4,a0,a4 lw a2,4(a4) snez a2,a2 j .L43
311
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
riscv64
-O3
RISC-V 64 gcc 15.2.0
match_recursive: lbu a5,0(a1) mv a4,a0 mv a3,a1 li a6,42 li a7,46 bne a5,zero,.L2 j .L14 .L12: bne a0,a1,.L27 .L13: addi a4,a4,1 addi a3,a3,1 beq a5,zero,.L14 .L2: mv a1,a5 lbu a5,1(a3) beq a5,a6,.L31 lbu a0,0(a4) bne a1,a7,.L12 bne a0,zero,.L13 .L27: li a0,0 ret .L14: lbu a0,0(a4) seqz a0,a0 ret .L31: lw a5,0(a2) addi sp,sp,-48 sd s0,32(sp) sd ra,40(sp) addi s0,a3,2 bne a5,zero,.L10 j .L11 .L6: bne a5,a1,.L8 .L7: lw a5,0(a2) beq a5,zero,.L11 .L10: mv a1,s0 mv a0,a4 sd a3,24(sp) sd a2,16(sp) sd a4,8(sp) call match_recursive bne a0,zero,.L3 ld a3,24(sp) ld a4,8(sp) li a6,46 lbu a5,0(a3) lbu a1,0(a4) ld a2,16(sp) addi a4,a4,1 bne a5,a6,.L6 bne a1,zero,.L7 .L8: li a0,0 .L3: ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra .L11: sw zero,0(a2) j .L8 isMatch: addi sp,sp,-64 sd ra,56(sp) sd s0,48(sp) sd s1,40(sp) sd s2,32(sp) mv s1,a1 sd s3,24(sp) sd s4,16(sp) sd s5,8(sp) sd s6,0(sp) mv s6,a0 call strlen mv s5,a0 mv a0,s1 call strlen sext.w s4,s5 addiw s0,a0,1 addiw s2,s4,1 mulw s2,s2,s0 sext.w s3,a0 li a1,4 mv a0,s2 call calloc li a5,1 sw a5,0(a0) addiw t6,s3,-1 ble s3,zero,.L34 slli a5,t6,32 srli a2,a5,30 addi a5,a0,8 add a2,a2,a5 mv a4,s1 addi a5,a0,4 li a1,42 .L37: lbu a3,0(a4) addi a4,a4,1 bne a3,a1,.L36 lw a3,-8(a5) sw a3,0(a5) .L36: addi a5,a5,4 bne a2,a5,.L37 ble s4,zero,.L34 addiw a5,s5,-1 slli a5,a5,32 srli a5,a5,32 addi t5,s6,1 negw t4,s1 mv a7,s6 add t5,t5,a5 addw a1,s3,s0 mv t3,t4 addw t0,s1,s0 li a6,42 .L35: addw a5,t0,t3 mv a3,s1 addiw t1,t3,-1 j .L44 .L59: addw a4,t1,a3 slli a4,a4,2 add a4,a0,a4 lw a4,4(a4) beq a4,zero,.L40 lbu a4,0(a7) addi t2,a2,-46 seqz t2,t2 sub a4,a4,a2 seqz a4,a4 or a4,a4,t2 .L40: slli a2,a5,2 add a2,a0,a2 sw a4,4(a2) addiw a5,a5,1 addi a3,a3,1 beq a1,a5,.L58 .L44: lbu a2,0(a3) bne a2,a6,.L59 addiw a4,a5,-2 slli a4,a4,2 add a4,a0,a4 lw a4,4(a4) li a2,1 bne a4,zero,.L42 addiw a4,a5,-1 slli a4,a4,2 add a4,a0,a4 lw a2,4(a4) bne a2,zero,.L46 lbu t2,0(a7) lbu a4,-1(a3) beq t2,a4,.L47 addi a4,a4,-46 beq a4,zero,.L47 .L42: slli a4,a5,2 add a4,a0,a4 sw a2,4(a4) addiw a5,a5,1 addi a3,a3,1 bne a1,a5,.L44 .L58: addi a7,a7,1 addw a1,s0,a1 addw t4,s0,t4 addw t3,t3,s0 bne t5,a7,.L35 .L34: subw s2,s2,s0 addw a5,t6,s2 slli a5,a5,2 add a5,a0,a5 lw s0,4(a5) call free ld ra,56(sp) snez a0,s0 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 .L46: li a2,1 j .L42 .L47: addw a4,a3,t4 slli a4,a4,2 add a4,a0,a4 lw a2,4(a4) snez a2,a2 j .L42
312
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O0
x86-64 clang 21.1.0
match_recursive: push rbp mov rbp, rsp sub rsp, 32 mov qword ptr [rbp - 16], rdi mov qword ptr [rbp - 24], rsi mov qword ptr [rbp - 32], rdx mov rax, qword ptr [rbp - 24] movsx eax, byte ptr [rax] cmp eax, 0 jne .LBB0_2 mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] cmp eax, 0 sete al and al, 1 mov byte ptr [rbp - 1], al jmp .LBB0_20 .LBB0_2: mov rax, qword ptr [rbp - 24] movsx eax, byte ptr [rax + 1] cmp eax, 42 jne .LBB0_14 jmp .LBB0_4 .LBB0_4: mov rax, qword ptr [rbp - 32] cmp dword ptr [rax], 0 je .LBB0_13 mov rdi, qword ptr [rbp - 16] mov rsi, qword ptr [rbp - 24] add rsi, 2 mov rdx, qword ptr [rbp - 32] call match_recursive test al, 1 jne .LBB0_6 jmp .LBB0_7 .LBB0_6: mov byte ptr [rbp - 1], 1 jmp .LBB0_20 .LBB0_7: mov rax, qword ptr [rbp - 24] movsx eax, byte ptr [rax] cmp eax, 46 jne .LBB0_9 mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] cmp eax, 0 je .LBB0_11 .LBB0_9: mov rax, qword ptr [rbp - 24] movsx eax, byte ptr [rax] cmp eax, 46 je .LBB0_12 mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] mov rcx, qword ptr [rbp - 24] movsx ecx, byte ptr [rcx] cmp eax, ecx je .LBB0_12 .LBB0_11: mov byte ptr [rbp - 1], 0 jmp .LBB0_20 .LBB0_12: mov rax, qword ptr [rbp - 16] add rax, 1 mov qword ptr [rbp - 16], rax jmp .LBB0_4 .LBB0_13: mov rax, qword ptr [rbp - 32] mov dword ptr [rax], 0 mov byte ptr [rbp - 1], 0 jmp .LBB0_20 .LBB0_14: mov rax, qword ptr [rbp - 24] movsx eax, byte ptr [rax] cmp eax, 46 jne .LBB0_16 mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] cmp eax, 0 je .LBB0_18 .LBB0_16: mov rax, qword ptr [rbp - 24] movsx eax, byte ptr [rax] cmp eax, 46 je .LBB0_19 mov rax, qword ptr [rbp - 16] movsx eax, byte ptr [rax] mov rcx, qword ptr [rbp - 24] movsx ecx, byte ptr [rcx] cmp eax, ecx je .LBB0_19 .LBB0_18: mov byte ptr [rbp - 1], 0 jmp .LBB0_20 .LBB0_19: mov rdi, qword ptr [rbp - 16] add rdi, 1 mov rsi, qword ptr [rbp - 24] add rsi, 1 mov rdx, qword ptr [rbp - 32] call match_recursive and al, 1 mov byte ptr [rbp - 1], al .LBB0_20: mov al, byte ptr [rbp - 1] and al, 1 add rsp, 32 pop rbp ret isMatch: push rbp mov rbp, rsp sub rsp, 48 mov qword ptr [rbp - 8], rdi mov qword ptr [rbp - 16], rsi mov rdi, qword ptr [rbp - 8] call strlen@PLT mov dword ptr [rbp - 36], eax mov rdi, qword ptr [rbp - 16] call strlen@PLT mov dword ptr [rbp - 40], eax mov eax, dword ptr [rbp - 36] add eax, 1 mov ecx, dword ptr [rbp - 40] add ecx, 1 imul eax, ecx movsxd rdi, eax mov esi, 4 call calloc@PLT mov qword ptr [rbp - 24], rax mov rax, qword ptr [rbp - 24] mov dword ptr [rax], 1 mov dword ptr [rbp - 32], 0 .LBB1_1: mov eax, dword ptr [rbp - 32] cmp eax, dword ptr [rbp - 40] jge .LBB1_6 mov rax, qword ptr [rbp - 16] movsxd rcx, dword ptr [rbp - 32] movsx eax, byte ptr [rax + rcx] cmp eax, 42 jne .LBB1_4 mov rax, qword ptr [rbp - 24] mov ecx, dword ptr [rbp - 40] add ecx, 1 imul ecx, ecx, 0 mov edx, dword ptr [rbp - 32] sub edx, 2 add ecx, edx add ecx, 1 movsxd rcx, ecx mov edx, dword ptr [rax + 4*rcx] mov rax, qword ptr [rbp - 24] mov ecx, dword ptr [rbp - 40] add ecx, 1 imul ecx, ecx, 0 add ecx, dword ptr [rbp - 32] add ecx, 1 movsxd rcx, ecx mov dword ptr [rax + 4*rcx], edx .LBB1_4: jmp .LBB1_5 .LBB1_5: mov eax, dword ptr [rbp - 32] add eax, 1 mov dword ptr [rbp - 32], eax jmp .LBB1_1 .LBB1_6: mov dword ptr [rbp - 28], 0 .LBB1_7: mov eax, dword ptr [rbp - 28] cmp eax, dword ptr [rbp - 36] jge .LBB1_27 mov dword ptr [rbp - 32], 0 .LBB1_9: mov eax, dword ptr [rbp - 32] cmp eax, dword ptr [rbp - 40] jge .LBB1_25 mov rax, qword ptr [rbp - 16] movsxd rcx, dword ptr [rbp - 32] movsx eax, byte ptr [rax + rcx] cmp eax, 42 je .LBB1_16 mov rcx, qword ptr [rbp - 24] mov eax, dword ptr [rbp - 28] sub eax, 1 add eax, 1 mov edx, dword ptr [rbp - 40] add edx, 1 imul eax, edx mov edx, dword ptr [rbp - 32] sub edx, 1 add eax, edx add eax, 1 movsxd rdx, eax xor eax, eax cmp dword ptr [rcx + 4*rdx], 0 mov byte ptr [rbp - 41], al je .LBB1_15 mov rax, qword ptr [rbp - 8] movsxd rcx, dword ptr [rbp - 28] movsx ecx, byte ptr [rax + rcx] mov rax, qword ptr [rbp - 16] movsxd rdx, dword ptr [rbp - 32] movsx edx, byte ptr [rax + rdx] mov al, 1 cmp ecx, edx mov byte ptr [rbp - 42], al je .LBB1_14 mov rax, qword ptr [rbp - 16] movsxd rcx, dword ptr [rbp - 32] movsx eax, byte ptr [rax + rcx] cmp eax, 46 sete al mov byte ptr [rbp - 42], al .LBB1_14: mov al, byte ptr [rbp - 42] mov byte ptr [rbp - 41], al .LBB1_15: mov al, byte ptr [rbp - 41] and al, 1 movzx edx, al mov rax, qword ptr [rbp - 24] mov ecx, dword ptr [rbp - 28] add ecx, 1 mov esi, dword ptr [rbp - 40] add esi, 1 imul ecx, esi add ecx, dword ptr [rbp - 32] add ecx, 1 movsxd rcx, ecx mov dword ptr [rax + 4*rcx], edx jmp .LBB1_23 .LBB1_16: mov rcx, qword ptr [rbp - 24] mov eax, dword ptr [rbp - 28] add eax, 1 mov edx, dword ptr [rbp - 40] add edx, 1 imul eax, edx mov edx, dword ptr [rbp - 32] sub edx, 2 add eax, edx add eax, 1 movsxd rdx, eax mov al, 1 cmp dword ptr [rcx + 4*rdx], 0 mov byte ptr [rbp - 43], al jne .LBB1_22 mov rcx, qword ptr [rbp - 24] mov eax, dword ptr [rbp - 28] add eax, 1 mov edx, dword ptr [rbp - 40] add edx, 1 imul eax, edx mov edx, dword ptr [rbp - 32] sub edx, 1 add eax, edx add eax, 1 movsxd rdx, eax mov al, 1 cmp dword ptr [rcx + 4*rdx], 0 mov byte ptr [rbp - 43], al jne .LBB1_22 mov rax, qword ptr [rbp - 8] movsxd rcx, dword ptr [rbp - 28] movsx eax, byte ptr [rax + rcx] mov rcx, qword ptr [rbp - 16] mov edx, dword ptr [rbp - 32] sub edx, 1 movsxd rdx, edx movsx ecx, byte ptr [rcx + rdx] cmp eax, ecx je .LBB1_20 mov rax, qword ptr [rbp - 16] mov ecx, dword ptr [rbp - 32] sub ecx, 1 movsxd rcx, ecx movsx ecx, byte ptr [rax + rcx] xor eax, eax cmp ecx, 46 mov byte ptr [rbp - 44], al jne .LBB1_21 .LBB1_20: mov rax, qword ptr [rbp - 24] mov ecx, dword ptr [rbp - 28] sub ecx, 1 add ecx, 1 mov edx, dword ptr [rbp - 40] add edx, 1 imul ecx, edx add ecx, dword ptr [rbp - 32] add ecx, 1 movsxd rcx, ecx cmp dword ptr [rax + 4*rcx], 0 setne al mov byte ptr [rbp - 44], al .LBB1_21: mov al, byte ptr [rbp - 44] mov byte ptr [rbp - 43], al .LBB1_22: mov al, byte ptr [rbp - 43] and al, 1 movzx edx, al mov rax, qword ptr [rbp - 24] mov ecx, dword ptr [rbp - 28] add ecx, 1 mov esi, dword ptr [rbp - 40] add esi, 1 imul ecx, esi add ecx, dword ptr [rbp - 32] add ecx, 1 movsxd rcx, ecx mov dword ptr [rax + 4*rcx], edx .LBB1_23: jmp .LBB1_24 .LBB1_24: mov eax, dword ptr [rbp - 32] add eax, 1 mov dword ptr [rbp - 32], eax jmp .LBB1_9 .LBB1_25: jmp .LBB1_26 .LBB1_26: mov eax, dword ptr [rbp - 28] add eax, 1 mov dword ptr [rbp - 28], eax jmp .LBB1_7 .LBB1_27: mov rax, qword ptr [rbp - 24] mov ecx, dword ptr [rbp - 36] sub ecx, 1 add ecx, 1 mov edx, dword ptr [rbp - 40] add edx, 1 imul ecx, edx mov edx, dword ptr [rbp - 40] sub edx, 1 add ecx, edx add ecx, 1 movsxd rcx, ecx mov eax, dword ptr [rax + 4*rcx] mov dword ptr [rbp - 28], eax mov rdi, qword ptr [rbp - 24] call free@PLT cmp dword ptr [rbp - 28], 0 setne al and al, 1 add rsp, 48 pop rbp ret
313
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O1
x86-64 clang 21.1.0
match_recursive: push r15 push r14 push rbx mov rbx, rdx mov r15, rsi mov r14, rdi add r15, 2 jmp .LBB0_1 .LBB0_12: test cl, cl je .LBB0_15 .LBB0_13: inc r14 inc r15 .LBB0_1: movzx eax, byte ptr [r15 - 2] test al, al je .LBB0_2 cmp byte ptr [r15 - 1], 42 je .LBB0_4 movzx ecx, byte ptr [r14] cmp al, 46 je .LBB0_12 cmp cl, al je .LBB0_13 jmp .LBB0_15 .LBB0_8: cmp dl, cl jne .LBB0_16 .LBB0_9: inc r14 .LBB0_4: cmp dword ptr [rbx], 0 je .LBB0_10 mov rdi, r14 mov rsi, r15 mov rdx, rbx call match_recursive test al, al jne .LBB0_16 movzx ecx, byte ptr [r15 - 2] movzx edx, byte ptr [r14] cmp cl, 46 jne .LBB0_8 test dl, dl jne .LBB0_9 jmp .LBB0_16 .LBB0_2: cmp byte ptr [r14], 0 sete al jmp .LBB0_16 .LBB0_10: mov dword ptr [rbx], 0 .LBB0_15: xor eax, eax .LBB0_16: pop rbx pop r14 pop r15 ret isMatch: push rbp push r15 push r14 push r13 push r12 push rbx sub rsp, 72 mov rbx, rsi mov qword ptr [rsp + 40], rdi call strlen@PLT mov r14, rax mov rdi, rbx call strlen@PLT mov r15, rax lea eax, [r14 + 1] lea ecx, [r15 + 1] mov dword ptr [rsp + 4], ecx imul eax, ecx movsxd rdi, eax mov esi, 4 call calloc@PLT mov dword ptr [rax], 1 mov qword ptr [rsp + 8], r15 test r15d, r15d jle .LBB1_5 mov rcx, qword ptr [rsp + 8] and ecx, 2147483647 xor edx, edx jmp .LBB1_3 .LBB1_2: inc rdx cmp rcx, rdx je .LBB1_5 .LBB1_3: cmp byte ptr [rbx + rdx], 42 jne .LBB1_2 mov esi, dword ptr [rax + 4*rdx - 4] mov dword ptr [rax + 4*rdx + 4], esi jmp .LBB1_2 .LBB1_5: mov qword ptr [rsp + 16], r14 test r14d, r14d jle .LBB1_20 movabs rcx, 4294967296 mov rdx, rax add rdx, 4 movsxd rsi, dword ptr [rsp + 4] mov qword ptr [rsp + 32], rsi mov rsi, qword ptr [rsp + 16] and esi, 2147483647 mov qword ptr [rsp + 64], rsi mov rsi, qword ptr [rsp + 8] mov r8d, esi and r8d, 2147483647 movsxd r15, esi lea rsi, [4*r15 + 4] mov qword ptr [rsp + 48], rsi shl r15, 32 movabs rsi, -4294967296 add rsi, r15 mov qword ptr [rsp + 24], rsi add r15, rcx xor r13d, r13d mov qword ptr [rsp + 56], r15 mov rbp, rdx xor r12d, r12d jmp .LBB1_8 .LBB1_7: inc r12 add rbp, qword ptr [rsp + 48] mov rsi, qword ptr [rsp + 56] add qword ptr [rsp + 24], rsi add r13, qword ptr [rsp + 32] add r15, rsi cmp r12, qword ptr [rsp + 64] je .LBB1_20 .LBB1_8: cmp dword ptr [rsp + 8], 0 jle .LBB1_7 lea r10, [r12 + 1] imul r10, qword ptr [rsp + 32] mov r9, r15 mov r11, qword ptr [rsp + 24] xor edi, edi jmp .LBB1_13 .LBB1_19: xor r14d, r14d .LBB1_12: movzx esi, r14b lea r14, [rdx + 4*r10] mov dword ptr [r14 + 4*rdi], esi inc rdi add r11, rcx add r9, rcx cmp r8, rdi je .LBB1_7 .LBB1_13: movzx r14d, byte ptr [rbx + rdi] cmp r14b, 42 jne .LBB1_10 mov rsi, r11 sar rsi, 30 mov r14b, 1 cmp dword ptr [rax + rsi + 4], 0 jne .LBB1_12 mov rsi, r9 sar rsi, 30 cmp dword ptr [rax + rsi], 0 jne .LBB1_12 movzx esi, byte ptr [rbx + rdi - 1] mov r14, qword ptr [rsp + 40] cmp byte ptr [r14 + r12], sil setne r14b cmp sil, 46 setne sil test r14b, sil jne .LBB1_19 cmp dword ptr [rbp + 4*rdi], 0 setne r14b jmp .LBB1_12 .LBB1_10: lea esi, [rdi + r13] movsxd rsi, esi cmp dword ptr [rax + 4*rsi], 0 je .LBB1_19 mov rsi, qword ptr [rsp + 40] cmp byte ptr [rsi + r12], r14b sete sil cmp r14b, 46 sete r14b or r14b, sil jmp .LBB1_12 .LBB1_20: mov ecx, dword ptr [rsp + 4] imul ecx, dword ptr [rsp + 16] add ecx, dword ptr [rsp + 8] movsxd rcx, ecx cmp dword ptr [rax + 4*rcx], 0 setne bl mov rdi, rax call free@PLT mov eax, ebx add rsp, 72 pop rbx pop r12 pop r13 pop r14 pop r15 pop rbp ret
314
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O2
x86-64 clang 21.1.0
match_recursive: push r15 push r14 push rbx mov rbx, rdi movzx eax, byte ptr [rsi] test al, al je .LBB0_14 mov r14, rdx mov r15, rsi add r15, 2 jmp .LBB0_4 .LBB0_2: cmp dl, al jne .LBB0_7 .LBB0_3: inc rbx inc r15 mov eax, ecx test cl, cl je .LBB0_14 .LBB0_4: movzx ecx, byte ptr [r15 - 1] cmp cl, 42 je .LBB0_10 movzx edx, byte ptr [rbx] cmp al, 46 jne .LBB0_2 test dl, dl jne .LBB0_3 jmp .LBB0_7 .LBB0_8: cmp dl, cl jne .LBB0_15 .LBB0_9: inc rbx .LBB0_10: cmp dword ptr [r14], 0 je .LBB0_16 mov rdi, rbx mov rsi, r15 mov rdx, r14 call match_recursive test al, al jne .LBB0_15 movzx ecx, byte ptr [r15 - 2] movzx edx, byte ptr [rbx] cmp cl, 46 jne .LBB0_8 test dl, dl jne .LBB0_9 jmp .LBB0_15 .LBB0_14: cmp byte ptr [rbx], 0 sete al jmp .LBB0_15 .LBB0_16: mov dword ptr [r14], 0 .LBB0_7: xor eax, eax .LBB0_15: pop rbx pop r14 pop r15 ret isMatch: push rbp push r15 push r14 push r13 push r12 push rbx sub rsp, 56 mov rbx, rsi mov r14, rdi call strlen@PLT mov r15, rax mov rdi, rbx call strlen@PLT mov r12, rax lea eax, [r15 + 1] lea ecx, [r12 + 1] mov dword ptr [rsp + 12], ecx imul eax, ecx movsxd rdi, eax mov esi, 4 call calloc@PLT mov dword ptr [rax], 1 test r12d, r12d jle .LBB1_6 mov ecx, r12d and ecx, 2147483647 cmp ecx, 1 jne .LBB1_25 xor ecx, ecx .LBB1_3: test r12b, 1 je .LBB1_6 cmp byte ptr [rbx + rcx], 42 jne .LBB1_6 mov edx, dword ptr [rax + 4*rcx - 4] mov dword ptr [rax + 4*rcx + 4], edx .LBB1_6: mov qword ptr [rsp + 16], r15 test r15d, r15d jle .LBB1_24 movabs rcx, 4294967296 mov rdx, qword ptr [rsp + 16] and edx, 2147483647 mov qword ptr [rsp + 40], rdx mov esi, r12d and esi, 2147483647 movsxd rdi, r12d lea r8, [rax + 4*rdi] add r8, 8 lea rdx, [4*rdi + 4] mov qword ptr [rsp + 32], rdx shl rdi, 32 movabs r10, -4294967296 add r10, rdi add rdi, rcx xor ebp, ebp mov r13, rax xor r15d, r15d mov qword ptr [rsp + 24], r12 jmp .LBB1_10 .LBB1_8: mov r9, qword ptr [rsp + 48] mov r15, r9 mov r12, qword ptr [rsp + 24] .LBB1_9: mov rdx, qword ptr [rsp + 32] add r8, rdx add r13, rdx add rbp, rdi cmp r9, qword ptr [rsp + 40] je .LBB1_24 .LBB1_10: test r12d, r12d jle .LBB1_23 lea rdx, [r15 + 1] mov qword ptr [rsp + 48], rdx mov r12, rbp xor edx, edx jmp .LBB1_15 .LBB1_21: xor r9d, r9d .LBB1_14: movzx r9d, r9b mov dword ptr [r8 + 4*rdx], r9d inc rdx add r12, rcx cmp rsi, rdx je .LBB1_8 .LBB1_15: movzx r9d, byte ptr [rbx + rdx] cmp r9b, 42 jne .LBB1_12 lea r11, [r10 + r12] sar r11, 30 mov r9b, 1 cmp dword ptr [rax + r11 + 4], 0 jne .LBB1_14 lea r11, [rdi + r12] sar r11, 30 cmp dword ptr [rax + r11], 0 jne .LBB1_14 movzx r9d, byte ptr [rbx + rdx - 1] cmp byte ptr [r14 + r15], r9b je .LBB1_22 cmp r9b, 46 jne .LBB1_21 .LBB1_22: cmp dword ptr [r13 + 4*rdx + 4], 0 setne r9b jmp .LBB1_14 .LBB1_12: mov r11, r12 sar r11, 30 cmp dword ptr [rax + r11], 0 je .LBB1_21 cmp byte ptr [r14 + r15], r9b sete r11b cmp r9b, 46 sete r9b or r9b, r11b jmp .LBB1_14 .LBB1_23: inc r15 mov r9, r15 jmp .LBB1_9 .LBB1_24: mov ecx, dword ptr [rsp + 12] imul ecx, dword ptr [rsp + 16] add ecx, r12d movsxd rcx, ecx cmp dword ptr [rax + 4*rcx], 0 setne bl mov rdi, rax call free@PLT mov eax, ebx add rsp, 56 pop rbx pop r12 pop r13 pop r14 pop r15 pop rbp ret .LBB1_25: mov edx, r12d and edx, 2147483646 xor ecx, ecx jmp .LBB1_27 .LBB1_26: add rcx, 2 cmp rdx, rcx je .LBB1_3 .LBB1_27: cmp byte ptr [rbx + rcx], 42 jne .LBB1_29 mov esi, dword ptr [rax + 4*rcx - 4] mov dword ptr [rax + 4*rcx + 4], esi .LBB1_29: cmp byte ptr [rbx + rcx + 1], 42 jne .LBB1_26 mov esi, dword ptr [rax + 4*rcx] mov dword ptr [rax + 4*rcx + 8], esi jmp .LBB1_26
315
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O3
x86-64 clang 21.1.0
match_recursive: push r15 push r14 push rbx mov rbx, rdi movzx eax, byte ptr [rsi] test al, al je .LBB0_15 mov r14, rdx mov r15, rsi add r15, 2 jmp .LBB0_4 .LBB0_2: cmp dl, al jne .LBB0_7 .LBB0_3: inc rbx inc r15 mov eax, ecx test cl, cl je .LBB0_15 .LBB0_4: movzx ecx, byte ptr [r15 - 1] cmp cl, 42 je .LBB0_11 movzx edx, byte ptr [rbx] cmp al, 46 jne .LBB0_2 test dl, dl jne .LBB0_3 jmp .LBB0_7 .LBB0_9: cmp dl, cl jne .LBB0_8 .LBB0_10: inc rbx .LBB0_11: cmp dword ptr [r14], 0 je .LBB0_16 mov rdi, rbx mov rsi, r15 mov rdx, r14 call match_recursive test al, al jne .LBB0_8 movzx ecx, byte ptr [r15 - 2] movzx edx, byte ptr [rbx] cmp cl, 46 jne .LBB0_9 test dl, dl jne .LBB0_10 .LBB0_8: pop rbx pop r14 pop r15 ret .LBB0_15: cmp byte ptr [rbx], 0 sete al pop rbx pop r14 pop r15 ret .LBB0_16: mov dword ptr [r14], 0 .LBB0_7: xor eax, eax pop rbx pop r14 pop r15 ret isMatch: push rbp push r15 push r14 push r13 push r12 push rbx sub rsp, 56 mov rbx, rsi mov qword ptr [rsp + 24], rdi call strlen@PLT mov r15, rax mov rdi, rbx call strlen@PLT mov r12, rax mov qword ptr [rsp + 16], r15 lea eax, [r15 + 1] lea ecx, [r12 + 1] mov dword ptr [rsp + 4], ecx imul eax, ecx movsxd rdi, eax mov esi, 4 call calloc@PLT mov dword ptr [rax], 1 mov qword ptr [rsp + 8], r12 test r12d, r12d jle .LBB1_20 mov rdi, qword ptr [rsp + 8] mov ecx, edi and ecx, 2147483647 cmp ecx, 1 jne .LBB1_21 xor ecx, ecx .LBB1_3: test dil, 1 je .LBB1_6 cmp byte ptr [rbx + rcx], 42 jne .LBB1_6 mov edx, dword ptr [rax + 4*rcx - 4] mov dword ptr [rax + 4*rcx + 4], edx .LBB1_6: cmp dword ptr [rsp + 16], 0 jle .LBB1_20 mov edx, dword ptr [rsp + 4] mov rcx, qword ptr [rsp + 16] and ecx, 2147483647 mov qword ptr [rsp + 40], rcx mov rsi, qword ptr [rsp + 8] and esi, 2147483647 lea rcx, [4*rdx] mov qword ptr [rsp + 32], rcx lea r8, [rax + 4*rdx] add r8, 4 mov r9, rax add r9, 4 shl rdx, 32 movabs r10, -8589934592 mov r14, rdx add r10, rdx xor ecx, ecx movabs r13, 4294967296 mov r15, r9 xor r12d, r12d jmp .LBB1_9 .LBB1_8: mov rdx, qword ptr [rsp + 32] add r8, rdx add r15, rdx add rcx, r14 mov rdx, qword ptr [rsp + 48] mov r12, rdx cmp rdx, qword ptr [rsp + 40] je .LBB1_20 .LBB1_9: lea rdx, [r12 + 1] mov qword ptr [rsp + 48], rdx mov rdx, rcx xor edi, edi jmp .LBB1_13 .LBB1_19: xor ebp, ebp .LBB1_12: movzx r11d, bpl mov dword ptr [r8 + 4*rdi], r11d inc rdi add rdx, r13 cmp rsi, rdi je .LBB1_8 .LBB1_13: movzx ebp, byte ptr [rbx + rdi] cmp bpl, 42 jne .LBB1_10 lea r11, [r10 + rdx] sar r11, 30 mov bpl, 1 cmp dword ptr [r9 + r11], 0 jne .LBB1_12 lea r11, [r14 + rdx] sar r11, 30 cmp dword ptr [rax + r11], 0 jne .LBB1_12 movzx r11d, byte ptr [rbx + rdi - 1] mov rbp, qword ptr [rsp + 24] cmp byte ptr [rbp + r12], r11b setne bpl cmp r11b, 46 setne r11b test bpl, r11b jne .LBB1_19 cmp dword ptr [r15 + 4*rdi], 0 setne bpl jmp .LBB1_12 .LBB1_10: mov r11, rdx sar r11, 30 cmp dword ptr [rax + r11], 0 je .LBB1_19 mov r11, qword ptr [rsp + 24] cmp byte ptr [r11 + r12], bpl sete r11b cmp bpl, 46 sete bpl or bpl, r11b jmp .LBB1_12 .LBB1_20: mov ecx, dword ptr [rsp + 4] imul ecx, dword ptr [rsp + 16] add ecx, dword ptr [rsp + 8] movsxd rcx, ecx cmp dword ptr [rax + 4*rcx], 0 setne bl mov rdi, rax call free@PLT mov eax, ebx add rsp, 56 pop rbx pop r12 pop r13 pop r14 pop r15 pop rbp ret .LBB1_21: mov edx, edi and edx, 2147483646 xor ecx, ecx jmp .LBB1_23 .LBB1_22: add rcx, 2 cmp rdx, rcx je .LBB1_3 .LBB1_23: cmp byte ptr [rbx + rcx], 42 jne .LBB1_24 mov esi, dword ptr [rax + 4*rcx - 4] mov dword ptr [rax + 4*rcx + 4], esi cmp byte ptr [rbx + rcx + 1], 42 jne .LBB1_22 jmp .LBB1_26 .LBB1_24: cmp byte ptr [rbx + rcx + 1], 42 jne .LBB1_22 .LBB1_26: mov esi, dword ptr [rax + 4*rcx] mov dword ptr [rax + 4*rcx + 8], esi jmp .LBB1_22
316
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O0
x86-64 gcc 15.2
match_recursive: push rbp mov rbp, rsp sub rsp, 32 mov QWORD PTR [rbp-8], rdi mov QWORD PTR [rbp-16], rsi mov QWORD PTR [rbp-24], rdx mov rax, QWORD PTR [rbp-16] movzx eax, BYTE PTR [rax] test al, al jne .L2 mov rax, QWORD PTR [rbp-8] movzx eax, BYTE PTR [rax] test al, al sete al jmp .L3 .L2: mov rax, QWORD PTR [rbp-16] add rax, 1 movzx eax, BYTE PTR [rax] cmp al, 42 jne .L4 jmp .L5 .L10: mov rax, QWORD PTR [rbp-16] lea rcx, [rax+2] mov rdx, QWORD PTR [rbp-24] mov rax, QWORD PTR [rbp-8] mov rsi, rcx mov rdi, rax call match_recursive test al, al je .L6 mov eax, 1 jmp .L3 .L6: mov rax, QWORD PTR [rbp-16] movzx eax, BYTE PTR [rax] cmp al, 46 jne .L7 mov rax, QWORD PTR [rbp-8] movzx eax, BYTE PTR [rax] test al, al je .L8 .L7: mov rax, QWORD PTR [rbp-16] movzx eax, BYTE PTR [rax] cmp al, 46 je .L9 mov rax, QWORD PTR [rbp-8] movzx edx, BYTE PTR [rax] mov rax, QWORD PTR [rbp-16] movzx eax, BYTE PTR [rax] cmp dl, al je .L9 .L8: mov eax, 0 jmp .L3 .L9: add QWORD PTR [rbp-8], 1 .L5: mov rax, QWORD PTR [rbp-24] mov eax, DWORD PTR [rax] test eax, eax jne .L10 mov rax, QWORD PTR [rbp-24] mov DWORD PTR [rax], 0 mov eax, 0 jmp .L3 .L4: mov rax, QWORD PTR [rbp-16] movzx eax, BYTE PTR [rax] cmp al, 46 jne .L11 mov rax, QWORD PTR [rbp-8] movzx eax, BYTE PTR [rax] test al, al je .L12 .L11: mov rax, QWORD PTR [rbp-16] movzx eax, BYTE PTR [rax] cmp al, 46 je .L13 mov rax, QWORD PTR [rbp-8] movzx edx, BYTE PTR [rax] mov rax, QWORD PTR [rbp-16] movzx eax, BYTE PTR [rax] cmp dl, al je .L13 .L12: mov eax, 0 jmp .L3 .L13: mov rax, QWORD PTR [rbp-16] lea rsi, [rax+1] mov rax, QWORD PTR [rbp-8] lea rcx, [rax+1] mov rax, QWORD PTR [rbp-24] mov rdx, rax mov rdi, rcx call match_recursive .L3: leave ret isMatch: push rbp mov rbp, rsp sub rsp, 48 mov QWORD PTR [rbp-40], rdi mov QWORD PTR [rbp-48], rsi mov rax, QWORD PTR [rbp-40] mov rdi, rax call strlen mov DWORD PTR [rbp-12], eax mov rax, QWORD PTR [rbp-48] mov rdi, rax call strlen mov DWORD PTR [rbp-16], eax mov eax, DWORD PTR [rbp-12] lea edx, [rax+1] mov eax, DWORD PTR [rbp-16] add eax, 1 imul eax, edx cdqe mov esi, 4 mov rdi, rax call calloc mov QWORD PTR [rbp-24], rax mov rax, QWORD PTR [rbp-24] mov DWORD PTR [rax], 1 mov DWORD PTR [rbp-8], 0 jmp .L15 .L17: mov eax, DWORD PTR [rbp-8] movsx rdx, eax mov rax, QWORD PTR [rbp-48] add rax, rdx movzx eax, BYTE PTR [rax] cmp al, 42 jne .L16 mov eax, DWORD PTR [rbp-8] sub eax, 2 cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov edx, DWORD PTR [rbp-8] movsx rdx, edx add rdx, 1 lea rcx, [0+rdx*4] mov rdx, QWORD PTR [rbp-24] add rdx, rcx mov eax, DWORD PTR [rax] mov DWORD PTR [rdx], eax .L16: add DWORD PTR [rbp-8], 1 .L15: mov eax, DWORD PTR [rbp-8] cmp eax, DWORD PTR [rbp-16] jl .L17 mov DWORD PTR [rbp-4], 0 jmp .L18 .L30: mov DWORD PTR [rbp-8], 0 jmp .L19 .L29: mov eax, DWORD PTR [rbp-8] movsx rdx, eax mov rax, QWORD PTR [rbp-48] add rax, rdx movzx eax, BYTE PTR [rax] cmp al, 42 je .L20 mov eax, DWORD PTR [rbp-16] add eax, 1 imul eax, DWORD PTR [rbp-4] mov edx, DWORD PTR [rbp-8] sub edx, 1 add eax, edx cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov eax, DWORD PTR [rax] test eax, eax je .L21 mov eax, DWORD PTR [rbp-4] movsx rdx, eax mov rax, QWORD PTR [rbp-40] add rax, rdx movzx edx, BYTE PTR [rax] mov eax, DWORD PTR [rbp-8] movsx rcx, eax mov rax, QWORD PTR [rbp-48] add rax, rcx movzx eax, BYTE PTR [rax] cmp dl, al je .L22 mov eax, DWORD PTR [rbp-8] movsx rdx, eax mov rax, QWORD PTR [rbp-48] add rax, rdx movzx eax, BYTE PTR [rax] cmp al, 46 jne .L21 .L22: mov ecx, 1 jmp .L23 .L21: mov ecx, 0 .L23: mov eax, DWORD PTR [rbp-4] lea edx, [rax+1] mov eax, DWORD PTR [rbp-16] add eax, 1 imul edx, eax mov eax, DWORD PTR [rbp-8] add eax, edx cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov DWORD PTR [rax], ecx jmp .L24 .L20: mov eax, DWORD PTR [rbp-4] lea edx, [rax+1] mov eax, DWORD PTR [rbp-16] add eax, 1 imul eax, edx mov edx, DWORD PTR [rbp-8] sub edx, 2 add eax, edx cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov eax, DWORD PTR [rax] test eax, eax jne .L25 mov eax, DWORD PTR [rbp-4] lea edx, [rax+1] mov eax, DWORD PTR [rbp-16] add eax, 1 imul eax, edx mov edx, DWORD PTR [rbp-8] sub edx, 1 add eax, edx cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov eax, DWORD PTR [rax] test eax, eax jne .L25 mov eax, DWORD PTR [rbp-4] 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-48] add rax, rcx movzx eax, BYTE PTR [rax] cmp dl, al je .L26 mov eax, DWORD PTR [rbp-8] cdqe lea rdx, [rax-1] mov rax, QWORD PTR [rbp-48] add rax, rdx movzx eax, BYTE PTR [rax] cmp al, 46 jne .L27 .L26: mov eax, DWORD PTR [rbp-16] add eax, 1 imul eax, DWORD PTR [rbp-4] mov edx, eax mov eax, DWORD PTR [rbp-8] add eax, edx cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov eax, DWORD PTR [rax] test eax, eax je .L27 .L25: mov ecx, 1 jmp .L28 .L27: mov ecx, 0 .L28: mov eax, DWORD PTR [rbp-4] lea edx, [rax+1] mov eax, DWORD PTR [rbp-16] add eax, 1 imul edx, eax mov eax, DWORD PTR [rbp-8] add eax, edx cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov DWORD PTR [rax], ecx .L24: add DWORD PTR [rbp-8], 1 .L19: mov eax, DWORD PTR [rbp-8] cmp eax, DWORD PTR [rbp-16] jl .L29 add DWORD PTR [rbp-4], 1 .L18: mov eax, DWORD PTR [rbp-4] cmp eax, DWORD PTR [rbp-12] jl .L30 mov eax, DWORD PTR [rbp-16] add eax, 1 imul eax, DWORD PTR [rbp-12] mov edx, DWORD PTR [rbp-16] sub edx, 1 add eax, edx cdqe add rax, 1 lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-24] add rax, rdx mov eax, DWORD PTR [rax] mov DWORD PTR [rbp-4], eax mov rax, QWORD PTR [rbp-24] mov rdi, rax call free cmp DWORD PTR [rbp-4], 0 setne al leave ret
317
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O1
x86-64 gcc 15.2
match_recursive: push r14 push r12 push rbp push rbx sub rsp, 8 mov rbx, rdi mov r12, rdx movzx edx, BYTE PTR [rsi] test dl, dl je .L15 mov rbp, rsi cmp BYTE PTR [rsi+1], 42 je .L16 cmp dl, 46 je .L17 mov eax, 0 cmp dl, BYTE PTR [rdi] jne .L1 .L10: lea rsi, [rbp+1] lea rdi, [rbx+1] mov rdx, r12 call match_recursive jmp .L1 .L15: cmp BYTE PTR [rdi], 0 sete al .L1: add rsp, 8 pop rbx pop rbp pop r12 pop r14 ret .L16: cmp DWORD PTR [r12], 0 je .L5 lea rax, [rsi+2] mov r14, rax jmp .L8 .L18: cmp BYTE PTR [rbx], 0 je .L1 .L7: add rbx, 1 cmp DWORD PTR [r12], 0 je .L5 .L8: mov rdx, r12 mov rsi, r14 mov rdi, rbx call match_recursive test al, al jne .L1 movzx edx, BYTE PTR [rbp+0] cmp dl, 46 je .L18 cmp dl, BYTE PTR [rbx] je .L7 jmp .L1 .L5: mov DWORD PTR [r12], 0 mov eax, 0 jmp .L1 .L17: mov eax, 0 cmp BYTE PTR [rdi], 0 jne .L10 jmp .L1 isMatch: push r15 push r14 push r13 push r12 push rbp push rbx sub rsp, 40 mov r12, rdi mov rbp, rsi call strlen mov r13, rax mov rdi, rbp call strlen mov r15, rax mov QWORD PTR [rsp+16], rax mov DWORD PTR [rsp+4], eax lea ebx, [rax+1] lea eax, [r13+1] imul eax, ebx mov DWORD PTR [rsp+28], eax movsx rdi, eax mov esi, 4 call calloc mov r8, rax mov DWORD PTR [rax], 1 test r15d, r15d jle .L20 lea ecx, [r15-1] mov eax, 0 jmp .L22 .L21: lea rdx, [rax+1] cmp rax, rcx je .L20 mov rax, rdx .L22: cmp BYTE PTR [rbp+0+rax], 42 jne .L21 mov edx, DWORD PTR [r8-4+rax*4] mov DWORD PTR [r8+4+rax*4], edx jmp .L21 .L20: test r13d, r13d jle .L23 mov rdi, QWORD PTR [rsp+16] mov eax, edi mov DWORD PTR [rsp+24], edi lea r9d, [rbx-1+rdi] add eax, 1 sub eax, ebx mov ecx, eax mov r15d, edi not r15d mov r10, r12 mov r14d, ebp neg r14d lea eax, [r13-1] lea rdi, [r12+1+rax] mov r13d, 1 mov r11d, ecx mov QWORD PTR [rsp+8], rbp jmp .L24 .L26: add ecx, ebp movsx rcx, ecx mov DWORD PTR [r8+4+rcx*4], esi .L27: add rax, 1 add edx, 1 cmp edx, r9d je .L42 .L30: movzx ebx, BYTE PTR [rax] cmp bl, 42 je .L25 mov ecx, eax lea esi, [r12+rax] movsx rsi, esi mov esi, DWORD PTR [r8+4+rsi*4] test esi, esi je .L26 cmp BYTE PTR [r10], bl sete sil cmp bl, 46 sete bl or esi, ebx movzx esi, sil jmp .L26 .L25: lea ecx, [rdx-1] movsx rcx, ecx mov ebx, r13d cmp DWORD PTR [r8+4+rcx*4], 0 jne .L28 movsx rcx, edx mov ebx, DWORD PTR [r8+4+rcx*4] test ebx, ebx jne .L35 movzx ecx, BYTE PTR [rax-1] cmp BYTE PTR [r10], cl je .L36 cmp cl, 46 jne .L28 .L36: lea ecx, [rdx+1+r15] movsx rcx, ecx cmp DWORD PTR [r8+4+rcx*4], 0 setne bl movzx ebx, bl jmp .L28 .L35: mov ebx, r13d .L28: lea ecx, [rdx+1] movsx rcx, ecx mov DWORD PTR [r8+4+rcx*4], ebx jmp .L27 .L42: mov ebx, DWORD PTR [rsp] .L32: add r9d, ebx add r15d, r11d add r10, 1 add r14d, ebx cmp r10, rdi je .L23 .L24: mov edx, r9d mov eax, DWORD PTR [rsp+24] sub edx, eax mov rax, QWORD PTR [rsp+8] lea r12d, [r14-1] lea ebp, [rbx+r14] cmp DWORD PTR [rsp+4], 0 jle .L32 mov DWORD PTR [rsp], ebx jmp .L30 .L23: mov eax, DWORD PTR [rsp+28] sub eax, ebx mov rdi, QWORD PTR [rsp+16] lea eax, [rax-1+rdi] cdqe mov ebx, DWORD PTR [r8+4+rax*4] mov rdi, r8 call free test ebx, ebx setne al add rsp, 40 pop rbx pop rbp pop r12 pop r13 pop r14 pop r15 ret
318
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O2
x86-64 gcc 15.2
match_recursive: push r14 push r12 mov r12, rdx push rbp mov rbp, rsi push rbx mov rbx, rdi sub rsp, 8 movzx eax, BYTE PTR [rsi] test al, al jne .L2 jmp .L14 .L12: cmp cl, dl jne .L8 .L13: add rbp, 1 add rbx, 1 test al, al je .L14 .L2: mov edx, eax movzx eax, BYTE PTR [rbp+1] cmp al, 42 je .L27 movzx ecx, BYTE PTR [rbx] cmp dl, 46 jne .L12 test cl, cl jne .L13 .L8: xor eax, eax .L1: add rsp, 8 pop rbx pop rbp pop r12 pop r14 ret .L14: cmp BYTE PTR [rbx], 0 sete al add rsp, 8 pop rbx pop rbp pop r12 pop r14 ret .L27: mov edx, DWORD PTR [r12] lea r14, [rbp+2] test edx, edx jne .L10 jmp .L11 .L6: cmp al, dl jne .L8 .L7: mov eax, DWORD PTR [r12] add rbx, 1 test eax, eax je .L11 .L10: mov rdx, r12 mov rsi, r14 mov rdi, rbx call match_recursive test al, al jne .L1 movzx eax, BYTE PTR [rbp+0] movzx edx, BYTE PTR [rbx] cmp al, 46 jne .L6 test dl, dl jne .L7 jmp .L8 .L11: mov DWORD PTR [r12], 0 jmp .L8 isMatch: push r15 mov r15, rdi push r14 push r13 push r12 push rbp mov rbp, rsi push rbx sub rsp, 8 call strlen mov rdi, rbp mov r13, rax call strlen lea r14d, [r13+1] mov esi, 4 lea ebx, [rax+1] mov r12, rax imul r14d, ebx movsx rdi, r14d call calloc lea edx, [r12-1] mov DWORD PTR [rax], 1 mov rdi, rax xor eax, eax test r12d, r12d jg .L34 jmp .L35 .L43: add rax, 1 .L34: cmp BYTE PTR [rbp+0+rax], 42 jne .L33 mov ecx, DWORD PTR [rdi-4+rax*4] mov DWORD PTR [rdi+4+rax*4], ecx .L33: cmp rax, rdx jne .L43 .L35: test r13d, r13d jle .L30 lea eax, [r13-1] mov r13d, ebp lea r8d, [rbx+r12] mov r9, r15 neg r13d lea r15, [r15+1+rax] mov r11d, r13d test r12d, r12d jle .L32 .L42: mov eax, r8d mov rdx, rbp lea r10d, [r11-1] sub eax, r12d jmp .L41 .L60: lea ecx, [r10+rdx] movsx rcx, ecx mov ecx, DWORD PTR [rdi+4+rcx*4] test ecx, ecx je .L37 cmp BYTE PTR [r9], sil sete cl cmp sil, 46 sete sil or ecx, esi movzx ecx, cl .L37: movsx rsi, eax add eax, 1 add rdx, 1 mov DWORD PTR [rdi+4+rsi*4], ecx cmp r8d, eax je .L59 .L41: movzx esi, BYTE PTR [rdx] cmp sil, 42 jne .L60 lea ecx, [rax-2] mov esi, 1 movsx rcx, ecx mov ecx, DWORD PTR [rdi+4+rcx*4] test ecx, ecx jne .L39 lea ecx, [rax-1] movsx rcx, ecx mov esi, DWORD PTR [rdi+4+rcx*4] test esi, esi jne .L45 movzx ecx, BYTE PTR [rdx-1] cmp BYTE PTR [r9], cl je .L46 cmp cl, 46 je .L46 .L39: movsx rcx, eax add eax, 1 add rdx, 1 mov DWORD PTR [rdi+4+rcx*4], esi cmp r8d, eax jne .L41 .L59: add r9, 1 add r8d, ebx add r13d, ebx add r11d, ebx cmp r9, r15 jne .L42 .L30: sub r14d, ebx lea eax, [r14-1+r12] cdqe mov ebx, DWORD PTR [rdi+4+rax*4] call free test ebx, ebx setne al add rsp, 8 pop rbx pop rbp pop r12 pop r13 pop r14 pop r15 ret .L32: lea rax, [r9+1] cmp rax, r15 je .L30 add r9, 2 cmp r9, r15 jne .L32 jmp .L30 .L45: mov esi, 1 jmp .L39 .L46: lea ecx, [r13+0+rdx] xor esi, esi movsx rcx, ecx mov ecx, DWORD PTR [rdi+4+rcx*4] test ecx, ecx setne sil jmp .L39
319
10
Regular Expression Matching
Hard
/* 10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true */ #define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B))) #define MATCH(A, B) ((A) == (B) || (B) == '.') #define IDX(I, J) (((I) + 1) * (plen + 1) + (J) + 1) bool match_recursive(char *s, char *p, int *retry) { if (*p == 0) return *s == 0; if (*(p + 1) == '*') { while (*retry) { if (match_recursive(s, p + 2, retry)) { return true; } if (NOT_MATCH(*s, *p)) { return false; } s ++; } *retry = 0; return false; } if (NOT_MATCH(*s, *p)) { return false; } return match_recursive(s + 1, p + 1, retry); } bool isMatch(char* s, char* p) { #if 0 // 22ms int retry = 1; return match_recursive(s, p, &retry); #else // 9ms int *dp; int i, j; int slen, plen; slen = strlen(s); plen = strlen(p); dp = calloc((slen + 1) * (plen + 1), sizeof(int)); //assert(dp); dp[0] = 1; for (j = 0; j < plen; j ++) { if (p[j] == '*') { dp[IDX(-1, j)] = dp[IDX(-1, j - 2)]; } } for (i = 0; i < slen; i++) { for (j = 0; j < plen; j++) { if (p[j] != '*') { dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]); } else { dp[IDX(i, j)] = dp[IDX(i, j - 2)] || // no s dp[IDX(i, j - 1)] || // one s (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]); // more s } } } i = dp[IDX(slen - 1, plen - 1)]; free(dp); return i; #endif } /* Difficulty:Hard Total Accepted:147.1K Total Submissions:610.5K Companies Google Uber Airbnb Facebook Twitter Related Topics Dynamic Programming Backtracking String Similar Questions Wildcard Matching */
x86-64
-O3
x86-64 gcc 15.2
match_recursive: push r14 push r12 mov r12, rdx push rbp mov rbp, rsi push rbx mov rbx, rdi sub rsp, 8 movzx eax, BYTE PTR [rsi] test al, al jne .L2 jmp .L14 .L12: cmp cl, dl jne .L8 .L13: add rbp, 1 add rbx, 1 test al, al je .L14 .L2: mov edx, eax movzx eax, BYTE PTR [rbp+1] cmp al, 42 je .L27 movzx ecx, BYTE PTR [rbx] cmp dl, 46 jne .L12 test cl, cl jne .L13 .L8: xor eax, eax .L1: add rsp, 8 pop rbx pop rbp pop r12 pop r14 ret .L14: cmp BYTE PTR [rbx], 0 sete al add rsp, 8 pop rbx pop rbp pop r12 pop r14 ret .L27: mov edx, DWORD PTR [r12] lea r14, [rbp+2] test edx, edx jne .L10 jmp .L11 .L6: cmp al, dl jne .L8 .L7: mov eax, DWORD PTR [r12] add rbx, 1 test eax, eax je .L11 .L10: mov rdx, r12 mov rsi, r14 mov rdi, rbx call match_recursive test al, al jne .L1 movzx eax, BYTE PTR [rbp+0] movzx edx, BYTE PTR [rbx] cmp al, 46 jne .L6 test dl, dl jne .L7 jmp .L8 .L11: mov DWORD PTR [r12], 0 jmp .L8 isMatch: push r15 push r14 push r13 push r12 mov r12, rsi push rbp push rbx mov rbx, rdi sub rsp, 24 call strlen mov rdi, r12 mov r13, rax call strlen mov esi, 4 mov r14, rax lea ebp, [rax+1] lea eax, [r13+1] imul eax, ebp movsx rdi, eax mov DWORD PTR [rsp+12], eax call calloc mov edx, DWORD PTR [rsp+12] lea ecx, [r14-1] mov DWORD PTR [rax], 1 mov rdi, rax xor eax, eax test r14d, r14d jg .L33 jmp .L30 .L41: add rax, 1 .L33: cmp BYTE PTR [r12+rax], 42 jne .L32 mov esi, DWORD PTR [rdi-4+rax*4] mov DWORD PTR [rdi+4+rax*4], esi .L32: cmp rcx, rax jne .L41 test r13d, r13d jle .L30 mov r11d, r12d mov DWORD PTR [rsp+12], edx lea eax, [r13-1] lea r8d, [rbp+0+r14] neg r11d lea r15, [rbx+1+rax] lea r13d, [rbp+0+r12] mov r10d, r11d .L31: lea eax, [r13+0+r10] mov rdx, r12 lea r9d, [r10-1] jmp .L40 .L59: lea ecx, [r9+rdx] movsx rcx, ecx mov ecx, DWORD PTR [rdi+4+rcx*4] test ecx, ecx je .L36 cmp BYTE PTR [rbx], sil sete cl cmp sil, 46 sete sil or ecx, esi movzx ecx, cl .L36: movsx rsi, eax add eax, 1 add rdx, 1 mov DWORD PTR [rdi+4+rsi*4], ecx cmp eax, r8d je .L58 .L40: movzx esi, BYTE PTR [rdx] cmp sil, 42 jne .L59 lea ecx, [rax-2] mov esi, 1 movsx rcx, ecx mov ecx, DWORD PTR [rdi+4+rcx*4] test ecx, ecx jne .L38 lea ecx, [rax-1] movsx rcx, ecx mov esi, DWORD PTR [rdi+4+rcx*4] test esi, esi jne .L43 movzx ecx, BYTE PTR [rdx-1] cmp BYTE PTR [rbx], cl je .L44 cmp cl, 46 je .L44 .L38: movsx rcx, eax add eax, 1 add rdx, 1 mov DWORD PTR [rdi+4+rcx*4], esi cmp eax, r8d jne .L40 .L58: add rbx, 1 add r8d, ebp add r11d, ebp add r10d, ebp cmp r15, rbx jne .L31 mov edx, DWORD PTR [rsp+12] .L30: sub edx, ebp lea eax, [rdx-1+r14] cdqe mov ebx, DWORD PTR [rdi+4+rax*4] call free test ebx, ebx setne al add rsp, 24 pop rbx pop rbp pop r12 pop r13 pop r14 pop r15 ret .L43: mov esi, 1 jmp .L38 .L44: lea ecx, [r11+rdx] xor esi, esi movsx rcx, ecx mov ecx, DWORD PTR [rdi+4+rcx*4] test ecx, ecx setne sil jmp .L38
320
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O0
ARM64 gcc 15.2.0
maxArea: sub sp, sp, #48 str x0, [sp, 8] str w1, [sp, 4] str wzr, [sp, 44] str wzr, [sp, 40] ldr w0, [sp, 4] sub w0, w0, #1 str w0, [sp, 36] b .L2 .L5: ldrsw x0, [sp, 40] lsl x0, x0, 2 ldr x1, [sp, 8] add x0, x1, x0 ldr w0, [x0] str w0, [sp, 32] ldrsw x0, [sp, 36] lsl x0, x0, 2 ldr x1, [sp, 8] add x0, x1, x0 ldr w0, [x0] str w0, [sp, 28] ldr w0, [sp, 32] ldr w2, [sp, 28] ldr w1, [sp, 28] cmp w2, w0 csel w0, w1, w0, le str w0, [sp, 24] ldr w1, [sp, 36] ldr w0, [sp, 40] sub w0, w1, w0 ldr w1, [sp, 24] mul w0, w1, w0 str w0, [sp, 20] ldr w1, [sp, 44] ldr w0, [sp, 20] cmp w1, w0 bge .L3 ldr w0, [sp, 20] str w0, [sp, 44] .L3: ldr w1, [sp, 32] ldr w0, [sp, 28] cmp w1, w0 bge .L4 ldr w0, [sp, 40] add w0, w0, 1 str w0, [sp, 40] b .L2 .L4: ldr w0, [sp, 36] sub w0, w0, #1 str w0, [sp, 36] .L2: ldr w1, [sp, 40] ldr w0, [sp, 36] cmp w1, w0 blt .L5 ldr w0, [sp, 44] add sp, sp, 48 ret
321
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O1
ARM64 gcc 15.2.0
maxArea: mov x6, x0 sub w1, w1, #1 cmp w1, 0 ble .L6 mov w3, 0 mov w0, 0 b .L5 .L3: sub w1, w1, #1 .L4: cmp w1, w3 ble .L1 .L5: ldr w5, [x6, w3, sxtw 2] ldr w4, [x6, w1, sxtw 2] sub w2, w1, w3 cmp w5, w4 csel w7, w5, w4, le mul w2, w2, w7 cmp w0, w2 csel w0, w0, w2, ge cmp w5, w4 bge .L3 add w3, w3, 1 b .L4 .L6: mov w0, 0 .L1: ret
322
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O2
ARM64 gcc 15.2.0
maxArea: sub w1, w1, #1 mov x6, x0 cmp w1, 0 ble .L6 mov w3, 0 mov w0, 0 b .L5 .L9: add w3, w3, 1 cmp w3, w1 bge .L1 .L5: ldr w4, [x6, w3, uxtw 2] sub w2, w1, w3 ldr w5, [x6, w1, uxtw 2] cmp w5, w4 csel w7, w5, w4, le mul w2, w2, w7 cmp w0, w2 csel w0, w0, w2, ge cmp w5, w4 bgt .L9 sub w1, w1, #1 cmp w3, w1 blt .L5 .L1: ret .L6: mov w0, 0 ret
323
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O3
ARM64 gcc 15.2.0
maxArea: sub w1, w1, #1 mov x6, x0 cmp w1, 0 ble .L6 mov w3, 0 mov w0, 0 b .L5 .L9: add w3, w3, 1 cmp w1, w3 ble .L1 .L5: ldr w4, [x6, w3, uxtw 2] sub w2, w1, w3 ldr w5, [x6, w1, uxtw 2] cmp w5, w4 csel w7, w5, w4, le mul w2, w2, w7 cmp w0, w2 csel w0, w0, w2, ge cmp w5, w4 bgt .L9 sub w1, w1, #1 cmp w1, w3 bgt .L5 .L1: ret .L6: mov w0, 0 ret
324
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O0
armv8-a clang 21.1.0
maxArea: sub sp, sp, #48 str x0, [sp, #40] str w1, [sp, #36] str wzr, [sp, #16] str wzr, [sp, #12] ldr w8, [sp, #36] 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.ge .LBB0_11 b .LBB0_2 .LBB0_2: ldr x8, [sp, #40] ldrsw x9, [sp, #12] ldr w8, [x8, x9, lsl #2] str w8, [sp, #32] ldr x8, [sp, #40] ldrsw x9, [sp, #8] ldr w8, [x8, x9, lsl #2] str w8, [sp, #28] ldr w8, [sp, #32] ldr w9, [sp, #28] subs w8, w8, w9 b.ge .LBB0_4 b .LBB0_3 .LBB0_3: ldr w8, [sp, #32] str w8, [sp, #4] b .LBB0_5 .LBB0_4: ldr w8, [sp, #28] str w8, [sp, #4] b .LBB0_5 .LBB0_5: ldr w8, [sp, #4] str w8, [sp, #24] ldr w8, [sp, #24] ldr w9, [sp, #8] ldr w10, [sp, #12] subs w9, w9, w10 mul w8, w8, w9 str w8, [sp, #20] ldr w8, [sp, #16] ldr w9, [sp, #20] subs w8, w8, w9 b.ge .LBB0_7 b .LBB0_6 .LBB0_6: ldr w8, [sp, #20] str w8, [sp, #16] b .LBB0_7 .LBB0_7: ldr w8, [sp, #32] ldr w9, [sp, #28] subs w8, w8, w9 b.ge .LBB0_9 b .LBB0_8 .LBB0_8: ldr w8, [sp, #12] add w8, w8, #1 str w8, [sp, #12] b .LBB0_10 .LBB0_9: ldr w8, [sp, #8] subs w8, w8, #1 str w8, [sp, #8] b .LBB0_10 .LBB0_10: b .LBB0_1 .LBB0_11: ldr w0, [sp, #16] add sp, sp, #48 ret
325
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O1
armv8-a clang 21.1.0
maxArea: cmp w1, #2 b.lt .LBB0_4 mov w9, wzr mov w8, wzr sub w10, w1, #1 .LBB0_2: ldr w11, [x0, w9, uxtw #2] ldr w12, [x0, w10, sxtw #2] sub w13, w10, w9 cmp w11, w12 csel w11, w11, w12, lt cset w12, ge cinc w9, w9, lt mul w11, w11, w13 sub w10, w10, w12 cmp w8, w11 csel w8, w8, w11, gt cmp w9, w10 b.lt .LBB0_2 mov w0, w8 ret .LBB0_4: mov w0, wzr ret
326
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O2
armv8-a clang 21.1.0
maxArea: cmp w1, #2 b.lt .LBB0_4 mov w9, wzr mov w8, wzr sub w10, w1, #1 .LBB0_2: ldr w11, [x0, w9, uxtw #2] ldr w12, [x0, w10, sxtw #2] sub w13, w10, w9 cmp w11, w12 csel w11, w11, w12, lt cset w12, ge cinc w9, w9, lt mul w11, w11, w13 sub w10, w10, w12 cmp w8, w11 csel w8, w8, w11, gt cmp w9, w10 b.lt .LBB0_2 mov w0, w8 ret .LBB0_4: mov w0, wzr ret
327
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
aarch64
-O3
armv8-a clang 21.1.0
maxArea: cmp w1, #2 b.lt .LBB0_4 mov w9, wzr mov w8, wzr sub w10, w1, #1 .LBB0_2: ldr w11, [x0, w9, uxtw #2] ldr w12, [x0, w10, sxtw #2] sub w14, w10, w9 cmp w11, w12 csel w13, w11, w12, lt mul w13, w13, w14 cmp w8, w13 csel w8, w8, w13, gt sub w13, w10, #1 cmp w11, w12 cinc w9, w9, lt csel w10, w13, w10, ge cmp w9, w10 b.lt .LBB0_2 mov w0, w8 ret .LBB0_4: mov w0, wzr ret
328
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O0
mips64 clang 21.1.0
maxArea: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -64 sd $ra, 56($sp) sd $fp, 48($sp) move $fp, $sp move $1, $5 sd $4, 40($fp) sw $1, 36($fp) sw $zero, 16($fp) sw $zero, 12($fp) lw $1, 36($fp) addiu $1, $1, -1 sw $1, 8($fp) b .LBB0_1 nop .LBB0_1: lw $1, 12($fp) lw $2, 8($fp) slt $1, $1, $2 beqz $1, .LBB0_15 nop b .LBB0_3 nop .LBB0_3: ld $1, 40($fp) lw $2, 12($fp) dsll $2, $2, 2 daddu $1, $1, $2 lw $1, 0($1) sw $1, 32($fp) ld $1, 40($fp) lw $2, 8($fp) dsll $2, $2, 2 daddu $1, $1, $2 lw $1, 0($1) sw $1, 28($fp) lw $1, 32($fp) lw $2, 28($fp) slt $1, $1, $2 beqz $1, .LBB0_6 nop b .LBB0_5 nop .LBB0_5: lw $1, 32($fp) sw $1, 4($fp) b .LBB0_7 nop .LBB0_6: lw $1, 28($fp) sw $1, 4($fp) b .LBB0_7 nop .LBB0_7: lw $1, 4($fp) sw $1, 24($fp) lw $1, 24($fp) lw $2, 8($fp) lw $3, 12($fp) subu $2, $2, $3 mul $1, $1, $2 sw $1, 20($fp) lw $1, 16($fp) lw $2, 20($fp) slt $1, $1, $2 beqz $1, .LBB0_10 nop b .LBB0_9 nop .LBB0_9: lw $1, 20($fp) sw $1, 16($fp) b .LBB0_10 nop .LBB0_10: lw $1, 32($fp) lw $2, 28($fp) slt $1, $1, $2 beqz $1, .LBB0_13 nop b .LBB0_12 nop .LBB0_12: lw $1, 12($fp) addiu $1, $1, 1 sw $1, 12($fp) b .LBB0_14 nop .LBB0_13: lw $1, 8($fp) addiu $1, $1, -1 sw $1, 8($fp) b .LBB0_14 nop .LBB0_14: b .LBB0_1 nop .LBB0_15: lw $2, 16($fp) move $sp, $fp ld $fp, 48($sp) ld $ra, 56($sp) daddiu $sp, $sp, 64 jr $ra nop
329
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O1
mips64 clang 21.1.0
maxArea: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp slti $1, $5, 2 bnez $1, .LBB0_3 addiu $6, $zero, 0 addiu $2, $5, -1 addiu $3, $zero, 0 addiu $5, $zero, 0 .LBB0_2: sll $1, $2, 0 dsll $1, $1, 2 daddu $1, $4, $1 lw $1, 0($1) sll $6, $3, 0 dsll $6, $6, 2 daddu $6, $4, $6 lw $6, 0($6) slt $7, $6, $1 movn $1, $6, $7 subu $6, $2, $3 mul $6, $1, $6 slt $1, $6, $5 movn $6, $5, $1 addu $3, $3, $7 xori $1, $7, 1 subu $2, $2, $1 slt $1, $3, $2 bnez $1, .LBB0_2 move $5, $6 .LBB0_3: sll $2, $6, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
330
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O2
mips64 clang 21.1.0
maxArea: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp slti $1, $5, 2 bnez $1, .LBB0_3 addiu $6, $zero, 0 addiu $2, $5, -1 addiu $3, $zero, 0 addiu $5, $zero, 0 .LBB0_2: sll $1, $2, 0 dsll $1, $1, 2 daddu $1, $4, $1 lw $1, 0($1) sll $6, $3, 0 dsll $6, $6, 2 daddu $6, $4, $6 lw $6, 0($6) slt $7, $6, $1 movn $1, $6, $7 subu $6, $2, $3 mul $6, $1, $6 slt $1, $6, $5 movn $6, $5, $1 addu $3, $3, $7 xori $1, $7, 1 subu $2, $2, $1 slt $1, $3, $2 bnez $1, .LBB0_2 move $5, $6 .LBB0_3: sll $2, $6, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
331
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O3
mips64 clang 21.1.0
maxArea: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp slti $1, $5, 2 bnez $1, .LBB0_3 addiu $6, $zero, 0 addiu $2, $5, -1 addiu $3, $zero, 0 addiu $5, $zero, 0 .LBB0_2: sll $1, $2, 0 sll $6, $3, 0 dsll $1, $1, 2 dsll $6, $6, 2 daddu $1, $4, $1 daddu $6, $4, $6 lw $1, 0($1) lw $6, 0($6) slt $7, $6, $1 movn $1, $6, $7 subu $6, $2, $3 addu $3, $3, $7 mul $6, $1, $6 slt $1, $6, $5 movn $6, $5, $1 xori $1, $7, 1 subu $2, $2, $1 slt $1, $3, $2 bnez $1, .LBB0_2 move $5, $6 .LBB0_3: sll $2, $6, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16
332
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O0
mips64 gcc 15.2.0
maxArea: daddiu $sp,$sp,-64 sd $fp,56($sp) move $fp,$sp sd $4,32($fp) move $2,$5 sll $2,$2,0 sw $2,40($fp) sw $0,0($fp) sw $0,4($fp) lw $2,40($fp) addiu $2,$2,-1 sw $2,8($fp) b .L2 nop .L6: lw $2,4($fp) dsll $2,$2,2 ld $3,32($fp) daddu $2,$3,$2 lw $2,0($2) sw $2,12($fp) lw $2,8($fp) dsll $2,$2,2 ld $3,32($fp) daddu $2,$3,$2 lw $2,0($2) sw $2,16($fp) lw $3,12($fp) lw $2,16($fp) slt $4,$3,$2 beq $4,$0,.L3 nop move $2,$3 .L3: sw $2,20($fp) lw $3,8($fp) lw $2,4($fp) subu $2,$3,$2 lw $3,20($fp) mult $3,$2 mflo $2 sw $2,24($fp) lw $3,0($fp) lw $2,24($fp) slt $2,$3,$2 beq $2,$0,.L4 nop lw $2,24($fp) sw $2,0($fp) .L4: lw $3,12($fp) lw $2,16($fp) slt $2,$3,$2 beq $2,$0,.L5 nop lw $2,4($fp) addiu $2,$2,1 sw $2,4($fp) b .L2 nop .L5: lw $2,8($fp) addiu $2,$2,-1 sw $2,8($fp) .L2: lw $3,4($fp) lw $2,8($fp) slt $2,$3,$2 bne $2,$0,.L6 nop lw $2,0($fp) move $sp,$fp ld $fp,56($sp) daddiu $sp,$sp,64 jr $31 nop
333
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O1
mips64 gcc 15.2.0
maxArea: addiu $5,$5,-1 blez $5,.L8 move $7,$0 b .L7 move $2,$0 .L5: addiu $5,$5,-1 .L6: slt $3,$7,$5 beq $3,$0,.L12 nop .L7: dsll $3,$7,2 daddu $3,$4,$3 lw $3,0($3) dsll $6,$5,2 daddu $6,$4,$6 lw $8,0($6) subu $9,$5,$7 slt $10,$8,$3 beq $10,$0,.L3 move $6,$3 move $6,$8 .L3: mult $9,$6 mflo $6 move $9,$6 slt $6,$6,$2 bnel $6,$0,.L4 move $9,$2 .L4: slt $3,$3,$8 beq $3,$0,.L5 move $2,$9 b .L6 addiu $7,$7,1 .L12: jr $31 nop .L8: jr $31 move $2,$0
334
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O2
mips64 gcc 15.2.0
maxArea: addiu $5,$5,-1 blez $5,.L8 move $7,$0 b .L7 move $2,$0 .L13: addiu $7,$7,1 slt $3,$7,$5 beq $3,$0,.L15 nop .L7: dsll $6,$7,32 .L14: dsll $3,$5,32 dsrl $6,$6,32 dsrl $3,$3,32 dsll $6,$6,2 dsll $3,$3,2 daddu $6,$4,$6 daddu $3,$4,$3 lw $8,0($6) lw $3,0($3) slt $6,$8,$3 beq $6,$0,.L3 subu $9,$5,$7 move $3,$8 .L3: mult $9,$3 mflo $3 slt $8,$3,$2 bnel $8,$0,.L4 move $3,$2 .L4: bne $6,$0,.L13 move $2,$3 addiu $5,$5,-1 slt $3,$7,$5 bne $3,$0,.L14 dsll $6,$7,32 .L15: jr $31 nop .L8: jr $31 move $2,$0
335
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
mips64
-O3
mips64 gcc 15.2.0
maxArea: addiu $5,$5,-1 blez $5,.L8 move $7,$0 b .L7 move $2,$0 .L13: addiu $7,$7,1 slt $3,$7,$5 beq $3,$0,.L15 nop .L7: dsll $6,$7,32 .L14: dsll $3,$5,32 dsrl $6,$6,32 dsrl $3,$3,32 dsll $6,$6,2 dsll $3,$3,2 daddu $6,$4,$6 daddu $3,$4,$3 lw $8,0($6) lw $3,0($3) slt $6,$8,$3 beq $6,$0,.L3 subu $9,$5,$7 move $3,$8 .L3: mult $9,$3 mflo $3 slt $8,$3,$2 bnel $8,$0,.L4 move $3,$2 .L4: bne $6,$0,.L13 move $2,$3 addiu $5,$5,-1 slt $3,$7,$5 bne $3,$0,.L14 dsll $6,$7,32 .L15: jr $31 nop .L8: jr $31 move $2,$0
336
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O0
RISC-V 64 clang 21.1.0
maxArea: addi sp, sp, -64 sd ra, 56(sp) sd s0, 48(sp) addi s0, sp, 64 sd a0, -24(s0) sw a1, -28(s0) li a0, 0 sw a0, -48(s0) sw a0, -52(s0) lw a0, -28(s0) addiw a0, a0, -1 sw a0, -56(s0) j .LBB0_1 .LBB0_1: lw a0, -52(s0) lw a1, -56(s0) bge a0, a1, .LBB0_11 j .LBB0_2 .LBB0_2: ld a0, -24(s0) lw a1, -52(s0) slli a1, a1, 2 add a0, a0, a1 lw a0, 0(a0) sw a0, -32(s0) ld a0, -24(s0) lw a1, -56(s0) slli a1, a1, 2 add a0, a0, a1 lw a0, 0(a0) sw a0, -36(s0) lw a0, -32(s0) lw a1, -36(s0) bge a0, a1, .LBB0_4 j .LBB0_3 .LBB0_3: lw a0, -32(s0) sd a0, -64(s0) j .LBB0_5 .LBB0_4: lw a0, -36(s0) sd a0, -64(s0) j .LBB0_5 .LBB0_5: ld a0, -64(s0) sw a0, -40(s0) lw a0, -40(s0) lw a1, -56(s0) lw a2, -52(s0) subw a1, a1, a2 mulw a0, a0, a1 sw a0, -44(s0) lw a0, -48(s0) lw a1, -44(s0) bge a0, a1, .LBB0_7 j .LBB0_6 .LBB0_6: lw a0, -44(s0) sw a0, -48(s0) j .LBB0_7 .LBB0_7: lw a0, -32(s0) lw a1, -36(s0) bge a0, a1, .LBB0_9 j .LBB0_8 .LBB0_8: lw a0, -52(s0) addiw a0, a0, 1 sw a0, -52(s0) j .LBB0_10 .LBB0_9: lw a0, -56(s0) addiw a0, a0, -1 sw a0, -56(s0) j .LBB0_10 .LBB0_10: j .LBB0_1 .LBB0_11: lw a0, -48(s0) ld ra, 56(sp) ld s0, 48(sp) addi sp, sp, 64 ret
337
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O1
RISC-V 64 clang 21.1.0
maxArea: li a2, 2 blt a1, a2, .LBB0_7 li a3, 0 li a6, 0 addiw a4, a1, -1 j .LBB0_3 .LBB0_2: slt a1, a2, a5 xori a2, a1, 1 addw a3, a3, a1 subw a4, a4, a2 bge a3, a4, .LBB0_8 .LBB0_3: slli a2, a3, 2 slli a5, a4, 2 add a2, a2, a0 add a5, a5, a0 lw a2, 0(a2) lw a5, 0(a5) mv a7, a2 blt a2, a5, .LBB0_5 mv a7, a5 .LBB0_5: subw a1, a4, a3 mulw a1, a7, a1 blt a1, a6, .LBB0_2 mv a6, a1 j .LBB0_2 .LBB0_7: li a0, 0 ret .LBB0_8: mv a0, a6 ret
338
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O2
RISC-V 64 clang 21.1.0
maxArea: li a2, 2 blt a1, a2, .LBB0_7 li a3, 0 li a6, 0 addiw a4, a1, -1 j .LBB0_3 .LBB0_2: slt a1, a2, a5 xori a2, a1, 1 addw a3, a3, a1 subw a4, a4, a2 bge a3, a4, .LBB0_8 .LBB0_3: slli a2, a3, 2 slli a5, a4, 2 add a2, a2, a0 add a5, a5, a0 lw a2, 0(a2) lw a5, 0(a5) mv a7, a2 blt a2, a5, .LBB0_5 mv a7, a5 .LBB0_5: subw a1, a4, a3 mulw a1, a7, a1 blt a1, a6, .LBB0_2 mv a6, a1 j .LBB0_2 .LBB0_7: li a0, 0 ret .LBB0_8: mv a0, a6 ret
339
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O3
RISC-V 64 clang 21.1.0
maxArea: li a2, 2 blt a1, a2, .LBB0_7 li a3, 0 li a6, 0 addiw a4, a1, -1 j .LBB0_3 .LBB0_2: slt a1, a2, a5 xori a2, a1, 1 addw a3, a3, a1 subw a4, a4, a2 bge a3, a4, .LBB0_8 .LBB0_3: slli a2, a3, 2 slli a5, a4, 2 add a2, a2, a0 add a5, a5, a0 lw a2, 0(a2) lw a5, 0(a5) mv a7, a2 bge a2, a5, .LBB0_5 subw a1, a4, a3 mulw a1, a7, a1 blt a1, a6, .LBB0_2 j .LBB0_6 .LBB0_5: subw a1, a4, a3 mulw a1, a5, a1 blt a1, a6, .LBB0_2 .LBB0_6: mv a6, a1 j .LBB0_2 .LBB0_7: li a0, 0 ret .LBB0_8: mv a0, a6 ret
340
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O0
RISC-V 64 gcc 15.2.0
maxArea: addi sp,sp,-64 sd ra,56(sp) sd s0,48(sp) addi s0,sp,64 sd a0,-56(s0) mv a5,a1 sw a5,-60(s0) sw zero,-20(s0) sw zero,-24(s0) lw a5,-60(s0) addiw a5,a5,-1 sw a5,-28(s0) j .L2 .L6: lw a5,-24(s0) slli a5,a5,2 ld a4,-56(s0) add a5,a4,a5 lw a5,0(a5) sw a5,-32(s0) lw a5,-28(s0) slli a5,a5,2 ld a4,-56(s0) add a5,a4,a5 lw a5,0(a5) sw a5,-36(s0) lw a5,-32(s0) mv a2,a5 lw a5,-36(s0) sext.w a3,a5 sext.w a4,a2 ble a3,a4,.L3 mv a5,a2 .L3: sw a5,-40(s0) lw a5,-28(s0) mv a4,a5 lw a5,-24(s0) subw a5,a4,a5 sext.w a5,a5 lw a4,-40(s0) mulw a5,a4,a5 sw a5,-44(s0) lw a5,-20(s0) mv a4,a5 lw a5,-44(s0) sext.w a4,a4 sext.w a5,a5 bge a4,a5,.L4 lw a5,-44(s0) sw a5,-20(s0) .L4: lw a5,-32(s0) mv a4,a5 lw a5,-36(s0) sext.w a4,a4 sext.w a5,a5 bge a4,a5,.L5 lw a5,-24(s0) addiw a5,a5,1 sw a5,-24(s0) j .L2 .L5: lw a5,-28(s0) addiw a5,a5,-1 sw a5,-28(s0) .L2: lw a5,-24(s0) mv a4,a5 lw a5,-28(s0) sext.w a4,a4 sext.w a5,a5 blt a4,a5,.L6 lw a5,-20(s0) mv a0,a5 ld ra,56(sp) ld s0,48(sp) addi sp,sp,64 jr ra
341
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O1
RISC-V 64 gcc 15.2.0
maxArea: mv a7,a0 addiw a1,a1,-1 ble a1,zero,.L8 li a4,0 li a0,0 j .L7 .L5: addiw a1,a1,-1 .L6: ble a1,a4,.L10 .L7: slli a5,a4,2 add a5,a7,a5 lw a3,0(a5) slli a5,a1,2 add a5,a7,a5 lw a2,0(a5) subw a6,a1,a4 mv a5,a3 ble a3,a2,.L3 mv a5,a2 .L3: mulw a5,a5,a6 mv a6,a5 bge a5,a0,.L4 mv a6,a0 .L4: sext.w a0,a6 bge a3,a2,.L5 addiw a4,a4,1 j .L6 .L10: ret .L8: li a0,0 ret
342
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O2
RISC-V 64 gcc 15.2.0
maxArea: addiw a1,a1,-1 mv a7,a0 ble a1,zero,.L8 li a3,0 li a0,0 .L7: slli a4,a1,2 slli a5,a3,2 add a5,a7,a5 add a4,a7,a4 lw a4,0(a4) lw a2,0(a5) subw a6,a1,a3 mv a5,a4 ble a4,a2,.L3 mv a5,a2 .L3: mulw a5,a5,a6 mv a6,a5 bge a5,a0,.L4 mv a6,a0 .L4: sext.w a0,a6 ble a4,a2,.L5 addiw a3,a3,1 blt a3,a1,.L7 ret .L5: addiw a1,a1,-1 blt a3,a1,.L7 ret .L8: li a0,0 ret
343
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
riscv64
-O3
RISC-V 64 gcc 15.2.0
maxArea: addiw a1,a1,-1 mv a7,a0 ble a1,zero,.L8 li a3,0 li a0,0 .L7: slli a4,a1,2 slli a5,a3,2 add a5,a7,a5 add a4,a7,a4 lw a4,0(a4) lw a2,0(a5) subw a6,a1,a3 mv a5,a4 ble a4,a2,.L3 mv a5,a2 .L3: mulw a5,a5,a6 mv a6,a5 bge a5,a0,.L4 mv a6,a0 .L4: sext.w a0,a6 ble a4,a2,.L5 addiw a3,a3,1 bgt a1,a3,.L7 ret .L5: addiw a1,a1,-1 bgt a1,a3,.L7 ret .L8: li a0,0 ret
344
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O0
x86-64 clang 21.1.0
maxArea: push rbp mov rbp, rsp mov qword ptr [rbp - 8], rdi mov dword ptr [rbp - 12], esi mov dword ptr [rbp - 32], 0 mov dword ptr [rbp - 36], 0 mov eax, dword ptr [rbp - 12] sub eax, 1 mov dword ptr [rbp - 40], eax .LBB0_1: mov eax, dword ptr [rbp - 36] cmp eax, dword ptr [rbp - 40] jge .LBB0_11 mov rax, qword ptr [rbp - 8] movsxd rcx, dword ptr [rbp - 36] mov eax, dword ptr [rax + 4*rcx] mov dword ptr [rbp - 16], eax mov rax, qword ptr [rbp - 8] movsxd rcx, dword ptr [rbp - 40] mov eax, dword ptr [rax + 4*rcx] mov dword ptr [rbp - 20], eax mov eax, dword ptr [rbp - 16] cmp eax, dword ptr [rbp - 20] jge .LBB0_4 mov eax, dword ptr [rbp - 16] mov dword ptr [rbp - 44], eax jmp .LBB0_5 .LBB0_4: mov eax, dword ptr [rbp - 20] mov dword ptr [rbp - 44], eax .LBB0_5: mov eax, dword ptr [rbp - 44] mov dword ptr [rbp - 24], eax mov eax, dword ptr [rbp - 24] mov ecx, dword ptr [rbp - 40] sub ecx, dword ptr [rbp - 36] imul eax, ecx mov dword ptr [rbp - 28], eax mov eax, dword ptr [rbp - 32] cmp eax, dword ptr [rbp - 28] jge .LBB0_7 mov eax, dword ptr [rbp - 28] mov dword ptr [rbp - 32], eax .LBB0_7: mov eax, dword ptr [rbp - 16] cmp eax, dword ptr [rbp - 20] jge .LBB0_9 mov eax, dword ptr [rbp - 36] add eax, 1 mov dword ptr [rbp - 36], eax jmp .LBB0_10 .LBB0_9: mov eax, dword ptr [rbp - 40] add eax, -1 mov dword ptr [rbp - 40], eax .LBB0_10: jmp .LBB0_1 .LBB0_11: mov eax, dword ptr [rbp - 32] pop rbp ret
345
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O1
x86-64 clang 21.1.0
maxArea: xor eax, eax cmp esi, 2 jl .LBB0_3 dec esi xor ecx, ecx xor eax, eax .LBB0_2: mov edx, ecx mov edx, dword ptr [rdi + 4*rdx] movsxd rsi, esi mov r8d, dword ptr [rdi + 4*rsi] xor r9d, r9d xor r10d, r10d cmp edx, r8d setge r9b setl r10b cmovl r8d, edx mov edx, esi sub edx, ecx imul edx, r8d cmp eax, edx cmovle eax, edx add ecx, r10d sub esi, r9d cmp ecx, esi jl .LBB0_2 .LBB0_3: ret
346
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O2
x86-64 clang 21.1.0
maxArea: xor eax, eax cmp esi, 2 jl .LBB0_3 dec esi xor ecx, ecx xor eax, eax .LBB0_2: mov edx, ecx mov edx, dword ptr [rdi + 4*rdx] movsxd rsi, esi mov r8d, dword ptr [rdi + 4*rsi] xor r9d, r9d xor r10d, r10d cmp edx, r8d setge r9b setl r10b cmovl r8d, edx mov edx, esi sub edx, ecx imul edx, r8d cmp eax, edx cmovle eax, edx add ecx, r10d sub esi, r9d cmp ecx, esi jl .LBB0_2 .LBB0_3: ret
347
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O3
x86-64 clang 21.1.0
maxArea: xor eax, eax cmp esi, 2 jl .LBB0_3 dec esi xor ecx, ecx xor eax, eax .LBB0_2: mov edx, ecx mov edx, dword ptr [rdi + 4*rdx] movsxd rsi, esi mov r8d, dword ptr [rdi + 4*rsi] xor r9d, r9d xor r10d, r10d cmp edx, r8d setge r9b setl r10b cmovl r8d, edx mov edx, esi sub edx, ecx imul edx, r8d cmp eax, edx cmovle eax, edx add ecx, r10d sub esi, r9d cmp ecx, esi jl .LBB0_2 .LBB0_3: ret
348
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O0
x86-64 gcc 15.2
maxArea: push rbp mov rbp, rsp mov QWORD PTR [rbp-40], rdi mov DWORD PTR [rbp-44], esi mov DWORD PTR [rbp-4], 0 mov DWORD PTR [rbp-8], 0 mov eax, DWORD PTR [rbp-44] sub eax, 1 mov DWORD PTR [rbp-12], eax jmp .L2 .L5: mov eax, DWORD PTR [rbp-8] cdqe lea rdx, [0+rax*4] mov rax, QWORD PTR [rbp-40] add rax, rdx mov eax, DWORD PTR [rax] mov DWORD PTR [rbp-16], eax 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] mov DWORD PTR [rbp-20], eax mov edx, DWORD PTR [rbp-20] mov eax, DWORD PTR [rbp-16] cmp edx, eax cmovle eax, edx mov DWORD PTR [rbp-24], eax mov eax, DWORD PTR [rbp-12] sub eax, DWORD PTR [rbp-8] mov edx, DWORD PTR [rbp-24] imul eax, edx mov DWORD PTR [rbp-28], eax mov eax, DWORD PTR [rbp-4] cmp eax, DWORD PTR [rbp-28] jge .L3 mov eax, DWORD PTR [rbp-28] mov DWORD PTR [rbp-4], eax .L3: mov eax, DWORD PTR [rbp-16] cmp eax, DWORD PTR [rbp-20] jge .L4 add DWORD PTR [rbp-8], 1 jmp .L2 .L4: sub DWORD PTR [rbp-12], 1 .L2: mov eax, DWORD PTR [rbp-8] cmp eax, DWORD PTR [rbp-12] jl .L5 mov eax, DWORD PTR [rbp-4] pop rbp ret
349
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O1
x86-64 gcc 15.2
maxArea: sub esi, 1 test esi, esi jle .L6 mov edx, 0 mov r9d, 0 jmp .L5 .L3: sub esi, 1 .L4: cmp esi, edx jle .L1 .L5: movsx rax, edx mov r8d, DWORD PTR [rdi+rax*4] movsx rax, esi mov ecx, DWORD PTR [rdi+rax*4] mov eax, esi sub eax, edx cmp r8d, ecx mov r10d, ecx cmovle r10d, r8d imul eax, r10d cmp r9d, eax cmovl r9d, eax cmp r8d, ecx jge .L3 add edx, 1 jmp .L4 .L6: mov r9d, 0 .L1: mov eax, r9d ret
350
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O2
x86-64 gcc 15.2
maxArea: sub esi, 1 mov r9, rdi test esi, esi jle .L6 xor edx, edx xor r8d, r8d jmp .L5 .L9: add edx, 1 cmp edx, esi jge .L1 .L5: movsx rax, edx mov ecx, DWORD PTR [r9+rax*4] movsx rax, esi mov edi, DWORD PTR [r9+rax*4] mov eax, esi sub eax, edx mov r10d, ecx cmp edi, ecx cmovle r10d, edi imul eax, r10d cmp r8d, eax cmovl r8d, eax cmp edi, ecx jg .L9 sub esi, 1 cmp edx, esi jl .L5 .L1: mov eax, r8d ret .L6: xor r8d, r8d mov eax, r8d ret
351
11
Container With Most Water
Medium
/* 11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. */ int maxArea(int* height, int heightSize) { int l, r, x, water, max = 0; int i, j; #if 0 for (i = 0; i < heightSize - 1; i ++) { if (i == 0 || height[i] > l) { l = height[i]; for (j = i + 1; j < heightSize; j ++) { r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; } } } #else i = 0; j = heightSize - 1; while (i < j) { l = height[i]; r = height[j]; x = l < r ? l : r; water = x * (j - i); if (max < water) max = water; if (l < r) i ++; else j --; } #endif return max; } /* Difficulty:Medium Total Accepted:144.5K Total Submissions:395.2K Companies Bloomberg Related Topics Array Two Pointers Similar Questions Trapping Rain Water */
x86-64
-O3
x86-64 gcc 15.2
maxArea: sub esi, 1 mov r9, rdi test esi, esi jle .L6 xor edx, edx xor r8d, r8d jmp .L5 .L9: add edx, 1 cmp esi, edx jle .L1 .L5: movsx rax, edx mov ecx, DWORD PTR [r9+rax*4] movsx rax, esi mov edi, DWORD PTR [r9+rax*4] mov eax, esi sub eax, edx mov r10d, ecx cmp edi, ecx cmovle r10d, edi imul eax, r10d cmp r8d, eax cmovl r8d, eax cmp edi, ecx jg .L9 sub esi, 1 cmp esi, edx jg .L5 .L1: mov eax, r8d ret .L6: xor r8d, r8d mov eax, r8d ret
352
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O0
ARM64 gcc 15.2.0
.LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" map: .xword .LC0 .word 1 .zero 4 .xword .LC1 .word 4 .zero 4 .xword .LC2 .word 5 .zero 4 .xword .LC3 .word 9 .zero 4 .xword .LC4 .word 10 .zero 4 .xword .LC5 .word 40 .zero 4 .xword .LC6 .word 50 .zero 4 .xword .LC7 .word 90 .zero 4 .xword .LC8 .word 100 .zero 4 .xword .LC9 .word 400 .zero 4 .xword .LC10 .word 500 .zero 4 .xword .LC11 .word 900 .zero 4 .xword .LC12 .word 1000 .zero 4 intToRoman: stp x29, x30, [sp, -48]! mov x29, sp str w0, [sp, 28] mov w0, 12 str w0, [sp, 40] mov x0, 1024 bl malloc str x0, [sp, 32] ldr x0, [sp, 32] strb wzr, [x0] b .L2 .L5: adrp x0, map add x1, x0, :lo12:map ldrsw x0, [sp, 40] lsl x0, x0, 4 add x0, x1, x0 ldr w0, [x0, 8] ldr w1, [sp, 28] sdiv w0, w1, w0 str w0, [sp, 44] b .L3 .L4: adrp x0, map add x1, x0, :lo12:map ldrsw x0, [sp, 40] lsl x0, x0, 4 add x0, x1, x0 ldr x0, [x0] mov x1, x0 ldr x0, [sp, 32] bl strcat ldr w0, [sp, 44] sub w0, w0, #1 str w0, [sp, 44] .L3: ldr w0, [sp, 44] cmp w0, 0 bne .L4 adrp x0, map add x1, x0, :lo12:map ldrsw x0, [sp, 40] lsl x0, x0, 4 add x0, x1, x0 ldr w1, [x0, 8] ldr w0, [sp, 28] sdiv w2, w0, w1 mul w1, w2, w1 sub w0, w0, w1 str w0, [sp, 28] ldr w0, [sp, 40] sub w0, w0, #1 str w0, [sp, 40] .L2: ldr w0, [sp, 28] cmp w0, 0 bne .L5 ldr x0, [sp, 32] ldp x29, x30, [sp], 48 ret
353
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O1
ARM64 gcc 15.2.0
intToRoman: stp x29, x30, [sp, -64]! mov x29, sp stp x21, x22, [sp, 32] mov w22, w0 mov x0, 1024 bl malloc mov x21, x0 strb wzr, [x0] cbz w22, .L1 stp x19, x20, [sp, 16] stp x23, x24, [sp, 48] adrp x23, .LANCHOR0 add x23, x23, :lo12:.LANCHOR0 .L5: mov x20, x23 ldr w24, [x23, 200] sdiv w19, w22, w24 cbz w19, .L3 .L4: ldr x1, [x20, 192] mov x0, x21 bl strcat subs w19, w19, #1 bne .L4 .L3: sdiv w0, w22, w24 msub w22, w0, w24, w22 sub x23, x23, #16 cbnz w22, .L5 ldp x19, x20, [sp, 16] ldp x23, x24, [sp, 48] .L1: mov x0, x21 ldp x21, x22, [sp, 32] ldp x29, x30, [sp], 64 ret .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" .set .LANCHOR0,. + 0 map: .xword .LC0 .word 1 .zero 4 .xword .LC1 .word 4 .zero 4 .xword .LC2 .word 5 .zero 4 .xword .LC3 .word 9 .zero 4 .xword .LC4 .word 10 .zero 4 .xword .LC5 .word 40 .zero 4 .xword .LC6 .word 50 .zero 4 .xword .LC7 .word 90 .zero 4 .xword .LC8 .word 100 .zero 4 .xword .LC9 .word 400 .zero 4 .xword .LC10 .word 500 .zero 4 .xword .LC11 .word 900 .zero 4 .xword .LC12 .word 1000 .zero 4
354
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O2
ARM64 gcc 15.2.0
intToRoman: stp x29, x30, [sp, -64]! mov x29, sp stp x21, x22, [sp, 32] mov w21, w0 mov x0, 1024 bl malloc strb wzr, [x0] mov x2, x0 cbz w21, .L1 adrp x22, .LANCHOR0 add x22, x22, :lo12:.LANCHOR0 stp x19, x20, [sp, 16] stp x23, x24, [sp, 48] .L4: ldr w24, [x22, 200] sdiv w23, w21, w24 mov w19, w23 cbz w23, .L6 ldr x20, [x22, 192] .L5: mov x0, x2 mov x1, x20 bl strcat subs w19, w19, #1 mov x2, x0 bne .L5 .L6: msub w21, w23, w24, w21 sub x22, x22, #16 cbnz w21, .L4 ldp x19, x20, [sp, 16] ldp x23, x24, [sp, 48] .L1: mov x0, x2 ldp x21, x22, [sp, 32] ldp x29, x30, [sp], 64 ret .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" .set .LANCHOR0,. + 0 map: .xword .LC0 .word 1 .zero 4 .xword .LC1 .word 4 .zero 4 .xword .LC2 .word 5 .zero 4 .xword .LC3 .word 9 .zero 4 .xword .LC4 .word 10 .zero 4 .xword .LC5 .word 40 .zero 4 .xword .LC6 .word 50 .zero 4 .xword .LC7 .word 90 .zero 4 .xword .LC8 .word 100 .zero 4 .xword .LC9 .word 400 .zero 4 .xword .LC10 .word 500 .zero 4 .xword .LC11 .word 900 .zero 4 .xword .LC12 .word 1000 .zero 4
355
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O3
ARM64 gcc 15.2.0
intToRoman: stp x29, x30, [sp, -64]! mov x29, sp stp x21, x22, [sp, 32] mov w21, w0 mov x0, 1024 bl malloc strb wzr, [x0] mov x2, x0 cbz w21, .L1 adrp x22, .LANCHOR0 add x22, x22, :lo12:.LANCHOR0 stp x19, x20, [sp, 16] stp x23, x24, [sp, 48] .L4: ldr w24, [x22, 200] sdiv w23, w21, w24 mov w19, w23 cbz w23, .L6 ldr x20, [x22, 192] .L5: mov x0, x2 mov x1, x20 bl strcat subs w19, w19, #1 mov x2, x0 bne .L5 .L6: msub w21, w23, w24, w21 sub x22, x22, #16 cbnz w21, .L4 ldp x19, x20, [sp, 16] ldp x23, x24, [sp, 48] .L1: mov x0, x2 ldp x21, x22, [sp, 32] ldp x29, x30, [sp], 64 ret .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" .set .LANCHOR0,. + 0 map: .xword .LC0 .word 1 .zero 4 .xword .LC1 .word 4 .zero 4 .xword .LC2 .word 5 .zero 4 .xword .LC3 .word 9 .zero 4 .xword .LC4 .word 10 .zero 4 .xword .LC5 .word 40 .zero 4 .xword .LC6 .word 50 .zero 4 .xword .LC7 .word 90 .zero 4 .xword .LC8 .word 100 .zero 4 .xword .LC9 .word 400 .zero 4 .xword .LC10 .word 500 .zero 4 .xword .LC11 .word 900 .zero 4 .xword .LC12 .word 1000 .zero 4
356
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O0
armv8-a clang 21.1.0
intToRoman: sub sp, sp, #48 stp x29, x30, [sp, #32] add x29, sp, #32 stur w0, [x29, #-4] mov w8, #12 stur w8, [x29, #-12] mov x0, #1024 bl malloc str x0, [sp, #8] ldr x8, [sp, #8] strb wzr, [x8] b .LBB0_1 .LBB0_1: ldur w8, [x29, #-4] cbz w8, .LBB0_6 b .LBB0_2 .LBB0_2: ldur w8, [x29, #-4] ldursw x10, [x29, #-12] adrp x9, map add x9, x9, :lo12:map add x9, x9, x10, lsl #4 ldr w9, [x9, #8] sdiv w8, w8, w9 stur w8, [x29, #-8] b .LBB0_3 .LBB0_3: ldur w8, [x29, #-8] cbz w8, .LBB0_5 b .LBB0_4 .LBB0_4: ldr x0, [sp, #8] ldursw x8, [x29, #-12] lsl x9, x8, #4 adrp x8, map add x8, x8, :lo12:map ldr x1, [x8, x9] bl strcat ldur w8, [x29, #-8] subs w8, w8, #1 stur w8, [x29, #-8] b .LBB0_3 .LBB0_5: ldur w8, [x29, #-4] ldursw x10, [x29, #-12] adrp x9, map add x9, x9, :lo12:map add x9, x9, x10, lsl #4 ldr w10, [x9, #8] sdiv w9, w8, w10 mul w9, w9, w10 subs w8, w8, w9 stur w8, [x29, #-4] ldur w8, [x29, #-12] subs w8, w8, #1 stur w8, [x29, #-12] b .LBB0_1 .LBB0_6: ldr x0, [sp, #8] ldp x29, x30, [sp, #32] add sp, sp, #48 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .xword .L.str .word 1 .zero 4 .xword .L.str.1 .word 4 .zero 4 .xword .L.str.2 .word 5 .zero 4 .xword .L.str.3 .word 9 .zero 4 .xword .L.str.4 .word 10 .zero 4 .xword .L.str.5 .word 40 .zero 4 .xword .L.str.6 .word 50 .zero 4 .xword .L.str.7 .word 90 .zero 4 .xword .L.str.8 .word 100 .zero 4 .xword .L.str.9 .word 400 .zero 4 .xword .L.str.10 .word 500 .zero 4 .xword .L.str.11 .word 900 .zero 4 .xword .L.str.12 .word 1000 .zero 4
357
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O1
armv8-a clang 21.1.0
intToRoman: stp x29, x30, [sp, #-80]! str x25, [sp, #16] stp x24, x23, [sp, #32] stp x22, x21, [sp, #48] stp x20, x19, [sp, #64] mov x29, sp mov w19, w0 mov w0, #1024 bl malloc strb wzr, [x0] cbz w19, .LBB0_6 mov w21, #12 adrp x22, map add x22, x22, :lo12:map b .LBB0_3 .LBB0_2: msub w19, w24, w23, w19 sub x21, x21, #1 cbz w19, .LBB0_6 .LBB0_3: add x8, x22, x21, lsl #4 ldr w23, [x8, #8] sdiv w24, w19, w23 cbz w24, .LBB0_2 ldr x20, [x8] mov w25, w24 .LBB0_5: mov x1, x20 bl strcat subs w25, w25, #1 b.ne .LBB0_5 b .LBB0_2 .LBB0_6: ldp x20, x19, [sp, #64] ldr x25, [sp, #16] ldp x22, x21, [sp, #48] ldp x24, x23, [sp, #32] ldp x29, x30, [sp], #80 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .xword .L.str .word 1 .zero 4 .xword .L.str.1 .word 4 .zero 4 .xword .L.str.2 .word 5 .zero 4 .xword .L.str.3 .word 9 .zero 4 .xword .L.str.4 .word 10 .zero 4 .xword .L.str.5 .word 40 .zero 4 .xword .L.str.6 .word 50 .zero 4 .xword .L.str.7 .word 90 .zero 4 .xword .L.str.8 .word 100 .zero 4 .xword .L.str.9 .word 400 .zero 4 .xword .L.str.10 .word 500 .zero 4 .xword .L.str.11 .word 900 .zero 4 .xword .L.str.12 .word 1000 .zero 4
358
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O2
armv8-a clang 21.1.0
intToRoman: stp x29, x30, [sp, #-80]! str x25, [sp, #16] stp x24, x23, [sp, #32] stp x22, x21, [sp, #48] stp x20, x19, [sp, #64] mov x29, sp mov w19, w0 mov w0, #1024 bl malloc strb wzr, [x0] cbz w19, .LBB0_6 mov w21, #12 adrp x22, map add x22, x22, :lo12:map b .LBB0_3 .LBB0_2: msub w19, w24, w23, w19 sub x21, x21, #1 cbz w19, .LBB0_6 .LBB0_3: add x8, x22, x21, lsl #4 ldr w23, [x8, #8] sdiv w24, w19, w23 cbz w24, .LBB0_2 ldr x20, [x8] mov w25, w24 .LBB0_5: mov x1, x20 bl strcat subs w25, w25, #1 b.ne .LBB0_5 b .LBB0_2 .LBB0_6: ldp x20, x19, [sp, #64] ldr x25, [sp, #16] ldp x22, x21, [sp, #48] ldp x24, x23, [sp, #32] ldp x29, x30, [sp], #80 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .xword .L.str .word 1 .zero 4 .xword .L.str.1 .word 4 .zero 4 .xword .L.str.2 .word 5 .zero 4 .xword .L.str.3 .word 9 .zero 4 .xword .L.str.4 .word 10 .zero 4 .xword .L.str.5 .word 40 .zero 4 .xword .L.str.6 .word 50 .zero 4 .xword .L.str.7 .word 90 .zero 4 .xword .L.str.8 .word 100 .zero 4 .xword .L.str.9 .word 400 .zero 4 .xword .L.str.10 .word 500 .zero 4 .xword .L.str.11 .word 900 .zero 4 .xword .L.str.12 .word 1000 .zero 4
359
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
aarch64
-O3
armv8-a clang 21.1.0
intToRoman: stp x29, x30, [sp, #-80]! str x25, [sp, #16] stp x24, x23, [sp, #32] stp x22, x21, [sp, #48] stp x20, x19, [sp, #64] mov x29, sp mov w19, w0 mov w0, #1024 bl malloc strb wzr, [x0] cbz w19, .LBB0_6 mov w21, #12 adrp x22, map add x22, x22, :lo12:map b .LBB0_3 .LBB0_2: msub w19, w24, w23, w19 sub x21, x21, #1 cbz w19, .LBB0_6 .LBB0_3: add x8, x22, x21, lsl #4 ldr w23, [x8, #8] sdiv w24, w19, w23 cbz w24, .LBB0_2 ldr x20, [x8] mov w25, w24 .LBB0_5: mov x1, x20 bl strcat subs w25, w25, #1 b.ne .LBB0_5 b .LBB0_2 .LBB0_6: ldp x20, x19, [sp, #64] ldr x25, [sp, #16] ldp x22, x21, [sp, #48] ldp x24, x23, [sp, #32] ldp x29, x30, [sp], #80 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .xword .L.str .word 1 .zero 4 .xword .L.str.1 .word 4 .zero 4 .xword .L.str.2 .word 5 .zero 4 .xword .L.str.3 .word 9 .zero 4 .xword .L.str.4 .word 10 .zero 4 .xword .L.str.5 .word 40 .zero 4 .xword .L.str.6 .word 50 .zero 4 .xword .L.str.7 .word 90 .zero 4 .xword .L.str.8 .word 100 .zero 4 .xword .L.str.9 .word 400 .zero 4 .xword .L.str.10 .word 500 .zero 4 .xword .L.str.11 .word 900 .zero 4 .xword .L.str.12 .word 1000 .zero 4
360
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O0
mips64 clang 21.1.0
intToRoman: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -64 sd $ra, 56($sp) sd $fp, 48($sp) sd $gp, 40($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(intToRoman))) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(intToRoman))) sd $gp, 8($fp) move $1, $4 sw $1, 36($fp) addiu $1, $zero, 12 sw $1, 28($fp) ld $25, %call16(malloc)($gp) daddiu $4, $zero, 1024 jalr $25 nop sd $2, 16($fp) ld $1, 16($fp) addiu $2, $zero, 0 sb $zero, 0($1) b .LBB0_1 nop .LBB0_1: lw $1, 36($fp) beqz $1, .LBB0_8 nop b .LBB0_3 nop .LBB0_3: ld $1, 8($fp) lw $2, 36($fp) lw $3, 28($fp) dsll $3, $3, 4 ld $1, %got_disp(map)($1) daddu $1, $1, $3 lw $1, 8($1) div $zero, $2, $1 teq $1, $zero, 7 mflo $1 sw $1, 32($fp) b .LBB0_4 nop .LBB0_4: lw $1, 32($fp) beqz $1, .LBB0_7 nop b .LBB0_6 nop .LBB0_6: ld $gp, 8($fp) ld $4, 16($fp) lw $1, 28($fp) dsll $2, $1, 4 ld $1, %got_disp(map)($gp) daddu $1, $1, $2 ld $5, 0($1) ld $25, %call16(strcat)($gp) jalr $25 nop lw $1, 32($fp) addiu $1, $1, -1 sw $1, 32($fp) b .LBB0_4 nop .LBB0_7: ld $1, 8($fp) lw $2, 36($fp) lw $3, 28($fp) dsll $3, $3, 4 ld $1, %got_disp(map)($1) daddu $1, $1, $3 lw $1, 8($1) div $zero, $2, $1 teq $1, $zero, 7 mfhi $1 sw $1, 36($fp) lw $1, 28($fp) addiu $1, $1, -1 sw $1, 28($fp) b .LBB0_1 nop .LBB0_8: ld $2, 16($fp) move $sp, $fp ld $gp, 40($sp) ld $fp, 48($sp) ld $ra, 56($sp) daddiu $sp, $sp, 64 jr $ra nop .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .8byte .L.str .4byte 1 .space 4 .8byte .L.str.1 .4byte 4 .space 4 .8byte .L.str.2 .4byte 5 .space 4 .8byte .L.str.3 .4byte 9 .space 4 .8byte .L.str.4 .4byte 10 .space 4 .8byte .L.str.5 .4byte 40 .space 4 .8byte .L.str.6 .4byte 50 .space 4 .8byte .L.str.7 .4byte 90 .space 4 .8byte .L.str.8 .4byte 100 .space 4 .8byte .L.str.9 .4byte 400 .space 4 .8byte .L.str.10 .4byte 500 .space 4 .8byte .L.str.11 .4byte 900 .space 4 .8byte .L.str.12 .4byte 1000 .space 4
361
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O1
mips64 clang 21.1.0
intToRoman: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -80 sd $ra, 72($sp) sd $fp, 64($sp) sd $gp, 56($sp) sd $21, 48($sp) sd $20, 40($sp) sd $19, 32($sp) sd $18, 24($sp) sd $17, 16($sp) sd $16, 8($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(intToRoman))) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(intToRoman))) move $16, $4 ld $25, %call16(malloc)($gp) jalr $25 daddiu $4, $zero, 1024 move $17, $2 beqz $16, .LBB0_7 sb $zero, 0($2) ld $20, %got_disp(map)($gp) b .LBB0_3 daddiu $19, $zero, 12 .LBB0_2: beqz $16, .LBB0_7 daddiu $19, $19, -1 .LBB0_3: dsll $1, $19, 4 daddu $2, $20, $1 lw $1, 8($2) div $zero, $16, $1 teq $1, $zero, 7 mflo $21 beqz $21, .LBB0_2 mfhi $16 ld $18, 0($2) .LBB0_5: ld $25, %call16(strcat)($gp) move $4, $17 jalr $25 move $5, $18 addiu $21, $21, -1 bnez $21, .LBB0_5 nop b .LBB0_2 nop .LBB0_7: move $2, $17 move $sp, $fp ld $16, 8($sp) ld $17, 16($sp) ld $18, 24($sp) ld $19, 32($sp) ld $20, 40($sp) ld $21, 48($sp) ld $gp, 56($sp) ld $fp, 64($sp) ld $ra, 72($sp) jr $ra daddiu $sp, $sp, 80 .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .8byte .L.str .4byte 1 .space 4 .8byte .L.str.1 .4byte 4 .space 4 .8byte .L.str.2 .4byte 5 .space 4 .8byte .L.str.3 .4byte 9 .space 4 .8byte .L.str.4 .4byte 10 .space 4 .8byte .L.str.5 .4byte 40 .space 4 .8byte .L.str.6 .4byte 50 .space 4 .8byte .L.str.7 .4byte 90 .space 4 .8byte .L.str.8 .4byte 100 .space 4 .8byte .L.str.9 .4byte 400 .space 4 .8byte .L.str.10 .4byte 500 .space 4 .8byte .L.str.11 .4byte 900 .space 4 .8byte .L.str.12 .4byte 1000 .space 4
362
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O2
mips64 clang 21.1.0
intToRoman: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -80 sd $ra, 72($sp) sd $fp, 64($sp) sd $gp, 56($sp) sd $21, 48($sp) sd $20, 40($sp) sd $19, 32($sp) sd $18, 24($sp) sd $17, 16($sp) sd $16, 8($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(intToRoman))) daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(intToRoman))) move $16, $4 ld $25, %call16(malloc)($gp) jalr $25 daddiu $4, $zero, 1024 move $17, $2 beqz $16, .LBB0_7 sb $zero, 0($2) ld $20, %got_disp(map)($gp) b .LBB0_3 daddiu $19, $zero, 12 .LBB0_2: beqz $16, .LBB0_7 daddiu $19, $19, -1 .LBB0_3: dsll $1, $19, 4 daddu $2, $20, $1 lw $1, 8($2) div $zero, $16, $1 teq $1, $zero, 7 mflo $21 beqz $21, .LBB0_2 mfhi $16 ld $18, 0($2) .LBB0_5: ld $25, %call16(strcat)($gp) move $4, $17 jalr $25 move $5, $18 addiu $21, $21, -1 bnez $21, .LBB0_5 nop b .LBB0_2 nop .LBB0_7: move $2, $17 move $sp, $fp ld $16, 8($sp) ld $17, 16($sp) ld $18, 24($sp) ld $19, 32($sp) ld $20, 40($sp) ld $21, 48($sp) ld $gp, 56($sp) ld $fp, 64($sp) ld $ra, 72($sp) jr $ra daddiu $sp, $sp, 80 .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .8byte .L.str .4byte 1 .space 4 .8byte .L.str.1 .4byte 4 .space 4 .8byte .L.str.2 .4byte 5 .space 4 .8byte .L.str.3 .4byte 9 .space 4 .8byte .L.str.4 .4byte 10 .space 4 .8byte .L.str.5 .4byte 40 .space 4 .8byte .L.str.6 .4byte 50 .space 4 .8byte .L.str.7 .4byte 90 .space 4 .8byte .L.str.8 .4byte 100 .space 4 .8byte .L.str.9 .4byte 400 .space 4 .8byte .L.str.10 .4byte 500 .space 4 .8byte .L.str.11 .4byte 900 .space 4 .8byte .L.str.12 .4byte 1000 .space 4
363
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O3
mips64 clang 21.1.0
intToRoman: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -80 sd $ra, 72($sp) sd $fp, 64($sp) sd $gp, 56($sp) sd $21, 48($sp) sd $20, 40($sp) sd $19, 32($sp) sd $18, 24($sp) sd $17, 16($sp) sd $16, 8($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(intToRoman))) move $16, $4 daddu $1, $1, $25 daddiu $gp, $1, %lo(%neg(%gp_rel(intToRoman))) ld $25, %call16(malloc)($gp) jalr $25 daddiu $4, $zero, 1024 move $17, $2 beqz $16, .LBB0_7 sb $zero, 0($2) ld $20, %got_disp(map)($gp) b .LBB0_3 daddiu $19, $zero, 12 .LBB0_2: beqz $16, .LBB0_7 daddiu $19, $19, -1 .LBB0_3: dsll $1, $19, 4 daddu $2, $20, $1 lw $1, 8($2) div $zero, $16, $1 teq $1, $zero, 7 mflo $21 beqz $21, .LBB0_2 mfhi $16 ld $18, 0($2) .LBB0_5: ld $25, %call16(strcat)($gp) move $4, $17 jalr $25 move $5, $18 addiu $21, $21, -1 bnez $21, .LBB0_5 nop b .LBB0_2 nop .LBB0_7: move $2, $17 move $sp, $fp ld $16, 8($sp) ld $17, 16($sp) ld $18, 24($sp) ld $19, 32($sp) ld $20, 40($sp) ld $21, 48($sp) ld $gp, 56($sp) ld $fp, 64($sp) ld $ra, 72($sp) jr $ra daddiu $sp, $sp, 80 .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .8byte .L.str .4byte 1 .space 4 .8byte .L.str.1 .4byte 4 .space 4 .8byte .L.str.2 .4byte 5 .space 4 .8byte .L.str.3 .4byte 9 .space 4 .8byte .L.str.4 .4byte 10 .space 4 .8byte .L.str.5 .4byte 40 .space 4 .8byte .L.str.6 .4byte 50 .space 4 .8byte .L.str.7 .4byte 90 .space 4 .8byte .L.str.8 .4byte 100 .space 4 .8byte .L.str.9 .4byte 400 .space 4 .8byte .L.str.10 .4byte 500 .space 4 .8byte .L.str.11 .4byte 900 .space 4 .8byte .L.str.12 .4byte 1000 .space 4
364
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O0
mips64 gcc 15.2.0
.LC0: .ascii "I\000" .LC1: .ascii "IV\000" .LC2: .ascii "V\000" .LC3: .ascii "IX\000" .LC4: .ascii "X\000" .LC5: .ascii "XL\000" .LC6: .ascii "L\000" .LC7: .ascii "XC\000" .LC8: .ascii "C\000" .LC9: .ascii "CD\000" .LC10: .ascii "D\000" .LC11: .ascii "CM\000" .LC12: .ascii "M\000" map: .dword .LC0 .word 1 .space 4 .dword .LC1 .word 4 .space 4 .dword .LC2 .word 5 .space 4 .dword .LC3 .word 9 .space 4 .dword .LC4 .word 10 .space 4 .dword .LC5 .word 40 .space 4 .dword .LC6 .word 50 .space 4 .dword .LC7 .word 90 .space 4 .dword .LC8 .word 100 .space 4 .dword .LC9 .word 400 .space 4 .dword .LC10 .word 500 .space 4 .dword .LC11 .word 900 .space 4 .dword .LC12 .word 1000 .space 4 intToRoman: daddiu $sp,$sp,-64 sd $31,56($sp) sd $fp,48($sp) sd $28,40($sp) move $fp,$sp lui $28,%hi(%neg(%gp_rel(intToRoman))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(intToRoman))) move $2,$4 sll $2,$2,0 sw $2,16($fp) li $2,12 # 0xc sw $2,4($fp) li $4,1024 # 0x400 ld $2,%call16(malloc)($28) mtlo $2 mflo $25 jalr $25 nop sd $2,8($fp) ld $2,8($fp) sb $0,0($2) b .L2 nop .L5: ld $3,%got_disp(map)($28) lw $2,4($fp) dsll $2,$2,4 daddu $2,$3,$2 lw $2,8($2) lw $3,16($fp) div $0,$3,$2 teq $2,$0,7 mfhi $2 mflo $2 sw $2,0($fp) b .L3 nop .L4: ld $3,%got_disp(map)($28) lw $2,4($fp) dsll $2,$2,4 daddu $2,$3,$2 ld $2,0($2) move $5,$2 ld $4,8($fp) ld $2,%call16(strcat)($28) mtlo $2 mflo $25 jalr $25 nop lw $2,0($fp) addiu $2,$2,-1 sw $2,0($fp) .L3: lw $2,0($fp) bne $2,$0,.L4 nop ld $3,%got_disp(map)($28) lw $2,4($fp) dsll $2,$2,4 daddu $2,$3,$2 lw $2,8($2) lw $3,16($fp) div $0,$3,$2 teq $2,$0,7 mfhi $2 sw $2,16($fp) lw $2,4($fp) addiu $2,$2,-1 sw $2,4($fp) .L2: lw $2,16($fp) bne $2,$0,.L5 nop ld $2,8($fp) move $sp,$fp ld $31,56($sp) ld $fp,48($sp) ld $28,40($sp) daddiu $sp,$sp,64 jr $31 nop
365
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O1
mips64 gcc 15.2.0
intToRoman: daddiu $sp,$sp,-64 sd $31,56($sp) sd $28,48($sp) sd $21,40($sp) sd $20,32($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) sd $16,0($sp) lui $28,%hi(%neg(%gp_rel(intToRoman))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(intToRoman))) move $20,$4 ld $25,%call16(malloc)($28) 1: jalr $25 li $4,1024 # 0x400 move $18,$2 beq $20,$0,.L1 sb $0,0($2) ld $21,%got_disp(map)($28) daddiu $21,$21,192 .L5: lw $19,8($21) div $0,$20,$19 teq $19,$0,7 mflo $16 beq $16,$0,.L3 move $17,$21 ld $5,0($17) .L9: ld $25,%call16(strcat)($28) 1: jalr $25 move $4,$18 addiu $16,$16,-1 bnel $16,$0,.L9 ld $5,0($17) .L3: div $0,$20,$19 teq $19,$0,7 mfhi $19 move $20,$19 bne $19,$0,.L5 daddiu $21,$21,-16 .L1: move $2,$18 ld $31,56($sp) ld $28,48($sp) ld $21,40($sp) ld $20,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) jr $31 daddiu $sp,$sp,64 .LC0: .ascii "I\000" .LC1: .ascii "IV\000" .LC2: .ascii "V\000" .LC3: .ascii "IX\000" .LC4: .ascii "X\000" .LC5: .ascii "XL\000" .LC6: .ascii "L\000" .LC7: .ascii "XC\000" .LC8: .ascii "C\000" .LC9: .ascii "CD\000" .LC10: .ascii "D\000" .LC11: .ascii "CM\000" .LC12: .ascii "M\000" map: .dword .LC0 .word 1 .space 4 .dword .LC1 .word 4 .space 4 .dword .LC2 .word 5 .space 4 .dword .LC3 .word 9 .space 4 .dword .LC4 .word 10 .space 4 .dword .LC5 .word 40 .space 4 .dword .LC6 .word 50 .space 4 .dword .LC7 .word 90 .space 4 .dword .LC8 .word 100 .space 4 .dword .LC9 .word 400 .space 4 .dword .LC10 .word 500 .space 4 .dword .LC11 .word 900 .space 4 .dword .LC12 .word 1000 .space 4
366
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O2
mips64 gcc 15.2.0
intToRoman: daddiu $sp,$sp,-48 sd $28,32($sp) lui $28,%hi(%neg(%gp_rel(intToRoman))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(intToRoman))) ld $25,%call16(malloc)($28) sd $16,0($sp) sd $31,40($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) move $16,$4 1: jalr $25 li $4,1024 # 0x400 move $4,$2 beq $16,$0,.L1 sb $0,0($2) ld $19,%got_disp(map)($28) daddiu $19,$19,192 .L4: lw $18,8($19) div $0,$16,$18 teq $18,$0,7 mflo $2 mfhi $18 beq $2,$0,.L6 move $16,$2 ld $17,0($19) .L5: ld $25,%call16(strcat)($28) move $5,$17 1: jalr $25 addiu $16,$16,-1 bne $16,$0,.L5 move $4,$2 .L6: move $16,$18 bne $18,$0,.L4 daddiu $19,$19,-16 .L1: ld $31,40($sp) ld $28,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) move $2,$4 jr $31 daddiu $sp,$sp,48 .LC0: .ascii "I\000" .LC1: .ascii "IV\000" .LC2: .ascii "V\000" .LC3: .ascii "IX\000" .LC4: .ascii "X\000" .LC5: .ascii "XL\000" .LC6: .ascii "L\000" .LC7: .ascii "XC\000" .LC8: .ascii "C\000" .LC9: .ascii "CD\000" .LC10: .ascii "D\000" .LC11: .ascii "CM\000" .LC12: .ascii "M\000" map: .dword .LC0 .word 1 .space 4 .dword .LC1 .word 4 .space 4 .dword .LC2 .word 5 .space 4 .dword .LC3 .word 9 .space 4 .dword .LC4 .word 10 .space 4 .dword .LC5 .word 40 .space 4 .dword .LC6 .word 50 .space 4 .dword .LC7 .word 90 .space 4 .dword .LC8 .word 100 .space 4 .dword .LC9 .word 400 .space 4 .dword .LC10 .word 500 .space 4 .dword .LC11 .word 900 .space 4 .dword .LC12 .word 1000 .space 4
367
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
mips64
-O3
mips64 gcc 15.2.0
intToRoman: daddiu $sp,$sp,-48 sd $28,32($sp) lui $28,%hi(%neg(%gp_rel(intToRoman))) daddu $28,$28,$25 daddiu $28,$28,%lo(%neg(%gp_rel(intToRoman))) ld $25,%call16(malloc)($28) sd $16,0($sp) sd $31,40($sp) sd $19,24($sp) sd $18,16($sp) sd $17,8($sp) move $16,$4 1: jalr $25 li $4,1024 # 0x400 move $4,$2 beq $16,$0,.L1 sb $0,0($2) ld $19,%got_disp(map)($28) daddiu $19,$19,192 .L4: lw $18,8($19) div $0,$16,$18 teq $18,$0,7 mflo $2 mfhi $18 beq $2,$0,.L6 move $16,$2 ld $17,0($19) .L5: ld $25,%call16(strcat)($28) move $5,$17 1: jalr $25 addiu $16,$16,-1 bne $16,$0,.L5 move $4,$2 .L6: move $16,$18 bne $18,$0,.L4 daddiu $19,$19,-16 .L1: ld $31,40($sp) ld $28,32($sp) ld $19,24($sp) ld $18,16($sp) ld $17,8($sp) ld $16,0($sp) move $2,$4 jr $31 daddiu $sp,$sp,48 .LC0: .ascii "I\000" .LC1: .ascii "IV\000" .LC2: .ascii "V\000" .LC3: .ascii "IX\000" .LC4: .ascii "X\000" .LC5: .ascii "XL\000" .LC6: .ascii "L\000" .LC7: .ascii "XC\000" .LC8: .ascii "C\000" .LC9: .ascii "CD\000" .LC10: .ascii "D\000" .LC11: .ascii "CM\000" .LC12: .ascii "M\000" map: .dword .LC0 .word 1 .space 4 .dword .LC1 .word 4 .space 4 .dword .LC2 .word 5 .space 4 .dword .LC3 .word 9 .space 4 .dword .LC4 .word 10 .space 4 .dword .LC5 .word 40 .space 4 .dword .LC6 .word 50 .space 4 .dword .LC7 .word 90 .space 4 .dword .LC8 .word 100 .space 4 .dword .LC9 .word 400 .space 4 .dword .LC10 .word 500 .space 4 .dword .LC11 .word 900 .space 4 .dword .LC12 .word 1000 .space 4
368
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O0
RISC-V 64 clang 21.1.0
intToRoman: addi sp, sp, -48 sd ra, 40(sp) sd s0, 32(sp) addi s0, sp, 48 sw a0, -20(s0) li a0, 12 sw a0, -28(s0) li a0, 1024 call malloc sd a0, -40(s0) ld a1, -40(s0) li a0, 0 sb a0, 0(a1) j .LBB0_1 .LBB0_1: lw a0, -20(s0) beqz a0, .LBB0_6 j .LBB0_2 .LBB0_2: lw a0, -20(s0) lw a1, -28(s0) slli a2, a1, 4 .Lpcrel_hi0: auipc a1, %pcrel_hi(map) addi a1, a1, %pcrel_lo(.Lpcrel_hi0) add a1, a1, a2 lw a1, 8(a1) divw a0, a0, a1 sw a0, -24(s0) j .LBB0_3 .LBB0_3: lw a0, -24(s0) beqz a0, .LBB0_5 j .LBB0_4 .LBB0_4: ld a0, -40(s0) lw a1, -28(s0) slli a2, a1, 4 .Lpcrel_hi1: auipc a1, %pcrel_hi(map) addi a1, a1, %pcrel_lo(.Lpcrel_hi1) add a1, a1, a2 ld a1, 0(a1) call strcat lw a0, -24(s0) addiw a0, a0, -1 sw a0, -24(s0) j .LBB0_3 .LBB0_5: lw a0, -20(s0) lw a1, -28(s0) slli a2, a1, 4 .Lpcrel_hi2: auipc a1, %pcrel_hi(map) addi a1, a1, %pcrel_lo(.Lpcrel_hi2) add a1, a1, a2 lw a1, 8(a1) remw a0, a0, a1 sw a0, -20(s0) lw a0, -28(s0) addiw a0, a0, -1 sw a0, -28(s0) j .LBB0_1 .LBB0_6: ld a0, -40(s0) ld ra, 40(sp) ld s0, 32(sp) addi sp, sp, 48 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .word 1 .zero 4 .quad .L.str.1 .word 4 .zero 4 .quad .L.str.2 .word 5 .zero 4 .quad .L.str.3 .word 9 .zero 4 .quad .L.str.4 .word 10 .zero 4 .quad .L.str.5 .word 40 .zero 4 .quad .L.str.6 .word 50 .zero 4 .quad .L.str.7 .word 90 .zero 4 .quad .L.str.8 .word 100 .zero 4 .quad .L.str.9 .word 400 .zero 4 .quad .L.str.10 .word 500 .zero 4 .quad .L.str.11 .word 900 .zero 4 .quad .L.str.12 .word 1000 .zero 4
369
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O1
RISC-V 64 clang 21.1.0
intToRoman: addi sp, sp, -80 sd ra, 72(sp) sd s0, 64(sp) sd s1, 56(sp) sd s2, 48(sp) sd s3, 40(sp) sd s4, 32(sp) sd s5, 24(sp) sd s6, 16(sp) sd s7, 8(sp) mv s2, a0 li a0, 1024 call malloc mv s7, a0 sb zero, 0(a0) beqz s2, .LBB0_6 li s3, 12 .Lpcrel_hi0: auipc a0, %pcrel_hi(map) addi s4, a0, %pcrel_lo(.Lpcrel_hi0) j .LBB0_3 .LBB0_2: mul a0, s6, s5 subw s2, s2, a0 addi s3, s3, -1 beqz s2, .LBB0_6 .LBB0_3: slli a0, s3, 4 add a0, a0, s4 lw s5, 8(a0) divw s6, s2, s5 beqz s6, .LBB0_2 ld s0, 0(a0) mv s1, s6 .LBB0_5: mv a0, s7 mv a1, s0 call strcat addiw s1, s1, -1 bnez s1, .LBB0_5 j .LBB0_2 .LBB0_6: mv a0, s7 ld ra, 72(sp) ld s0, 64(sp) ld s1, 56(sp) ld s2, 48(sp) ld s3, 40(sp) ld s4, 32(sp) ld s5, 24(sp) ld s6, 16(sp) ld s7, 8(sp) addi sp, sp, 80 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .word 1 .zero 4 .quad .L.str.1 .word 4 .zero 4 .quad .L.str.2 .word 5 .zero 4 .quad .L.str.3 .word 9 .zero 4 .quad .L.str.4 .word 10 .zero 4 .quad .L.str.5 .word 40 .zero 4 .quad .L.str.6 .word 50 .zero 4 .quad .L.str.7 .word 90 .zero 4 .quad .L.str.8 .word 100 .zero 4 .quad .L.str.9 .word 400 .zero 4 .quad .L.str.10 .word 500 .zero 4 .quad .L.str.11 .word 900 .zero 4 .quad .L.str.12 .word 1000 .zero 4
370
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O2
RISC-V 64 clang 21.1.0
intToRoman: addi sp, sp, -80 sd ra, 72(sp) sd s0, 64(sp) sd s1, 56(sp) sd s2, 48(sp) sd s3, 40(sp) sd s4, 32(sp) sd s5, 24(sp) sd s6, 16(sp) sd s7, 8(sp) mv s2, a0 li a0, 1024 call malloc mv s7, a0 sb zero, 0(a0) beqz s2, .LBB0_6 li s3, 12 .Lpcrel_hi0: auipc a0, %pcrel_hi(map) addi s4, a0, %pcrel_lo(.Lpcrel_hi0) j .LBB0_3 .LBB0_2: mul a0, s6, s5 subw s2, s2, a0 addi s3, s3, -1 beqz s2, .LBB0_6 .LBB0_3: slli a0, s3, 4 add a0, a0, s4 lw s5, 8(a0) divw s6, s2, s5 beqz s6, .LBB0_2 ld s0, 0(a0) mv s1, s6 .LBB0_5: mv a0, s7 mv a1, s0 call strcat addiw s1, s1, -1 bnez s1, .LBB0_5 j .LBB0_2 .LBB0_6: mv a0, s7 ld ra, 72(sp) ld s0, 64(sp) ld s1, 56(sp) ld s2, 48(sp) ld s3, 40(sp) ld s4, 32(sp) ld s5, 24(sp) ld s6, 16(sp) ld s7, 8(sp) addi sp, sp, 80 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .word 1 .zero 4 .quad .L.str.1 .word 4 .zero 4 .quad .L.str.2 .word 5 .zero 4 .quad .L.str.3 .word 9 .zero 4 .quad .L.str.4 .word 10 .zero 4 .quad .L.str.5 .word 40 .zero 4 .quad .L.str.6 .word 50 .zero 4 .quad .L.str.7 .word 90 .zero 4 .quad .L.str.8 .word 100 .zero 4 .quad .L.str.9 .word 400 .zero 4 .quad .L.str.10 .word 500 .zero 4 .quad .L.str.11 .word 900 .zero 4 .quad .L.str.12 .word 1000 .zero 4
371
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O3
RISC-V 64 clang 21.1.0
intToRoman: addi sp, sp, -80 sd ra, 72(sp) sd s0, 64(sp) sd s1, 56(sp) sd s2, 48(sp) sd s3, 40(sp) sd s4, 32(sp) sd s5, 24(sp) sd s6, 16(sp) sd s7, 8(sp) mv s2, a0 li a0, 1024 call malloc mv s7, a0 sb zero, 0(a0) beqz s2, .LBB0_6 li s3, 12 .Lpcrel_hi0: auipc a0, %pcrel_hi(map) addi s4, a0, %pcrel_lo(.Lpcrel_hi0) j .LBB0_3 .LBB0_2: mul a0, s6, s5 subw s2, s2, a0 addi s3, s3, -1 beqz s2, .LBB0_6 .LBB0_3: slli a0, s3, 4 add a0, a0, s4 lw s5, 8(a0) divw s6, s2, s5 beqz s6, .LBB0_2 ld s0, 0(a0) mv s1, s6 .LBB0_5: mv a0, s7 mv a1, s0 call strcat addiw s1, s1, -1 bnez s1, .LBB0_5 j .LBB0_2 .LBB0_6: mv a0, s7 ld ra, 72(sp) ld s0, 64(sp) ld s1, 56(sp) ld s2, 48(sp) ld s3, 40(sp) ld s4, 32(sp) ld s5, 24(sp) ld s6, 16(sp) ld s7, 8(sp) addi sp, sp, 80 ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .word 1 .zero 4 .quad .L.str.1 .word 4 .zero 4 .quad .L.str.2 .word 5 .zero 4 .quad .L.str.3 .word 9 .zero 4 .quad .L.str.4 .word 10 .zero 4 .quad .L.str.5 .word 40 .zero 4 .quad .L.str.6 .word 50 .zero 4 .quad .L.str.7 .word 90 .zero 4 .quad .L.str.8 .word 100 .zero 4 .quad .L.str.9 .word 400 .zero 4 .quad .L.str.10 .word 500 .zero 4 .quad .L.str.11 .word 900 .zero 4 .quad .L.str.12 .word 1000 .zero 4
372
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O0
RISC-V 64 gcc 15.2.0
.LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" map: .dword .LC0 .word 1 .zero 4 .dword .LC1 .word 4 .zero 4 .dword .LC2 .word 5 .zero 4 .dword .LC3 .word 9 .zero 4 .dword .LC4 .word 10 .zero 4 .dword .LC5 .word 40 .zero 4 .dword .LC6 .word 50 .zero 4 .dword .LC7 .word 90 .zero 4 .dword .LC8 .word 100 .zero 4 .dword .LC9 .word 400 .zero 4 .dword .LC10 .word 500 .zero 4 .dword .LC11 .word 900 .zero 4 .dword .LC12 .word 1000 .zero 4 intToRoman: addi sp,sp,-48 sd ra,40(sp) sd s0,32(sp) addi s0,sp,48 mv a5,a0 sw a5,-36(s0) li a5,12 sw a5,-24(s0) li a0,1024 call malloc mv a5,a0 sd a5,-32(s0) ld a5,-32(s0) sb zero,0(a5) j .L2 .L5: lui a5,%hi(map) addi a4,a5,%lo(map) lw a5,-24(s0) slli a5,a5,4 add a5,a4,a5 lw a5,8(a5) lw a4,-36(s0) divw a5,a4,a5 sw a5,-20(s0) j .L3 .L4: lui a5,%hi(map) addi a4,a5,%lo(map) lw a5,-24(s0) slli a5,a5,4 add a5,a4,a5 ld a5,0(a5) mv a1,a5 ld a0,-32(s0) call strcat lw a5,-20(s0) addiw a5,a5,-1 sw a5,-20(s0) .L3: lw a5,-20(s0) sext.w a5,a5 bne a5,zero,.L4 lui a5,%hi(map) addi a4,a5,%lo(map) lw a5,-24(s0) slli a5,a5,4 add a5,a4,a5 lw a5,8(a5) lw a4,-36(s0) remw a5,a4,a5 sw a5,-36(s0) lw a5,-24(s0) addiw a5,a5,-1 sw a5,-24(s0) .L2: lw a5,-36(s0) sext.w a5,a5 bne a5,zero,.L5 ld a5,-32(s0) mv a0,a5 ld ra,40(sp) ld s0,32(sp) addi sp,sp,48 jr ra
373
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O1
RISC-V 64 gcc 15.2.0
intToRoman: addi sp,sp,-64 sd ra,56(sp) sd s2,32(sp) sd s3,24(sp) mv s3,a0 li a0,1024 call malloc mv s2,a0 sb zero,0(a0) beq s3,zero,.L1 sd s0,48(sp) sd s1,40(sp) sd s4,16(sp) sd s5,8(sp) lui s4,%hi(.LANCHOR0+192) addi s4,s4,%lo(.LANCHOR0+192) .L5: mv s1,s4 lw s5,8(s4) divw s0,s3,s5 beq s0,zero,.L3 .L4: ld a1,0(s1) mv a0,s2 call strcat addiw s0,s0,-1 bne s0,zero,.L4 .L3: remw s3,s3,s5 addi s4,s4,-16 bne s3,zero,.L5 ld s0,48(sp) ld s1,40(sp) ld s4,16(sp) ld s5,8(sp) .L1: mv a0,s2 ld ra,56(sp) ld s2,32(sp) ld s3,24(sp) addi sp,sp,64 jr ra .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" .set .LANCHOR0,. + 0 map: .dword .LC0 .word 1 .zero 4 .dword .LC1 .word 4 .zero 4 .dword .LC2 .word 5 .zero 4 .dword .LC3 .word 9 .zero 4 .dword .LC4 .word 10 .zero 4 .dword .LC5 .word 40 .zero 4 .dword .LC6 .word 50 .zero 4 .dword .LC7 .word 90 .zero 4 .dword .LC8 .word 100 .zero 4 .dword .LC9 .word 400 .zero 4 .dword .LC10 .word 500 .zero 4 .dword .LC11 .word 900 .zero 4 .dword .LC12 .word 1000 .zero 4
374
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O2
RISC-V 64 gcc 15.2.0
intToRoman: addi sp,sp,-48 sd s2,16(sp) mv s2,a0 li a0,1024 sd ra,40(sp) call malloc sb zero,0(a0) mv a5,a0 beq s2,zero,.L1 sd s3,8(sp) lui s3,%hi(.LANCHOR0+192) sd s0,32(sp) sd s1,24(sp) sd s4,0(sp) addi s3,s3,%lo(.LANCHOR0+192) .L4: lw s4,8(s3) divw s0,s2,s4 beq s0,zero,.L6 ld s1,0(s3) .L5: mv a0,a5 mv a1,s1 call strcat addiw s0,s0,-1 mv a5,a0 bne s0,zero,.L5 .L6: remw s2,s2,s4 addi s3,s3,-16 bne s2,zero,.L4 ld s0,32(sp) ld s1,24(sp) ld s3,8(sp) ld s4,0(sp) .L1: ld ra,40(sp) ld s2,16(sp) mv a0,a5 addi sp,sp,48 jr ra .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" .set .LANCHOR0,. + 0 map: .dword .LC0 .word 1 .zero 4 .dword .LC1 .word 4 .zero 4 .dword .LC2 .word 5 .zero 4 .dword .LC3 .word 9 .zero 4 .dword .LC4 .word 10 .zero 4 .dword .LC5 .word 40 .zero 4 .dword .LC6 .word 50 .zero 4 .dword .LC7 .word 90 .zero 4 .dword .LC8 .word 100 .zero 4 .dword .LC9 .word 400 .zero 4 .dword .LC10 .word 500 .zero 4 .dword .LC11 .word 900 .zero 4 .dword .LC12 .word 1000 .zero 4
375
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
riscv64
-O3
RISC-V 64 gcc 15.2.0
intToRoman: addi sp,sp,-48 sd s2,16(sp) mv s2,a0 li a0,1024 sd ra,40(sp) call malloc sb zero,0(a0) mv a5,a0 beq s2,zero,.L1 sd s3,8(sp) lui s3,%hi(.LANCHOR0+192) sd s0,32(sp) sd s1,24(sp) sd s4,0(sp) addi s3,s3,%lo(.LANCHOR0+192) .L4: lw s4,8(s3) divw s0,s2,s4 beq s0,zero,.L6 ld s1,0(s3) .L5: mv a0,a5 mv a1,s1 call strcat addiw s0,s0,-1 mv a5,a0 bne s0,zero,.L5 .L6: remw s2,s2,s4 addi s3,s3,-16 bne s2,zero,.L4 ld s0,32(sp) ld s1,24(sp) ld s3,8(sp) ld s4,0(sp) .L1: ld ra,40(sp) ld s2,16(sp) mv a0,a5 addi sp,sp,48 jr ra .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" .set .LANCHOR0,. + 0 map: .dword .LC0 .word 1 .zero 4 .dword .LC1 .word 4 .zero 4 .dword .LC2 .word 5 .zero 4 .dword .LC3 .word 9 .zero 4 .dword .LC4 .word 10 .zero 4 .dword .LC5 .word 40 .zero 4 .dword .LC6 .word 50 .zero 4 .dword .LC7 .word 90 .zero 4 .dword .LC8 .word 100 .zero 4 .dword .LC9 .word 400 .zero 4 .dword .LC10 .word 500 .zero 4 .dword .LC11 .word 900 .zero 4 .dword .LC12 .word 1000 .zero 4
376
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O0
x86-64 clang 21.1.0
intToRoman: push rbp mov rbp, rsp sub rsp, 32 mov dword ptr [rbp - 4], edi mov dword ptr [rbp - 12], 12 mov edi, 1024 call malloc@PLT mov qword ptr [rbp - 24], rax mov rax, qword ptr [rbp - 24] mov byte ptr [rax], 0 .LBB0_1: cmp dword ptr [rbp - 4], 0 je .LBB0_6 mov eax, dword ptr [rbp - 4] movsxd rdx, dword ptr [rbp - 12] lea rcx, [rip + map] shl rdx, 4 add rcx, rdx cdq idiv dword ptr [rcx + 8] mov dword ptr [rbp - 8], eax .LBB0_3: cmp dword ptr [rbp - 8], 0 je .LBB0_5 mov rdi, qword ptr [rbp - 24] movsxd rcx, dword ptr [rbp - 12] lea rax, [rip + map] shl rcx, 4 add rax, rcx mov rsi, qword ptr [rax] call strcat@PLT mov eax, dword ptr [rbp - 8] add eax, -1 mov dword ptr [rbp - 8], eax jmp .LBB0_3 .LBB0_5: mov eax, dword ptr [rbp - 4] movsxd rdx, dword ptr [rbp - 12] lea rcx, [rip + map] shl rdx, 4 add rcx, rdx cdq idiv dword ptr [rcx + 8] mov dword ptr [rbp - 4], edx mov eax, dword ptr [rbp - 12] add eax, -1 mov dword ptr [rbp - 12], eax jmp .LBB0_1 .LBB0_6: mov rax, qword ptr [rbp - 24] add rsp, 32 pop rbp ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .long 1 .zero 4 .quad .L.str.1 .long 4 .zero 4 .quad .L.str.2 .long 5 .zero 4 .quad .L.str.3 .long 9 .zero 4 .quad .L.str.4 .long 10 .zero 4 .quad .L.str.5 .long 40 .zero 4 .quad .L.str.6 .long 50 .zero 4 .quad .L.str.7 .long 90 .zero 4 .quad .L.str.8 .long 100 .zero 4 .quad .L.str.9 .long 400 .zero 4 .quad .L.str.10 .long 500 .zero 4 .quad .L.str.11 .long 900 .zero 4 .quad .L.str.12 .long 1000 .zero 4
377
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O1
x86-64 clang 21.1.0
intToRoman: push rbp push r15 push r14 push r13 push r12 push rbx push rax mov ebp, edi mov edi, 1024 call malloc@PLT mov rbx, rax mov byte ptr [rax], 0 test ebp, ebp je .LBB0_6 mov r12d, 12 lea r13, [rip + map] jmp .LBB0_2 .LBB0_5: dec r12 test ebp, ebp je .LBB0_6 .LBB0_2: mov rcx, r12 shl rcx, 4 mov eax, ebp cdq idiv dword ptr [rcx + r13 + 8] mov ebp, edx test eax, eax je .LBB0_5 mov r14d, eax add rcx, r13 mov r15, qword ptr [rcx] .LBB0_4: mov rdi, rbx mov rsi, r15 call strcat@PLT dec r14d jne .LBB0_4 jmp .LBB0_5 .LBB0_6: mov rax, rbx add rsp, 8 pop rbx pop r12 pop r13 pop r14 pop r15 pop rbp ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .long 1 .zero 4 .quad .L.str.1 .long 4 .zero 4 .quad .L.str.2 .long 5 .zero 4 .quad .L.str.3 .long 9 .zero 4 .quad .L.str.4 .long 10 .zero 4 .quad .L.str.5 .long 40 .zero 4 .quad .L.str.6 .long 50 .zero 4 .quad .L.str.7 .long 90 .zero 4 .quad .L.str.8 .long 100 .zero 4 .quad .L.str.9 .long 400 .zero 4 .quad .L.str.10 .long 500 .zero 4 .quad .L.str.11 .long 900 .zero 4 .quad .L.str.12 .long 1000 .zero 4
378
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O2
x86-64 clang 21.1.0
intToRoman: push rbp push r15 push r14 push r13 push r12 push rbx push rax mov ebp, edi mov edi, 1024 call malloc@PLT mov rbx, rax mov byte ptr [rax], 0 test ebp, ebp je .LBB0_6 mov r12d, 12 lea r13, [rip + map] jmp .LBB0_2 .LBB0_5: dec r12 test ebp, ebp je .LBB0_6 .LBB0_2: mov rcx, r12 shl rcx, 4 mov eax, ebp cdq idiv dword ptr [rcx + r13 + 8] mov ebp, edx test eax, eax je .LBB0_5 mov r14d, eax add rcx, r13 mov r15, qword ptr [rcx] .LBB0_4: mov rdi, rbx mov rsi, r15 call strcat@PLT dec r14d jne .LBB0_4 jmp .LBB0_5 .LBB0_6: mov rax, rbx add rsp, 8 pop rbx pop r12 pop r13 pop r14 pop r15 pop rbp ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .long 1 .zero 4 .quad .L.str.1 .long 4 .zero 4 .quad .L.str.2 .long 5 .zero 4 .quad .L.str.3 .long 9 .zero 4 .quad .L.str.4 .long 10 .zero 4 .quad .L.str.5 .long 40 .zero 4 .quad .L.str.6 .long 50 .zero 4 .quad .L.str.7 .long 90 .zero 4 .quad .L.str.8 .long 100 .zero 4 .quad .L.str.9 .long 400 .zero 4 .quad .L.str.10 .long 500 .zero 4 .quad .L.str.11 .long 900 .zero 4 .quad .L.str.12 .long 1000 .zero 4
379
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O3
x86-64 clang 21.1.0
intToRoman: push rbp push r15 push r14 push r13 push r12 push rbx push rax mov ebp, edi mov edi, 1024 call malloc@PLT mov rbx, rax mov byte ptr [rax], 0 test ebp, ebp je .LBB0_6 mov r12d, 12 lea r13, [rip + map] jmp .LBB0_2 .LBB0_5: dec r12 test ebp, ebp je .LBB0_6 .LBB0_2: mov rcx, r12 shl rcx, 4 mov eax, ebp cdq idiv dword ptr [rcx + r13 + 8] mov ebp, edx test eax, eax je .LBB0_5 mov r14d, eax add rcx, r13 mov r15, qword ptr [rcx] .LBB0_4: mov rdi, rbx mov rsi, r15 call strcat@PLT dec r14d jne .LBB0_4 jmp .LBB0_5 .LBB0_6: mov rax, rbx add rsp, 8 pop rbx pop r12 pop r13 pop r14 pop r15 pop rbp ret .L.str: .asciz "I" .L.str.1: .asciz "IV" .L.str.2: .asciz "V" .L.str.3: .asciz "IX" .L.str.4: .asciz "X" .L.str.5: .asciz "XL" .L.str.6: .asciz "L" .L.str.7: .asciz "XC" .L.str.8: .asciz "C" .L.str.9: .asciz "CD" .L.str.10: .asciz "D" .L.str.11: .asciz "CM" .L.str.12: .asciz "M" map: .quad .L.str .long 1 .zero 4 .quad .L.str.1 .long 4 .zero 4 .quad .L.str.2 .long 5 .zero 4 .quad .L.str.3 .long 9 .zero 4 .quad .L.str.4 .long 10 .zero 4 .quad .L.str.5 .long 40 .zero 4 .quad .L.str.6 .long 50 .zero 4 .quad .L.str.7 .long 90 .zero 4 .quad .L.str.8 .long 100 .zero 4 .quad .L.str.9 .long 400 .zero 4 .quad .L.str.10 .long 500 .zero 4 .quad .L.str.11 .long 900 .zero 4 .quad .L.str.12 .long 1000 .zero 4
380
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O0
x86-64 gcc 15.2
.LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" map: .quad .LC0 .long 1 .zero 4 .quad .LC1 .long 4 .zero 4 .quad .LC2 .long 5 .zero 4 .quad .LC3 .long 9 .zero 4 .quad .LC4 .long 10 .zero 4 .quad .LC5 .long 40 .zero 4 .quad .LC6 .long 50 .zero 4 .quad .LC7 .long 90 .zero 4 .quad .LC8 .long 100 .zero 4 .quad .LC9 .long 400 .zero 4 .quad .LC10 .long 500 .zero 4 .quad .LC11 .long 900 .zero 4 .quad .LC12 .long 1000 .zero 4 intToRoman: push rbp mov rbp, rsp sub rsp, 32 mov DWORD PTR [rbp-20], edi mov DWORD PTR [rbp-8], 12 mov edi, 1024 call malloc mov QWORD PTR [rbp-16], rax mov rax, QWORD PTR [rbp-16] mov BYTE PTR [rax], 0 jmp .L2 .L5: mov eax, DWORD PTR [rbp-8] cdqe sal rax, 4 add rax, OFFSET FLAT:map+8 mov esi, DWORD PTR [rax] mov eax, DWORD PTR [rbp-20] cdq idiv esi mov DWORD PTR [rbp-4], eax jmp .L3 .L4: mov eax, DWORD PTR [rbp-8] cdqe sal rax, 4 add rax, OFFSET FLAT:map mov rdx, QWORD PTR [rax] mov rax, QWORD PTR [rbp-16] mov rsi, rdx mov rdi, rax call strcat sub DWORD PTR [rbp-4], 1 .L3: cmp DWORD PTR [rbp-4], 0 jne .L4 mov eax, DWORD PTR [rbp-8] cdqe sal rax, 4 add rax, OFFSET FLAT:map+8 mov ecx, DWORD PTR [rax] mov eax, DWORD PTR [rbp-20] cdq idiv ecx mov DWORD PTR [rbp-20], edx sub DWORD PTR [rbp-8], 1 .L2: cmp DWORD PTR [rbp-20], 0 jne .L5 mov rax, QWORD PTR [rbp-16] leave ret
381
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O1
x86-64 gcc 15.2
intToRoman: push r15 push r14 push r13 push r12 push rbp push rbx sub rsp, 8 mov r13d, edi mov edi, 1024 call malloc mov r12, rax mov BYTE PTR [rax], 0 test r13d, r13d je .L1 mov r15d, OFFSET FLAT:map+192 .L5: mov rbp, r15 mov r14d, DWORD PTR [r15+8] mov eax, r13d cdq idiv r14d mov ebx, eax test eax, eax je .L3 .L4: mov rsi, QWORD PTR [rbp+0] mov rdi, r12 call strcat sub ebx, 1 jne .L4 .L3: mov eax, r13d cdq idiv r14d mov r13d, edx sub r15, 16 test edx, edx jne .L5 .L1: mov rax, r12 add rsp, 8 pop rbx pop rbp pop r12 pop r13 pop r14 pop r15 ret .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" map: .quad .LC0 .long 1 .zero 4 .quad .LC1 .long 4 .zero 4 .quad .LC2 .long 5 .zero 4 .quad .LC3 .long 9 .zero 4 .quad .LC4 .long 10 .zero 4 .quad .LC5 .long 40 .zero 4 .quad .LC6 .long 50 .zero 4 .quad .LC7 .long 90 .zero 4 .quad .LC8 .long 100 .zero 4 .quad .LC9 .long 400 .zero 4 .quad .LC10 .long 500 .zero 4 .quad .LC11 .long 900 .zero 4 .quad .LC12 .long 1000 .zero 4
382
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O2
x86-64 gcc 15.2
intToRoman: push r13 push r12 push rbp push rbx mov ebx, edi mov edi, 1024 sub rsp, 8 call malloc mov BYTE PTR [rax], 0 mov rcx, rax test ebx, ebx je .L1 mov r13d, OFFSET FLAT:map+192 .L4: mov eax, ebx cdq idiv DWORD PTR [r13+8] mov r12d, edx mov ebx, eax test eax, eax je .L6 mov rbp, QWORD PTR [r13+0] .L5: mov rdi, rcx mov rsi, rbp call strcat mov rcx, rax sub ebx, 1 jne .L5 .L6: mov ebx, r12d sub r13, 16 test r12d, r12d jne .L4 .L1: add rsp, 8 mov rax, rcx pop rbx pop rbp pop r12 pop r13 ret .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" map: .quad .LC0 .long 1 .zero 4 .quad .LC1 .long 4 .zero 4 .quad .LC2 .long 5 .zero 4 .quad .LC3 .long 9 .zero 4 .quad .LC4 .long 10 .zero 4 .quad .LC5 .long 40 .zero 4 .quad .LC6 .long 50 .zero 4 .quad .LC7 .long 90 .zero 4 .quad .LC8 .long 100 .zero 4 .quad .LC9 .long 400 .zero 4 .quad .LC10 .long 500 .zero 4 .quad .LC11 .long 900 .zero 4 .quad .LC12 .long 1000 .zero 4
383
12
Integer to Roman
Medium
/* 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3. Example 5: Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ typedef struct { char *s; int v; } map_t; const map_t map[] = { { "I", 1 }, { "IV", 4 }, { "V", 5 }, { "IX", 9 }, { "X", 10 }, { "XL", 40 }, { "L", 50 }, { "XC", 90 }, { "C", 100 }, { "CD", 400 }, { "D", 500 }, { "CM", 900 }, { "M", 1000 } }; #define SZ (sizeof(map)/sizeof(map[0])) char* intToRoman(int num) { int n, l = SZ - 1; char *p = malloc(1024); //assert(p); p[0] = 0; while (num) { n = num / map[l].v; while (n) { strcat(p, map[l].s); n --; } num = num % map[l].v; l --; } return p; } /* Difficulty:Medium */
x86-64
-O3
x86-64 gcc 15.2
intToRoman: push r13 push r12 push rbp push rbx mov ebx, edi mov edi, 1024 sub rsp, 8 call malloc mov BYTE PTR [rax], 0 mov rcx, rax test ebx, ebx je .L1 mov r13d, OFFSET FLAT:map+192 .L4: mov eax, ebx cdq idiv DWORD PTR [r13+8] mov r12d, edx mov ebx, eax test eax, eax je .L6 mov rbp, QWORD PTR [r13+0] .L5: mov rdi, rcx mov rsi, rbp call strcat mov rcx, rax sub ebx, 1 jne .L5 .L6: mov ebx, r12d sub r13, 16 test r12d, r12d jne .L4 .L1: add rsp, 8 mov rax, rcx pop rbx pop rbp pop r12 pop r13 ret .LC0: .string "I" .LC1: .string "IV" .LC2: .string "V" .LC3: .string "IX" .LC4: .string "X" .LC5: .string "XL" .LC6: .string "L" .LC7: .string "XC" .LC8: .string "C" .LC9: .string "CD" .LC10: .string "D" .LC11: .string "CM" .LC12: .string "M" map: .quad .LC0 .long 1 .zero 4 .quad .LC1 .long 4 .zero 4 .quad .LC2 .long 5 .zero 4 .quad .LC3 .long 9 .zero 4 .quad .LC4 .long 10 .zero 4 .quad .LC5 .long 40 .zero 4 .quad .LC6 .long 50 .zero 4 .quad .LC7 .long 90 .zero 4 .quad .LC8 .long 100 .zero 4 .quad .LC9 .long 400 .zero 4 .quad .LC10 .long 500 .zero 4 .quad .LC11 .long 900 .zero 4 .quad .LC12 .long 1000 .zero 4
384
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O0
ARM64 gcc 15.2.0
romanToInt: sub sp, sp, #32 str x0, [sp, 8] str wzr, [sp, 28] b .L2 .L20: ldrb w0, [sp, 27] cmp w0, 88 beq .L3 cmp w0, 88 bgt .L22 cmp w0, 86 beq .L5 cmp w0, 86 bgt .L22 cmp w0, 77 beq .L6 cmp w0, 77 bgt .L22 cmp w0, 76 beq .L7 cmp w0, 76 bgt .L22 cmp w0, 73 beq .L8 cmp w0, 73 bgt .L22 cmp w0, 67 beq .L9 cmp w0, 68 beq .L10 b .L4 .L8: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 86 bne .L11 ldr w0, [sp, 28] add w0, w0, 4 str w0, [sp, 28] ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] b .L2 .L11: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 88 bne .L13 ldr w0, [sp, 28] add w0, w0, 9 str w0, [sp, 28] ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] b .L2 .L13: ldr w0, [sp, 28] add w0, w0, 1 str w0, [sp, 28] b .L2 .L5: ldr w0, [sp, 28] add w0, w0, 5 str w0, [sp, 28] b .L2 .L3: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 76 bne .L14 ldr w0, [sp, 28] add w0, w0, 40 str w0, [sp, 28] ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] b .L2 .L14: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 67 bne .L16 ldr w0, [sp, 28] add w0, w0, 90 str w0, [sp, 28] b .L2 .L16: ldr w0, [sp, 28] add w0, w0, 10 str w0, [sp, 28] b .L2 .L7: ldr w0, [sp, 28] add w0, w0, 50 str w0, [sp, 28] b .L2 .L9: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 68 bne .L17 ldr w0, [sp, 28] add w0, w0, 400 str w0, [sp, 28] ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] b .L2 .L17: ldr x0, [sp, 8] ldrb w0, [x0] cmp w0, 77 bne .L19 ldr w0, [sp, 28] add w0, w0, 900 str w0, [sp, 28] ldr x0, [sp, 8] add x0, x0, 1 str x0, [sp, 8] b .L2 .L19: ldr w0, [sp, 28] add w0, w0, 100 str w0, [sp, 28] b .L2 .L10: ldr w0, [sp, 28] add w0, w0, 500 str w0, [sp, 28] b .L2 .L6: ldr w0, [sp, 28] add w0, w0, 1000 str w0, [sp, 28] b .L2 .L4: .L22: nop .L2: ldr x0, [sp, 8] add x1, x0, 1 str x1, [sp, 8] ldrb w0, [x0] strb w0, [sp, 27] ldrb w0, [sp, 27] cmp w0, 0 bne .L20 ldr w0, [sp, 28] add sp, sp, 32 ret
385
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O1
ARM64 gcc 15.2.0
romanToInt: mov x3, x0 ldrb w1, [x0] cbz w1, .L19 add x2, x0, 1 mov w0, 0 b .L18 .L23: mov x3, x2 b .L8 .L4: cmp w1, 86 beq .L9 cmp w1, 88 beq .L10 mov x3, x2 cmp w1, 77 bne .L8 add w0, w0, 1000 b .L8 .L6: ldrb w1, [x3, 1] cmp w1, 86 beq .L21 cmp w1, 88 beq .L22 add w0, w0, 1 mov x3, x2 b .L8 .L21: add w0, w0, 4 add x3, x3, 2 b .L8 .L22: add w0, w0, 9 add x3, x3, 2 b .L8 .L9: add w0, w0, 5 mov x3, x2 .L8: mov x2, x3 ldrb w1, [x2], 1 cbz w1, .L1 .L18: cmp w1, 76 beq .L3 bhi .L4 cmp w1, 68 beq .L5 cmp w1, 73 beq .L6 cmp w1, 67 bne .L23 ldrb w1, [x3, 1] cmp w1, 68 beq .L24 cmp w1, 77 beq .L25 add w0, w0, 100 mov x3, x2 b .L8 .L10: ldrb w1, [x3, 1] cmp w1, 76 beq .L26 cmp w1, 67 beq .L27 add w0, w0, 10 mov x3, x2 b .L8 .L26: add w0, w0, 40 add x3, x3, 2 b .L8 .L27: add w0, w0, 90 mov x3, x2 b .L8 .L3: add w0, w0, 50 mov x3, x2 b .L8 .L24: add w0, w0, 400 add x3, x3, 2 b .L8 .L25: add w0, w0, 900 add x3, x3, 2 b .L8 .L5: add w0, w0, 500 mov x3, x2 b .L8 .L19: mov w0, 0 .L1: ret
386
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O2
ARM64 gcc 15.2.0
romanToInt: ldrb w1, [x0] mov x3, x0 cbz w1, .L19 add x2, x0, 1 mov w0, 0 .L18: cmp w1, 76 beq .L3 .L26: bhi .L4 cmp w1, 68 beq .L5 cmp w1, 73 beq .L6 cmp w1, 67 bne .L21 ldrb w1, [x3, 1] cmp w1, 68 beq .L24 cmp w1, 77 beq .L25 mov x3, x2 add w0, w0, 100 mov x2, x3 ldrb w1, [x2], 1 cbnz w1, .L18 .L1: ret .L4: cmp w1, 86 beq .L9 cmp w1, 88 beq .L10 cmp w1, 77 bne .L21 add w0, w0, 1000 .L21: mov x3, x2 .L8: mov x2, x3 ldrb w1, [x2], 1 cbz w1, .L1 cmp w1, 76 bne .L26 .L3: add w0, w0, 50 mov x3, x2 b .L8 .L6: ldrb w1, [x3, 1] cmp w1, 86 beq .L27 cmp w1, 88 beq .L28 add w0, w0, 1 mov x3, x2 b .L8 .L5: add w0, w0, 500 mov x3, x2 b .L8 .L10: ldrb w1, [x3, 1] cmp w1, 76 beq .L29 cmp w1, 67 beq .L30 add w0, w0, 10 mov x3, x2 b .L8 .L9: add w0, w0, 5 mov x3, x2 b .L8 .L25: add w0, w0, 900 add x3, x3, 2 b .L8 .L30: add w0, w0, 90 mov x3, x2 b .L8 .L28: add w0, w0, 9 add x3, x3, 2 b .L8 .L29: add w0, w0, 40 add x3, x3, 2 b .L8 .L27: add w0, w0, 4 add x3, x3, 2 b .L8 .L24: add w0, w0, 400 add x3, x3, 2 b .L8 .L19: mov w0, 0 ret
387
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O3
ARM64 gcc 15.2.0
romanToInt: ldrb w1, [x0] mov x3, x0 mov w0, 0 cbz w1, .L1 add x2, x3, 1 .L18: cmp w1, 76 beq .L3 .L26: bhi .L4 cmp w1, 68 beq .L5 cmp w1, 73 beq .L6 cmp w1, 67 bne .L21 ldrb w1, [x3, 1] cmp w1, 68 beq .L24 cmp w1, 77 beq .L25 mov x3, x2 add w0, w0, 100 mov x2, x3 ldrb w1, [x2], 1 cbnz w1, .L18 .L1: ret .L4: cmp w1, 86 beq .L9 cmp w1, 88 beq .L10 cmp w1, 77 bne .L21 add w0, w0, 1000 .L21: mov x3, x2 .L8: mov x2, x3 ldrb w1, [x2], 1 cbz w1, .L1 cmp w1, 76 bne .L26 .L3: add w0, w0, 50 mov x3, x2 b .L8 .L6: ldrb w1, [x3, 1] cmp w1, 86 beq .L27 cmp w1, 88 beq .L28 add w0, w0, 1 mov x3, x2 b .L8 .L5: add w0, w0, 500 mov x3, x2 b .L8 .L10: ldrb w1, [x3, 1] cmp w1, 76 beq .L29 cmp w1, 67 beq .L30 add w0, w0, 10 mov x3, x2 b .L8 .L9: add w0, w0, 5 mov x3, x2 b .L8 .L30: add w0, w0, 90 mov x3, x2 b .L8 .L28: add w0, w0, 9 add x3, x3, 2 b .L8 .L25: add w0, w0, 900 add x3, x3, 2 b .L8 .L24: add w0, w0, 400 add x3, x3, 2 b .L8 .L29: add w0, w0, 40 add x3, x3, 2 b .L8 .L27: add w0, w0, 4 add x3, x3, 2 b .L8
388
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O0
armv8-a clang 21.1.0
romanToInt: sub sp, sp, #32 str x0, [sp, #24] str wzr, [sp, #20] b .LBB0_1 .LBB0_1: ldr x8, [sp, #24] add x9, x8, #1 str x9, [sp, #24] ldrb w8, [x8] strb w8, [sp, #19] cbz w8, .LBB0_36 b .LBB0_2 .LBB0_2: ldrb w8, [sp, #19] str w8, [sp, #12] subs w8, w8, #67 b.eq .LBB0_25 b .LBB0_3 .LBB0_3: ldr w8, [sp, #12] subs w8, w8, #68 b.eq .LBB0_32 b .LBB0_4 .LBB0_4: ldr w8, [sp, #12] subs w8, w8, #73 b.eq .LBB0_9 b .LBB0_5 .LBB0_5: ldr w8, [sp, #12] subs w8, w8, #76 b.eq .LBB0_24 b .LBB0_6 .LBB0_6: ldr w8, [sp, #12] subs w8, w8, #77 b.eq .LBB0_33 b .LBB0_7 .LBB0_7: ldr w8, [sp, #12] subs w8, w8, #86 b.eq .LBB0_16 b .LBB0_8 .LBB0_8: ldr w8, [sp, #12] subs w8, w8, #88 b.eq .LBB0_17 b .LBB0_34 .LBB0_9: ldr x8, [sp, #24] ldrb w8, [x8] subs w8, w8, #86 b.ne .LBB0_11 b .LBB0_10 .LBB0_10: ldr w8, [sp, #20] add w8, w8, #4 str w8, [sp, #20] ldr x8, [sp, #24] add x8, x8, #1 str x8, [sp, #24] b .LBB0_15 .LBB0_11: ldr x8, [sp, #24] ldrb w8, [x8] subs w8, w8, #88 b.ne .LBB0_13 b .LBB0_12 .LBB0_12: ldr w8, [sp, #20] add w8, w8, #9 str w8, [sp, #20] ldr x8, [sp, #24] add x8, x8, #1 str x8, [sp, #24] b .LBB0_14 .LBB0_13: ldr w8, [sp, #20] add w8, w8, #1 str w8, [sp, #20] b .LBB0_14 .LBB0_14: b .LBB0_15 .LBB0_15: b .LBB0_35 .LBB0_16: ldr w8, [sp, #20] add w8, w8, #5 str w8, [sp, #20] b .LBB0_35 .LBB0_17: ldr x8, [sp, #24] ldrb w8, [x8] subs w8, w8, #76 b.ne .LBB0_19 b .LBB0_18 .LBB0_18: ldr w8, [sp, #20] add w8, w8, #40 str w8, [sp, #20] ldr x8, [sp, #24] add x8, x8, #1 str x8, [sp, #24] b .LBB0_23 .LBB0_19: ldr x8, [sp, #24] ldrb w8, [x8] subs w8, w8, #67 b.ne .LBB0_21 b .LBB0_20 .LBB0_20: ldr w8, [sp, #20] add w8, w8, #90 str w8, [sp, #20] b .LBB0_22 .LBB0_21: ldr w8, [sp, #20] add w8, w8, #10 str w8, [sp, #20] b .LBB0_22 .LBB0_22: b .LBB0_23 .LBB0_23: b .LBB0_35 .LBB0_24: ldr w8, [sp, #20] add w8, w8, #50 str w8, [sp, #20] b .LBB0_35 .LBB0_25: ldr x8, [sp, #24] ldrb w8, [x8] subs w8, w8, #68 b.ne .LBB0_27 b .LBB0_26 .LBB0_26: ldr w8, [sp, #20] add w8, w8, #400 str w8, [sp, #20] ldr x8, [sp, #24] add x8, x8, #1 str x8, [sp, #24] b .LBB0_31 .LBB0_27: ldr x8, [sp, #24] ldrb w8, [x8] subs w8, w8, #77 b.ne .LBB0_29 b .LBB0_28 .LBB0_28: ldr w8, [sp, #20] add w8, w8, #900 str w8, [sp, #20] ldr x8, [sp, #24] add x8, x8, #1 str x8, [sp, #24] b .LBB0_30 .LBB0_29: ldr w8, [sp, #20] add w8, w8, #100 str w8, [sp, #20] b .LBB0_30 .LBB0_30: b .LBB0_31 .LBB0_31: b .LBB0_35 .LBB0_32: ldr w8, [sp, #20] add w8, w8, #500 str w8, [sp, #20] b .LBB0_35 .LBB0_33: ldr w8, [sp, #20] add w8, w8, #1000 str w8, [sp, #20] b .LBB0_35 .LBB0_34: b .LBB0_35 .LBB0_35: b .LBB0_1 .LBB0_36: ldr w0, [sp, #20] add sp, sp, #32 ret
389
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O1
armv8-a clang 21.1.0
romanToInt: mov x8, x0 mov w0, wzr b .LBB0_2 .LBB0_1: add w0, w0, #500 .LBB0_2: mov x9, x8 ldrb w10, [x8], #1 cmp w10, #75 b.le .LBB0_7 cmp w10, #85 b.gt .LBB0_13 cmp w10, #76 b.eq .LBB0_22 cmp w10, #77 b.ne .LBB0_2 add w0, w0, #1000 b .LBB0_2 .LBB0_7: cmp w10, #67 b.le .LBB0_18 cmp w10, #68 b.eq .LBB0_1 cmp w10, #73 b.ne .LBB0_2 ldrb w10, [x8] cmp w10, #88 b.eq .LBB0_25 cmp w10, #86 b.ne .LBB0_28 add w0, w0, #4 add x8, x9, #2 b .LBB0_2 .LBB0_13: cmp w10, #86 b.eq .LBB0_23 cmp w10, #88 b.ne .LBB0_2 ldrb w10, [x8] cmp w10, #67 b.eq .LBB0_26 cmp w10, #76 b.ne .LBB0_29 add w0, w0, #40 add x8, x9, #2 b .LBB0_2 .LBB0_18: b.ne .LBB0_30 ldrb w10, [x8] cmp w10, #77 b.eq .LBB0_24 cmp w10, #68 b.ne .LBB0_27 add w0, w0, #400 add x8, x9, #2 b .LBB0_2 .LBB0_22: add w0, w0, #50 b .LBB0_2 .LBB0_23: add w0, w0, #5 b .LBB0_2 .LBB0_24: add w0, w0, #900 add x8, x9, #2 b .LBB0_2 .LBB0_25: add w0, w0, #9 add x8, x9, #2 b .LBB0_2 .LBB0_26: add w0, w0, #90 b .LBB0_2 .LBB0_27: add w0, w0, #100 b .LBB0_2 .LBB0_28: add w0, w0, #1 b .LBB0_2 .LBB0_29: add w0, w0, #10 b .LBB0_2 .LBB0_30: cbnz w10, .LBB0_2 ret
390
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O2
armv8-a clang 21.1.0
romanToInt: mov x8, x0 mov w0, wzr b .LBB0_2 .LBB0_1: add w0, w0, #500 .LBB0_2: mov x9, x8 ldrb w10, [x8], #1 cmp w10, #75 b.le .LBB0_7 cmp w10, #85 b.gt .LBB0_13 cmp w10, #76 b.eq .LBB0_22 cmp w10, #77 b.ne .LBB0_2 add w0, w0, #1000 b .LBB0_2 .LBB0_7: cmp w10, #67 b.le .LBB0_18 cmp w10, #68 b.eq .LBB0_1 cmp w10, #73 b.ne .LBB0_2 ldrb w10, [x8] cmp w10, #88 b.eq .LBB0_25 cmp w10, #86 b.ne .LBB0_28 add w0, w0, #4 add x8, x9, #2 b .LBB0_2 .LBB0_13: cmp w10, #86 b.eq .LBB0_23 cmp w10, #88 b.ne .LBB0_2 ldrb w10, [x8] cmp w10, #67 b.eq .LBB0_26 cmp w10, #76 b.ne .LBB0_29 add w0, w0, #40 add x8, x9, #2 b .LBB0_2 .LBB0_18: b.ne .LBB0_30 ldrb w10, [x8] cmp w10, #77 b.eq .LBB0_24 cmp w10, #68 b.ne .LBB0_27 add w0, w0, #400 add x8, x9, #2 b .LBB0_2 .LBB0_22: add w0, w0, #50 b .LBB0_2 .LBB0_23: add w0, w0, #5 b .LBB0_2 .LBB0_24: add w0, w0, #900 add x8, x9, #2 b .LBB0_2 .LBB0_25: add w0, w0, #9 add x8, x9, #2 b .LBB0_2 .LBB0_26: add w0, w0, #90 b .LBB0_2 .LBB0_27: add w0, w0, #100 b .LBB0_2 .LBB0_28: add w0, w0, #1 b .LBB0_2 .LBB0_29: add w0, w0, #10 b .LBB0_2 .LBB0_30: cbnz w10, .LBB0_2 ret
391
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
aarch64
-O3
armv8-a clang 21.1.0
romanToInt: mov x8, x0 mov w0, wzr b .LBB0_2 .LBB0_1: add w0, w0, #500 .LBB0_2: mov x9, x8 ldrb w10, [x8], #1 cmp w10, #75 b.le .LBB0_7 cmp w10, #85 b.gt .LBB0_13 cmp w10, #76 b.eq .LBB0_22 cmp w10, #77 b.ne .LBB0_2 add w0, w0, #1000 b .LBB0_2 .LBB0_7: cmp w10, #67 b.le .LBB0_18 cmp w10, #68 b.eq .LBB0_1 cmp w10, #73 b.ne .LBB0_2 ldrb w10, [x8] cmp w10, #88 b.eq .LBB0_25 cmp w10, #86 b.ne .LBB0_28 add w0, w0, #4 add x8, x9, #2 b .LBB0_2 .LBB0_13: cmp w10, #86 b.eq .LBB0_23 cmp w10, #88 b.ne .LBB0_2 ldrb w10, [x8] cmp w10, #67 b.eq .LBB0_26 cmp w10, #76 b.ne .LBB0_29 add w0, w0, #40 add x8, x9, #2 b .LBB0_2 .LBB0_18: b.ne .LBB0_30 ldrb w10, [x8] cmp w10, #77 b.eq .LBB0_24 cmp w10, #68 b.ne .LBB0_27 add w0, w0, #400 add x8, x9, #2 b .LBB0_2 .LBB0_22: add w0, w0, #50 b .LBB0_2 .LBB0_23: add w0, w0, #5 b .LBB0_2 .LBB0_24: add w0, w0, #900 add x8, x9, #2 b .LBB0_2 .LBB0_25: add w0, w0, #9 add x8, x9, #2 b .LBB0_2 .LBB0_26: add w0, w0, #90 b .LBB0_2 .LBB0_27: add w0, w0, #100 b .LBB0_2 .LBB0_28: add w0, w0, #1 b .LBB0_2 .LBB0_29: add w0, w0, #10 b .LBB0_2 .LBB0_30: cbnz w10, .LBB0_2 ret
392
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O0
mips64 clang 21.1.0
romanToInt: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -48 sd $ra, 40($sp) sd $fp, 32($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(romanToInt))) daddu $1, $1, $25 daddiu $1, $1, %lo(%neg(%gp_rel(romanToInt))) sd $1, 8($fp) sd $4, 24($fp) sw $zero, 20($fp) b .LBB0_1 nop .LBB0_1: ld $1, 24($fp) daddiu $2, $1, 1 sd $2, 24($fp) lbu $1, 0($1) sb $1, 19($fp) beqz $1, .LBB0_38 nop b .LBB0_3 nop .LBB0_3: lb $1, 19($fp) addiu $2, $1, -67 sltiu $1, $2, 22 dext $2, $2, 0, 32 sd $2, 0($fp) beqz $1, .LBB0_36 nop ld $1, 8($fp) ld $2, 0($fp) dsll $2, $2, 3 ld $3, %got_page(.LJTI0_0)($1) daddu $2, $2, $3 ld $2, %got_ofst(.LJTI0_0)($2) daddu $1, $1, $2 jr $1 nop ld $1, 24($fp) lb $1, 0($1) addiu $2, $zero, 86 bne $1, $2, .LBB0_8 nop b .LBB0_7 nop .LBB0_7: lw $1, 20($fp) addiu $1, $1, 4 sw $1, 20($fp) ld $1, 24($fp) daddiu $1, $1, 1 sd $1, 24($fp) b .LBB0_13 nop .LBB0_8: ld $1, 24($fp) lb $1, 0($1) addiu $2, $zero, 88 bne $1, $2, .LBB0_11 nop b .LBB0_10 nop .LBB0_10: lw $1, 20($fp) addiu $1, $1, 9 sw $1, 20($fp) ld $1, 24($fp) daddiu $1, $1, 1 sd $1, 24($fp) b .LBB0_12 nop .LBB0_11: lw $1, 20($fp) addiu $1, $1, 1 sw $1, 20($fp) b .LBB0_12 nop .LBB0_12: b .LBB0_13 nop .LBB0_13: b .LBB0_37 nop lw $1, 20($fp) addiu $1, $1, 5 sw $1, 20($fp) b .LBB0_37 nop ld $1, 24($fp) lb $1, 0($1) addiu $2, $zero, 76 bne $1, $2, .LBB0_18 nop b .LBB0_17 nop .LBB0_17: lw $1, 20($fp) addiu $1, $1, 40 sw $1, 20($fp) ld $1, 24($fp) daddiu $1, $1, 1 sd $1, 24($fp) b .LBB0_23 nop .LBB0_18: ld $1, 24($fp) lb $1, 0($1) addiu $2, $zero, 67 bne $1, $2, .LBB0_21 nop b .LBB0_20 nop .LBB0_20: lw $1, 20($fp) addiu $1, $1, 90 sw $1, 20($fp) b .LBB0_22 nop .LBB0_21: lw $1, 20($fp) addiu $1, $1, 10 sw $1, 20($fp) b .LBB0_22 nop .LBB0_22: b .LBB0_23 nop .LBB0_23: b .LBB0_37 nop lw $1, 20($fp) addiu $1, $1, 50 sw $1, 20($fp) b .LBB0_37 nop ld $1, 24($fp) lb $1, 0($1) addiu $2, $zero, 68 bne $1, $2, .LBB0_28 nop b .LBB0_27 nop .LBB0_27: lw $1, 20($fp) addiu $1, $1, 400 sw $1, 20($fp) ld $1, 24($fp) daddiu $1, $1, 1 sd $1, 24($fp) b .LBB0_33 nop .LBB0_28: ld $1, 24($fp) lb $1, 0($1) addiu $2, $zero, 77 bne $1, $2, .LBB0_31 nop b .LBB0_30 nop .LBB0_30: lw $1, 20($fp) addiu $1, $1, 900 sw $1, 20($fp) ld $1, 24($fp) daddiu $1, $1, 1 sd $1, 24($fp) b .LBB0_32 nop .LBB0_31: lw $1, 20($fp) addiu $1, $1, 100 sw $1, 20($fp) b .LBB0_32 nop .LBB0_32: b .LBB0_33 nop .LBB0_33: b .LBB0_37 nop lw $1, 20($fp) addiu $1, $1, 500 sw $1, 20($fp) b .LBB0_37 nop lw $1, 20($fp) addiu $1, $1, 1000 sw $1, 20($fp) b .LBB0_37 nop .LBB0_36: b .LBB0_37 nop .LBB0_37: b .LBB0_1 nop .LBB0_38: lw $2, 20($fp) move $sp, $fp ld $fp, 32($sp) ld $ra, 40($sp) daddiu $sp, $sp, 48 jr $ra nop .LJTI0_0:
393
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O1
mips64 clang 21.1.0
romanToInt: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(romanToInt))) daddu $1, $1, $25 daddiu $2, $1, %lo(%neg(%gp_rel(romanToInt))) addiu $3, $zero, 0 ld $5, %got_page(.LJTI0_0)($2) addiu $6, $zero, 77 addiu $7, $zero, 68 addiu $8, $zero, 88 addiu $9, $zero, 86 addiu $10, $zero, 67 b .LBB0_2 addiu $11, $zero, 76 addiu $3, $3, 1000 .LBB0_2: move $12, $4 lbu $13, 0($12) addiu $14, $13, -67 sltiu $1, $14, 22 beqz $1, .LBB0_16 daddiu $4, $4, 1 dext $1, $14, 0, 32 dsll $1, $1, 3 daddu $1, $1, $5 ld $1, %got_ofst(.LJTI0_0)($1) daddu $1, $2, $1 jr $1 nop lbu $13, 0($4) beq $13, $6, .LBB0_19 nop bne $13, $7, .LBB0_22 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 400 lbu $13, 0($4) beq $13, $8, .LBB0_18 nop bne $13, $9, .LBB0_21 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 4 b .LBB0_2 addiu $3, $3, 50 b .LBB0_2 addiu $3, $3, 500 b .LBB0_2 addiu $3, $3, 5 lbu $13, 0($4) beq $13, $10, .LBB0_20 nop bne $13, $11, .LBB0_23 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 40 .LBB0_16: bnez $13, .LBB0_2 nop b .LBB0_24 nop .LBB0_18: daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 9 .LBB0_19: daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 900 .LBB0_20: b .LBB0_2 addiu $3, $3, 90 .LBB0_21: b .LBB0_2 addiu $3, $3, 1 .LBB0_22: b .LBB0_2 addiu $3, $3, 100 .LBB0_23: b .LBB0_2 addiu $3, $3, 10 .LBB0_24: sll $2, $3, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16 .LJTI0_0:
394
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O2
mips64 clang 21.1.0
romanToInt: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(romanToInt))) daddu $1, $1, $25 daddiu $2, $1, %lo(%neg(%gp_rel(romanToInt))) addiu $3, $zero, 0 ld $5, %got_page(.LJTI0_0)($2) addiu $6, $zero, 77 addiu $7, $zero, 68 addiu $8, $zero, 88 addiu $9, $zero, 86 addiu $10, $zero, 67 b .LBB0_2 addiu $11, $zero, 76 addiu $3, $3, 1000 .LBB0_2: move $12, $4 lbu $13, 0($12) addiu $14, $13, -67 sltiu $1, $14, 22 beqz $1, .LBB0_16 daddiu $4, $4, 1 dext $1, $14, 0, 32 dsll $1, $1, 3 daddu $1, $1, $5 ld $1, %got_ofst(.LJTI0_0)($1) daddu $1, $2, $1 jr $1 nop lbu $13, 0($4) beq $13, $6, .LBB0_19 nop bne $13, $7, .LBB0_22 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 400 lbu $13, 0($4) beq $13, $8, .LBB0_18 nop bne $13, $9, .LBB0_21 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 4 b .LBB0_2 addiu $3, $3, 50 b .LBB0_2 addiu $3, $3, 500 b .LBB0_2 addiu $3, $3, 5 lbu $13, 0($4) beq $13, $10, .LBB0_20 nop bne $13, $11, .LBB0_23 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 40 .LBB0_16: bnez $13, .LBB0_2 nop b .LBB0_24 nop .LBB0_18: daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 9 .LBB0_19: daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 900 .LBB0_20: b .LBB0_2 addiu $3, $3, 90 .LBB0_21: b .LBB0_2 addiu $3, $3, 1 .LBB0_22: b .LBB0_2 addiu $3, $3, 100 .LBB0_23: b .LBB0_2 addiu $3, $3, 10 .LBB0_24: sll $2, $3, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16 .LJTI0_0:
395
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O3
mips64 clang 21.1.0
romanToInt: .Lfunc_begin0 = .Ltmp0 daddiu $sp, $sp, -16 sd $ra, 8($sp) sd $fp, 0($sp) move $fp, $sp lui $1, %hi(%neg(%gp_rel(romanToInt))) addiu $3, $zero, 0 addiu $6, $zero, 77 addiu $7, $zero, 68 addiu $8, $zero, 88 addiu $9, $zero, 86 addiu $10, $zero, 67 daddu $1, $1, $25 daddiu $2, $1, %lo(%neg(%gp_rel(romanToInt))) ld $5, %got_page(.LJTI0_0)($2) b .LBB0_2 addiu $11, $zero, 76 addiu $3, $3, 1000 .LBB0_2: move $12, $4 lbu $13, 0($12) addiu $14, $13, -67 sltiu $1, $14, 22 beqz $1, .LBB0_16 daddiu $4, $4, 1 dext $1, $14, 0, 32 dsll $1, $1, 3 daddu $1, $1, $5 ld $1, %got_ofst(.LJTI0_0)($1) daddu $1, $2, $1 jr $1 nop lbu $13, 0($4) beq $13, $6, .LBB0_19 nop bne $13, $7, .LBB0_22 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 400 lbu $13, 0($4) beq $13, $8, .LBB0_18 nop bne $13, $9, .LBB0_21 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 4 b .LBB0_2 addiu $3, $3, 50 b .LBB0_2 addiu $3, $3, 500 b .LBB0_2 addiu $3, $3, 5 lbu $13, 0($4) beq $13, $10, .LBB0_20 nop bne $13, $11, .LBB0_23 nop daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 40 .LBB0_16: bnez $13, .LBB0_2 nop b .LBB0_24 nop .LBB0_18: daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 9 .LBB0_19: daddiu $4, $12, 2 b .LBB0_2 addiu $3, $3, 900 .LBB0_20: b .LBB0_2 addiu $3, $3, 90 .LBB0_21: b .LBB0_2 addiu $3, $3, 1 .LBB0_22: b .LBB0_2 addiu $3, $3, 100 .LBB0_23: b .LBB0_2 addiu $3, $3, 10 .LBB0_24: sll $2, $3, 0 move $sp, $fp ld $fp, 0($sp) ld $ra, 8($sp) jr $ra daddiu $sp, $sp, 16 .LJTI0_0:
396
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O0
mips64 gcc 15.2.0
romanToInt: daddiu $sp,$sp,-48 sd $fp,40($sp) move $fp,$sp sd $4,16($fp) sw $0,0($fp) b .L2 nop .L20: lb $2,4($fp) li $3,88 # 0x58 beq $2,$3,.L3 nop slt $3,$2,89 beq $3,$0,.L22 nop li $3,86 # 0x56 beq $2,$3,.L5 nop slt $3,$2,87 beq $3,$0,.L22 nop li $3,77 # 0x4d beq $2,$3,.L6 nop slt $3,$2,78 beq $3,$0,.L22 nop li $3,76 # 0x4c beq $2,$3,.L7 nop slt $3,$2,77 beq $3,$0,.L22 nop li $3,73 # 0x49 beq $2,$3,.L8 nop slt $3,$2,74 beq $3,$0,.L22 nop li $3,67 # 0x43 beq $2,$3,.L9 nop li $3,68 # 0x44 beq $2,$3,.L10 nop b .L4 nop .L8: ld $2,16($fp) lb $2,0($2) li $3,86 # 0x56 bne $2,$3,.L11 nop lw $2,0($fp) addiu $2,$2,4 sw $2,0($fp) ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) b .L2 nop .L11: ld $2,16($fp) lb $2,0($2) li $3,88 # 0x58 bne $2,$3,.L13 nop lw $2,0($fp) addiu $2,$2,9 sw $2,0($fp) ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) b .L2 nop .L13: lw $2,0($fp) addiu $2,$2,1 sw $2,0($fp) b .L2 nop .L5: lw $2,0($fp) addiu $2,$2,5 sw $2,0($fp) b .L2 nop .L3: ld $2,16($fp) lb $2,0($2) li $3,76 # 0x4c bne $2,$3,.L14 nop lw $2,0($fp) addiu $2,$2,40 sw $2,0($fp) ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) b .L2 nop .L14: ld $2,16($fp) lb $2,0($2) li $3,67 # 0x43 bne $2,$3,.L16 nop lw $2,0($fp) addiu $2,$2,90 sw $2,0($fp) b .L2 nop .L16: lw $2,0($fp) addiu $2,$2,10 sw $2,0($fp) b .L2 nop .L7: lw $2,0($fp) addiu $2,$2,50 sw $2,0($fp) b .L2 nop .L9: ld $2,16($fp) lb $2,0($2) li $3,68 # 0x44 bne $2,$3,.L17 nop lw $2,0($fp) addiu $2,$2,400 sw $2,0($fp) ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) b .L2 nop .L17: ld $2,16($fp) lb $2,0($2) li $3,77 # 0x4d bne $2,$3,.L19 nop lw $2,0($fp) addiu $2,$2,900 sw $2,0($fp) ld $2,16($fp) daddiu $2,$2,1 sd $2,16($fp) b .L2 nop .L19: lw $2,0($fp) addiu $2,$2,100 sw $2,0($fp) b .L2 nop .L10: lw $2,0($fp) addiu $2,$2,500 sw $2,0($fp) b .L2 nop .L6: lw $2,0($fp) addiu $2,$2,1000 sw $2,0($fp) b .L2 nop .L4: .L22: nop .L2: ld $2,16($fp) daddiu $3,$2,1 sd $3,16($fp) lbu $2,0($2) sb $2,4($fp) lb $2,4($fp) bne $2,$0,.L20 nop lw $2,0($fp) move $sp,$fp ld $fp,40($sp) daddiu $sp,$sp,48 jr $31 nop
397
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O1
mips64 gcc 15.2.0
romanToInt: lui $11,%hi(%neg(%gp_rel(romanToInt))) daddu $11,$11,$25 lb $3,0($4) beq $3,$0,.L19 daddiu $11,$11,%lo(%neg(%gp_rel(romanToInt))) daddiu $5,$4,1 move $2,$0 ld $7,%got_page(.L5)($11) daddiu $7,$7,%got_ofst(.L5) li $10,68 # 0x44 li $9,76 # 0x4c b .L18 li $8,86 # 0x56 lb $3,1($4) beq $3,$8,.L22 li $6,88 # 0x58 beql $3,$6,.L23 addiu $2,$2,9 b .L3 addiu $2,$2,1 .L22: addiu $2,$2,4 b .L3 daddiu $5,$4,2 .L23: b .L3 daddiu $5,$4,2 addiu $2,$2,5 .L3: .L29: lb $3,0($5) beq $3,$0,.L30 daddiu $6,$5,1 move $4,$5 move $5,$6 .L18: addiu $3,$3,-67 andi $6,$3,0x00ff sltu $6,$6,22 beq $6,$0,.L3 andi $3,$3,0x00ff dsll $3,$3,3 daddu $3,$7,$3 ld $3,0($3) daddu $3,$3,$11 jr $3 nop .L5: lb $3,1($4) beql $3,$9,.L25 addiu $2,$2,40 li $4,67 # 0x43 beql $3,$4,.L29 addiu $2,$2,90 b .L3 addiu $2,$2,10 .L25: b .L3 daddiu $5,$4,2 b .L3 addiu $2,$2,50 lb $3,1($4) beq $3,$10,.L27 li $6,77 # 0x4d beql $3,$6,.L28 addiu $2,$2,900 b .L3 addiu $2,$2,100 .L27: addiu $2,$2,400 b .L3 daddiu $5,$4,2 .L28: b .L3 daddiu $5,$4,2 b .L3 addiu $2,$2,500 b .L3 addiu $2,$2,1000 .L30: jr $31 nop .L19: jr $31 move $2,$0
398
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O2
mips64 gcc 15.2.0
romanToInt: lb $3,0($4) lui $11,%hi(%neg(%gp_rel(romanToInt))) daddu $11,$11,$25 beq $3,$0,.L19 daddiu $11,$11,%lo(%neg(%gp_rel(romanToInt))) ld $7,%got_page(.L5)($11) daddiu $6,$4,1 move $2,$0 daddiu $7,$7,%got_ofst(.L5) li $10,68 # 0x44 li $9,76 # 0x4c li $8,86 # 0x56 .L18: addiu $3,$3,-67 andi $5,$3,0x00ff sltu $5,$5,22 beq $5,$0,.L3 andi $3,$3,0x00ff dsll $3,$3,3 daddu $3,$7,$3 ld $3,0($3) daddu $3,$3,$11 jr $3 nop .L5: addiu $2,$2,50 .L3: lb $3,0($6) beq $3,$0,.L30 nop .L20: move $4,$6 b .L18 daddiu $6,$6,1 lb $3,1($4) beql $3,$9,.L24 addiu $2,$2,40 li $4,67 # 0x43 beql $3,$4,.L3 addiu $2,$2,90 lb $3,0($6) bne $3,$0,.L20 addiu $2,$2,10 .L30: jr $31 nop b .L3 addiu $2,$2,5 b .L3 addiu $2,$2,1000 lb $3,1($4) beq $3,$8,.L26 li $5,88 # 0x58 beql $3,$5,.L27 addiu $2,$2,9 b .L3 addiu $2,$2,1 b .L3 addiu $2,$2,500 lb $3,1($4) beq $3,$10,.L28 li $5,77 # 0x4d beql $3,$5,.L29 addiu $2,$2,900 b .L3 addiu $2,$2,100 .L29: b .L3 daddiu $6,$4,2 .L27: b .L3 daddiu $6,$4,2 .L28: addiu $2,$2,400 b .L3 daddiu $6,$4,2 .L26: addiu $2,$2,4 b .L3 daddiu $6,$4,2 .L24: b .L3 daddiu $6,$4,2 .L19: jr $31 move $2,$0
399
13
Roman to Integer
Easy
/* 13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: "III" Output: 3 Example 2: Input: "IV" Output: 4 Example 3: Input: "IX" Output: 9 Example 4: Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5: Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. */ // An elegant solution is to make a state machine. int romanToInt(char* s) { int n = 0; char c; while (c = *s ++) { switch (c) { case 'I': if (*s == 'V') { n += 4; s ++; } else if (*s == 'X') { n += 9; s ++; } else { n += 1; } break; case 'V': n += 5; break; case 'X': if (*s == 'L') { n += 40; s ++; } else if (*s == 'C') { n += 90; } else { n += 10; } break; case 'L': n += 50; break; case 'C': if (*s == 'D') { n += 400; s ++; } else if (*s == 'M') { n += 900; s ++; } else { n += 100; } break; case 'D': n += 500; break; case 'M': n += 1000; break; default: break; } } return n; } /* Difficulty:Easy */
mips64
-O3
mips64 gcc 15.2.0
romanToInt: lb $3,0($4) lui $11,%hi(%neg(%gp_rel(romanToInt))) daddu $11,$11,$25 beq $3,$0,.L19 daddiu $11,$11,%lo(%neg(%gp_rel(romanToInt))) ld $7,%got_page(.L5)($11) daddiu $6,$4,1 move $2,$0 daddiu $7,$7,%got_ofst(.L5) li $10,68 # 0x44 li $9,76 # 0x4c li $8,86 # 0x56 .L18: addiu $3,$3,-67 andi $5,$3,0x00ff sltu $5,$5,22 beq $5,$0,.L3 andi $3,$3,0x00ff dsll $3,$3,3 daddu $3,$7,$3 ld $3,0($3) daddu $3,$3,$11 jr $3 nop .L5: addiu $2,$2,50 .L3: lb $3,0($6) beq $3,$0,.L30 nop .L20: move $4,$6 b .L18 daddiu $6,$6,1 lb $3,1($4) beql $3,$9,.L24 addiu $2,$2,40 li $4,67 # 0x43 beql $3,$4,.L3 addiu $2,$2,90 lb $3,0($6) bne $3,$0,.L20 addiu $2,$2,10 .L30: jr $31 nop b .L3 addiu $2,$2,5 b .L3 addiu $2,$2,1000 lb $3,1($4) beq $3,$8,.L26 li $5,88 # 0x58 beql $3,$5,.L27 addiu $2,$2,9 b .L3 addiu $2,$2,1 b .L3 addiu $2,$2,500 lb $3,1($4) beq $3,$10,.L28 li $5,77 # 0x4d beql $3,$5,.L29 addiu $2,$2,900 b .L3 addiu $2,$2,100 .L29: b .L3 daddiu $6,$4,2 .L27: b .L3 daddiu $6,$4,2 .L28: addiu $2,$2,400 b .L3 daddiu $6,$4,2 .L26: addiu $2,$2,4 b .L3 daddiu $6,$4,2 .L24: b .L3 daddiu $6,$4,2 .L19: jr $31 move $2,$0