package iii_conventions

import java.time.Month

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
    override fun compareTo(date: MyDate): Int {
        if (this.year.compareTo(date.year) == 0) {
            if (this.month.compareTo(date.month) == 0) {
                if (this.dayOfMonth.compareTo(date.dayOfMonth) == 0) {
                    return 0
                }else{
                    return this.dayOfMonth.compareTo(date.dayOfMonth)
                }
            } else {
                return this.month.compareTo(date.month)
            }
        }
        return this.year.compareTo(date.year)
    }
}

operator fun MyDate.plus(b: MyDate) = MyDate(this.year + b.year, this.month + b.month, this.dayOfMonth + b.dayOfMonth)

operator fun MyDate.plus(b: TimeInterval) = when (b) {
    TimeInterval.DAY -> MyDate(this.year, this.month, this.dayOfMonth + 1)
    TimeInterval.WEEK -> MyDate(this.year + 1, this.month, this.dayOfMonth)
    TimeInterval.YEAR -> MyDate(this.year + 1, this.month, this.dayOfMonth)
    else -> MyDate(this.year, this.month, this.dayOfMonth)

}

operator fun MyDate.rangeTo(other: MyDate): DateRange = DateRange(this, other)

enum class TimeInterval {
    DAY,
    WEEK,
    YEAR
}

class DateRange(val start: MyDate, val endInclusive: MyDate) : Iterable<MyDate> {
    override fun iterator(): Iterator {
        return Iterator
    }

    companion object Iterator : kotlin.collections.Iterator<MyDate> {
        override fun next(): MyDate {
            if (this.hasNext()) {

            }
            return MyDate(1, 1, 1)
        }

        override fun hasNext(): Boolean {
            return false;
        }
    }

}

operator fun DateRange.contains(other: MyDate): Boolean {
    return other.compareTo(start) in 0..1 && other.compareTo(endInclusive) in -1..0
}

Compiled from "MyDate.kt"
public final class iii_conventions.MyDate implements java.lang.Comparable<iii_conventions.MyDate> {
  private final int year;

  private final int month;

  private final int dayOfMonth;

  public iii_conventions.MyDate(int, int, int);
    Code:
       0: aload_0
       1: invokespecial #12                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iload_1
       6: putfield      #16                 // Field year:I
       9: aload_0
      10: iload_2
      11: putfield      #19                 // Field month:I
      14: aload_0
      15: iload_3
      16: putfield      #22                 // Field dayOfMonth:I
      19: return

  public final int getYear();
    Code:
       0: aload_0
       1: getfield      #16                 // Field year:I
       4: ireturn

  public final int getMonth();
    Code:
       0: aload_0
       1: getfield      #19                 // Field month:I
       4: ireturn

  public final int getDayOfMonth();
    Code:
       0: aload_0
       1: getfield      #22                 // Field dayOfMonth:I
       4: ireturn

  public int compareTo(iii_conventions.MyDate);
    Code:
       0: aload_1
       1: ldc           #33                 // String date
       3: invokestatic  #39                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: getfield      #16                 // Field year:I
      10: aload_1
      11: getfield      #16                 // Field year:I
      14: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.compare:(II)I
      17: ifne          74
      20: aload_0
      21: getfield      #19                 // Field month:I
      24: aload_1
      25: getfield      #19                 // Field month:I
      28: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.compare:(II)I
      31: ifne          62
      34: aload_0
      35: getfield      #22                 // Field dayOfMonth:I
      38: aload_1
      39: getfield      #22                 // Field dayOfMonth:I
      42: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.compare:(II)I
      45: ifne          50
      48: iconst_0
      49: ireturn
      50: aload_0
      51: getfield      #22                 // Field dayOfMonth:I
      54: aload_1
      55: getfield      #22                 // Field dayOfMonth:I
      58: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.compare:(II)I
      61: ireturn
      62: aload_0
      63: getfield      #19                 // Field month:I
      66: aload_1
      67: getfield      #19                 // Field month:I
      70: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.compare:(II)I
      73: ireturn
      74: aload_0
      75: getfield      #16                 // Field year:I
      78: aload_1
      79: getfield      #16                 // Field year:I
      82: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.compare:(II)I
      85: ireturn

  public final int component1();
    Code:
       0: aload_0
       1: getfield      #16                 // Field year:I
       4: ireturn

  public final int component2();
    Code:
       0: aload_0
       1: getfield      #19                 // Field month:I
       4: ireturn

  public final int component3();
    Code:
       0: aload_0
       1: getfield      #22                 // Field dayOfMonth:I
       4: ireturn

  public final iii_conventions.MyDate copy(int, int, int);
    Code:
       0: new           #2                  // class iii_conventions/MyDate
       3: dup
       4: iload_1
       5: iload_2
       6: iload_3
       7: invokespecial #50                 // Method "<init>":(III)V
      10: areturn

  public static iii_conventions.MyDate copy$default(iii_conventions.MyDate, int, int, int, int, java.lang.Object);
    Code:
       0: iload         4
       2: iconst_1
       3: iand
       4: ifeq          12
       7: aload_0
       8: getfield      #16                 // Field year:I
      11: istore_1
      12: iload         4
      14: iconst_2
      15: iand
      16: ifeq          24
      19: aload_0
      20: getfield      #19                 // Field month:I
      23: istore_2
      24: iload         4
      26: iconst_4
      27: iand
      28: ifeq          36
      31: aload_0
      32: getfield      #22                 // Field dayOfMonth:I
      35: istore_3
      36: aload_0
      37: iload_1
      38: iload_2
      39: iload_3
      40: invokevirtual #54                 // Method copy:(III)Liii_conventions/MyDate;
      43: areturn

  public java.lang.String toString();
    Code:
       0: new           #58                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #59                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #61                 // String MyDate(year=
       9: invokevirtual #65                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: getfield      #16                 // Field year:I
      16: invokevirtual #68                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      19: ldc           #70                 // String , month=
      21: invokevirtual #65                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: aload_0
      25: getfield      #19                 // Field month:I
      28: invokevirtual #68                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      31: ldc           #72                 // String , dayOfMonth=
      33: invokevirtual #65                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      36: aload_0
      37: getfield      #22                 // Field dayOfMonth:I
      40: invokevirtual #68                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      43: bipush        41
      45: invokevirtual #75                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      48: invokevirtual #77                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      51: areturn

  public int hashCode();
    Code:
       0: aload_0
       1: getfield      #16                 // Field year:I
       4: invokestatic  #83                 // Method java/lang/Integer.hashCode:(I)I
       7: istore_1
       8: iload_1
       9: bipush        31
      11: imul
      12: aload_0
      13: getfield      #19                 // Field month:I
      16: invokestatic  #83                 // Method java/lang/Integer.hashCode:(I)I
      19: iadd
      20: istore_1
      21: iload_1
      22: bipush        31
      24: imul
      25: aload_0
      26: getfield      #22                 // Field dayOfMonth:I
      29: invokestatic  #83                 // Method java/lang/Integer.hashCode:(I)I
      32: iadd
      33: istore_1
      34: iload_1
      35: ireturn

