package org.baichuan.sample.algorithms.leetcode.middle

/**
 * https://leetcode.cn/problems/swap-nodes-in-pairs/
 */
class SwapPairs {
    fun swapPairs(head: ListNode?): ListNode? {
        if (head?.next == null) {
            return head
        }
        val newHead = head.next
        //交换后head变成后面的节点，所以head需要连接递归后的后续链表
        head.next = swapPairs(newHead?.next)
        //第二个节点变成头节点, 原头节点跟在第二个节点后
        newHead?.next = head
        return newHead
    }

    /**
     * 非递归解法
     */
    fun swapPairs1(head: ListNode?): ListNode? {
        if (head?.next == null) {
            return head
        }
        var curr = head
        val result = head.next
        var pre: ListNode? = null
        while (curr?.next != null) {
            val newHead = curr.next
            val newCurr = newHead?.next
            newHead?.next = curr
            curr.next = newCurr
            pre?.next = newHead
            pre = curr
            if (newCurr == null) {
                break
            }

            curr = newCurr
        }
        return result
    }
}

fun main() {
    val head1 = ListNode(1)
    val head2 = ListNode(2)
    val head3 = ListNode(3)
//    val head4 = ListNode(4)
    head1.next = head2
    head2.next = head3
//    head3.next = head4
    SwapPairs().swapPairs1(head1)
}

Compiled from "SwapPairs.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.SwapPairsKt {
  public static final void main();
    Code:
       0: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
       3: dup
       4: iconst_1
       5: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
       8: astore_0
       9: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
      12: dup
      13: iconst_2
      14: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
      17: astore_1
      18: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
      21: dup
      22: iconst_3
      23: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
      26: astore_2
      27: aload_0
      28: aload_1
      29: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      32: aload_1
      33: aload_2
      34: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      37: new           #18                 // class org/baichuan/sample/algorithms/leetcode/middle/SwapPairs
      40: dup
      41: invokespecial #20                 // Method org/baichuan/sample/algorithms/leetcode/middle/SwapPairs."<init>":()V
      44: aload_0
      45: invokevirtual #24                 // Method org/baichuan/sample/algorithms/leetcode/middle/SwapPairs.swapPairs1:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      48: pop
      49: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #31                 // Method main:()V
       3: return
}

Compiled from "SwapPairs.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.SwapPairs {
  public org.baichuan.sample.algorithms.leetcode.middle.SwapPairs();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final org.baichuan.sample.algorithms.leetcode.middle.ListNode swapPairs(org.baichuan.sample.algorithms.leetcode.middle.ListNode);
    Code:
       0: aload_1
       1: dup
       2: ifnull        11
       5: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
       8: goto          13
      11: pop
      12: aconst_null
      13: ifnonnull     18
      16: aload_1
      17: areturn
      18: aload_1
      19: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      22: astore_2
      23: aload_1
      24: aload_0
      25: aload_2
      26: dup
      27: ifnull        36
      30: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      33: goto          38
      36: pop
      37: aconst_null
      38: invokevirtual #21                 // Method swapPairs:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      41: invokevirtual #25                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      44: aload_2
      45: dup
      46: ifnull        56
      49: aload_1
      50: invokevirtual #25                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      53: goto          57
      56: pop
      57: aload_2
      58: areturn

  public final org.baichuan.sample.algorithms.leetcode.middle.ListNode swapPairs1(org.baichuan.sample.algorithms.leetcode.middle.ListNode);
    Code:
       0: aload_1
       1: dup
       2: ifnull        11
       5: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
       8: goto          13
      11: pop
      12: aconst_null
      13: ifnonnull     18
      16: aload_1
      17: areturn
      18: aload_1
      19: astore_2
      20: aload_1
      21: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      24: astore_3
      25: aconst_null
      26: astore        4
      28: aload_2
      29: dup
      30: ifnull        39
      33: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      36: goto          41
      39: pop
      40: aconst_null
      41: ifnull        118
      44: aload_2
      45: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      48: astore        5
      50: aload         5
      52: dup
      53: ifnull        62
      56: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      59: goto          64
      62: pop
      63: aconst_null
      64: astore        6
      66: aload         5
      68: dup
      69: ifnull        79
      72: aload_2
      73: invokevirtual #25                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      76: goto          80
      79: pop
      80: aload_2
      81: aload         6
      83: invokevirtual #25                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      86: aload         4
      88: dup
      89: ifnull        100
      92: aload         5
      94: invokevirtual #25                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      97: goto          101
     100: pop
     101: aload_2
     102: astore        4
     104: aload         6
     106: ifnonnull     112
     109: goto          118
     112: aload         6
     114: astore_2
     115: goto          28
     118: aload_3
     119: areturn
}


package org.baichuan.sample.algorithms.leetcode.middle

/**
 * https://leetcode.cn/problems/divide-two-integers/
 * 两数相除
 * 核心思路： 38/3 = 3*2 + 32 = 3*2*2+26= 3*2*2*2 + 14 = 3*2*2*2 + 3*2*2 +2 = 3(2*2*2 + 2*2) +2 = 3 * 12 + 2 = 12
 */
class Divide {
    fun divide(dividend: Int, divisor: Int): Int {
        if (dividend == 0) {
            return 0
        }
        if (dividend == Integer.MIN_VALUE) {
            if (divisor == 1) {
                return Integer.MIN_VALUE;
            }
            if (divisor == -1) {
                return Integer.MAX_VALUE;
            }
        }
        if (divisor == 1) {
            return dividend
        }
        if (divisor == -1) {
            return -dividend
        }
        var result = 1
        var sign = 0
        var divided1 = if (dividend > 0) {
            sign++
            -dividend
        } else {
            sign--
            dividend
        }
        var divisor1 = if (divisor > 0) {
            sign++
            -divisor
        } else {
            sign--
            divisor
        }
        val divisor2 = divisor1

        if (divided1 > divisor1) {
            return 0
        }

        if (divisor1 == divided1) {
            return if (sign == 0) -result else result
        }

        while (divided1 - divisor1 <= divisor1) {
            divisor1 = divisor1 shl 1
            result += result
        }

        divided1 -= divisor1
        var tmp1 = result
        while (divided1 < divisor2) {
            divisor1 = divisor1 shr 1
            tmp1 = tmp1 shr 1
            if (divided1 - divisor1 <= 0) {
                divided1 -= divisor1
                result += tmp1
            }
        }
        if (divided1 == divisor2) {
            result += 1
        }
        return if (sign == 0) -result else result
    }
}

fun main() {
    println(Divide().divide(2147483647, 1))
    println(-10 shr 1)
}

Compiled from "Divide.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.Divide {
  public org.baichuan.sample.algorithms.leetcode.middle.Divide();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int divide(int, int);
    Code:
       0: iload_1
       1: ifne          6
       4: iconst_0
       5: ireturn
       6: iload_1
       7: ldc           #13                 // int -2147483648
       9: if_icmpne     28
      12: iload_2
      13: iconst_1
      14: if_icmpne     20
      17: ldc           #13                 // int -2147483648
      19: ireturn
      20: iload_2
      21: iconst_m1
      22: if_icmpne     28
      25: ldc           #14                 // int 2147483647
      27: ireturn
      28: iload_2
      29: iconst_1
      30: if_icmpne     35
      33: iload_1
      34: ireturn
      35: iload_2
      36: iconst_m1
      37: if_icmpne     43
      40: iload_1
      41: ineg
      42: ireturn
      43: iconst_1
      44: istore_3
      45: iconst_0
      46: istore        4
      48: iload_1
      49: ifle          60
      52: iinc          4, 1
      55: iload_1
      56: ineg
      57: goto          64
      60: iinc          4, -1
      63: iload_1
      64: istore        5
      66: iload_2
      67: ifle          78
      70: iinc          4, 1
      73: iload_2
      74: ineg
      75: goto          82
      78: iinc          4, -1
      81: iload_2
      82: istore        6
      84: iload         6
      86: istore        7
      88: iload         5
      90: iload         6
      92: if_icmple     97
      95: iconst_0
      96: ireturn
      97: iload         6
      99: iload         5
     101: if_icmpne     116
     104: iload         4
     106: ifne          114
     109: iload_3
     110: ineg
     111: goto          115
     114: iload_3
     115: ireturn
     116: iload         5
     118: iload         6
     120: isub
     121: iload         6
     123: if_icmpgt     139
     126: iload         6
     128: iconst_1
     129: ishl
     130: istore        6
     132: iload_3
     133: iload_3
     134: iadd
     135: istore_3
     136: goto          116
     139: iload         5
     141: iload         6
     143: isub
     144: istore        5
     146: iload_3
     147: istore        8
     149: iload         5
     151: iload         7
     153: if_icmpge     191
     156: iload         6
     158: iconst_1
     159: ishr
     160: istore        6
     162: iload         8
     164: iconst_1
     165: ishr
     166: istore        8
     168: iload         5
     170: iload         6
     172: isub
     173: ifgt          149
     176: iload         5
     178: iload         6
     180: isub
     181: istore        5
     183: iload_3
     184: iload         8
     186: iadd
     187: istore_3
     188: goto          149
     191: iload         5
     193: iload         7
     195: if_icmpne     201
     198: iinc          3, 1
     201: iload         4
     203: ifne          211
     206: iload_3
     207: ineg
     208: goto          212
     211: iload_3
     212: ireturn
}

