Сообщения

DSA: LinkedList

 

DSA: Recursion

1. Factorial: package main.lesson5; public class Main { public static void main (String[] args) { System. out .println( factorial ( 5 )); System. out .println( factorial2 ( 5 )); } public static int factorial ( int n) { if (n == 0 ) return 1 ; return n * factorial (n - 1 ); } public static int factorial2 ( int n) { int currentValue = n; if (currentValue == 0 ) { return 1 ; } int previousValue = currentValue - 1 ; int recursiveResult = factorial (previousValue); int result = currentValue * recursiveResult; return result; } }   2. Fibonacci: package main.lesson5; public class Main { public static void main (String[] args) { System. out .println( fibonacci ( 6 )); } public static int fibonacci ( int n) { if (n == 0 || n == 1 ) return n; return fibonacci (n - 1 ) + fibonacci (n - 2 ); } public static int fibonacci2 ( int...

DSA: Arrays

Изображение
1. Best Score:  Given an array, write a function to get first, second best scores from the array and return it in new array. myArray = { 84 , 85 , 86 , 87 , 85 , 90 , 85 , 83 , 23 , 45 , 84 , 1 , 2 , 0 } firstSecond ( myArray ) // {90, 87}   package test; import java.util.Arrays; public class Main { public static void main (String[] args) { int [] arr = { 5 , 91 , 15 , 3 , 90 , 8 , 79 , 91 , 13 }; System. out .println(Arrays. toString ( findTopTwoScores (arr))); } public static int [] findTopTwoScores ( int [] array) { int [] result = new int [ 2 ]; int first = array[ 0 ]; int second = first; for ( int i = 1 ; i < array. length ; i++) { if (array[i] > first) { second = first; first = array[i]; } else if (array[i] > second && array[i] < first) { second = array[i]; } } result[ 0 ] = first; re...