  public boolean equals(java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: if_acmpne     7
       5: iconst_1
       6: ireturn
       7: aload_1
       8: instanceof    #2                  // class iii_conventions/MyDate
      11: ifne          16
      14: iconst_0
      15: ireturn
      16: aload_1
      17: checkcast     #2                  // class iii_conventions/MyDate
      20: astore_2
      21: aload_0
      22: getfield      #16                 // Field year:I
      25: aload_2
      26: getfield      #16                 // Field year:I
      29: if_icmpeq     34
      32: iconst_0
      33: ireturn
      34: aload_0
      35: getfield      #19                 // Field month:I
      38: aload_2
      39: getfield      #19                 // Field month:I
      42: if_icmpeq     47
      45: iconst_0
      46: ireturn
      47: aload_0
      48: getfield      #22                 // Field dayOfMonth:I
      51: aload_2
      52: getfield      #22                 // Field dayOfMonth:I
      55: if_icmpeq     60
      58: iconst_0
      59: ireturn
      60: iconst_1
      61: ireturn

  public int compareTo(java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: checkcast     #2                  // class iii_conventions/MyDate
       5: invokevirtual #92                 // Method compareTo:(Liii_conventions/MyDate;)I
       8: ireturn
}

Compiled from "MyDate.kt"
public final class iii_conventions.MyDateKt {
  public static final iii_conventions.MyDate plus(iii_conventions.MyDate, iii_conventions.MyDate);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String b
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: new           #19                 // class iii_conventions/MyDate
      15: dup
      16: aload_0
      17: invokevirtual #23                 // Method iii_conventions/MyDate.getYear:()I
      20: aload_1
      21: invokevirtual #23                 // Method iii_conventions/MyDate.getYear:()I
      24: iadd
      25: aload_0
      26: invokevirtual #26                 // Method iii_conventions/MyDate.getMonth:()I
      29: aload_1
      30: invokevirtual #26                 // Method iii_conventions/MyDate.getMonth:()I
      33: iadd
      34: aload_0
      35: invokevirtual #29                 // Method iii_conventions/MyDate.getDayOfMonth:()I
      38: aload_1
      39: invokevirtual #29                 // Method iii_conventions/MyDate.getDayOfMonth:()I
      42: iadd
      43: invokespecial #33                 // Method iii_conventions/MyDate."<init>":(III)V
      46: areturn

  public static final iii_conventions.MyDate plus(iii_conventions.MyDate, iii_conventions.TimeInterval);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String b
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: getstatic     #42                 // Field iii_conventions/MyDateKt$WhenMappings.$EnumSwitchMapping$0:[I
      16: swap
      17: invokevirtual #47                 // Method iii_conventions/TimeInterval.ordinal:()I
      20: iaload
      21: tableswitch   { // 1 to 3
                     1: 48
                     2: 72
                     3: 96
               default: 120
          }
      48: new           #19                 // class iii_conventions/MyDate
      51: dup
      52: aload_0
      53: invokevirtual #23                 // Method iii_conventions/MyDate.getYear:()I
      56: aload_0
      57: invokevirtual #26                 // Method iii_conventions/MyDate.getMonth:()I
      60: aload_0
      61: invokevirtual #29                 // Method iii_conventions/MyDate.getDayOfMonth:()I
      64: iconst_1
      65: iadd
      66: invokespecial #33                 // Method iii_conventions/MyDate."<init>":(III)V
      69: goto          139
      72: new           #19                 // class iii_conventions/MyDate
      75: dup
      76: aload_0
      77: invokevirtual #23                 // Method iii_conventions/MyDate.getYear:()I
      80: iconst_1
      81: iadd
      82: aload_0
      83: invokevirtual #26                 // Method iii_conventions/MyDate.getMonth:()I
      86: aload_0
      87: invokevirtual #29                 // Method iii_conventions/MyDate.getDayOfMonth:()I
      90: invokespecial #33                 // Method iii_conventions/MyDate."<init>":(III)V
      93: goto          139
      96: new           #19                 // class iii_conventions/MyDate
      99: dup
     100: aload_0
     101: invokevirtual #23                 // Method iii_conventions/MyDate.getYear:()I
     104: iconst_1
     105: iadd
     106: aload_0
     107: invokevirtual #26                 // Method iii_conventions/MyDate.getMonth:()I
     110: aload_0
     111: invokevirtual #29                 // Method iii_conventions/MyDate.getDayOfMonth:()I
     114: invokespecial #33                 // Method iii_conventions/MyDate."<init>":(III)V
     117: goto          139
     120: new           #19                 // class iii_conventions/MyDate
     123: dup
     124: aload_0
     125: invokevirtual #23                 // Method iii_conventions/MyDate.getYear:()I
     128: aload_0
     129: invokevirtual #26                 // Method iii_conventions/MyDate.getMonth:()I
     132: aload_0
     133: invokevirtual #29                 // Method iii_conventions/MyDate.getDayOfMonth:()I
     136: invokespecial #33                 // Method iii_conventions/MyDate."<init>":(III)V
     139: areturn

  public static final iii_conventions.DateRange rangeTo(iii_conventions.MyDate, iii_conventions.MyDate);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #52                 // String other
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: new           #54                 // class iii_conventions/DateRange
      15: dup
      16: aload_0
      17: aload_1
      18: invokespecial #57                 // Method iii_conventions/DateRange."<init>":(Liii_conventions/MyDate;Liii_conventions/MyDate;)V
      21: areturn

  public static final boolean contains(iii_conventions.DateRange, iii_conventions.MyDate);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #52                 // String other
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: aload_0
      14: invokevirtual #64                 // Method iii_conventions/DateRange.getStart:()Liii_conventions/MyDate;
      17: invokevirtual #68                 // Method iii_conventions/MyDate.compareTo:(Liii_conventions/MyDate;)I
      20: istore_2
      21: iconst_0
      22: iload_2
      23: if_icmpgt     39
      26: iload_2
      27: iconst_2
      28: if_icmpge     35
      31: iconst_1
      32: goto          40
      35: iconst_0
      36: goto          40
      39: iconst_0
      40: ifeq          78
      43: aload_1
      44: aload_0
      45: invokevirtual #71                 // Method iii_conventions/DateRange.getEndInclusive:()Liii_conventions/MyDate;
      48: invokevirtual #68                 // Method iii_conventions/MyDate.compareTo:(Liii_conventions/MyDate;)I
      51: istore_2
      52: iconst_m1
      53: iload_2
      54: if_icmpgt     70
      57: iload_2
      58: iconst_1
      59: if_icmpge     66
      62: iconst_1
      63: goto          71
      66: iconst_0
      67: goto          71
      70: iconst_0
      71: ifeq          78
      74: iconst_1
      75: goto          79
      78: iconst_0
      79: ireturn
}

Compiled from "MyDate.kt"
public final class iii_conventions.MyDateKt$WhenMappings {
  public static final int[] $EnumSwitchMapping$0;

  static {};
    Code:
       0: invokestatic  #14                 // Method iii_conventions/TimeInterval.values:()[Liii_conventions/TimeInterval;
       3: arraylength
       4: newarray       int
       6: astore_0
       7: nop
       8: aload_0
       9: getstatic     #18                 // Field iii_conventions/TimeInterval.DAY:Liii_conventions/TimeInterval;
      12: invokevirtual #22                 // Method iii_conventions/TimeInterval.ordinal:()I
      15: iconst_1
      16: iastore
      17: goto          21
      20: astore_1
      21: nop
      22: aload_0
      23: getstatic     #25                 // Field iii_conventions/TimeInterval.WEEK:Liii_conventions/TimeInterval;
      26: invokevirtual #22                 // Method iii_conventions/TimeInterval.ordinal:()I
      29: iconst_2
      30: iastore
      31: goto          35
      34: astore_1
      35: nop
      36: aload_0
      37: getstatic     #28                 // Field iii_conventions/TimeInterval.YEAR:Liii_conventions/TimeInterval;
      40: invokevirtual #22                 // Method iii_conventions/TimeInterval.ordinal:()I
      43: iconst_3
      44: iastore
      45: goto          49
      48: astore_1
      49: aload_0
      50: putstatic     #32                 // Field $EnumSwitchMapping$0:[I
      53: return
    Exception table:
       from    to  target type
           7    17    20   Class java/lang/NoSuchFieldError
          21    31    34   Class java/lang/NoSuchFieldError
          35    45    48   Class java/lang/NoSuchFieldError
}


package com.quakbo.euler

/*
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a^2 + b^2 = c^2

For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

According to Wikipedia, every Pythagorean triplet is expressed by

    a = m^2 - n^2, b = 2mn, c = m^2 + n^2

    where m > n > 0
    and m and n are coprime
    and m and n are both not odd

So an intelligent brute force approach is to increment m and n one at a time and find a, b, and c.
*/

fun main(args: Array<String>) {

    val sum = 1000L

    for (m in 2L..99L) {
        for (n in 1L until m) {
            val (a, b, c) = createPythagoreanTriplet(m, n) ?: continue

            if ((a + b + c) == sum) {
                println("Found ($a, $b, $c) with sum=${a + b + c} and product = ${a * b * c})")
                return
            }
        }
    }

    println("No pythagorean triplets found which sum to 1000.")
}

internal fun createPythagoreanTriplet(m: Long, n: Long): Triple<Long, Long, Long>? {
    if (m <= n || m < 1) {
        return null
    }

    if (isOdd(m) && isOdd(n)) {
        return null
    }

    if (coprime(m, n)) {
        return null
    }

    val a: Long = (m * m) - (n * n)
    val b: Long = 2 * m * n
    val c: Long = (m * m) + (n * n)

    return Triple(a, b, c)
}

private fun isOdd(i: Long): Boolean {
    return i % 2L != 0L
}

/** Return true if the greatest common divisor of m and n is 1. */
private fun coprime(m: Long, n: Long): Boolean {
    var gcd = 1
    var i = 1

    while (i <= m && i <= n) {
        if (m % i == 0L && n % i == 0L) {
            gcd = i
        }
        i++
    }

    return gcd == 1
}

Compiled from "Euler9.kt"
public final class com.quakbo.euler.Euler9Kt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc2_w        #16                 // long 1000l
       9: lstore_1
      10: ldc2_w        #18                 // long 2l
      13: lstore_3
      14: lload_3
      15: ldc2_w        #20                 // long 100l
      18: lcmp
      19: ifge          201
      22: lconst_1
      23: lstore        5
      25: lload         5
      27: lload_3
      28: lcmp
      29: ifge          194
      32: lload_3
      33: lload         5
      35: invokestatic  #25                 // Method createPythagoreanTriplet:(JJ)Lkotlin/Triple;
      38: dup
      39: ifnonnull     46
      42: pop
      43: goto          185
      46: astore        7
      48: aload         7
      50: invokevirtual #31                 // Method kotlin/Triple.component1:()Ljava/lang/Object;
      53: checkcast     #33                 // class java/lang/Number
      56: invokevirtual #37                 // Method java/lang/Number.longValue:()J
      59: lstore        8
      61: aload         7
      63: invokevirtual #40                 // Method kotlin/Triple.component2:()Ljava/lang/Object;
      66: checkcast     #33                 // class java/lang/Number
      69: invokevirtual #37                 // Method java/lang/Number.longValue:()J
      72: lstore        10
      74: aload         7
      76: invokevirtual #43                 // Method kotlin/Triple.component3:()Ljava/lang/Object;
      79: checkcast     #33                 // class java/lang/Number
      82: invokevirtual #37                 // Method java/lang/Number.longValue:()J
      85: lstore        12
      87: lload         8
      89: lload         10
      91: ladd
      92: lload         12
      94: ladd
      95: lload_1
      96: lcmp
      97: ifne          185
     100: new           #45                 // class java/lang/StringBuilder
     103: dup
     104: invokespecial #49                 // Method java/lang/StringBuilder."<init>":()V
     107: ldc           #51                 // String Found (
     109: invokevirtual #55                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     112: lload         8
     114: invokevirtual #58                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     117: ldc           #60                 // String ,
     119: invokevirtual #55                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     122: lload         10
     124: invokevirtual #58                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     127: ldc           #60                 // String ,
     129: invokevirtual #55                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     132: lload         12
     134: invokevirtual #58                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     137: ldc           #62                 // String ) with sum=
     139: invokevirtual #55                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     142: lload         8
     144: lload         10
     146: ladd
     147: lload         12
     149: ladd
     150: invokevirtual #58                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     153: ldc           #64                 // String  and product =
     155: invokevirtual #55                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     158: lload         8
     160: lload         10
     162: lmul
     163: lload         12
     165: lmul
     166: invokevirtual #58                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     169: bipush        41
     171: invokevirtual #67                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     174: invokevirtual #71                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     177: getstatic     #77                 // Field java/lang/System.out:Ljava/io/PrintStream;
     180: swap
     181: invokevirtual #83                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     184: return
     185: lload         5
     187: lconst_1
     188: ladd
     189: lstore        5
     191: goto          25
     194: lload_3
     195: lconst_1
     196: ladd
     197: lstore_3
     198: goto          14
     201: ldc           #85                 // String No pythagorean triplets found which sum to 1000.
     203: getstatic     #77                 // Field java/lang/System.out:Ljava/io/PrintStream;
     206: swap
     207: invokevirtual #83                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     210: return

  public static final kotlin.Triple<java.lang.Long, java.lang.Long, java.lang.Long> createPythagoreanTriplet(long, long);
    Code:
       0: lload_0
       1: lload_2
       2: lcmp
       3: ifle          12
       6: lload_0
       7: lconst_1
       8: lcmp
       9: ifge          14
      12: aconst_null
      13: areturn
      14: lload_0
      15: invokestatic  #99                 // Method isOdd:(J)Z
      18: ifeq          30
      21: lload_2
      22: invokestatic  #99                 // Method isOdd:(J)Z
      25: ifeq          30
      28: aconst_null
      29: areturn
      30: lload_0
      31: lload_2
      32: invokestatic  #103                // Method coprime:(JJ)Z
      35: ifeq          40
      38: aconst_null
      39: areturn
      40: lload_0
      41: lload_0
      42: lmul
      43: lload_2
      44: lload_2
      45: lmul
      46: lsub
      47: lstore        4
      49: iconst_2
      50: i2l
      51: lload_0
      52: lmul
      53: lload_2
      54: lmul
      55: lstore        6
      57: lload_0
      58: lload_0
      59: lmul
      60: lload_2
      61: lload_2
      62: lmul
      63: ladd
      64: lstore        8
      66: new           #27                 // class kotlin/Triple
      69: dup
      70: lload         4
      72: invokestatic  #109                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      75: lload         6
      77: invokestatic  #109                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      80: lload         8
      82: invokestatic  #109                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      85: invokespecial #112                // Method kotlin/Triple."<init>":(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
      88: areturn

  private static final boolean isOdd(long);
    Code:
       0: lload_0
       1: ldc2_w        #18                 // long 2l
       4: lrem
       5: lconst_0
       6: lcmp
       7: ifeq          14
      10: iconst_1
      11: goto          15
      14: iconst_0
      15: ireturn

  private static final boolean coprime(long, long);
    Code:
       0: iconst_1
       1: istore        4
       3: iconst_1
       4: istore        5
       6: iload         5
       8: i2l
       9: lload_0
      10: lcmp
      11: ifgt          52
      14: iload         5
      16: i2l
      17: lload_2
      18: lcmp
      19: ifgt          52
      22: lload_0
      23: iload         5
      25: i2l
      26: lrem
      27: lconst_0
      28: lcmp
      29: ifne          46
      32: lload_2
      33: iload         5
      35: i2l
      36: lrem
      37: lconst_0
      38: lcmp
      39: ifne          46
      42: iload         5
      44: istore        4
      46: iinc          5, 1
      49: goto          6
      52: iload         4
      54: iconst_1
      55: if_icmpne     62
      58: iconst_1
      59: goto          63
      62: iconst_0
      63: ireturn
}


package com.quakbo.euler

/*
The sum of the squares of the first ten natural numbers is,

1^2 + 2^2 + ... + 10^2 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^2 = 55^2 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
 */

private fun sumOfSquares(sequence: Sequence<Int>): Int {
    return sequence
            .map { it * it }
            .sum()
}

private fun squareOfSums(sequence: Sequence<Int>): Int {
    val sum = sequence.sum()
    return sum * sum
}

fun main(args: Array<String>) {
    val firstHundred = generateSequence(1, {i -> i + 1} ).take(100)
    println(squareOfSums(firstHundred) - sumOfSquares(firstHundred))
}

/*
Sequences are a lot like Streams, but you can consume them as many times as you like.  *hold for applause*
 */

Compiled from "Euler6.kt"
public final class com.quakbo.euler.Euler6Kt {
  private static final int sumOfSquares(kotlin.sequences.Sequence<java.lang.Integer>);
    Code:
       0: aload_0
       1: invokedynamic #27,  0             // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
       6: invokestatic  #33                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
       9: invokestatic  #36                 // Method kotlin/sequences/SequencesKt.sumOfInt:(Lkotlin/sequences/Sequence;)I
      12: ireturn