Compiled from "Divide.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.DivideKt {
  public static final void main();
    Code:
       0: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/Divide
       3: dup
       4: invokespecial #11                 // Method org/baichuan/sample/algorithms/leetcode/middle/Divide."<init>":()V
       7: ldc           #12                 // int 2147483647
       9: iconst_1
      10: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/middle/Divide.divide:(II)I
      13: istore_0
      14: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      17: iload_0
      18: invokevirtual #28                 // Method java/io/PrintStream.println:(I)V
      21: bipush        -5
      23: istore_0
      24: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      27: iload_0
      28: invokevirtual #28                 // Method java/io/PrintStream.println:(I)V
      31: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #31                 // Method main:()V
       3: return
}


package org.baichuan.sample.algorithms.leetcode.middle

/**
 * @author: kuntang (<EMAIL>)
 * @date: 2022/4/12
 * https://leetcode-cn.com/problems/reverse-linked-list-ii/
 *
 * 执行用时：136 ms, 在所有 Kotlin 提交中击败了97.62%的用户
 * 内存消耗：32.7 MB, 在所有 Kotlin 提交中击败了100.00%的用户
 */
class ReverseBetween {
    fun reverseBetween(head: ListNode?, left: Int, right: Int): ListNode? {
        if (head == null) {
            return null
        }
        if (left == right) {
            return head
        }
        var currIndex = 0
        var currNode = head
        var preNode: ListNode? = null
        var leftNode: ListNode? = head
        var rightNode: ListNode?

        var reverseRightNode: ListNode? = head
        var reverseLeftNode: ListNode? = head
        while (currNode != null) {
            val nextNode = currNode.next
            if (currIndex > left - 2 && currIndex < right) {
                if (currIndex == left - 1) {
                    leftNode = preNode
                    reverseRightNode = currNode
                }
                if (currIndex == right - 1) {
                    rightNode = nextNode
                    reverseRightNode?.next = rightNode
                    reverseLeftNode = currNode
                    leftNode?.next = reverseLeftNode
                }
                currNode.next = preNode
            }
            preNode = currNode
            currNode = nextNode
            currIndex++
        }
        return if (left == 1) {
            reverseLeftNode
        } else {
            head
        }
    }

    private fun doReverse(head: ListNode?) {
        if (head == null) {
            return
        }
        var currNode = head
        var preNode: ListNode? = null
        while (currNode != null) {
            val nextNode = currNode.next
            currNode.next = preNode
            preNode = currNode
            currNode = nextNode
        }
    }
}

fun main() {
    //case 1
    val a = ListNode(1)
    val b = ListNode(2)
    val c = ListNode(3)
    val d = ListNode(4)
    val e = ListNode(5)
    a.next = b
    b.next = c
    c.next = d
    d.next = e
    val reverseBetween = ReverseBetween()
    val reverseBetween1 = reverseBetween.reverseBetween(a, 2, 4)

    //case 2
//    val a = ListNode(3)
//    val b = ListNode(5)
//    a.next = b
//    val reverseBetween = ReverseBetween()
//    val reverseBetween1 = reverseBetween.reverseBetween(a, 1, 2)

    println(reverseBetween1)
}

class ListNode(var `val`: Int) {
    var next: ListNode? = null
}

Compiled from "ReverseBetween.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.ReverseBetweenKt {
  public static final void main();
    Code:
       0: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
       3: dup
       4: iconst_1
       5: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
       8: astore_0
       9: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
      12: dup
      13: iconst_2
      14: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
      17: astore_1
      18: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
      21: dup
      22: iconst_3
      23: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
      26: astore_2
      27: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
      30: dup
      31: iconst_4
      32: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
      35: astore_3
      36: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/ListNode
      39: dup
      40: iconst_5
      41: invokespecial #12                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode."<init>":(I)V
      44: astore        4
      46: aload_0
      47: aload_1
      48: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      51: aload_1
      52: aload_2
      53: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      56: aload_2
      57: aload_3
      58: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      61: aload_3
      62: aload         4
      64: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      67: new           #18                 // class org/baichuan/sample/algorithms/leetcode/middle/ReverseBetween
      70: dup
      71: invokespecial #20                 // Method org/baichuan/sample/algorithms/leetcode/middle/ReverseBetween."<init>":()V
      74: astore        5
      76: aload         5
      78: aload_0
      79: iconst_2
      80: iconst_4
      81: invokevirtual #24                 // Method org/baichuan/sample/algorithms/leetcode/middle/ReverseBetween.reverseBetween:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;II)Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      84: astore        6
      86: getstatic     #30                 // Field java/lang/System.out:Ljava/io/PrintStream;
      89: aload         6
      91: invokevirtual #36                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      94: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #47                 // Method main:()V
       3: return
}

Compiled from "ReverseBetween.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.ReverseBetween {
  public org.baichuan.sample.algorithms.leetcode.middle.ReverseBetween();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final org.baichuan.sample.algorithms.leetcode.middle.ListNode reverseBetween(org.baichuan.sample.algorithms.leetcode.middle.ListNode, int, int);
    Code:
       0: aload_1
       1: ifnonnull     6
       4: aconst_null
       5: areturn
       6: iload_2
       7: iload_3
       8: if_icmpne     13
      11: aload_1
      12: areturn
      13: iconst_0
      14: istore        4
      16: aload_1
      17: astore        5
      19: aconst_null
      20: astore        6
      22: aload_1
      23: astore        7
      25: aconst_null
      26: astore        8
      28: aload_1
      29: astore        9
      31: aload_1
      32: astore        10
      34: aload         5
      36: ifnull        135
      39: aload         5
      41: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      44: astore        11
      46: iload         4
      48: iload_2
      49: iconst_2
      50: isub
      51: if_icmple     121
      54: iload         4
      56: iload_3
      57: if_icmpge     121
      60: iload         4
      62: iload_2
      63: iconst_1
      64: isub
      65: if_icmpne     76
      68: aload         6
      70: astore        7
      72: aload         5
      74: astore        9
      76: iload         4
      78: iload_3
      79: iconst_1
      80: isub
      81: if_icmpne     114
      84: aload         11
      86: astore        8
      88: aload         9
      90: aload         8
      92: invokevirtual #23                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      95: aload         5
      97: astore        10
      99: aload         7
     101: dup
     102: ifnull        113
     105: aload         10
     107: invokevirtual #23                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
     110: goto          114
     113: pop
     114: aload         5
     116: aload         6
     118: invokevirtual #23                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
     121: aload         5
     123: astore        6
     125: aload         11
     127: astore        5
     129: iinc          4, 1
     132: goto          34
     135: iload_2
     136: iconst_1
     137: if_icmpne     145
     140: aload         10
     142: goto          146
     145: aload_1
     146: areturn

  private final void doReverse(org.baichuan.sample.algorithms.leetcode.middle.ListNode);
    Code:
       0: aload_1
       1: ifnonnull     5
       4: return
       5: aload_1
       6: astore_2
       7: aconst_null
       8: astore_3
       9: aload_2
      10: ifnull        32
      13: aload_2
      14: invokevirtual #19                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.getNext:()Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;
      17: astore        4
      19: aload_2
      20: aload_3
      21: invokevirtual #23                 // Method org/baichuan/sample/algorithms/leetcode/middle/ListNode.setNext:(Lorg/baichuan/sample/algorithms/leetcode/middle/ListNode;)V
      24: aload_2
      25: astore_3
      26: aload         4
      28: astore_2
      29: goto          9
      32: return
}


package org.baichuan.sample.algorithms.leetcode.middle

/**
 * @author: tk (<EMAIL>)
 * @date: 2022/1/11
 */
class Operations {
    var negatives = LongArray(32)
    var positives = LongArray(32)
    var cacheA = LongArray(32)
    var cacheB = LongArray(32)
    val NEGATIVE_UNIT = -1

    fun Operations() {
        negatives[0] = NEGATIVE_UNIT.toLong()
        positives[0] = 1
        for (i in 1..31) {
            negatives[i] = negatives[i + NEGATIVE_UNIT] + negatives[i + NEGATIVE_UNIT]
            positives[i] = positives[i + NEGATIVE_UNIT] + positives[i + NEGATIVE_UNIT]
        }
    }

