├── .gitignore ├── readme.md └── src ├── easy ├── MissingNumber.java ├── RemoveDuplicatesFromSortedArray.java ├── FibonacciNumber.java ├── PalindromeNumberMain.java ├── ValidParentheses.java ├── RemoveElement.java ├── RomanToIntegerMain.java ├── TwoSumMain.java └── LongestCommonPrefixMain.java └── medium └── AddTwoNumbers.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .github 3 | out 4 | *.iml -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Hello everyone! 2 | I have about four years of commercial software development experience. I decided to practice more. That's why I solve 3 | problems on LeetCode. 4 | In my project, only solutions are presented. Each class contains the result of the execution and a link to the problem. -------------------------------------------------------------------------------- /src/easy/MissingNumber.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.logging.Logger; 4 | 5 | /** 6 | * Link of the problem: ... 7 | *

8 | * Results of the execution: 9 | * Runtime: 1 ms, faster than 82.03% of Java online submissions for Missing Number. 10 | * Memory Usage: 42.8 MB, less than 96.58% of Java online submissions for Missing Number. 11 | *

12 | * Author Evgenii Alelekov 26.09.2022 13 | * */ 14 | public class MissingNumber { 15 | private static final Logger LOGGER = Logger.getLogger(MissingNumber.class.getName()); 16 | 17 | public static void main(String[] args) { 18 | int[] inputArray = {0,1,2,3}; 19 | String resultMessage = String.valueOf(MissingNumber.MissingNumberSolution.missingNumber(inputArray)); 20 | LOGGER.info(resultMessage); 21 | } 22 | 23 | private static class MissingNumberSolution { 24 | public static int missingNumber(int[] inputArray) { 25 | Integer actualSum = 0; 26 | for (int num : inputArray) { 27 | actualSum += num; 28 | } 29 | return (inputArray.length*(inputArray.length+1)/2) - actualSum; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/easy/RemoveDuplicatesFromSortedArray.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.logging.Logger; 4 | 5 | /** 6 | * Link of the problem: ... 7 | *

8 | * Results of the execution: 9 | * Runtime: 1 ms, faster than 86.77% of Java online submissions for Remove Duplicates from Sorted Array. 10 | * Memory Usage: 43.6 MB, less than 95.50% of Java online submissions for Remove Duplicates from Sorted Array. 11 | *

12 | * Author Evgenii Alelekov 27.04.2022 13 | */ 14 | public class RemoveDuplicatesFromSortedArray { 15 | private static final Logger LOGGER = Logger.getLogger(RemoveDuplicatesFromSortedArray.class.getName()); 16 | public static void main(String[] args) { 17 | int[] nums = {1,1,2}; 18 | String resultMessage = String.valueOf(removeDuplicates(nums)); 19 | LOGGER.info(resultMessage); 20 | } 21 | public static int removeDuplicates(int[] nums) { 22 | int count = 0; 23 | for (int index = 0; index < nums.length; index++) { 24 | if (index == 0) { 25 | count = 1; 26 | } else if (nums[index] != nums[index-1]) { 27 | nums[count] = nums[index]; 28 | count++; 29 | } 30 | } 31 | return count; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/easy/FibonacciNumber.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.logging.Logger; 4 | 5 | /** 6 | * Link of the problem: ... 7 | *

8 | * Results of the execution: 9 | * Runtime: 0 ms, faster than 100.00% of Java online submissions for Fibonacci Number. 10 | * Memory Usage: 39.4 MB, less than 85.99% of Java online submissions for Fibonacci Number. 11 | *

12 | * Author Evgenii Alelekov 01.09.2022 13 | * */ 14 | public class FibonacciNumber { 15 | private static final Logger LOGGER = Logger.getLogger(FibonacciNumber.class.getName()); 16 | public static void main(String[] args) { 17 | int n = 10; 18 | String resultMessage = String.valueOf(FibonacciNumberSolution.fib(n)); 19 | LOGGER.info(resultMessage); 20 | } 21 | 22 | private static class FibonacciNumberSolution { 23 | 24 | public static int fib(int n) { 25 | if(n<0) { 26 | throw new IllegalArgumentException("Value can't be negative"); 27 | } 28 | int prev=0; 29 | int tmp=1; 30 | int next=n; 31 | for(int i=1; i... 7 | *

8 | * Results of the execution: 9 | * Runtime: Runtime: 20 ms, faster than 18.31% of Java online submissions for Palindrome Number. 10 | * Memory Usage: 47.4 MB, less than 8.27% of Java online submissions for Palindrome Number. 11 | *

12 | * Author Evgenii Alelekov 23.04.2022 13 | * */ 14 | public class PalindromeNumberMain { 15 | private static final Logger LOGGER = Logger.getLogger(PalindromeNumberMain.class.getName()); 16 | public static void main(String[] args) { 17 | String resultMessage = String.valueOf(PalindromeNumberSolution.isPalindrome(1221)); 18 | LOGGER.info(resultMessage); 19 | } 20 | } 21 | class PalindromeNumberSolution { 22 | /** 23 | * SonarLint says: Utility classes, which are collections of static members, are not meant to be instantiated. 24 | * Even abstract utility classes, which can be extended, should not have public constructors. 25 | * Java adds an implicit public constructor to every class which does not define at least one explicitly. 26 | * Hence, at least one non-public constructor should be defined. 27 | * */ 28 | PalindromeNumberSolution() { 29 | } 30 | 31 | public static boolean isPalindrome(int x) { 32 | return String.valueOf(x).equals(new StringBuffer(String.valueOf(x)).reverse().toString()); 33 | } 34 | } -------------------------------------------------------------------------------- /src/easy/ValidParentheses.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Stack; 6 | import java.util.logging.Logger; 7 | 8 | /** 9 | * Link of the problem: ... 10 | *

11 | * Results of the execution: 12 | * Runtime: 2 ms, faster than 80.46% of Java online submissions for Valid Parentheses. 13 | * Memory Usage: 40.3 MB, less than 83.85% of Java online submissions for Valid Parentheses. 14 | *

15 | * Author Evgenii Alelekov 24.04.2022 16 | * */ 17 | public class ValidParentheses { 18 | private static final Logger LOGGER = Logger.getLogger(ValidParentheses.class.getName()); 19 | private static final Map PARENTHESES = new HashMap<>(); 20 | static { 21 | PARENTHESES.put(')', '('); 22 | PARENTHESES.put('}', '{'); 23 | PARENTHESES.put(']', '['); 24 | } 25 | public static void main(String[] args) { 26 | String resultMessage = String.valueOf(isValid("(){}{}{}{}}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}]")); 27 | LOGGER.info(resultMessage); 28 | } 29 | public static boolean isValid(String s) { 30 | Stack letters = new Stack<>(); 31 | char[] sArray = s.toCharArray(); 32 | for(int i = 0; i < s.length(); i++){ 33 | char temp = sArray[i]; 34 | if(PARENTHESES.containsKey(temp)){ 35 | if(letters.isEmpty() || !letters.pop().equals(PARENTHESES.get(temp))){ 36 | return false; 37 | } 38 | } else{ 39 | letters.push(temp); 40 | } 41 | } 42 | return letters.isEmpty(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/easy/RemoveElement.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.logging.Logger; 4 | 5 | /** 6 | * Link of the problem: ... 7 | *

8 | * Results of the execution: 9 | * Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Element. 10 | * Memory Usage: 40.8 MB, less than 92.98% of Java online submissions for Remove Element. 11 | *

12 | * Author Evgenii Alelekov 30.08.2022 13 | * */ 14 | public class RemoveElement { 15 | private static final Logger LOGGER = Logger.getLogger(RemoveElement.class.getName()); 16 | public static void main(String[] args) { 17 | int[] inputArray = {7,7,9,7}; 18 | int needDelete = 7; 19 | String resultMessage = String.valueOf(RemoveElementSolution.removeElement(inputArray, needDelete)); 20 | LOGGER.info(resultMessage); 21 | } 22 | 23 | private static class RemoveElementSolution { 24 | private static int countRemoveElements; 25 | public static int removeElement(int[] inputArray, int needDelete) { 26 | for(int i=0; i(inputArray.length - countRemoveElements)){ 30 | break; 31 | } 32 | } 33 | } 34 | return inputArray.length - countRemoveElements; 35 | } 36 | 37 | private static void changeArray(int[] inputArray, int i, int needDelete) { 38 | int emptySlot = -1; //we can't wrap int array to Integer array 39 | for(int j = i; j < inputArray.length; j++){ 40 | if(inputArray.length>(j+1)) { 41 | inputArray[j] = inputArray[j+1]; 42 | } else { 43 | inputArray[j] = emptySlot; 44 | countRemoveElements++; 45 | if(inputArray[i]==needDelete) { 46 | changeArray(inputArray,i,needDelete); 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/easy/RomanToIntegerMain.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.logging.Logger; 6 | 7 | /** 8 | * Link of the problem: ... 9 | *

10 | * Results of the execution: 11 | * Runtime: 10 ms, faster than 43.66% of Java online submissions for Roman to Integer. 12 | * Memory Usage: 46.8 MB, less than 26.85% of Java online submissions for Roman to Integer. 13 | *

14 | * Author Evgenii Alelekov 24.04.2022 15 | */ 16 | public class RomanToIntegerMain { 17 | private static final Logger LOGGER = Logger.getLogger(RomanToIntegerMain.class.getName()); 18 | public static void main(String[] args) { 19 | String resultMessage = String.valueOf(RomanToIntegerSolution.romanToInt("IV")); 20 | LOGGER.info(resultMessage); 21 | } 22 | } 23 | 24 | class RomanToIntegerSolution { 25 | /** 26 | * SonarLint says: Utility classes, which are collections of static members, are not meant to be instantiated. 27 | * Even abstract utility classes, which can be extended, should not have public constructors. 28 | * Java adds an implicit public constructor to every class which does not define at least one explicitly. 29 | * Hence, at least one non-public constructor should be defined. 30 | * */ 31 | RomanToIntegerSolution() { 32 | } 33 | 34 | private static final Map MAP_LETTERS_VALUES = new HashMap<>(); 35 | static { 36 | MAP_LETTERS_VALUES.put("I", 1); 37 | MAP_LETTERS_VALUES.put("V", 5); 38 | MAP_LETTERS_VALUES.put("X", 10); 39 | MAP_LETTERS_VALUES.put("L", 50); 40 | MAP_LETTERS_VALUES.put("C", 100); 41 | MAP_LETTERS_VALUES.put("D", 500); 42 | MAP_LETTERS_VALUES.put("M", 1000); 43 | } 44 | 45 | public static int romanToInt(String s) { 46 | int result = 0; 47 | int previous = 0; 48 | 49 | for (char letter : s.toCharArray()) { 50 | int valueLetter = MAP_LETTERS_VALUES.get(String.valueOf(letter)); 51 | if (valueLetter > previous) { 52 | result -= previous; 53 | result += valueLetter - previous; 54 | } else { 55 | result += valueLetter; 56 | } 57 | previous = valueLetter; 58 | } 59 | return result; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/easy/TwoSumMain.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.Arrays; 4 | import java.util.logging.Logger; 5 | 6 | /** 7 | * Link of the problem: ... 8 | *

9 | * Results of the execution: 10 | * Runtime: 105 ms, faster than 11.94% of Java online submissions for Two Sum. 11 | * Memory Usage: 42.4 MB, less than 86.91% of Java online submissions for Two Sum. 12 | *

13 | * Author Evgenii Alelekov 23.04.2022 14 | * */ 15 | public class TwoSumMain { 16 | private static final Logger LOGGER = Logger.getLogger(TwoSumMain.class.getName()); 17 | public static void main(String[] args) { 18 | int[] arr1 = {-1,-2,-3,-4,-5}; 19 | int xxx = -8; 20 | String resultMessage = Arrays.toString(TwoSumMainSolution.twoSum(arr1, xxx)); 21 | LOGGER.info(resultMessage); 22 | } 23 | } 24 | class TwoSumMainSolution { 25 | /** 26 | * SonarLint says: Utility classes, which are collections of static members, are not meant to be instantiated. 27 | * Even abstract utility classes, which can be extended, should not have public constructors. 28 | * Java adds an implicit public constructor to every class which does not define at least one explicitly. 29 | * Hence, at least one non-public constructor should be defined. 30 | * */ 31 | TwoSumMainSolution() { 32 | } 33 | 34 | /** 35 | * If you know how to avoid Cognitive Complexity, please text me. 36 | * */ 37 | public static int[] twoSum(int[] nums, int target) { 38 | if(nums.length < 2 || nums.length > 10000){ 39 | throw new IllegalArgumentException(); 40 | } 41 | if(target < -1000000000 || target > 1000000000){ 42 | throw new IllegalArgumentException(); 43 | } 44 | if(nums[0] < -1000000000 || nums[0] > 1000000000){ 45 | throw new IllegalArgumentException(); 46 | } 47 | int[] res = new int[2]; 48 | boolean check = false; 49 | for(int i = 0; i < nums.length-1; i++) { 50 | if(check) break; 51 | for(int y = i+1; y < nums.length; y++) { 52 | if(nums[y] < -1000000000 || nums[y] > 1000000000){ 53 | throw new IllegalArgumentException(); 54 | } 55 | if (nums[i]+nums[y]==target){ 56 | res[0]=i; 57 | res[1]=y; 58 | check = true; 59 | break; 60 | } 61 | } 62 | } 63 | return res; 64 | } 65 | } -------------------------------------------------------------------------------- /src/medium/AddTwoNumbers.java: -------------------------------------------------------------------------------- 1 | package medium; 2 | 3 | import java.util.logging.Logger; 4 | 5 | /** 6 | * Link of the problem: ... 7 | *

8 | * Results of the execution: 9 | * Runtime: 3 ms, faster than 80.48% of Java online submissions for Add Two Numbers. 10 | * Memory Usage: 47.9 MB, less than 48.53% of Java online submissions for Add Two Numbers. 11 | *

12 | * Author Evgenii Alelekov 02.08.2022 13 | */ 14 | public class AddTwoNumbers { 15 | private static final Logger LOGGER = Logger.getLogger(AddTwoNumbers.class.getName()); 16 | public static void main(String[] args) { 17 | int l1n1 = 0; 18 | int l1n2 = 5; 19 | int l1n3 = 3; 20 | int l2n1 = 5; 21 | int l2n2 = 6; 22 | int l2n3 = 4; 23 | String resultMessage = AddTwoNumbersSolution.addTwoNumbers(new ListNode(l1n3, (new ListNode(l1n2, 24 | (new ListNode(l1n1))))), new ListNode(l2n3, (new ListNode(l2n2, (new ListNode(l2n1)))))).toString(); 25 | LOGGER.info(resultMessage); 26 | } 27 | } 28 | 29 | class AddTwoNumbersSolution { 30 | /** 31 | * SonarLint says: Utility classes, which are collections of static members, are not meant to be instantiated. 32 | * Even abstract utility classes, which can be extended, should not have public constructors. 33 | * Java adds an implicit public constructor to every class which does not define at least one explicitly. 34 | * Hence, at least one non-public constructor should be defined. 35 | * */ 36 | AddTwoNumbersSolution() { 37 | } 38 | public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { 39 | ListNode dummyHead = new ListNode(0); 40 | ListNode curr = dummyHead; 41 | int carry = 0; 42 | while (l1 != null || l2 != null || carry != 0) { 43 | int x = (l1 != null) ? l1.val : 0; 44 | int y = (l2 != null) ? l2.val : 0; 45 | int sum = carry + x + y; 46 | carry = sum / 10; 47 | curr.next = new ListNode(sum % 10); 48 | curr = curr.next; 49 | if (l1 != null) 50 | l1 = l1.next; 51 | if (l2 != null) 52 | l2 = l2.next; 53 | } 54 | return dummyHead.next; 55 | } 56 | } 57 | 58 | class ListNode { 59 | int val; 60 | ListNode next; 61 | ListNode() {} 62 | ListNode(int val) { 63 | this.val = val; 64 | } 65 | ListNode(int val, ListNode next) { 66 | this.val = val; this.next = next; 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "ListNode{" + 72 | "val=" + val + 73 | ", next=" + next + 74 | '}'; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/easy/LongestCommonPrefixMain.java: -------------------------------------------------------------------------------- 1 | package easy; 2 | 3 | import java.util.logging.Logger; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | /** 8 | * Link of the problem: ... 9 | *

10 | * Results of the execution: 11 | * Runtime: 5 ms, faster than 26.74% of Java online submissions for Longest Common Prefix. 12 | * Memory Usage: 40.3 MB, less than 85.90% of Java online submissions for Longest Common Prefix. 13 | *

14 | * Author Evgenii Alelekov 24.04.2022 15 | * */ 16 | public class LongestCommonPrefixMain { 17 | private static final Logger LOGGER = Logger.getLogger(LongestCommonPrefixMain.class.getName()); 18 | public static void main(String[] args) { 19 | String resultMessage = LongestCommonPrefixSolution.longestCommonPrefix(new String[]{"f","flow", "flight"}); 20 | LOGGER.info(resultMessage); 21 | } 22 | } 23 | class LongestCommonPrefixSolution { 24 | /** 25 | * SonarLint says: Utility classes, which are collections of static members, are not meant to be instantiated. 26 | * Even abstract utility classes, which can be extended, should not have public constructors. 27 | * Java adds an implicit public constructor to every class which does not define at least one explicitly. 28 | * Hence, at least one non-public constructor should be defined. 29 | * */ 30 | LongestCommonPrefixSolution() { 31 | } 32 | 33 | /** 34 | * If you know how to avoid Cognitive Complexity, please text me. 35 | * */ 36 | public static String longestCommonPrefix(String[] strs) { 37 | String result = ""; 38 | for (String currentWord : strs) { 39 | if (currentWord.equals("")) { 40 | return ""; 41 | } 42 | if (result.equals("")) { 43 | result = currentWord; 44 | } else if (!currentWord.equals(result)) { 45 | char[] resultArray = result.toCharArray(); 46 | char[] interimResultArray = new char[result.length()]; 47 | char[] currentWordArray = currentWord.toCharArray(); 48 | for (int indexLetter = 0; indexLetter < result.length() && indexLetter <= currentWord.length(); 49 | indexLetter++) { 50 | if (indexLetter < currentWord.length() && resultArray[indexLetter]==currentWordArray[indexLetter]) { 51 | interimResultArray[indexLetter] = resultArray[indexLetter]; 52 | } 53 | else { 54 | result = String.valueOf(interimResultArray); 55 | break; 56 | } 57 | } 58 | } 59 | } 60 | Pattern pattern = Pattern.compile("([^\u0000]*)"); 61 | Matcher matcher = pattern.matcher(result); 62 | if(matcher.find(0)){ 63 | return matcher.group(); 64 | } else { 65 | return result; 66 | } 67 | } 68 | } --------------------------------------------------------------------------------