  private static final int squareOfSums(kotlin.sequences.Sequence<java.lang.Integer>);
    Code:
       0: aload_0
       1: invokestatic  #36                 // Method kotlin/sequences/SequencesKt.sumOfInt:(Lkotlin/sequences/Sequence;)I
       4: istore_1
       5: iload_1
       6: iload_1
       7: imul
       8: ireturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #46                 // String args
       3: invokestatic  #52                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_1
       7: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      10: invokedynamic #63,  0             // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function1;
      15: invokestatic  #67                 // Method kotlin/sequences/SequencesKt.generateSequence:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      18: bipush        100
      20: invokestatic  #71                 // Method kotlin/sequences/SequencesKt.take:(Lkotlin/sequences/Sequence;I)Lkotlin/sequences/Sequence;
      23: astore_1
      24: aload_1
      25: invokestatic  #73                 // Method squareOfSums:(Lkotlin/sequences/Sequence;)I
      28: aload_1
      29: invokestatic  #75                 // Method sumOfSquares:(Lkotlin/sequences/Sequence;)I
      32: isub
      33: istore_2
      34: getstatic     #81                 // Field java/lang/System.out:Ljava/io/PrintStream;
      37: iload_2
      38: invokevirtual #87                 // Method java/io/PrintStream.println:(I)V
      41: return

  private static final int sumOfSquares$lambda$0(int);
    Code:
       0: iload_0
       1: iload_0
       2: imul
       3: ireturn