    fun minus(a: Int, b: Int): Int {
        var aVar = a
        var bVar = b
        if (aVar == bVar) {
            return 0
        }
        var index = 31
        while (index >= 0) {
            if (bVar > 0) {
                if (bVar >= positives[index]) {
                    bVar += (negatives[index]).toInt()
                    aVar += (negatives[index]).toInt()
                } else {
                    index += NEGATIVE_UNIT
                }
            } else {
                if (bVar <= negatives[index]) {
                    bVar += (positives[index]).toInt()
                    aVar += (positives[index]).toInt()
                } else {
                    index += NEGATIVE_UNIT
                }
            }
        }
        return aVar
    }

    fun multiply(a: Int, b: Int): Int {
        var aVar = a
        var bVar = b
        if (aVar == 0 || bVar == 0) return 0
        if (aVar == 1) return bVar
        if (bVar == 1) return aVar
        if (aVar == NEGATIVE_UNIT) return minus(0, bVar)
        if (bVar == NEGATIVE_UNIT) return minus(0, aVar)
        val aSign = aVar > 0
        //最终结果的正负，true：正 false：负
        val resultSign = aVar > 0 && bVar > 0 || aVar < 0 && bVar < 0
        /**
         * 转负数是为了简化代码
         * 为什么不转正数？因为-2147483648无法转成有效的4字节正数
         */
        if (aVar > 0) {
            aVar = minus(0, aVar)
        }
        if (bVar > 0) {
            bVar = minus(0, bVar)
        }
        cacheA[0] = aVar.toLong()
        for (i in 1..30) {
            cacheA[i] = cacheA[i + NEGATIVE_UNIT] + cacheA[i + NEGATIVE_UNIT]
        }
        var result = 0
        var index = 31
        while (index >= 0) {
            if (bVar <= negatives[index]) {
                bVar += (positives[index]).toInt()
                result += (cacheA[index]).toInt()
            }
            index += NEGATIVE_UNIT
        }

        //原始结果符号，用来判断计算结果是否溢出
        val originalResultSign = result > 0
        //确保溢出的情况不会影响结果的正负
        if (originalResultSign != aSign) {
            result = minus(0, result)
        }

        //确定最终结果的正负
        if (resultSign && result < 0 || !resultSign && result > 0) {
            result = minus(0, result)
        }
        return result
    }

    fun divide(a: Int, b: Int): Int {
        var aVar = a
        var bVar = b
        if (aVar == 0 || aVar > 0 && aVar < bVar || aVar < 0 && aVar > bVar) return 0
        var result = 0
        val resultSign = aVar > 0 && bVar > 0 || aVar < 0 && bVar < 0
        if (aVar > 0) {
            aVar = minus(0, aVar)
        }
        if (bVar > 0) {
            bVar = minus(0, bVar)
        }

        //cacheB[0] = b* 2^0
        cacheB[0] = bVar.toLong()
        for (i in 1..31) {
            cacheB[i] = cacheB[i + NEGATIVE_UNIT] + cacheB[i + NEGATIVE_UNIT]
        }
        var index = 31
        var preDividedB: Long = 0
        while (index >= 0) {
            if (aVar <= cacheB[index] + preDividedB) {
                preDividedB += cacheB[index]
                result += (positives[index]).toInt()
            }
            index += NEGATIVE_UNIT
        }

        //确定最终结果的正负
        if (resultSign && result < 0 || !resultSign && result > 0) {
            result = minus(0, result)
        }
        return result
    }
}

