Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 42 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
87 changes: 87 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.util.*;

public class Main {
public static void main(String[] args) {
//palindrome
Scanner scanner = new Scanner(System.in);
System.out.print("Введіть текст: ");
String input = scanner.nextLine();
String cleanText = input.replaceAll("[^a-zA-Zа-яА-Я0-9]", "");
String lowercaseText = cleanText.toLowerCase();

if (isPalindrome(lowercaseText)) {
System.out.println("Текст є паліндромом.");
} else {
System.out.println("Текст не є паліндромом.");
}

//arrays
int[] nums1 = {1,2,3,1}; //true
int[] nums2 = {1,2,3,4}; //false
int[] nums3 = {1,1,1,3,3,4,3,2,4,2}; //true
System.out.println(containsDuplicate(nums1));
System.out.println(containsDuplicate(nums2));
System.out.println(containsDuplicate(nums3));

//arrays+target
int[] nums_1 = {2,7,11,15};
int target_1 = 9;
int[] nums_2 = {3,2,4};
int target_2 = 6;
int[] nums_3 = {3,3};
int target_3 = 6;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase we use in java not a a snakeCase


System.out.println(Arrays.toString(twoSum(nums_1,target_1)));
System.out.println(Arrays.toString(twoSum(nums_2,target_2)));
System.out.println(Arrays.toString(twoSum(nums_3,target_3)));

//maxProfit
int[] prices1 = {7,1,5,3,6,4};//5
int[] prices2 = {7,6,4,3,1};//0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a very good name

System.out.println(MaxProfit.maxProfit(prices1));
System.out.println(MaxProfit.maxProfit(prices2));

//reverseArray
int[] startArray = {1,2,3,4,5};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad var name

System.out.println("Start array: "+Arrays.toString(startArray));
System.out.println("Reversed array: "+Arrays.toString(ReverseArray.reverseArray(startArray)));


}
private static boolean isPalindrome(String text) {
int length = text.length();
for (int i = 0; i < length / 2; i++) {
if (text.charAt(i) != text.charAt(length - 1 - i)) {
return false;
}
}
return true;
}

private static boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad var name

for (int num : nums) {
if (set.contains(num)) {
return true;
}
set.add(num);
}
return false;
}

private static int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
Map<Integer, Integer> map = new HashMap<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad var name


for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
result[0] = map.get(complement);
result[1] = i;
break;
}
map.put(nums[i], i);
}
return result;
}
}
16 changes: 16 additions & 0 deletions src/main/java/MaxProfit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class MaxProfit {
public static int maxProfit(int[] prices) {
int maxProfit = 0;
int minPrice = Integer.MAX_VALUE;

for (int i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
} else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}

return maxProfit;
}
}
16 changes: 16 additions & 0 deletions src/main/java/ReverseArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class ReverseArray {
public static int[] reverseArray(int[] array) {
int start = 0;
int end = array.length - 1;

while (start < end) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}

return array;
}
}