  private static final java.lang.Integer main$lambda$1(int);
    Code:
       0: iload_0
       1: iconst_1
       2: iadd
       3: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       6: areturn
}


package com.quakbo.euler

/*
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.
 */

internal fun isPalindrome(n: Int): Boolean {
    val s = n.toString()
    return s.reversed() == s
}

fun main(args: Array<String>) {
    done@ for (i in 999 downTo 900) {
        for (j in 999 downTo 900) {
            val product = i * j
            if (isPalindrome(product)) {
                println(product)
                break@done
            }
        }
    }
}

/*
Earlier was saw the range operator which creates a Sequence.

    1..999

We can also create a sequence of decreasing values with the downTo operator.

In Kotlin we write a foreach loop as

    for (v in someRangeOfValues)

The in operator can be used in the following ways

 - specifies the object being iterated in a for loop
 - is used as an infix operator to check that a value belongs to a range, a collection or another entity that defines the 'contains' method
 - is used in when expressions for the same purpose

done@ is a label to help with flow control.  When we say break@done, we're saying break out of the loop labeled with done@.
Without the label we would have broken out of the loop at line 16 and begun another iteration of the outer for loop.

There are implicit labels for loop constructs.  So you can do things like this...

    while (foo < 100) {
        for (i in 1..foo) {
            if (foo == 2) { continue@while }
        }
    }

If blocks are known as conditional expressions because they return a value.  So you are allowed to assign from them.

fun maxOf(a: Int, b: Int) = if (a > b) a else b
 */

Compiled from "Euler4.kt"
public final class com.quakbo.euler.Euler4Kt {
  public static final boolean isPalindrome(int);
    Code:
       0: iload_0
       1: invokestatic  #12                 // Method java/lang/String.valueOf:(I)Ljava/lang/String;
       4: astore_1
       5: aload_1
       6: checkcast     #14                 // class java/lang/CharSequence
       9: invokestatic  #20                 // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      12: invokevirtual #24                 // Method java/lang/Object.toString:()Ljava/lang/String;
      15: aload_1
      16: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      19: ireturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #39                 // String args
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: sipush        999
       9: istore_1
      10: sipush        899
      13: iload_1
      14: if_icmpge     61
      17: sipush        999
      20: istore_2
      21: sipush        899
      24: iload_2
      25: if_icmpge     55
      28: iload_1
      29: iload_2
      30: imul
      31: istore_3
      32: iload_3
      33: invokestatic  #45                 // Method isPalindrome:(I)Z
      36: ifeq          49
      39: getstatic     #51                 // Field java/lang/System.out:Ljava/io/PrintStream;
      42: iload_3
      43: invokevirtual #57                 // Method java/io/PrintStream.println:(I)V
      46: goto          61
      49: iinc          2, -1
      52: goto          21
      55: iinc          1, -1
      58: goto          10
      61: return
}


package com.hlq.leetcode.array

/**
 * @author HLQ_Struggle
 * @date 2020/11/30
 * @desc LeetCode 26. 删除排序数组中的重复项 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
 */

fun main() {
    val nums = intArrayOf(0,0,1,1,1,2,2,3,3,4)

    println("----> ${removeDuplicates(nums)}")
}

/**
 * 双指针解法
 */
fun removeDuplicates(nums: IntArray): Int {
    if(nums.isEmpty()){
        return 0
    }
    var resultCount = 0
    for(i in nums.indices){
        if(nums[i] != nums[resultCount]){
            nums[++resultCount] = nums[i]
        }
    }
    return ++resultCount
}

/**
 * LeetCode 执行用时范例
 */
fun removeDuplicates1(nums: IntArray): Int {
    if (nums.isEmpty()) return 0
    var i = 0
    for (j in 1 until nums.size) {
        if (nums[j] != nums[i]) {
            i++
            nums[i] = nums[j]
        }
    }
    return i + 1
}

/**
 * LeetCode 执行消耗内存范例
 */
fun removeDuplicates2(nums: IntArray): Int {
    if(nums == null){
        return 0
    }
    if(nums.size == 1){
        return 1
    }
    var i = 0
    for(j in 1 until nums.size){
        if(nums[i] != nums[j]){
            i++
            nums[i] = nums[j]
        }
    }
    return i+1
}

Compiled from "Question26.kt"
public final class com.hlq.leetcode.array.Question26Kt {
  public static final void main();
    Code:
       0: bipush        10
       2: newarray       int
       4: astore_1
       5: aload_1
       6: iconst_0
       7: iconst_0
       8: iastore
       9: aload_1
      10: iconst_1
      11: iconst_0
      12: iastore
      13: aload_1
      14: iconst_2
      15: iconst_1
      16: iastore
      17: aload_1
      18: iconst_3
      19: iconst_1
      20: iastore
      21: aload_1
      22: iconst_4
      23: iconst_1
      24: iastore
      25: aload_1
      26: iconst_5
      27: iconst_2
      28: iastore
      29: aload_1
      30: bipush        6
      32: iconst_2
      33: iastore
      34: aload_1
      35: bipush        7
      37: iconst_3
      38: iastore
      39: aload_1
      40: bipush        8
      42: iconst_3
      43: iastore
      44: aload_1
      45: bipush        9
      47: iconst_4
      48: iastore
      49: aload_1
      50: astore_0
      51: new           #8                  // class java/lang/StringBuilder
      54: dup
      55: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
      58: ldc           #13                 // String ---->
      60: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      63: aload_0
      64: invokestatic  #21                 // Method removeDuplicates:([I)I
      67: invokevirtual #24                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      70: invokevirtual #28                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      73: getstatic     #34                 // Field java/lang/System.out:Ljava/io/PrintStream;
      76: swap
      77: invokevirtual #40                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      80: return

  public static final int removeDuplicates(int[]);
    Code:
       0: aload_0
       1: ldc           #44                 // String nums
       3: invokestatic  #50                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          21
      19: iconst_0
      20: ireturn
      21: iconst_0
      22: istore_1
      23: iconst_0
      24: istore_2
      25: aload_0
      26: arraylength
      27: istore_3
      28: iload_2
      29: iload_3
      30: if_icmpge     57
      33: aload_0
      34: iload_2
      35: iaload
      36: aload_0
      37: iload_1
      38: iaload
      39: if_icmpeq     51
      42: aload_0
      43: iinc          1, 1
      46: iload_1
      47: aload_0
      48: iload_2
      49: iaload
      50: iastore
      51: iinc          2, 1
      54: goto          28
      57: iinc          1, 1
      60: iload_1
      61: ireturn

  public static final int removeDuplicates1(int[]);
    Code:
       0: aload_0
       1: ldc           #44                 // String nums
       3: invokestatic  #50                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          21
      19: iconst_0
      20: ireturn
      21: iconst_0
      22: istore_1
      23: iconst_1
      24: istore_2
      25: aload_0
      26: arraylength
      27: istore_3
      28: iload_2
      29: iload_3
      30: if_icmpge     57
      33: aload_0
      34: iload_2
      35: iaload
      36: aload_0
      37: iload_1
      38: iaload
      39: if_icmpeq     51
      42: iinc          1, 1
      45: aload_0
      46: iload_1
      47: aload_0
      48: iload_2
      49: iaload
      50: iastore
      51: iinc          2, 1
      54: goto          28
      57: iload_1
      58: iconst_1
      59: iadd
      60: ireturn

  public static final int removeDuplicates2(int[]);
    Code:
       0: aload_0
       1: ldc           #44                 // String nums
       3: invokestatic  #50                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: nop
       7: aload_0
       8: arraylength
       9: iconst_1
      10: if_icmpne     15
      13: iconst_1
      14: ireturn
      15: iconst_0
      16: istore_1
      17: iconst_1
      18: istore_2
      19: aload_0
      20: arraylength
      21: istore_3
      22: iload_2
      23: iload_3
      24: if_icmpge     51
      27: aload_0
      28: iload_1
      29: iaload
      30: aload_0
      31: iload_2
      32: iaload
      33: if_icmpeq     45
      36: iinc          1, 1
      39: aload_0
      40: iload_1
      41: aload_0
      42: iload_2
      43: iaload
      44: iastore
      45: iinc          2, 1
      48: goto          22
      51: iload_1
      52: iconst_1
      53: iadd
      54: ireturn

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


package com.hlq.leetcode.array

/**
 * @author HLQ_Struggle
 * @date 2020/11/08
 * @desc LeetCode 349. 两个数组的交集 https://leetcode-cn.com/problems/intersection-of-two-arrays/
 */
fun main() {
    val nums1 = IntArray(4)
    nums1[0] = 1
    nums1[1] = 2
    nums1[2] = 2
    nums1[3] = 1

    val nums2 = IntArray(2)
    nums2[0] = 7
    nums2[1] = 9

    // Set + contains
    intersection(nums1, nums2)
    // Kotlin intersect
    intersection1(nums1, nums2)
    intersection2(nums1, nums2)
    // 排序 + 双指针
    intersection3(nums1, nums2)
}

/**
 * 排序 + 双指针
 */
fun intersection3(nums1: IntArray, nums2: IntArray): IntArray {
    if (nums1.isEmpty() || nums2.isEmpty()) {
        return IntArray(0)
    }
    var i = 0
    var j = 0
    var index = 0
    var resultSet = hashSetOf<Int>()
    nums1.sort()
    nums2.sort()
    while (i < nums1.size && j < nums2.size) {
        if (nums1[i] == nums2[j]) {
            resultSet.add(nums1[i])
            i++
            j++
        } else if (nums1[i] < nums2[j]) {
            i++
        } else if (nums1[i] > nums2[j]) {
            j++
        }
    }
    var resultArray = IntArray(resultSet.size)
    for (resultNum in resultSet) {
        resultArray[index++] = resultNum
    }
    println(resultArray.asList())
    return resultArray
}

/**
 * Kotlin intersect
 */
fun intersection2(nums1: IntArray, nums2: IntArray): IntArray {
    if (nums1.isEmpty() || nums2.isEmpty()) {
        return IntArray(0)
    }
    return nums1.intersect(nums2.asList()).toIntArray()
}

/**
 * Kotlin intersect
 */
fun intersection1(nums1: IntArray, nums2: IntArray): IntArray {
    if (nums1.isEmpty() || nums2.isEmpty()) {
        return IntArray(0)
    }
    val tempSet = nums1.asList().intersect(nums2.asList())
    var resultArray = IntArray(tempSet.size)
    var index = 0
    for (result in tempSet) {
        resultArray[index++] = result
    }
    println(resultArray.asList())
    return resultArray
}

/**
 * Set + contains
 */
fun intersection(nums1: IntArray, nums2: IntArray): IntArray {
    if (nums1.isEmpty() || nums2.isEmpty()) {
        return IntArray(0)
    }
    var tempSet = hashSetOf<Int>()
    var resultSet = hashSetOf<Int>()
    for (num1 in nums1) {
        tempSet.add(num1)
    }
    for (num2 in nums2) {
        if (tempSet.contains(num2)) {
            resultSet.add(num2)
        }
    }
    var resultIntArray = IntArray(resultSet.size)
    var index = 0
    for (resultNum in resultSet) {
        resultIntArray[index++] = resultNum
    }
    println(resultIntArray.asList())
    return resultIntArray
}

Compiled from "Question349.kt"
public final class com.hlq.leetcode.array.Question349Kt {
  public static final void main();
    Code:
       0: iconst_4
       1: newarray       int
       3: astore_0
       4: aload_0
       5: iconst_0
       6: iconst_1
       7: iastore
       8: aload_0
       9: iconst_1
      10: iconst_2
      11: iastore
      12: aload_0
      13: iconst_2
      14: iconst_2
      15: iastore
      16: aload_0
      17: iconst_3
      18: iconst_1
      19: iastore
      20: iconst_2
      21: newarray       int
      23: astore_1
      24: aload_1
      25: iconst_0
      26: bipush        7
      28: iastore
      29: aload_1
      30: iconst_1
      31: bipush        9
      33: iastore
      34: aload_0
      35: aload_1
      36: invokestatic  #10                 // Method intersection:([I[I)[I
      39: pop
      40: aload_0
      41: aload_1
      42: invokestatic  #13                 // Method intersection1:([I[I)[I
      45: pop
      46: aload_0
      47: aload_1
      48: invokestatic  #16                 // Method intersection2:([I[I)[I
      51: pop
      52: aload_0
      53: aload_1
      54: invokestatic  #19                 // Method intersection3:([I[I)[I
      57: pop
      58: return