Compiled from "Operations.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.Operations {
  private long[] negatives;

  private long[] positives;

  private long[] cacheA;

  private long[] cacheB;

  private final int NEGATIVE_UNIT;

  public org.baichuan.sample.algorithms.leetcode.middle.Operations();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: bipush        32
       7: newarray       long
       9: putfield      #12                 // Field negatives:[J
      12: aload_0
      13: bipush        32
      15: newarray       long
      17: putfield      #15                 // Field positives:[J
      20: aload_0
      21: bipush        32
      23: newarray       long
      25: putfield      #18                 // Field cacheA:[J
      28: aload_0
      29: bipush        32
      31: newarray       long
      33: putfield      #21                 // Field cacheB:[J
      36: aload_0
      37: iconst_m1
      38: putfield      #25                 // Field NEGATIVE_UNIT:I
      41: return

  public final long[] getNegatives();
    Code:
       0: aload_0
       1: getfield      #12                 // Field negatives:[J
       4: areturn

  public final void setNegatives(long[]);
    Code:
       0: aload_1
       1: ldc           #34                 // String <set-?>
       3: invokestatic  #40                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #12                 // Field negatives:[J
      11: return

  public final long[] getPositives();
    Code:
       0: aload_0
       1: getfield      #15                 // Field positives:[J
       4: areturn

  public final void setPositives(long[]);
    Code:
       0: aload_1
       1: ldc           #34                 // String <set-?>
       3: invokestatic  #40                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #15                 // Field positives:[J
      11: return

  public final long[] getCacheA();
    Code:
       0: aload_0
       1: getfield      #18                 // Field cacheA:[J
       4: areturn

  public final void setCacheA(long[]);
    Code:
       0: aload_1
       1: ldc           #34                 // String <set-?>
       3: invokestatic  #40                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #18                 // Field cacheA:[J
      11: return

  public final long[] getCacheB();
    Code:
       0: aload_0
       1: getfield      #21                 // Field cacheB:[J
       4: areturn

  public final void setCacheB(long[]);
    Code:
       0: aload_1
       1: ldc           #34                 // String <set-?>
       3: invokestatic  #40                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: putfield      #21                 // Field cacheB:[J
      11: return

  public final int getNEGATIVE_UNIT();
    Code:
       0: aload_0
       1: getfield      #25                 // Field NEGATIVE_UNIT:I
       4: ireturn

  public final void Operations();
    Code:
       0: aload_0
       1: getfield      #12                 // Field negatives:[J
       4: iconst_0
       5: aload_0
       6: getfield      #25                 // Field NEGATIVE_UNIT:I
       9: i2l
      10: lastore
      11: aload_0
      12: getfield      #15                 // Field positives:[J
      15: iconst_0
      16: lconst_1
      17: lastore
      18: iconst_1
      19: istore_1
      20: iload_1
      21: bipush        32
      23: if_icmpge     90
      26: aload_0
      27: getfield      #12                 // Field negatives:[J
      30: iload_1
      31: aload_0
      32: getfield      #12                 // Field negatives:[J
      35: iload_1
      36: aload_0
      37: getfield      #25                 // Field NEGATIVE_UNIT:I
      40: iadd
      41: laload
      42: aload_0
      43: getfield      #12                 // Field negatives:[J
      46: iload_1
      47: aload_0
      48: getfield      #25                 // Field NEGATIVE_UNIT:I
      51: iadd
      52: laload
      53: ladd
      54: lastore
      55: aload_0
      56: getfield      #15                 // Field positives:[J
      59: iload_1
      60: aload_0
      61: getfield      #15                 // Field positives:[J
      64: iload_1
      65: aload_0
      66: getfield      #25                 // Field NEGATIVE_UNIT:I
      69: iadd
      70: laload
      71: aload_0
      72: getfield      #15                 // Field positives:[J
      75: iload_1
      76: aload_0
      77: getfield      #25                 // Field NEGATIVE_UNIT:I
      80: iadd
      81: laload
      82: ladd
      83: lastore
      84: iinc          1, 1
      87: goto          20
      90: return

  public final int minus(int, int);
    Code:
       0: iload_1
       1: istore_3
       2: iload_2
       3: istore        4
       5: iload_3
       6: iload         4
       8: if_icmpne     13
      11: iconst_0
      12: ireturn
      13: bipush        31
      15: istore        5
      17: iload         5
      19: iflt          133
      22: iload         4
      24: ifle          80
      27: iload         4
      29: i2l
      30: aload_0
      31: getfield      #15                 // Field positives:[J
      34: iload         5
      36: laload
      37: lcmp
      38: iflt          68
      41: iload         4
      43: aload_0
      44: getfield      #12                 // Field negatives:[J
      47: iload         5
      49: laload
      50: l2i
      51: iadd
      52: istore        4
      54: iload_3
      55: aload_0
      56: getfield      #12                 // Field negatives:[J
      59: iload         5
      61: laload
      62: l2i
      63: iadd
      64: istore_3
      65: goto          17
      68: iload         5
      70: aload_0
      71: getfield      #25                 // Field NEGATIVE_UNIT:I
      74: iadd
      75: istore        5
      77: goto          17
      80: iload         4
      82: i2l
      83: aload_0
      84: getfield      #12                 // Field negatives:[J
      87: iload         5
      89: laload
      90: lcmp
      91: ifgt          121
      94: iload         4
      96: aload_0
      97: getfield      #15                 // Field positives:[J
     100: iload         5
     102: laload
     103: l2i
     104: iadd
     105: istore        4
     107: iload_3
     108: aload_0
     109: getfield      #15                 // Field positives:[J
     112: iload         5
     114: laload
     115: l2i
     116: iadd
     117: istore_3
     118: goto          17
     121: iload         5
     123: aload_0
     124: getfield      #25                 // Field NEGATIVE_UNIT:I
     127: iadd
     128: istore        5
     130: goto          17
     133: iload_3
     134: ireturn

  public final int multiply(int, int);
    Code:
       0: iload_1
       1: istore_3
       2: iload_2
       3: istore        4
       5: iload_3
       6: ifeq          14
       9: iload         4
      11: ifne          16
      14: iconst_0
      15: ireturn
      16: iload_3
      17: iconst_1
      18: if_icmpne     24
      21: iload         4
      23: ireturn
      24: iload         4
      26: iconst_1
      27: if_icmpne     32
      30: iload_3
      31: ireturn
      32: iload_3
      33: aload_0
      34: getfield      #25                 // Field NEGATIVE_UNIT:I
      37: if_icmpne     48
      40: aload_0
      41: iconst_0
      42: iload         4
      44: invokevirtual #60                 // Method minus:(II)I
      47: ireturn
      48: iload         4
      50: aload_0
      51: getfield      #25                 // Field NEGATIVE_UNIT:I
      54: if_icmpne     64
      57: aload_0
      58: iconst_0
      59: iload_3
      60: invokevirtual #60                 // Method minus:(II)I
      63: ireturn
      64: iload_3
      65: ifle          72
      68: iconst_1
      69: goto          73
      72: iconst_0
      73: istore        5
      75: iload_3
      76: ifle          84
      79: iload         4
      81: ifgt          93
      84: iload_3
      85: ifge          97
      88: iload         4
      90: ifge          97
      93: iconst_1
      94: goto          98
      97: iconst_0
      98: istore        6
     100: iload_3
     101: ifle          111
     104: aload_0
     105: iconst_0
     106: iload_3
     107: invokevirtual #60                 // Method minus:(II)I
     110: istore_3
     111: iload         4
     113: ifle          125
     116: aload_0
     117: iconst_0
     118: iload         4
     120: invokevirtual #60                 // Method minus:(II)I
     123: istore        4
     125: aload_0
     126: getfield      #18                 // Field cacheA:[J
     129: iconst_0
     130: iload_3
     131: i2l
     132: lastore
     133: iconst_1
     134: istore        7
     136: iload         7
     138: bipush        31
     140: if_icmpge     181
     143: aload_0
     144: getfield      #18                 // Field cacheA:[J
     147: iload         7
     149: aload_0
     150: getfield      #18                 // Field cacheA:[J
     153: iload         7
     155: aload_0
     156: getfield      #25                 // Field NEGATIVE_UNIT:I
     159: iadd
     160: laload
     161: aload_0
     162: getfield      #18                 // Field cacheA:[J
     165: iload         7
     167: aload_0
     168: getfield      #25                 // Field NEGATIVE_UNIT:I
     171: iadd
     172: laload
     173: ladd
     174: lastore
     175: iinc          7, 1
     178: goto          136
     181: iconst_0
     182: istore        7
     184: bipush        31
     186: istore        8
     188: iload         8
     190: iflt          245
     193: iload         4
     195: i2l
     196: aload_0
     197: getfield      #12                 // Field negatives:[J
     200: iload         8
     202: laload
     203: lcmp
     204: ifgt          233
     207: iload         4
     209: aload_0
     210: getfield      #15                 // Field positives:[J
     213: iload         8
     215: laload
     216: l2i
     217: iadd
     218: istore        4
     220: iload         7
     222: aload_0
     223: getfield      #18                 // Field cacheA:[J
     226: iload         8
     228: laload
     229: l2i
     230: iadd
     231: istore        7
     233: iload         8
     235: aload_0
     236: getfield      #25                 // Field NEGATIVE_UNIT:I
     239: iadd
     240: istore        8
     242: goto          188
     245: iload         7
     247: ifle          254
     250: iconst_1
     251: goto          255
     254: iconst_0
     255: istore        9
     257: iload         9
     259: iload         5
     261: if_icmpeq     273
     264: aload_0
     265: iconst_0
     266: iload         7
     268: invokevirtual #60                 // Method minus:(II)I
     271: istore        7
     273: iload         6
     275: ifeq          283
     278: iload         7
     280: iflt          293
     283: iload         6
     285: ifne          302
     288: iload         7
     290: ifle          302
     293: aload_0
     294: iconst_0
     295: iload         7
     297: invokevirtual #60                 // Method minus:(II)I
     300: istore        7
     302: iload         7
     304: ireturn

  public final int divide(int, int);
    Code:
       0: iload_1
       1: istore_3
       2: iload_2
       3: istore        4
       5: iload_3
       6: ifeq          29
       9: iload_3
      10: ifle          19
      13: iload_3
      14: iload         4
      16: if_icmplt     29
      19: iload_3
      20: ifge          31
      23: iload_3
      24: iload         4
      26: if_icmple     31
      29: iconst_0
      30: ireturn
      31: iconst_0
      32: istore        5
      34: iload_3
      35: ifle          43
      38: iload         4
      40: ifgt          52
      43: iload_3
      44: ifge          56
      47: iload         4
      49: ifge          56
      52: iconst_1
      53: goto          57
      56: iconst_0
      57: istore        6
      59: iload_3
      60: ifle          70
      63: aload_0
      64: iconst_0
      65: iload_3
      66: invokevirtual #60                 // Method minus:(II)I
      69: istore_3
      70: iload         4
      72: ifle          84
      75: aload_0
      76: iconst_0
      77: iload         4
      79: invokevirtual #60                 // Method minus:(II)I
      82: istore        4
      84: aload_0
      85: getfield      #21                 // Field cacheB:[J
      88: iconst_0
      89: iload         4
      91: i2l
      92: lastore
      93: iconst_1
      94: istore        7
      96: iload         7
      98: bipush        32
     100: if_icmpge     141
     103: aload_0
     104: getfield      #21                 // Field cacheB:[J
     107: iload         7
     109: aload_0
     110: getfield      #21                 // Field cacheB:[J
     113: iload         7
     115: aload_0
     116: getfield      #25                 // Field NEGATIVE_UNIT:I
     119: iadd
     120: laload
     121: aload_0
     122: getfield      #21                 // Field cacheB:[J
     125: iload         7
     127: aload_0
     128: getfield      #25                 // Field NEGATIVE_UNIT:I
     131: iadd
     132: laload
     133: ladd
     134: lastore
     135: iinc          7, 1
     138: goto          96
     141: bipush        31
     143: istore        7
     145: lconst_0
     146: lstore        8
     148: iload         7
     150: iflt          206
     153: iload_3
     154: i2l
     155: aload_0
     156: getfield      #21                 // Field cacheB:[J
     159: iload         7
     161: laload
     162: lload         8
     164: ladd
     165: lcmp
     166: ifgt          194
     169: lload         8
     171: aload_0
     172: getfield      #21                 // Field cacheB:[J
     175: iload         7
     177: laload
     178: ladd
     179: lstore        8
     181: iload         5
     183: aload_0
     184: getfield      #15                 // Field positives:[J
     187: iload         7
     189: laload
     190: l2i
     191: iadd
     192: istore        5
     194: iload         7
     196: aload_0
     197: getfield      #25                 // Field NEGATIVE_UNIT:I
     200: iadd
     201: istore        7
     203: goto          148
     206: iload         6
     208: ifeq          216
     211: iload         5
     213: iflt          226
     216: iload         6
     218: ifne          235
     221: iload         5
     223: ifle          235
     226: aload_0
     227: iconst_0
     228: iload         5
     230: invokevirtual #60                 // Method minus:(II)I
     233: istore        5
     235: iload         5
     237: ireturn
}


package org.baichuan.sample.algorithms.leetcode.middle

/**
 * https://leetcode.cn/problems/search-in-rotated-sorted-array/
 * 33. 搜索旋转排序数组
 */
class Search {
    fun search(nums: IntArray, target: Int): Int {
        val first = nums[0]
        if (target == first) {
            return 0
        }
        var i = nums.size / 2
        var left = 0
        var right = nums.size - 1
        while (right > left) {
            if (target == nums[i]) {
                return i
            }
            if (target > nums[i]) {
                if (nums[i] < first && target > first) {
                    right = i
                    i = (right + left) / 2
                } else {
                    if (i == nums.size - 1) return -1
                    left = i + 1
                    i = (right + left) / 2
                }
            } else {
                if (nums[i] > first) {
                    if (target > first) {
                        right = i
                        i = (right + left) / 2
                    } else {
                        if (i == nums.size - 1) return -1
                        left = i + 1
                        i = (right + left) / 2
                    }
                } else {
                    right = i
                    i = (right + left) / 2
                }
            }
        }
        if (nums[left] == target) {
            return left
        }
        return -1
    }
}

