zoukankan      html  css  js  c++  java
  • Get the Middle Character

    You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.

    #Examples:

    Kata.getMiddle("test") should return "es"
    
    Kata.getMiddle("testing") should return "t"
    
    Kata.getMiddle("middle") should return "dd"
    
    Kata.getMiddle("A") should return "A"
    

    #Input

    A word (string) of length 0 < str < 1000 (In javascript you may get slightly more than 1000 in some test cases due to an error in the test cases). You do not need to test for this. This is only here to tell you that you do not need to worry about your solution timing out.

    #Output

    The middle character(s) of the word represented as a string.

    自己的编译都无法通过的垃圾代码

    section .text
    global get_middle
    extern strlen
    extern malloc
    ; void get_middle(const char *s, char *buf)
    ; Write the result to `buf`. Don't forget to add a null byte.
    get_middle:
      call strlen
      mov rcx,rax
      
      cmp rcx,0
      je .st2
      
      mov rsi,rdi
      and rcx,1
      
      cmp rcx,1
      je .st1
      
      
      mov rcx,rax
      shr rcx,2
      mov rdi,3
      call malloc
      mov byte [rdi],byte [rsi+rcx-1]
      mov byte [rdi+1],byte [rsi+rcx]
      mov byte[rdi+2],0
      mov buf,rdi
      jmp .st2
      .st1:
      mov rcx,rax
      shr rcx,2
      mov rdi,2
      call malloc
      mov byte[rdi],byte[rsi+rcx]
      mov byte[rdi+1],0
      mov buf,rdi
      
      .st2:
      ret

    大神的代码

    section .text
    global get_middle
    ; void get_middle(const char *s, char *buf)
    ; Write the result to `buf`. Don't forget to add a null byte.
    get_middle:
      xor rcx, rcx
      dec rcx ; rcx = 0xFFFFFFFFFFFFFFFF
      xor rax, rax ; rax = 
      mov [rsi], rax ; buf = {0, 0, 0,}
      push rdi
      cld
      repne scasb
      pop rdi ; rdi = s
      neg rcx
      sub rcx, 2 ; rcx = strlen(s)
      mov rax, rcx
      xor rdx, rdx ; rdx:rax = strlen(s)
      mov rbx, 2
      div rbx ; rax = strlen(s) / 2
      mov bl, [rdi + rax] ; rbx = center or right symbol
      test rcx, 1
      jnz return ; if (rcx & 1 == 0) even
      mov [rsi + 1], bl
      mov bl, [rdi + rax - 1] ; rbx = left symbol
    return:
      mov [rsi], bl
      ret

    第二个

    extern strlen
    
    section .text
    global get_middle
    ; void get_middle(const char *s, char *buf)
    get_middle:
                    push    rdi
                    push    rsi
                    push    rbp
                    call    strlen
                    pop     rbp
                    pop     rsi
                    pop     rdi
                    dec     rax
                    shr     rax, 1
                    sbb     ecx, ecx
                    mov     dl, [rdi + rax]
                    mov     [rsi], dl
                    and     cl, [rdi + rax + 1]
                    mov     [rsi + 1], cl
                    mov     [rsi + 2], byte 0                
                    ret

    第三个

    section .text
    global get_middle
    ; void get_middle(const char *s, char *buf)
    ; Write the result to `buf`. Don't forget to add a null byte.
    get_middle:
      call get_length
      mov rcx, 2
      cqo ;zero rdx for division instruction
      idiv rcx
      add rdi, rax;move to the halfway point
      add rdi, rdx;if it's odd, moves to the ACTUAL halfway point
      dec rdi; 0 index
      mov al, BYTE [rdi]
      mov BYTE [rsi], al;get our first character
      test rdx, rdx
      jnz finish
      inc rsi
      mov al, BYTE [rdi+1]
      mov BYTE [rsi], al;get the second character
      finish:
        mov BYTE [rsi+1], 0
      ret
    get_length:
      push rdi
      xor rax, rax
      bg_loop:
        xor rbx, rbx
        cmp BYTE [rdi], 0
        setne bl
        movzx rbx, bl
        add rax, rbx
        inc rdi
        test rbx, rbx
        jnz bg_loop
      pop rdi
      ret

     第四个

    section .text
    global get_middle
    get_middle:
      xor   rax,rax
      mov   rcx,-1
      repne scasb
      dec   rcx
      sar   rcx,1
      mov   ax,[rdi+rcx]
      mov   [rsi],ax
      adc   rsi,1
      mov   byte[rsi],0
      ret

    周日下午全部都看一遍  在重写一遍

  • 相关阅读:
    (转)一文讲清TCP/IP 协议
    Java框架之Spring Boot和Spring Cloud区别
    php大力力 [006节]初步接触认识phpMyAdmin
    php大力力 [005节] php大力力简单计算器001
    php大力力 [004节]PHP常量MAMP环境下加载网页
    php大力力 [003节]php在百度文库的几个基础教程mac环境下文本编辑工具
    php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动
    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28
    C# DataSet与DataTable的区别和用法 ---转载
    【SqlServer】利用sql语句附加,分离数据库-----转载
  • 原文地址:https://www.cnblogs.com/pppyyyzzz/p/13652318.html
Copyright © 2011-2022 走看看