  public static final int[] intersection3(int[], int[]);
    Code:
       0: aload_0
       1: ldc           #24                 // String nums1
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #31                 // String nums2
       9: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: arraylength
      14: ifne          21
      17: iconst_1
      18: goto          22
      21: iconst_0
      22: ifne          38
      25: aload_1
      26: arraylength
      27: ifne          34
      30: iconst_1
      31: goto          35
      34: iconst_0
      35: ifeq          42
      38: iconst_0
      39: newarray       int
      41: areturn
      42: iconst_0
      43: istore_2
      44: iconst_0
      45: istore_3
      46: iconst_0
      47: istore        4
      49: new           #33                 // class java/util/HashSet
      52: dup
      53: invokespecial #36                 // Method java/util/HashSet."<init>":()V
      56: astore        5
      58: aload_0
      59: invokestatic  #42                 // Method kotlin/collections/ArraysKt.sort:([I)V
      62: aload_1
      63: invokestatic  #42                 // Method kotlin/collections/ArraysKt.sort:([I)V
      66: iload_2
      67: aload_0
      68: arraylength
      69: if_icmpge     138
      72: iload_3
      73: aload_1
      74: arraylength
      75: if_icmpge     138
      78: aload_0
      79: iload_2
      80: iaload
      81: aload_1
      82: iload_3
      83: iaload
      84: if_icmpne     108
      87: aload         5
      89: aload_0
      90: iload_2
      91: iaload
      92: invokestatic  #48                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      95: invokevirtual #52                 // Method java/util/HashSet.add:(Ljava/lang/Object;)Z
      98: pop
      99: iinc          2, 1
     102: iinc          3, 1
     105: goto          66
     108: aload_0
     109: iload_2
     110: iaload
     111: aload_1
     112: iload_3
     113: iaload
     114: if_icmpge     123
     117: iinc          2, 1
     120: goto          66
     123: aload_0
     124: iload_2
     125: iaload
     126: aload_1
     127: iload_3
     128: iaload
     129: if_icmple     66
     132: iinc          3, 1
     135: goto          66
     138: aload         5
     140: invokevirtual #56                 // Method java/util/HashSet.size:()I
     143: newarray       int
     145: astore        6
     147: aload         5
     149: invokevirtual #60                 // Method java/util/HashSet.iterator:()Ljava/util/Iterator;
     152: dup
     153: ldc           #62                 // String iterator(...)
     155: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     158: astore        7
     160: aload         7
     162: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     167: ifeq          204
     170: aload         7
     172: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     177: dup
     178: ldc           #77                 // String next(...)
     180: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     183: checkcast     #79                 // class java/lang/Number
     186: invokevirtual #82                 // Method java/lang/Number.intValue:()I
     189: istore        8
     191: aload         6
     193: iload         4
     195: iinc          4, 1
     198: iload         8
     200: iastore
     201: goto          160
     204: aload         6
     206: invokestatic  #86                 // Method kotlin/collections/ArraysKt.asList:([I)Ljava/util/List;
     209: getstatic     #92                 // Field java/lang/System.out:Ljava/io/PrintStream;
     212: swap
     213: invokevirtual #98                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     216: aload         6
     218: areturn

  public static final int[] intersection2(int[], int[]);
    Code:
       0: aload_0
       1: ldc           #24                 // String nums1
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #31                 // String nums2
       9: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: arraylength
      14: ifne          21
      17: iconst_1
      18: goto          22
      21: iconst_0
      22: ifne          38
      25: aload_1
      26: arraylength
      27: ifne          34
      30: iconst_1
      31: goto          35
      34: iconst_0
      35: ifeq          42
      38: iconst_0
      39: newarray       int
      41: areturn
      42: aload_0
      43: aload_1
      44: invokestatic  #86                 // Method kotlin/collections/ArraysKt.asList:([I)Ljava/util/List;
      47: checkcast     #109                // class java/lang/Iterable
      50: invokestatic  #113                // Method kotlin/collections/ArraysKt.intersect:([ILjava/lang/Iterable;)Ljava/util/Set;
      53: checkcast     #115                // class java/util/Collection
      56: invokestatic  #121                // Method kotlin/collections/CollectionsKt.toIntArray:(Ljava/util/Collection;)[I
      59: areturn

  public static final int[] intersection1(int[], int[]);
    Code:
       0: aload_0
       1: ldc           #24                 // String nums1
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #31                 // String nums2
       9: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: arraylength
      14: ifne          21
      17: iconst_1
      18: goto          22
      21: iconst_0
      22: ifne          38
      25: aload_1
      26: arraylength
      27: ifne          34
      30: iconst_1
      31: goto          35
      34: iconst_0
      35: ifeq          42
      38: iconst_0
      39: newarray       int
      41: areturn
      42: aload_0
      43: invokestatic  #86                 // Method kotlin/collections/ArraysKt.asList:([I)Ljava/util/List;
      46: checkcast     #109                // class java/lang/Iterable
      49: aload_1
      50: invokestatic  #86                 // Method kotlin/collections/ArraysKt.asList:([I)Ljava/util/List;
      53: checkcast     #109                // class java/lang/Iterable
      56: invokestatic  #124                // Method kotlin/collections/CollectionsKt.intersect:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/Set;
      59: astore_2
      60: aload_2
      61: invokeinterface #127,  1          // InterfaceMethod java/util/Set.size:()I
      66: newarray       int
      68: astore_3
      69: iconst_0
      70: istore        4
      72: aload_2
      73: invokeinterface #128,  1          // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
      78: astore        5
      80: aload         5
      82: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      87: ifeq          117
      90: aload         5
      92: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      97: checkcast     #79                 // class java/lang/Number
     100: invokevirtual #82                 // Method java/lang/Number.intValue:()I
     103: istore        6
     105: aload_3
     106: iload         4
     108: iinc          4, 1
     111: iload         6
     113: iastore
     114: goto          80
     117: aload_3
     118: invokestatic  #86                 // Method kotlin/collections/ArraysKt.asList:([I)Ljava/util/List;
     121: getstatic     #92                 // Field java/lang/System.out:Ljava/io/PrintStream;
     124: swap
     125: invokevirtual #98                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     128: aload_3
     129: areturn