fun main() {
    Search().search(arrayOf(8, 9, 2, 3, 4).toIntArray(), 9)
}

Compiled from "Search.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.SearchKt {
  public static final void main();
    Code:
       0: new           #8                  // class org/baichuan/sample/algorithms/leetcode/middle/Search
       3: dup
       4: invokespecial #11                 // Method org/baichuan/sample/algorithms/leetcode/middle/Search."<init>":()V
       7: iconst_5
       8: anewarray     #13                 // class java/lang/Integer
      11: astore_0
      12: aload_0
      13: iconst_0
      14: bipush        8
      16: invokestatic  #17                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: aastore
      20: aload_0
      21: iconst_1
      22: bipush        9
      24: invokestatic  #17                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      27: aastore
      28: aload_0
      29: iconst_2
      30: iconst_2
      31: invokestatic  #17                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      34: aastore
      35: aload_0
      36: iconst_3
      37: iconst_3
      38: invokestatic  #17                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      41: aastore
      42: aload_0
      43: iconst_4
      44: iconst_4
      45: invokestatic  #17                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      48: aastore
      49: aload_0
      50: invokestatic  #23                 // Method kotlin/collections/ArraysKt.toIntArray:([Ljava/lang/Integer;)[I
      53: bipush        9
      55: invokevirtual #27                 // Method org/baichuan/sample/algorithms/leetcode/middle/Search.search:([II)I
      58: pop
      59: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #30                 // Method main:()V
       3: return
}

Compiled from "Search.kt"
public final class org.baichuan.sample.algorithms.leetcode.middle.Search {
  public org.baichuan.sample.algorithms.leetcode.middle.Search();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int search(int[], int);
    Code:
       0: aload_1
       1: ldc           #15                 // String nums
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: iconst_0
       8: iaload
       9: istore_3
      10: iload_2
      11: iload_3
      12: if_icmpne     17
      15: iconst_0
      16: ireturn
      17: aload_1
      18: arraylength
      19: iconst_2
      20: idiv
      21: istore        4
      23: iconst_0
      24: istore        5
      26: aload_1
      27: arraylength
      28: iconst_1
      29: isub
      30: istore        6
      32: iload         6
      34: iload         5
      36: if_icmple     190
      39: iload_2
      40: aload_1
      41: iload         4
      43: iaload
      44: if_icmpne     50
      47: iload         4
      49: ireturn
      50: iload_2
      51: aload_1
      52: iload         4
      54: iaload
      55: if_icmple     116
      58: aload_1
      59: iload         4
      61: iaload
      62: iload_3
      63: if_icmpge     87
      66: iload_2
      67: iload_3
      68: if_icmple     87
      71: iload         4
      73: istore        6
      75: iload         6
      77: iload         5
      79: iadd
      80: iconst_2
      81: idiv
      82: istore        4
      84: goto          32
      87: iload         4
      89: aload_1
      90: arraylength
      91: iconst_1
      92: isub
      93: if_icmpne     98
      96: iconst_m1
      97: ireturn
      98: iload         4
     100: iconst_1
     101: iadd
     102: istore        5
     104: iload         6
     106: iload         5
     108: iadd
     109: iconst_2
     110: idiv
     111: istore        4
     113: goto          32
     116: aload_1
     117: iload         4
     119: iaload
     120: iload_3
     121: if_icmple     174
     124: iload_2
     125: iload_3
     126: if_icmple     145
     129: iload         4
     131: istore        6
     133: iload         6
     135: iload         5
     137: iadd
     138: iconst_2
     139: idiv
     140: istore        4
     142: goto          32
     145: iload         4
     147: aload_1
     148: arraylength
     149: iconst_1
     150: isub
     151: if_icmpne     156
     154: iconst_m1
     155: ireturn
     156: iload         4
     158: iconst_1
     159: iadd
     160: istore        5
     162: iload         6
     164: iload         5
     166: iadd
     167: iconst_2
     168: idiv
     169: istore        4
     171: goto          32
     174: iload         4
     176: istore        6
     178: iload         6
     180: iload         5
     182: iadd
     183: iconst_2
     184: idiv
     185: istore        4
     187: goto          32
     190: aload_1
     191: iload         5
     193: iaload
     194: iload_2
     195: if_icmpne     201
     198: iload         5
     200: ireturn
     201: iconst_m1
     202: ireturn
}


package org.baichuan.sample.algorithms.leetcode.hard

/**
 * @author: tk (<EMAIL>)
 * @date: 2022/1/10
 * https://leetcode-cn.com/problems/english-int-lcci/
 * 面试题 16.08. 整数的英语表示
 */
class NumberToWords {
    fun numberToWords(num: Int): String {
        val unit = arrayOf("Billion", "Million", "Thousand", "Hundred")
        val numberOfUnit = arrayOf(1000000000L, 1000000L, 1000L, 100L)
        val numberWord = arrayOf(
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten",
            "Eleven",
            "Twelve",
            "Thirteen",
            "Fourteen",
            "Fifteen",
            "Sixteen",
            "Seventeen",
            "Eighteen",
            "Nineteen",
            "Twenty",
            "Thirty",
            "Forty",
            "Fifty",
            "Sixty",
            "Seventy",
            "Eighty",
            "Ninety"
        )
        val numberOfWord =
            arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90)

        /*var i = 0
        var result = ""
        var numVar = num
        while (i < number.size) {
            val l = numVar / number[i]
            if (l > 0) {
                result += findWord(l.toInt())
                numVar -= (l * number[i]).toInt()
            }
            i++
        }

        return result*/
        if (num == 0) {
            return "Zero"
        }
        val result = findResult(num, numberOfUnit, unit, numberOfWord)
        return result.substring(0, result.length - 1)
    }

    fun findResult(
        num: Int,
        numberOfUnit: Array<Long>,
        unit: Array<String>,
        numberOfWord: Array<Int>
    ): String {
        var i = 0
        var result = ""
        var numVar = num

        /**
         * 100及以上的数通过除法进行单位(百、千)单词查找
         * 100以下的数进行减法进行数量(具体数值)单词查找
         */
        if (numVar >= 100) {
            while (i < numberOfUnit.size) {
                val l = numVar / numberOfUnit[i]
                if (l > 0) {
                    result += findResult(l.toInt(), numberOfUnit, unit, numberOfWord) + unit[i] + " "
                    numVar -= (l * numberOfUnit[i]).toInt()
                    //如果有余数，且余数小于100则按照减法进行单词查找
                    if (numVar in 1..99) {
                        result += findResult(numVar, numberOfUnit, unit, numberOfWord)
                    }
                }
                i++
            }
        } else {
            var j = numberOfWord.size - 1
            while (j >= 0) {
                val l = numVar - numberOfWord[j]
                if (l >= 0) {
                    result += findWord(numberOfWord[j]) + findWord(l)
                    break
                } else {
                    j--
                }
            }
        }

        return result
    }

    fun findWord(num: Int): String {
        return when (num) {
            1 -> "One "
            2 -> "Two "
            3 -> "Three "
            4 -> "Four "
            5 -> "Five "
            6 -> "Six "
            7 -> "Seven "
            8 -> "Eight "
            9 -> "Nine "
            10 -> "Ten "
            11 -> "Eleven "
            12 -> "Twelve "
            13 -> "Thirteen "
            14 -> "Fourteen "
            15 -> "Fifteen "
            16 -> "Sixteen "
            17 -> "Seventeen "
            18 -> "Eighteen "
            19 -> "Nineteen "
            20 -> "Twenty "
            30 -> "Thirty "
            40 -> "Forty "
            50 -> "Fifty "
            60 -> "Sixty "
            70 -> "Seventy "
            80 -> "Eighty "
            90 -> "Ninety "
            else -> ""
        }
    }
}

fun main() {
    println(NumberToWords().numberToWords(1234567891))
}

Compiled from "NumberToWords.kt"
public final class org.baichuan.sample.algorithms.leetcode.hard.NumberToWordsKt {
  public static final void main();
    Code:
       0: new           #8                  // class org/baichuan/sample/algorithms/leetcode/hard/NumberToWords
       3: dup
       4: invokespecial #11                 // Method org/baichuan/sample/algorithms/leetcode/hard/NumberToWords."<init>":()V
       7: ldc           #12                 // int 1234567891
       9: invokevirtual #16                 // Method org/baichuan/sample/algorithms/leetcode/hard/NumberToWords.numberToWords:(I)Ljava/lang/String;
      12: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      15: swap
      16: invokevirtual #28                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      19: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #31                 // Method main:()V
       3: return
}

