Ad
Code
Diff
  • public class Primes {
      public static boolean isAPrime(int number) {    
        if (number == 1) {
          return false;
        }
      
        if (number == 2) {
          return true;
        }
        
        if (number % 2 == 0) {
          return false;
        }
      
        for (int i = 3; i*i <= number; i += 2) {
          if (number % i == 0)
            return false;
        }
        
        return true;
      }
    }
    • public class Primes {
    • public static boolean isAPrime(int number) {
    • for (int i=2;i*i<=number;i++)
    • {
    • public static boolean isAPrime(int number) {
    • if (number == 1) {
    • return false;
    • }
    • if (number == 2) {
    • return true;
    • }
    • if (number % 2 == 0) {
    • return false;
    • }
    • for (int i = 3; i*i <= number; i += 2) {
    • if (number % i == 0)
    • return false;
    • }
    • return true;
    • }
    • }
Code
Diff
  • public class Primes {
      public static boolean isAPrime(int number) {
        if (number == 2) return true;
        if (number % 2 == 0) return false;
        if (Math.sqrt(number) % 1 == 0) return false;
        return true;
      }
    }
    • public class Primes {
    • public static boolean isAPrime(int number) {
    • return number == 2 || number % 2 != 0;
    • if (number == 2) return true;
    • if (number % 2 == 0) return false;
    • if (Math.sqrt(number) % 1 == 0) return false;
    • return true;
    • }
    • }
Code
Diff
  • import java.util.*;;
    
    class Solution {
      public static int peakIndex(int[] nums) {
        if (nums.length == 1) return 0;
        if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1;
        
        boolean ascending = false;
        
        for (int i = 0; i < nums.length - 1; ++i) {
          if (nums[i] < nums[i + 1]) {
            ascending = true;
          } else {
            if (ascending && nums[i] > nums[i + 1]) {
              return i;
            }
            ascending = false;
          }
        }
        
        if (ascending) {
          return nums.length - 1;
        } else {
          return -1;
        }
      }
    }
    • import java.util.*;;
    • class Solution {
    • public static int peakIndex(int[] nums) {
    • int left = 0;
    • int right = nums.length - 1;
    • int peak = -1;
    • if (nums.length == 1) return 0;
    • if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1;
    • while(left <= right) {
    • if(left == right) {
    • peak = left;
    • break;
    • }
    • int mid = (left + right) / 2;
    • if(nums[mid] < nums[mid + 1]) {
    • left = mid + 1;
    • boolean ascending = false;
    • for (int i = 0; i < nums.length - 1; ++i) {
    • if (nums[i] < nums[i + 1]) {
    • ascending = true;
    • } else {
    • right = mid;
    • if (ascending && nums[i] > nums[i + 1]) {
    • return i;
    • }
    • ascending = false;
    • }
    • }
    • return peak;
    • if (ascending) {
    • return nums.length - 1;
    • } else {
    • return -1;
    • }
    • }
    • }
Code
Diff
  • import java.util.*;
    import java.util.stream.*;
    
    class Solution {
      public static String largestNumber(Integer[] nums) {
        final StringBuilder sb = new StringBuilder();
        return Arrays.stream(nums)
              .parallel()
              .map((n) -> n.toString())
              .sorted(Solution::compareNumberString)
              .collect(Collectors.joining())
              .toString();
      }
      
      private static int compareNumberString(String s1, String s2) {
        if (s2.startsWith(s1)) return compareNumberString(s2.substring(s1.length()), s1);
        if (s1.startsWith(s2)) return compareNumberString(s1.substring(s2.length()), s2);
        return s2.compareTo(s1);
      }
    }
    • import java.util.*;
    • import java.util.stream.*;
    • class Solution {
    • public static String largestNumber(Integer[] nums) {
    • Arrays.sort( nums, new Comparator<Integer>() {
    • @Override
    • public int compare(Integer a, Integer b) {
    • String aStr = a.toString();
    • String bStr = b.toString();
    • return (aStr + bStr).compareTo(bStr + aStr) * -1;
    • }
    • } );
    • String result = "";
    • for(Integer num : nums) {
    • result += num.toString();
    • }
    • return result;
    • final StringBuilder sb = new StringBuilder();
    • return Arrays.stream(nums)
    • .parallel()
    • .map((n) -> n.toString())
    • .sorted(Solution::compareNumberString)
    • .collect(Collectors.joining())
    • .toString();
    • }
    • private static int compareNumberString(String s1, String s2) {
    • if (s2.startsWith(s1)) return compareNumberString(s2.substring(s1.length()), s1);
    • if (s1.startsWith(s2)) return compareNumberString(s1.substring(s2.length()), s2);
    • return s2.compareTo(s1);
    • }
    • }