  public static final int[] intersection(int[], int[]);
    Code:
       0: aload_0
       1: ldc           #24                 // String nums1
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #31                 // String nums2
       9: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: arraylength
      14: ifne          21
      17: iconst_1
      18: goto          22
      21: iconst_0
      22: ifne          38
      25: aload_1
      26: arraylength
      27: ifne          34
      30: iconst_1
      31: goto          35
      34: iconst_0
      35: ifeq          42
      38: iconst_0
      39: newarray       int
      41: areturn
      42: new           #33                 // class java/util/HashSet
      45: dup
      46: invokespecial #36                 // Method java/util/HashSet."<init>":()V
      49: astore_2
      50: new           #33                 // class java/util/HashSet
      53: dup
      54: invokespecial #36                 // Method java/util/HashSet."<init>":()V
      57: astore_3
      58: iconst_0
      59: istore        4
      61: aload_0
      62: arraylength
      63: istore        5
      65: iload         4
      67: iload         5
      69: if_icmpge     94
      72: aload_0
      73: iload         4
      75: iaload
      76: istore        6
      78: aload_2
      79: iload         6
      81: invokestatic  #48                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      84: invokevirtual #52                 // Method java/util/HashSet.add:(Ljava/lang/Object;)Z
      87: pop
      88: iinc          4, 1
      91: goto          65
      94: iconst_0
      95: istore        4
      97: aload_1
      98: arraylength
      99: istore        5
     101: iload         4
     103: iload         5
     105: if_icmpge     142
     108: aload_1
     109: iload         4
     111: iaload
     112: istore        6
     114: aload_2
     115: iload         6
     117: invokestatic  #48                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     120: invokevirtual #134                // Method java/util/HashSet.contains:(Ljava/lang/Object;)Z
     123: ifeq          136
     126: aload_3
     127: iload         6
     129: invokestatic  #48                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     132: invokevirtual #52                 // Method java/util/HashSet.add:(Ljava/lang/Object;)Z
     135: pop
     136: iinc          4, 1
     139: goto          101
     142: aload_3
     143: invokevirtual #56                 // Method java/util/HashSet.size:()I
     146: newarray       int
     148: astore        4
     150: iconst_0
     151: istore        5
     153: aload_3
     154: invokevirtual #60                 // Method java/util/HashSet.iterator:()Ljava/util/Iterator;
     157: dup
     158: ldc           #62                 // String iterator(...)
     160: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     163: astore        6
     165: aload         6
     167: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     172: ifeq          209
     175: aload         6
     177: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     182: dup
     183: ldc           #77                 // String next(...)
     185: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     188: checkcast     #79                 // class java/lang/Number
     191: invokevirtual #82                 // Method java/lang/Number.intValue:()I
     194: istore        7
     196: aload         4
     198: iload         5
     200: iinc          5, 1
     203: iload         7
     205: iastore
     206: goto          165
     209: aload         4
     211: invokestatic  #86                 // Method kotlin/collections/ArraysKt.asList:([I)Ljava/util/List;
     214: getstatic     #92                 // Field java/lang/System.out:Ljava/io/PrintStream;
     217: swap
     218: invokevirtual #98                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     221: aload         4
     223: areturn

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


package com.hlq.leetcode.array

/**
 * @author HLQ_Struggle
 * @date 2020/11/09
 * @desc LeetCode 1. 两数之和 https://leetcode-cn.com/problems/two-sum/
 */
fun main() {
    val nums = intArrayOf(3, 2, 4)

    val target = 6

    // 暴力解法 一
    twoSum(nums, target)

    // 优化
    twoSum2(nums, target)
    twoSum3(nums, target)

    // 学习 LeetCode 执行用时范例
    twoSum4(nums, target)

    // 学习 LeetCode 执行消耗内存范例
    twoSum5(nums, target)
}

/**
 * 双指针解法
 */
fun twoSum(nums: IntArray, target: Int): IntArray {
    // 基本校验
    if (nums.isEmpty()) {
        return IntArray(0)
    }
    var resultArray = IntArray(2)
    var isl = false
    var i = 0
    var j = 1
    while (i < nums.size - 1) {
        while (j < nums.size) {
            if (nums[i] + nums[j] == target) {
                resultArray[0] = i
                resultArray[1] = j
                isl = true
                break
            }
            j++
        }
        if (isl) {
            break
        }
        i++
        j = i + 1
    }
    return resultArray
}

/**
 * 优化 一
 */
fun twoSum2(nums: IntArray, target: Int): IntArray {
    // 基本校验
    if (nums.isEmpty()) {
        return IntArray(0)
    }
    var i = 0
    var j = 1
    while (i < nums.size - 1) {
        while (j < nums.size) {
            if (nums[i] + nums[j] == target) {
                return intArrayOf(i, j)
            }
            j++
        }
        i++
        j = i + 1
    }
    return intArrayOf(0, 0)
}

/**
 * 继续优化
 */
fun twoSum3(nums: IntArray, target: Int): IntArray {
    if (nums.isEmpty()) {
        return intArrayOf(0, 0)
    }
    var i = 0
    var j = 1
    while (i < nums.size - 1) {
        while (j < nums.size) {
            if (nums[i] + nums[j] == target) {
                return intArrayOf(i, j)
            }
            j++
        }
        i++
        j = i + 1
    }
    return intArrayOf(0, 0)
}

/**
 * 学习 LeetCode 执行用时最短事例
 */
fun twoSum4(nums: IntArray, target: Int): IntArray {
    if (nums.isEmpty()) {
        return intArrayOf(0, 0)
    }
    for (i in nums.indices) {
        for (j in 0..i) {
            if (i == j) {
                continue
            }
            val a = nums[i];
            val b = nums[j];
            if (a + b == target) {
                return intArrayOf(j, i)
            }
        }
    }
    return intArrayOf(0, 0)
}

/**
 * 学习 LeetCode 执行消耗内存范例
 */
fun twoSum5(nums: IntArray, target: Int): IntArray {
    if (nums.isEmpty()) {
        return intArrayOf(0, 0)
    }
    val map: MutableMap<Int, Int> = HashMap()
    for (i in nums.indices) {
        val complement = target - nums[i]
        if (map.containsKey(complement)) {
            return intArrayOf(map[complement]!!, i)
        }
        map[nums[i]] = i
    }
    throw Exception()
}

Compiled from "Question1.kt"
public final class com.hlq.leetcode.array.Question1Kt {
  public static final void main();
    Code:
       0: iconst_3
       1: newarray       int
       3: astore_1
       4: aload_1
       5: iconst_0
       6: iconst_3
       7: iastore
       8: aload_1
       9: iconst_1
      10: iconst_2
      11: iastore
      12: aload_1
      13: iconst_2
      14: iconst_4
      15: iastore
      16: aload_1
      17: astore_0
      18: bipush        6
      20: istore_1
      21: aload_0
      22: iload_1
      23: invokestatic  #10                 // Method twoSum:([II)[I
      26: pop
      27: aload_0
      28: iload_1
      29: invokestatic  #13                 // Method twoSum2:([II)[I
      32: pop
      33: aload_0
      34: iload_1
      35: invokestatic  #16                 // Method twoSum3:([II)[I
      38: pop
      39: aload_0
      40: iload_1
      41: invokestatic  #19                 // Method twoSum4:([II)[I
      44: pop
      45: aload_0
      46: iload_1
      47: invokestatic  #22                 // Method twoSum5:([II)[I
      50: pop
      51: return

  public static final int[] twoSum(int[], int);
    Code:
       0: aload_0
       1: ldc           #28                 // String nums
       3: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          23
      19: iconst_0
      20: newarray       int
      22: areturn
      23: iconst_2
      24: newarray       int
      26: astore_2
      27: iconst_0
      28: istore_3
      29: iconst_0
      30: istore        4
      32: iconst_1
      33: istore        5
      35: iload         4
      37: aload_0
      38: arraylength
      39: iconst_1
      40: isub
      41: if_icmpge     104
      44: iload         5
      46: aload_0
      47: arraylength
      48: if_icmpge     85
      51: aload_0
      52: iload         4
      54: iaload
      55: aload_0
      56: iload         5
      58: iaload
      59: iadd
      60: iload_1
      61: if_icmpne     79
      64: aload_2
      65: iconst_0
      66: iload         4
      68: iastore
      69: aload_2
      70: iconst_1
      71: iload         5
      73: iastore
      74: iconst_1
      75: istore_3
      76: goto          85
      79: iinc          5, 1
      82: goto          44
      85: iload_3
      86: ifeq          92
      89: goto          104
      92: iinc          4, 1
      95: iload         4
      97: iconst_1
      98: iadd
      99: istore        5
     101: goto          35
     104: aload_2
     105: areturn

  public static final int[] twoSum2(int[], int);
    Code:
       0: aload_0
       1: ldc           #28                 // String nums
       3: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          23
      19: iconst_0
      20: newarray       int
      22: areturn
      23: iconst_0
      24: istore_2
      25: iconst_1
      26: istore_3
      27: iload_2
      28: aload_0
      29: arraylength
      30: iconst_1
      31: isub
      32: if_icmpge     86
      35: iload_3
      36: aload_0
      37: arraylength
      38: if_icmpge     76
      41: aload_0
      42: iload_2
      43: iaload
      44: aload_0
      45: iload_3
      46: iaload
      47: iadd
      48: iload_1
      49: if_icmpne     70
      52: iconst_2
      53: newarray       int
      55: astore        4
      57: aload         4
      59: iconst_0
      60: iload_2
      61: iastore
      62: aload         4
      64: iconst_1
      65: iload_3
      66: iastore
      67: aload         4
      69: areturn
      70: iinc          3, 1
      73: goto          35
      76: iinc          2, 1
      79: iload_2
      80: iconst_1
      81: iadd
      82: istore_3
      83: goto          27
      86: iconst_2
      87: newarray       int
      89: astore        4
      91: aload         4
      93: iconst_0
      94: iconst_0
      95: iastore
      96: aload         4
      98: iconst_1
      99: iconst_0
     100: iastore
     101: aload         4
     103: areturn

  public static final int[] twoSum3(int[], int);
    Code:
       0: aload_0
       1: ldc           #28                 // String nums
       3: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          33
      19: iconst_2
      20: newarray       int
      22: astore_2
      23: aload_2
      24: iconst_0
      25: iconst_0
      26: iastore
      27: aload_2
      28: iconst_1
      29: iconst_0
      30: iastore
      31: aload_2
      32: areturn
      33: iconst_0
      34: istore_2
      35: iconst_1
      36: istore_3
      37: iload_2
      38: aload_0
      39: arraylength
      40: iconst_1
      41: isub
      42: if_icmpge     96
      45: iload_3
      46: aload_0
      47: arraylength
      48: if_icmpge     86
      51: aload_0
      52: iload_2
      53: iaload
      54: aload_0
      55: iload_3
      56: iaload
      57: iadd
      58: iload_1
      59: if_icmpne     80
      62: iconst_2
      63: newarray       int
      65: astore        4
      67: aload         4
      69: iconst_0
      70: iload_2
      71: iastore
      72: aload         4
      74: iconst_1
      75: iload_3
      76: iastore
      77: aload         4
      79: areturn
      80: iinc          3, 1
      83: goto          45
      86: iinc          2, 1
      89: iload_2
      90: iconst_1
      91: iadd
      92: istore_3
      93: goto          37
      96: iconst_2
      97: newarray       int
      99: astore        4
     101: aload         4
     103: iconst_0
     104: iconst_0
     105: iastore
     106: aload         4
     108: iconst_1
     109: iconst_0
     110: iastore
     111: aload         4
     113: areturn