Compiled from "NumberToWords.kt"
public final class org.baichuan.sample.algorithms.leetcode.hard.NumberToWords {
  public org.baichuan.sample.algorithms.leetcode.hard.NumberToWords();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final java.lang.String numberToWords(int);
    Code:
       0: iconst_4
       1: anewarray     #15                 // class java/lang/String
       4: astore_3
       5: aload_3
       6: iconst_0
       7: ldc           #17                 // String Billion
       9: aastore
      10: aload_3
      11: iconst_1
      12: ldc           #19                 // String Million
      14: aastore
      15: aload_3
      16: iconst_2
      17: ldc           #21                 // String Thousand
      19: aastore
      20: aload_3
      21: iconst_3
      22: ldc           #23                 // String Hundred
      24: aastore
      25: aload_3
      26: astore_2
      27: iconst_4
      28: anewarray     #25                 // class java/lang/Long
      31: astore        4
      33: aload         4
      35: iconst_0
      36: ldc2_w        #26                 // long 1000000000l
      39: invokestatic  #31                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      42: aastore
      43: aload         4
      45: iconst_1
      46: ldc2_w        #32                 // long 1000000l
      49: invokestatic  #31                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      52: aastore
      53: aload         4
      55: iconst_2
      56: ldc2_w        #34                 // long 1000l
      59: invokestatic  #31                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      62: aastore
      63: aload         4
      65: iconst_3
      66: ldc2_w        #36                 // long 100l
      69: invokestatic  #31                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      72: aastore
      73: aload         4
      75: astore_3
      76: bipush        27
      78: anewarray     #15                 // class java/lang/String
      81: astore        5
      83: aload         5
      85: iconst_0
      86: ldc           #39                 // String One
      88: aastore
      89: aload         5
      91: iconst_1
      92: ldc           #41                 // String Two
      94: aastore
      95: aload         5
      97: iconst_2
      98: ldc           #43                 // String Three
     100: aastore
     101: aload         5
     103: iconst_3
     104: ldc           #45                 // String Four
     106: aastore
     107: aload         5
     109: iconst_4
     110: ldc           #47                 // String Five
     112: aastore
     113: aload         5
     115: iconst_5
     116: ldc           #49                 // String Six
     118: aastore
     119: aload         5
     121: bipush        6
     123: ldc           #51                 // String Seven
     125: aastore
     126: aload         5
     128: bipush        7
     130: ldc           #53                 // String Eight
     132: aastore
     133: aload         5
     135: bipush        8
     137: ldc           #55                 // String Nine
     139: aastore
     140: aload         5
     142: bipush        9
     144: ldc           #57                 // String Ten
     146: aastore
     147: aload         5
     149: bipush        10
     151: ldc           #59                 // String Eleven
     153: aastore
     154: aload         5
     156: bipush        11
     158: ldc           #61                 // String Twelve
     160: aastore
     161: aload         5
     163: bipush        12
     165: ldc           #63                 // String Thirteen
     167: aastore
     168: aload         5
     170: bipush        13
     172: ldc           #65                 // String Fourteen
     174: aastore
     175: aload         5
     177: bipush        14
     179: ldc           #67                 // String Fifteen
     181: aastore
     182: aload         5
     184: bipush        15
     186: ldc           #69                 // String Sixteen
     188: aastore
     189: aload         5
     191: bipush        16
     193: ldc           #71                 // String Seventeen
     195: aastore
     196: aload         5
     198: bipush        17
     200: ldc           #73                 // String Eighteen
     202: aastore
     203: aload         5
     205: bipush        18
     207: ldc           #75                 // String Nineteen
     209: aastore
     210: aload         5
     212: bipush        19
     214: ldc           #77                 // String Twenty
     216: aastore
     217: aload         5
     219: bipush        20
     221: ldc           #79                 // String Thirty
     223: aastore
     224: aload         5
     226: bipush        21
     228: ldc           #81                 // String Forty
     230: aastore
     231: aload         5
     233: bipush        22
     235: ldc           #83                 // String Fifty
     237: aastore
     238: aload         5
     240: bipush        23
     242: ldc           #85                 // String Sixty
     244: aastore
     245: aload         5
     247: bipush        24
     249: ldc           #87                 // String Seventy
     251: aastore
     252: aload         5
     254: bipush        25
     256: ldc           #89                 // String Eighty
     258: aastore
     259: aload         5
     261: bipush        26
     263: ldc           #91                 // String Ninety
     265: aastore
     266: aload         5
     268: astore        4
     270: bipush        27
     272: anewarray     #93                 // class java/lang/Integer
     275: astore        6
     277: aload         6
     279: iconst_0
     280: iconst_1
     281: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     284: aastore
     285: aload         6
     287: iconst_1
     288: iconst_2
     289: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     292: aastore
     293: aload         6
     295: iconst_2
     296: iconst_3
     297: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     300: aastore
     301: aload         6
     303: iconst_3
     304: iconst_4
     305: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     308: aastore
     309: aload         6
     311: iconst_4
     312: iconst_5
     313: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     316: aastore
     317: aload         6
     319: iconst_5
     320: bipush        6
     322: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     325: aastore
     326: aload         6
     328: bipush        6
     330: bipush        7
     332: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     335: aastore
     336: aload         6
     338: bipush        7
     340: bipush        8
     342: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     345: aastore
     346: aload         6
     348: bipush        8
     350: bipush        9
     352: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     355: aastore
     356: aload         6
     358: bipush        9
     360: bipush        10
     362: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     365: aastore
     366: aload         6
     368: bipush        10
     370: bipush        11
     372: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     375: aastore
     376: aload         6
     378: bipush        11
     380: bipush        12
     382: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     385: aastore
     386: aload         6
     388: bipush        12
     390: bipush        13
     392: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     395: aastore
     396: aload         6
     398: bipush        13
     400: bipush        14
     402: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     405: aastore
     406: aload         6
     408: bipush        14
     410: bipush        15
     412: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     415: aastore
     416: aload         6
     418: bipush        15
     420: bipush        16
     422: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     425: aastore
     426: aload         6
     428: bipush        16
     430: bipush        17
     432: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     435: aastore
     436: aload         6
     438: bipush        17
     440: bipush        18
     442: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     445: aastore
     446: aload         6
     448: bipush        18
     450: bipush        19
     452: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     455: aastore
     456: aload         6
     458: bipush        19
     460: bipush        20
     462: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     465: aastore
     466: aload         6
     468: bipush        20
     470: bipush        30
     472: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     475: aastore
     476: aload         6
     478: bipush        21
     480: bipush        40
     482: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     485: aastore
     486: aload         6
     488: bipush        22
     490: bipush        50
     492: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     495: aastore
     496: aload         6
     498: bipush        23
     500: bipush        60
     502: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     505: aastore
     506: aload         6
     508: bipush        24
     510: bipush        70
     512: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     515: aastore
     516: aload         6
     518: bipush        25
     520: bipush        80
     522: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     525: aastore
     526: aload         6
     528: bipush        26
     530: bipush        90
     532: invokestatic  #96                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     535: aastore
     536: aload         6
     538: astore        5
     540: iload_1
     541: ifne          547
     544: ldc           #98                 // String Zero
     546: areturn
     547: aload_0
     548: iload_1
     549: aload_3
     550: aload_2
     551: aload         5
     553: invokevirtual #102                // Method findResult:(I[Ljava/lang/Long;[Ljava/lang/String;[Ljava/lang/Integer;)Ljava/lang/String;
     556: astore        6
     558: aload         6
     560: iconst_0
     561: aload         6
     563: invokevirtual #106                // Method java/lang/String.length:()I
     566: iconst_1
     567: isub
     568: invokevirtual #110                // Method java/lang/String.substring:(II)Ljava/lang/String;
     571: dup
     572: ldc           #112                // String substring(...)
     574: invokestatic  #118                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     577: areturn

