Блог пользователя __goku__

Автор __goku__, история, 5 лет назад, По-английски

I have written an article on how to implement lower_bound() function of C++ in Java by creating our own method and also using java inbuilt functions. Feel free to comment if you have any query.

Link: https://medium.com/@ssrivastava990/alternative-implementation-of-lower-bound-in-java-23ec40a825ec

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится

This all seems rather unnecessary to me. You already have treeset.higher(), .lower(), .floor(), and .ceil(). In my experience (as a java main), no one seriously wants to convert from C++ to Java. Even if they did, the names of the methods in Java are super self-explanatory. i. e. "I want the first thing higher than x" "Okay, then use .higher(x)".

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится +10 Проголосовать: не нравится

    He talks about arrays, not sets. Arrays.binarySearch() is stupid returning random occurence, and you have to use pairs (value, index) to find first/last occurence or write it manually

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

If I were a Java user, I would use generic types so that any that the methods work for any class that implements the Comparable interface. Also I would make it return the end of the range if the search fails to be consistent with C++. Also in my experience from the Kotlin round, ArrayList is much slower than int[], so it would be worth it to implement the methods separately for each primitive type or find some way to convert a primitive array to an ArrayList in O(1).