  public static final int[] twoSum4(int[], int);
    Code:
       0: aload_0
       1: ldc           #28                 // String nums
       3: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          33
      19: iconst_2
      20: newarray       int
      22: astore_2
      23: aload_2
      24: iconst_0
      25: iconst_0
      26: iastore
      27: aload_2
      28: iconst_1
      29: iconst_0
      30: iastore
      31: aload_2
      32: areturn
      33: iconst_0
      34: istore_2
      35: aload_0
      36: arraylength
      37: istore_3
      38: iload_2
      39: iload_3
      40: if_icmpge     118
      43: iconst_0
      44: istore        4
      46: iload         4
      48: iload_2
      49: if_icmpgt     112
      52: iload_2
      53: iload         4
      55: if_icmpne     61
      58: goto          100
      61: aload_0
      62: iload_2
      63: iaload
      64: istore        5
      66: aload_0
      67: iload         4
      69: iaload
      70: istore        6
      72: iload         5
      74: iload         6
      76: iadd
      77: iload_1
      78: if_icmpne     100
      81: iconst_2
      82: newarray       int
      84: astore        7
      86: aload         7
      88: iconst_0
      89: iload         4
      91: iastore
      92: aload         7
      94: iconst_1
      95: iload_2
      96: iastore
      97: aload         7
      99: areturn
     100: iload         4
     102: iload_2
     103: if_icmpeq     112
     106: iinc          4, 1
     109: goto          52
     112: iinc          2, 1
     115: goto          38
     118: iconst_2
     119: newarray       int
     121: astore_2
     122: aload_2
     123: iconst_0
     124: iconst_0
     125: iastore
     126: aload_2
     127: iconst_1
     128: iconst_0
     129: iastore
     130: aload_2
     131: areturn

  public static final int[] twoSum5(int[], int);
    Code:
       0: aload_0
       1: ldc           #28                 // String nums
       3: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          33
      19: iconst_2
      20: newarray       int
      22: astore_2
      23: aload_2
      24: iconst_0
      25: iconst_0
      26: iastore
      27: aload_2
      28: iconst_1
      29: iconst_0
      30: iastore
      31: aload_2
      32: areturn
      33: new           #44                 // class java/util/HashMap
      36: dup
      37: invokespecial #47                 // Method java/util/HashMap."<init>":()V
      40: checkcast     #49                 // class java/util/Map
      43: astore_2
      44: iconst_0
      45: istore_3
      46: aload_0
      47: arraylength
      48: istore        4
      50: iload_3
      51: iload         4
      53: if_icmpge     142
      56: iload_1
      57: aload_0
      58: iload_3
      59: iaload
      60: isub
      61: istore        5
      63: aload_2
      64: iload         5
      66: invokestatic  #55                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      69: invokeinterface #59,  2           // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
      74: ifeq          115
      77: iconst_2
      78: newarray       int
      80: astore        6
      82: aload         6
      84: iconst_0
      85: aload_2
      86: iload         5
      88: invokestatic  #55                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      91: invokeinterface #63,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      96: dup
      97: invokestatic  #67                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     100: checkcast     #69                 // class java/lang/Number
     103: invokevirtual #73                 // Method java/lang/Number.intValue:()I
     106: iastore
     107: aload         6
     109: iconst_1
     110: iload_3
     111: iastore
     112: aload         6
     114: areturn
     115: iload_3
     116: invokestatic  #55                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     119: astore        7
     121: aload_2
     122: aload_0
     123: iload_3
     124: iaload
     125: invokestatic  #55                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     128: aload         7
     130: invokeinterface #77,  3           // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     135: pop
     136: iinc          3, 1
     139: goto          50
     142: new           #79                 // class java/lang/Exception
     145: dup
     146: invokespecial #80                 // Method java/lang/Exception."<init>":()V
     149: athrow

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


package com.hlq.leetcode.array

import java.util.*

/**
 * @author HLQ_Struggle
 * @date 2020/11/09
 * @desc LeetCode 14. 最长公共前缀 https://leetcode-cn.com/problems/longest-common-prefix/
 */
fun main() {
    val strs = arrayOf("flower", "flow", "flight")
    val strs1 = arrayOf("flower", "flower", "flower", "flower")
    val strsNull = arrayOf("dog", "racecar", "car")

    // 暴力解法 1
    println("----> ${longestCommonPrefix(strs)}")
    println("----> ${longestCommonPrefix(strs1)}")
    println("----> ${longestCommonPrefix(strsNull)}")

    // 暴力解法 2 - 3
    println("----> ${longestCommonPrefix2(strs)}")

    // 排序法
    println("----> ${longestCommonPrefix3(strs)}")

    // 获取最后一次相同位置
    println("----> ${longestCommonPrefix4(strs)}")
}

fun longestCommonPrefix4(strs: Array<String>): String {
    if (strs.isEmpty()) {
        return ""
    }
    if (strs.size == 1) {
        return if(strs[0].isEmpty()) "" else strs[0]
    }
    // 获取输入数组中最短长度
    var min = Int.MAX_VALUE
    for(str in strs){
        if(min > str.length){
            min = str.length
        }
    }
    // 获取最后一次相同的位置
    var isl = true
    var index = 0
    while (index < min){
        for(i in strs.indices){
            if(strs[0][index] != strs[i][index]){
                isl = false
                break
            }
        }
        if(!isl){
            break
        }
        index++
    }
    return strs[0].substring(0,index)
}

/**
 * 排序法
 */
fun longestCommonPrefix3(strs: Array<String>): String {
    var resultStr = ""
    if (strs.isEmpty()) {
        return resultStr
    }
    if (strs.size == 1) {
        return strs[0]
    }
    var len = strs.size
    Arrays.sort(strs)
    for (i in strs[0].indices) {
        if (strs[len - 1][i] == strs[0][i]) {
            resultStr += strs[0][i]
        } else {
            break
        }
    }
    return resultStr
}

/**
 * 暴力解法 2
 */
fun longestCommonPrefix2(strs: Array<String>): String {
    var resultStr = ""
    if (strs.isEmpty()) {
        return resultStr
    }
    if (strs.size == 1) {
        return strs[0]
    }
    resultStr = strs[0]
    for (i in 1 until strs.size) {
        var j = 0
        while (j < resultStr.length && j < strs[i].length) {
            if (strs[i][j] != resultStr[j]) {
                break
            }
            j++
        }
        resultStr = resultStr.substring(0, j)
        if (resultStr.isEmpty()) {
            break
        }
    }
    return resultStr
}

/**
 * 暴力解法 1
 */
fun longestCommonPrefix(strs: Array<String>): String {
    var resultStr = ""
    if (strs.isEmpty()) {
        return resultStr
    }
    if (strs.size == 1) {
        return strs[0]
    }
    resultStr = strs[0]
    for (str in strs) {
        var j = 0
        while (j < resultStr.length && j < str.length) {
            if (str.toCharArray()[j] != resultStr.toCharArray()[j]) {
                break
            }
            j++
        }
        resultStr = resultStr.substring(0, j)
        if (resultStr.isEmpty()) {
            break
        }
    }
    return resultStr
}

Compiled from "Question14.kt"
public final class com.hlq.leetcode.array.Question14Kt {
  public static final void main();
    Code:
       0: iconst_3
       1: anewarray     #8                  // class java/lang/String
       4: astore_1
       5: aload_1
       6: iconst_0
       7: ldc           #10                 // String flower
       9: aastore
      10: aload_1
      11: iconst_1
      12: ldc           #12                 // String flow
      14: aastore
      15: aload_1
      16: iconst_2
      17: ldc           #14                 // String flight
      19: aastore
      20: aload_1
      21: astore_0
      22: iconst_4
      23: anewarray     #8                  // class java/lang/String
      26: astore_2
      27: aload_2
      28: iconst_0
      29: ldc           #10                 // String flower
      31: aastore
      32: aload_2
      33: iconst_1
      34: ldc           #10                 // String flower
      36: aastore
      37: aload_2
      38: iconst_2
      39: ldc           #10                 // String flower
      41: aastore
      42: aload_2
      43: iconst_3
      44: ldc           #10                 // String flower
      46: aastore
      47: aload_2
      48: astore_1
      49: iconst_3
      50: anewarray     #8                  // class java/lang/String
      53: astore_3
      54: aload_3
      55: iconst_0
      56: ldc           #16                 // String dog
      58: aastore
      59: aload_3
      60: iconst_1
      61: ldc           #18                 // String racecar
      63: aastore
      64: aload_3
      65: iconst_2
      66: ldc           #20                 // String car
      68: aastore
      69: aload_3
      70: astore_2
      71: new           #22                 // class java/lang/StringBuilder
      74: dup
      75: invokespecial #25                 // Method java/lang/StringBuilder."<init>":()V
      78: ldc           #27                 // String ---->
      80: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      83: aload_0
      84: invokestatic  #35                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
      87: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      90: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      93: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
      96: swap
      97: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     100: new           #22                 // class java/lang/StringBuilder
     103: dup
     104: invokespecial #25                 // Method java/lang/StringBuilder."<init>":()V
     107: ldc           #27                 // String ---->
     109: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     112: aload_1
     113: invokestatic  #35                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
     116: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     119: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     122: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
     125: swap
     126: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     129: new           #22                 // class java/lang/StringBuilder
     132: dup
     133: invokespecial #25                 // Method java/lang/StringBuilder."<init>":()V
     136: ldc           #27                 // String ---->
     138: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     141: aload_2
     142: invokestatic  #35                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
     145: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     148: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     151: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
     154: swap
     155: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     158: new           #22                 // class java/lang/StringBuilder
     161: dup
     162: invokespecial #25                 // Method java/lang/StringBuilder."<init>":()V
     165: ldc           #27                 // String ---->
     167: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     170: aload_0
     171: invokestatic  #54                 // Method longestCommonPrefix2:([Ljava/lang/String;)Ljava/lang/String;
     174: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     177: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     180: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
     183: swap
     184: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     187: new           #22                 // class java/lang/StringBuilder
     190: dup
     191: invokespecial #25                 // Method java/lang/StringBuilder."<init>":()V
     194: ldc           #27                 // String ---->
     196: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     199: aload_0
     200: invokestatic  #57                 // Method longestCommonPrefix3:([Ljava/lang/String;)Ljava/lang/String;
     203: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     206: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     209: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
     212: swap
     213: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     216: new           #22                 // class java/lang/StringBuilder
     219: dup
     220: invokespecial #25                 // Method java/lang/StringBuilder."<init>":()V
     223: ldc           #27                 // String ---->
     225: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     228: aload_0
     229: invokestatic  #60                 // Method longestCommonPrefix4:([Ljava/lang/String;)Ljava/lang/String;
     232: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     235: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     238: getstatic     #45                 // Field java/lang/System.out:Ljava/io/PrintStream;
     241: swap
     242: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     245: return

