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;
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.Random; public class SolutionTest { private static final Random RANDOM = new Random(); @Test public void twoShouldBeAPrime() { testPrime(2, true); } @Test public void fourShouldNotBeAPrime() { testPrime(4, false); } @Test public void aSquareShouldNotAPrime() { for (int i = 0; i < 10000; ++i) { int randomNumber = RANDOM.nextInt(Short.MAX_VALUE) + 2; int numberToCheck = randomNumber * randomNumber; testPrime(numberToCheck, false); } } @Test public void aProductOfTwoIntegersShouldNotBeAPrime() { for (int i = 0; i < 100000; ++i) { int a = RANDOM.nextInt(Short.MAX_VALUE) + 2; int b = RANDOM.nextInt(Short.MAX_VALUE) + 2; int numberToCheck = a * b; testPrime(numberToCheck, false); } } private static void testPrime(int numberToCheck, boolean expected) { boolean actual = Primes.isAPrime(numberToCheck); assertEquals(Integer.toString(numberToCheck), expected, actual); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- import java.util.Random;
- public class SolutionTest {
- private static final Random RANDOM = new Random();
- @Test
- public void twoShouldBeAPrime() {
int numberToCheck = 2;boolean expected = true;- testPrime(2, true);
- }
- @Test
- public void fourShouldNotBeAPrime() {
- testPrime(4, false);
- }
- @Test
- public void aSquareShouldNotAPrime() {
- for (int i = 0; i < 10000; ++i) {
- int randomNumber = RANDOM.nextInt(Short.MAX_VALUE) + 2;
- int numberToCheck = randomNumber * randomNumber;
- testPrime(numberToCheck, false);
- }
- }
- @Test
- public void aProductOfTwoIntegersShouldNotBeAPrime() {
- for (int i = 0; i < 100000; ++i) {
- int a = RANDOM.nextInt(Short.MAX_VALUE) + 2;
- int b = RANDOM.nextInt(Short.MAX_VALUE) + 2;
- int numberToCheck = a * b;
- testPrime(numberToCheck, false);
- }
- }
- private static void testPrime(int numberToCheck, boolean expected) {
- boolean actual = Primes.isAPrime(numberToCheck);
assertEquals(expected, actual);- assertEquals(Integer.toString(numberToCheck), expected, actual);
- }
- }
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;
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.*; public class SolutionTest { private static final Random RANDOM = new Random(); @Test public void twoShouldBeAPrime() { testPrime(2, true); } @Test public void fourShouldNotBeAPrime() { testPrime(4, false); } @Test public void aSquareShouldNotAPrime() { for (int i = 0; i < 10; ++i) { int randomNumber = RANDOM.nextInt(Short.MAX_VALUE); int numberToCheck = randomNumber * randomNumber; testPrime(numberToCheck, false); } } @Test public void aProductOfTwoIntegersShouldNotBeAPrime() { for (int i = 0; i < 10; ++i) { int a = RANDOM.nextInt(Short.MAX_VALUE); int b = RANDOM.nextInt(Short.MAX_VALUE); int numberToCheck = a * b; testPrime(numberToCheck, false); } } private static void testPrime(int numberToCheck, boolean expected) { boolean actual = Primes.isAPrime(numberToCheck); assertEquals(Integer.toString(numberToCheck), expected, actual); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- import java.util.*;
- public class SolutionTest {
- private static final Random RANDOM = new Random();
- @Test
- public void twoShouldBeAPrime() {
int numberToCheck = 2;boolean expected = true;boolean actual = Primes.isAPrime(numberToCheck);assertEquals(expected, actual);- testPrime(2, true);
- }
- @Test
- public void fourShouldNotBeAPrime() {
int numberToCheck = 4;boolean expected = false;- testPrime(4, false);
- }
- @Test
- public void aSquareShouldNotAPrime() {
- for (int i = 0; i < 10; ++i) {
- int randomNumber = RANDOM.nextInt(Short.MAX_VALUE);
- int numberToCheck = randomNumber * randomNumber;
- testPrime(numberToCheck, false);
- }
- }
- @Test
- public void aProductOfTwoIntegersShouldNotBeAPrime() {
- for (int i = 0; i < 10; ++i) {
- int a = RANDOM.nextInt(Short.MAX_VALUE);
- int b = RANDOM.nextInt(Short.MAX_VALUE);
- int numberToCheck = a * b;
- testPrime(numberToCheck, false);
- }
- }
- private static void testPrime(int numberToCheck, boolean expected) {
- boolean actual = Primes.isAPrime(numberToCheck);
assertEquals(expected, actual);- assertEquals(Integer.toString(numberToCheck), expected, actual);
- }
- }
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;
- }
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; // TODO: Replace examples and use TDD development by writing your own tests public class SolutionTest { @Test public void test1() { assertEquals(2, Solution.peakIndex(new int[] {1, 2, 3, 1})); } @Test public void test2() { assertEquals(1, Solution.peakIndex(new int[] {1, 2, 1})); } @Test public void test3() { assertEquals(0, Solution.peakIndex(new int[] {2})); } @Test public void test4() { assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5, 4, 3, 2})); } @Test public void peakIsTheLastNumber() { assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5})); } @Test public void flatland() { assertEquals(-1, Solution.peakIndex(new int[] {1, 1, 1, 1, 1})); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- // TODO: Replace examples and use TDD development by writing your own tests
- public class SolutionTest {
- @Test
- public void test1() {
- assertEquals(2, Solution.peakIndex(new int[] {1, 2, 3, 1}));
- }
- @Test
- public void test2() {
- assertEquals(1, Solution.peakIndex(new int[] {1, 2, 1}));
- }
- @Test
- public void test3() {
- assertEquals(0, Solution.peakIndex(new int[] {2}));
- }
- @Test
- public void test4() {
- assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5, 4, 3, 2}));
- }
- @Test
public void test5() {assertEquals(14, Solution.peakIndex(new int[] {1, 2, 3, 2, 1, 2, 3, 4, 5, 4, 3, 4, 5, 6, 7, 6, 5, 4, 3, 7, 8, 1}));- public void peakIsTheLastNumber() {
- assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5}));
- }
- @Test
- public void flatland() {
- assertEquals(-1, Solution.peakIndex(new int[] {1, 1, 1, 1, 1}));
- }
- }
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>() {@Overridepublic 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);
- }
- }