-
Notifications
You must be signed in to change notification settings - Fork 0
ДЗ 10 задачка2 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\u0414\u0417_10_\u0437\u0430\u0434\u0430\u0447\u043A\u04302"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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; | ||
|
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
There was a problem hiding this comment.
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