  public static final java.lang.String longestCommonPrefix4(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #66                 // String strs
       3: invokestatic  #72                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          22
      19: ldc           #74                 // String
      21: areturn
      22: aload_0
      23: arraylength
      24: iconst_1
      25: if_icmpne     59
      28: aload_0
      29: iconst_0
      30: aaload
      31: checkcast     #76                 // class java/lang/CharSequence
      34: invokeinterface #80,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      39: ifne          46
      42: iconst_1
      43: goto          47
      46: iconst_0
      47: ifeq          55
      50: ldc           #74                 // String
      52: goto          58
      55: aload_0
      56: iconst_0
      57: aaload
      58: areturn
      59: ldc           #81                 // int 2147483647
      61: istore_1
      62: iconst_0
      63: istore_2
      64: aload_0
      65: arraylength
      66: istore_3
      67: iload_2
      68: iload_3
      69: if_icmpge     98
      72: aload_0
      73: iload_2
      74: aaload
      75: astore        4
      77: iload_1
      78: aload         4
      80: invokevirtual #82                 // Method java/lang/String.length:()I
      83: if_icmple     92
      86: aload         4
      88: invokevirtual #82                 // Method java/lang/String.length:()I
      91: istore_1
      92: iinc          2, 1
      95: goto          67
      98: iconst_1
      99: istore_2
     100: iconst_0
     101: istore_3
     102: iload_3
     103: iload_1
     104: if_icmpge     163
     107: iconst_0
     108: istore        4
     110: aload_0
     111: arraylength
     112: istore        5
     114: iload         4
     116: iload         5
     118: if_icmpge     150
     121: aload_0
     122: iconst_0
     123: aaload
     124: iload_3
     125: invokevirtual #86                 // Method java/lang/String.charAt:(I)C
     128: aload_0
     129: iload         4
     131: aaload
     132: iload_3
     133: invokevirtual #86                 // Method java/lang/String.charAt:(I)C
     136: if_icmpeq     144
     139: iconst_0
     140: istore_2
     141: goto          150
     144: iinc          4, 1
     147: goto          114
     150: iload_2
     151: ifne          157
     154: goto          163
     157: iinc          3, 1
     160: goto          102
     163: aload_0
     164: iconst_0
     165: aaload
     166: iconst_0
     167: iload_3
     168: invokevirtual #90                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     171: dup
     172: ldc           #92                 // String substring(...)
     174: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     177: areturn

  public static final java.lang.String longestCommonPrefix3(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #66                 // String strs
       3: invokestatic  #72                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #74                 // String
       8: astore_1
       9: aload_0
      10: arraylength
      11: ifne          18
      14: iconst_1
      15: goto          19
      18: iconst_0
      19: ifeq          24
      22: aload_1
      23: areturn
      24: aload_0
      25: arraylength
      26: iconst_1
      27: if_icmpne     34
      30: aload_0
      31: iconst_0
      32: aaload
      33: areturn
      34: aload_0
      35: arraylength
      36: istore_2
      37: aload_0
      38: invokestatic  #109                // Method java/util/Arrays.sort:([Ljava/lang/Object;)V
      41: iconst_0
      42: istore_3
      43: aload_0
      44: iconst_0
      45: aaload
      46: checkcast     #76                 // class java/lang/CharSequence
      49: invokeinterface #80,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      54: istore        4
      56: iload_3
      57: iload         4
      59: if_icmpge     118
      62: aload_0
      63: iload_2
      64: iconst_1
      65: isub
      66: aaload
      67: iload_3
      68: invokevirtual #86                 // Method java/lang/String.charAt:(I)C
      71: aload_0
      72: iconst_0
      73: aaload
      74: iload_3
      75: invokevirtual #86                 // Method java/lang/String.charAt:(I)C
      78: if_icmpne     109
      81: new           #22                 // class java/lang/StringBuilder
      84: dup
      85: invokespecial #25                 // Method java/lang/StringBuilder."<init>":()V
      88: aload_1
      89: invokevirtual #31                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      92: aload_0
      93: iconst_0
      94: aaload
      95: iload_3
      96: invokevirtual #86                 // Method java/lang/String.charAt:(I)C
      99: invokevirtual #112                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     102: invokevirtual #39                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     105: astore_1
     106: goto          112
     109: goto          118
     112: iinc          3, 1
     115: goto          56
     118: aload_1
     119: areturn

  public static final java.lang.String longestCommonPrefix2(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #66                 // String strs
       3: invokestatic  #72                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #74                 // String
       8: astore_1
       9: aload_0
      10: arraylength
      11: ifne          18
      14: iconst_1
      15: goto          19
      18: iconst_0
      19: ifeq          24
      22: aload_1
      23: areturn
      24: aload_0
      25: arraylength
      26: iconst_1
      27: if_icmpne     34
      30: aload_0
      31: iconst_0
      32: aaload
      33: areturn
      34: aload_0
      35: iconst_0
      36: aaload
      37: astore_1
      38: iconst_1
      39: istore_2
      40: aload_0
      41: arraylength
      42: istore_3
      43: iload_2
      44: iload_3
      45: if_icmpge     140
      48: iconst_0
      49: istore        4
      51: iload         4
      53: aload_1
      54: invokevirtual #82                 // Method java/lang/String.length:()I
      57: if_icmpge     97
      60: iload         4
      62: aload_0
      63: iload_2
      64: aaload
      65: invokevirtual #82                 // Method java/lang/String.length:()I
      68: if_icmpge     97
      71: aload_0
      72: iload_2
      73: aaload
      74: iload         4
      76: invokevirtual #86                 // Method java/lang/String.charAt:(I)C
      79: aload_1
      80: iload         4
      82: invokevirtual #86                 // Method java/lang/String.charAt:(I)C
      85: if_icmpeq     91
      88: goto          97
      91: iinc          4, 1
      94: goto          51
      97: aload_1
      98: iconst_0
      99: iload         4
     101: invokevirtual #90                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     104: dup
     105: ldc           #92                 // String substring(...)
     107: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     110: astore_1
     111: aload_1
     112: checkcast     #76                 // class java/lang/CharSequence
     115: invokeinterface #80,  1           // InterfaceMethod java/lang/CharSequence.length:()I
     120: ifne          127
     123: iconst_1
     124: goto          128
     127: iconst_0
     128: ifeq          134
     131: goto          140
     134: iinc          2, 1
     137: goto          43
     140: aload_1
     141: areturn

  public static final java.lang.String longestCommonPrefix(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #66                 // String strs
       3: invokestatic  #72                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #74                 // String
       8: astore_1
       9: aload_0
      10: arraylength
      11: ifne          18
      14: iconst_1
      15: goto          19
      18: iconst_0
      19: ifeq          24
      22: aload_1
      23: areturn
      24: aload_0
      25: arraylength
      26: iconst_1
      27: if_icmpne     34
      30: aload_0
      31: iconst_0
      32: aaload
      33: areturn
      34: aload_0
      35: iconst_0
      36: aaload
      37: astore_1
      38: iconst_0
      39: istore_2
      40: aload_0
      41: arraylength
      42: istore_3
      43: iload_2
      44: iload_3
      45: if_icmpge     157
      48: aload_0
      49: iload_2
      50: aaload
      51: astore        4
      53: iconst_0
      54: istore        5
      56: iload         5
      58: aload_1
      59: invokevirtual #82                 // Method java/lang/String.length:()I
      62: if_icmpge     114
      65: iload         5
      67: aload         4
      69: invokevirtual #82                 // Method java/lang/String.length:()I
      72: if_icmpge     114
      75: aload         4
      77: invokevirtual #119                // Method java/lang/String.toCharArray:()[C
      80: dup
      81: ldc           #121                // String toCharArray(...)
      83: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      86: iload         5
      88: caload
      89: aload_1
      90: invokevirtual #119                // Method java/lang/String.toCharArray:()[C
      93: dup
      94: ldc           #121                // String toCharArray(...)
      96: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      99: iload         5
     101: caload
     102: if_icmpeq     108
     105: goto          114
     108: iinc          5, 1
     111: goto          56
     114: aload_1
     115: iconst_0
     116: iload         5
     118: invokevirtual #90                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     121: dup
     122: ldc           #92                 // String substring(...)
     124: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     127: astore_1
     128: aload_1
     129: checkcast     #76                 // class java/lang/CharSequence
     132: invokeinterface #80,  1           // InterfaceMethod java/lang/CharSequence.length:()I
     137: ifne          144
     140: iconst_1
     141: goto          145
     144: iconst_0
     145: ifeq          151
     148: goto          157
     151: iinc          2, 1
     154: goto          43
     157: aload_1
     158: areturn

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