  public final java.lang.String findResult(int, java.lang.Long[], java.lang.String[], java.lang.Integer[]);
    Code:
       0: aload_2
       1: ldc           #133                // String numberOfUnit
       3: invokestatic  #136                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_3
       7: ldc           #137                // String unit
       9: invokestatic  #136                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload         4
      14: ldc           #138                // String numberOfWord
      16: invokestatic  #136                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      19: iconst_0
      20: istore        5
      22: ldc           #140                // String
      24: astore        6
      26: iload_1
      27: istore        7
      29: iload         7
      31: bipush        100
      33: if_icmplt     183
      36: iload         5
      38: aload_2
      39: arraylength
      40: if_icmpge     263
      43: iload         7
      45: i2l
      46: aload_2
      47: iload         5
      49: aaload
      50: invokevirtual #144                // Method java/lang/Long.longValue:()J
      53: ldiv
      54: lstore        8
      56: lload         8
      58: lconst_0
      59: lcmp
      60: ifle          177
      63: new           #146                // class java/lang/StringBuilder
      66: dup
      67: invokespecial #147                // Method java/lang/StringBuilder."<init>":()V
      70: aload         6
      72: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      75: aload_0
      76: lload         8
      78: l2i
      79: aload_2
      80: aload_3
      81: aload         4
      83: invokevirtual #102                // Method findResult:(I[Ljava/lang/Long;[Ljava/lang/String;[Ljava/lang/Integer;)Ljava/lang/String;
      86: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      89: aload_3
      90: iload         5
      92: aaload
      93: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      96: bipush        32
      98: invokevirtual #154                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     101: invokevirtual #158                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     104: astore        6
     106: iload         7
     108: lload         8
     110: aload_2
     111: iload         5
     113: aaload
     114: invokevirtual #144                // Method java/lang/Long.longValue:()J
     117: lmul
     118: l2i
     119: isub
     120: istore        7
     122: iconst_1
     123: iload         7
     125: if_icmpgt     143
     128: iload         7
     130: bipush        100
     132: if_icmpge     139
     135: iconst_1
     136: goto          144
     139: iconst_0
     140: goto          144
     143: iconst_0
     144: ifeq          177
     147: new           #146                // class java/lang/StringBuilder
     150: dup
     151: invokespecial #147                // Method java/lang/StringBuilder."<init>":()V
     154: aload         6
     156: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     159: aload_0
     160: iload         7
     162: aload_2
     163: aload_3
     164: aload         4
     166: invokevirtual #102                // Method findResult:(I[Ljava/lang/Long;[Ljava/lang/String;[Ljava/lang/Integer;)Ljava/lang/String;
     169: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     172: invokevirtual #158                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     175: astore        6
     177: iinc          5, 1
     180: goto          36
     183: aload         4
     185: arraylength
     186: iconst_1
     187: isub
     188: istore        8
     190: iload         8
     192: iflt          263
     195: iload         7
     197: aload         4
     199: iload         8
     201: aaload
     202: invokevirtual #161                // Method java/lang/Integer.intValue:()I
     205: isub
     206: istore        9
     208: iload         9
     210: iflt          257
     213: new           #146                // class java/lang/StringBuilder
     216: dup
     217: invokespecial #147                // Method java/lang/StringBuilder."<init>":()V
     220: aload         6
     222: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     225: aload_0
     226: aload         4
     228: iload         8
     230: aaload
     231: invokevirtual #161                // Method java/lang/Integer.intValue:()I
     234: invokevirtual #164                // Method findWord:(I)Ljava/lang/String;
     237: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     240: aload_0
     241: iload         9
     243: invokevirtual #164                // Method findWord:(I)Ljava/lang/String;
     246: invokevirtual #151                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     249: invokevirtual #158                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     252: astore        6
     254: goto          263
     257: iinc          8, -1
     260: goto          190
     263: aload         6
     265: areturn

  public final java.lang.String findWord(int);
    Code:
       0: iload_1
       1: lookupswitch  { // 27
                     1: 228
                     2: 233
                     3: 238
                     4: 243
                     5: 248
                     6: 253
                     7: 258
                     8: 263
                     9: 268
                    10: 273
                    11: 278
                    12: 283
                    13: 288
                    14: 293
                    15: 298
                    16: 303
                    17: 308
                    18: 313
                    19: 318
                    20: 323
                    30: 328
                    40: 333
                    50: 338
                    60: 343
                    70: 348
                    80: 353
                    90: 358
               default: 363
          }
     228: ldc           #171                // String One
     230: goto          365
     233: ldc           #173                // String Two
     235: goto          365
     238: ldc           #175                // String Three
     240: goto          365
     243: ldc           #177                // String Four
     245: goto          365
     248: ldc           #179                // String Five
     250: goto          365
     253: ldc           #181                // String Six
     255: goto          365
     258: ldc           #183                // String Seven
     260: goto          365
     263: ldc           #185                // String Eight
     265: goto          365
     268: ldc           #187                // String Nine
     270: goto          365
     273: ldc           #189                // String Ten
     275: goto          365
     278: ldc           #191                // String Eleven
     280: goto          365
     283: ldc           #193                // String Twelve
     285: goto          365
     288: ldc           #195                // String Thirteen
     290: goto          365
     293: ldc           #197                // String Fourteen
     295: goto          365
     298: ldc           #199                // String Fifteen
     300: goto          365
     303: ldc           #201                // String Sixteen
     305: goto          365
     308: ldc           #203                // String Seventeen
     310: goto          365
     313: ldc           #205                // String Eighteen
     315: goto          365
     318: ldc           #207                // String Nineteen
     320: goto          365
     323: ldc           #209                // String Twenty
     325: goto          365
     328: ldc           #211                // String Thirty
     330: goto          365
     333: ldc           #213                // String Forty
     335: goto          365
     338: ldc           #215                // String Fifty
     340: goto          365
     343: ldc           #217                // String Sixty
     345: goto          365
     348: ldc           #219                // String Seventy
     350: goto          365
     353: ldc           #221                // String Eighty
     355: goto          365
     358: ldc           #223                // String Ninety
     360: goto          365
     363: ldc           #140                // String
     365: areturn
}


package org.baichuan.sample.algorithms.leetcode.simple.interview

/**
 * 面试题 17.10. 主要元素
 * https://leetcode.cn/problems/find-majority-element-lcci/
 */
class MajorityElement {
    /**
     * 摩尔投票解法
     * 遍历，并对不同的元素进行抵消。
     * 核心思路是：
     *    1. **如果y的个数超过数组一半大小**，则经过抵消后剩下的数一定是y。
     *    2. 如果个数超过一半大小的数不存在。则抵消后最后剩下的数可能是数组中任何一个数。因为抵消是随机的
     *
     * 所以最后要对这个剩下的数的数目进行验证，就可以判断是否存在满足条件的数以及这个数的值
     */
    fun majorityElement(nums: IntArray): Int {
        var x = 0
        var y = 0
        for (num in nums) {
            if (x == 0) {
                y = num
                x = 1
            } else {
                x += if (y == num) 1 else -1
            }
        }
        x = 0
        for (num in nums) if (y == num) x++
        return if (nums.size / 2 < x) y else -1
    }
}

Compiled from "MajorityElement.kt"
public final class org.baichuan.sample.algorithms.leetcode.simple.interview.MajorityElement {
  public org.baichuan.sample.algorithms.leetcode.simple.interview.MajorityElement();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int majorityElement(int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String nums
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: iconst_0
       9: istore_3
      10: iconst_0
      11: istore        4
      13: aload_1
      14: arraylength
      15: istore        5
      17: iload         4
      19: iload         5
      21: if_icmpge     62
      24: aload_1
      25: iload         4
      27: iaload
      28: istore        6
      30: iload_2
      31: ifne          42
      34: iload         6
      36: istore_3
      37: iconst_1
      38: istore_2
      39: goto          56
      42: iload_2
      43: iload_3
      44: iload         6
      46: if_icmpne     53
      49: iconst_1
      50: goto          54
      53: iconst_m1
      54: iadd
      55: istore_2
      56: iinc          4, 1
      59: goto          17
      62: iconst_0
      63: istore_2
      64: iconst_0
      65: istore        4
      67: aload_1
      68: arraylength
      69: istore        5
      71: iload         4
      73: iload         5
      75: if_icmpge     99
      78: aload_1
      79: iload         4
      81: iaload
      82: istore        6
      84: iload_3
      85: iload         6
      87: if_icmpne     93
      90: iinc          2, 1
      93: iinc          4, 1
      96: goto          71
      99: aload_1
     100: arraylength
     101: iconst_2
     102: idiv
     103: iload_2
     104: if_icmpge     111
     107: iload_3
     108: goto          112
     111: iconst_m1
     112: ireturn
}


package org.baichuan.sample.algorithms.leetcode.simple.interview

/**
 * https://leetcode.cn/problems/the-masseuse-lcci/
 * 面试题 17.16. 按摩师
 */
class Massage {
    /**
     * 时间复杂度：O(n)
     * 空间复杂度：O(n)
     * 状态转移方程： dp[i] = max(dp[i-1],dp[i-2])
     */
    fun massage(nums: IntArray): Int {
        if (nums.isEmpty()) {
            return 0
        }
        if (nums.size == 1) {
            return nums[0]
        }
        val dp = IntArray(nums.size)
        dp[0] = nums[0]
        dp[1] = Math.max(nums[0], nums[1])
        for (i in 2 until nums.size) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i])
        }
        return dp[nums.size - 1]
    }

    /**
     * 时间复杂度：O(n)
     * 空间复杂度：O(1)
     * 状态转移方程： dp[i] = max(dp[i-1],dp[i-2])
     * 从递推关系来看，每次计算时需要保存的状态只有3个：dp[i] dp[i-1] dp[i-2]，所以可以考虑每次循环可以丢弃i-3
     * 所以只需要保证每次循环，i i-1 i-2这3个连续的数对应的结果在dp里，并且能通过下表找到对应的值即可。而dp本身只有0,1,2几个空位。
     * 而x%3 = (x-3)%3。而且在i连续的情况下取模产生的数也是连续的。所以，dp[i%3] = max(dp[(i-1)%3],dp[(i-2)%3])
     */
    fun massage1(nums: IntArray): Int {
        if (nums.isEmpty()) {
            return 0
        }
        if (nums.size == 1) {
            return nums[0]
        }
        val dp = IntArray(3)
        dp[0] = nums[0]
        dp[1] = Math.max(nums[0], nums[1])
        for (i in 2 until nums.size) {
            dp[i % 3] = Math.max(dp[(i - 1) % 3], dp[(i - 2) % 3] + nums[i])
        }
        return dp[(nums.size - 1) % 3]
    }
}

fun main() {

}

Compiled from "Massage.kt"
public final class org.baichuan.sample.algorithms.leetcode.simple.interview.MassageKt {
  public static final void main();
    Code:
       0: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #9                  // Method main:()V
       3: return
}

Compiled from "Massage.kt"
public final class org.baichuan.sample.algorithms.leetcode.simple.interview.Massage {
  public org.baichuan.sample.algorithms.leetcode.simple.interview.Massage();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final int massage(int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String nums
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          21
      19: iconst_0
      20: ireturn
      21: aload_1
      22: arraylength
      23: iconst_1
      24: if_icmpne     31
      27: aload_1
      28: iconst_0
      29: iaload
      30: ireturn
      31: aload_1
      32: arraylength
      33: newarray       int
      35: astore_2
      36: aload_2
      37: iconst_0
      38: aload_1
      39: iconst_0
      40: iaload
      41: iastore
      42: aload_2
      43: iconst_1
      44: aload_1
      45: iconst_0
      46: iaload
      47: aload_1
      48: iconst_1
      49: iaload
      50: invokestatic  #27                 // Method java/lang/Math.max:(II)I
      53: iastore
      54: iconst_2
      55: istore_3
      56: aload_1
      57: arraylength
      58: istore        4
      60: iload_3
      61: iload         4
      63: if_icmpge     92
      66: aload_2
      67: iload_3
      68: aload_2
      69: iload_3
      70: iconst_1
      71: isub
      72: iaload
      73: aload_2
      74: iload_3
      75: iconst_2
      76: isub
      77: iaload
      78: aload_1
      79: iload_3
      80: iaload
      81: iadd
      82: invokestatic  #27                 // Method java/lang/Math.max:(II)I
      85: iastore
      86: iinc          3, 1
      89: goto          60
      92: aload_2
      93: aload_1
      94: arraylength
      95: iconst_1
      96: isub
      97: iaload
      98: ireturn

  public final int massage1(int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String nums
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          21
      19: iconst_0
      20: ireturn
      21: aload_1
      22: arraylength
      23: iconst_1
      24: if_icmpne     31
      27: aload_1
      28: iconst_0
      29: iaload
      30: ireturn
      31: iconst_3
      32: newarray       int
      34: astore_2
      35: aload_2
      36: iconst_0
      37: aload_1
      38: iconst_0
      39: iaload
      40: iastore
      41: aload_2
      42: iconst_1
      43: aload_1
      44: iconst_0
      45: iaload
      46: aload_1
      47: iconst_1
      48: iaload
      49: invokestatic  #27                 // Method java/lang/Math.max:(II)I
      52: iastore
      53: iconst_2
      54: istore_3
      55: aload_1
      56: arraylength
      57: istore        4
      59: iload_3
      60: iload         4
      62: if_icmpge     97
      65: aload_2
      66: iload_3
      67: iconst_3
      68: irem
      69: aload_2
      70: iload_3
      71: iconst_1
      72: isub
      73: iconst_3
      74: irem
      75: iaload
      76: aload_2
      77: iload_3
      78: iconst_2
      79: isub
      80: iconst_3
      81: irem
      82: iaload
      83: aload_1
      84: iload_3
      85: iaload
      86: iadd
      87: invokestatic  #27                 // Method java/lang/Math.max:(II)I
      90: iastore
      91: iinc          3, 1
      94: goto          59
      97: aload_2
      98: aload_1
      99: arraylength
     100: iconst_1
     101: isub
     102: iconst_3
     103: irem
     104: iaload
     105: ireturn
}


package leetcode_problems.easy

import kotlin.math.*

/*Given an unsorted array,
return 1 if there exists a pair or more of elements in the array whose difference is 1.
else return 0

Examples:

-Input: arr[] = {5, 20, 3, 2, 6}
=Output: 1

-Input: arr[] = {90, 7, 2, 9, 50}
=Output: 0
*/

fun findPairDifference(nums: IntArray): Int {
// O(N*N) Solution
//    nums.forEach { i ->
//        val list = nums.toMutableList()
//        list.remove(i)
//        list.forEach { j ->
//            if (abs(i - j) == 1)
//                return 1
//        }
//    }
//    return 0
    val sorted = nums.sorted()
     //                      [2, 7,  20, 21, 22]
    var l = 0           //2
    var r = nums.size -1 //22
    while (l >= r){
        val middle = (l + r) / 2  // 20
        if (abs(sorted[middle] - sorted[l]) == 1)
            return 1
        else if (abs(sorted[middle] - sorted[l]) < 1)
            r = middle -1
        else
            l = middle + 1
    }
    return 0
}

fun main() {
    val result = findPairDifference(nums = intArrayOf( 7, 2, 20, 22, 21))
    println(result.toString())

}

Compiled from "FindGivenDifference.kt"
public final class leetcode_problems.easy.FindGivenDifferenceKt {
  public static final int findPairDifference(int[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String nums
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #21                 // Method kotlin/collections/ArraysKt.sorted:([I)Ljava/util/List;
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_0
      14: arraylength
      15: iconst_1
      16: isub
      17: istore_3
      18: iload_2
      19: iload_3
      20: if_icmplt     118
      23: iload_2
      24: iload_3
      25: iadd
      26: iconst_2
      27: idiv
      28: istore        4
      30: aload_1
      31: iload         4
      33: invokeinterface #27,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      38: checkcast     #29                 // class java/lang/Number
      41: invokevirtual #33                 // Method java/lang/Number.intValue:()I
      44: aload_1
      45: iload_2
      46: invokeinterface #27,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      51: checkcast     #29                 // class java/lang/Number
      54: invokevirtual #33                 // Method java/lang/Number.intValue:()I
      57: isub
      58: invokestatic  #39                 // Method java/lang/Math.abs:(I)I
      61: iconst_1
      62: if_icmpne     67
      65: iconst_1
      66: ireturn
      67: aload_1
      68: iload         4
      70: invokeinterface #27,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      75: checkcast     #29                 // class java/lang/Number
      78: invokevirtual #33                 // Method java/lang/Number.intValue:()I
      81: aload_1
      82: iload_2
      83: invokeinterface #27,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      88: checkcast     #29                 // class java/lang/Number
      91: invokevirtual #33                 // Method java/lang/Number.intValue:()I
      94: isub
      95: invokestatic  #39                 // Method java/lang/Math.abs:(I)I
      98: iconst_1
      99: if_icmpge     110
     102: iload         4
     104: iconst_1
     105: isub
     106: istore_3
     107: goto          18
     110: iload         4
     112: iconst_1
     113: iadd
     114: istore_2
     115: goto          18
     118: iconst_0
     119: ireturn

  public static final void main();
    Code:
       0: iconst_5
       1: newarray       int
       3: astore_1
       4: aload_1
       5: iconst_0
       6: bipush        7
       8: iastore
       9: aload_1
      10: iconst_1
      11: iconst_2
      12: iastore
      13: aload_1
      14: iconst_2
      15: bipush        20
      17: iastore
      18: aload_1
      19: iconst_3
      20: bipush        22
      22: iastore
      23: aload_1
      24: iconst_4
      25: bipush        21
      27: iastore
      28: aload_1
      29: invokestatic  #49                 // Method findPairDifference:([I)I
      32: istore_0
      33: iload_0
      34: invokestatic  #55                 // Method java/lang/String.valueOf:(I)Ljava/lang/String;
      37: getstatic     #61                 // Field java/lang/System.out:Ljava/io/PrintStream;
      40: swap
      41: invokevirtual #67                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      44: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #71                 // Method main:()V
       3: return
}