leet problem with stream

parent ca860a1c
import java.util.*;
import java.util.stream.Collectors;
// APPLIED STREAM
// LeetCode: 2248
public class ArrayIntersection {
public List<Integer> solution(int[][] nums) {
List<Integer> result = new ArrayList<>();
int lengthOfNums = nums.length;
Map<Integer, Integer> myMap = new HashMap<>();
for (int i = 0; i < lengthOfNums; i++) {
......@@ -16,18 +18,26 @@ public class ArrayIntersection {
}
}
}
for(int key: myMap.keySet()) {
int mapKeyValue = myMap.get(key);
if (mapKeyValue == lengthOfNums) {
result.add(key);
}
}
result.sort(Comparator.naturalOrder());
// List<Integer> result = new ArrayList<>();
// for(int key: myMap.keySet()) {
// int mapKeyValue = myMap.get(key);
// if (mapKeyValue == lengthOfNums) {
// result.add(key);
// }
// }
// result.sort(Comparator.naturalOrder());
List<Integer> result = myMap.keySet().stream()
.filter(x -> myMap.get(x) == lengthOfNums)
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList());
//
return result;
}
public static void main(String[] args) {
int[][] nums = {{7,34,45,10,12,27,13},{27,21,45,10,12,13}};
System.out.println(nums[1].length);
// System.out.println(nums[1].length);
ArrayIntersection myArray = new ArrayIntersection();
System.out.println(myArray.solution(nums));
}
......
import java.util.Arrays;
// 238. Product of Array Except Self
public class ArrayProductExceptSelf {
public int[] solution(int[] nums) {
int[] answer = new int[nums.length];
int product;
for (int i = 0; i < nums.length; i++) {
product = 1;
for (int j = 0; j < nums.length; j++) {
if(j != i) {
product *= nums[j];
}
}
answer[i] = product;
}
// Arrays.stream(nums).boxed().forEach(x -> {
// int products = 1;
// Arrays.stream(nums).boxed().forEach(y -> {
// if(x != y){
// product *= y;
// }
// });
// });
return answer;
}
public static void main(String[] args) {
int[] nums = {1,2,3};
ArrayProductExceptSelf product = new ArrayProductExceptSelf();
System.out.println(Arrays.toString(product.solution(nums)));
System.out.println("Main");
}
}
import java.util.HashMap;
import java.util.Map;
// 217. Contains Duplicate
public class ContainsDuplicate {
public boolean solution(int[] nums) {
Map<Integer, Integer> numberCount = new HashMap<>();
for(int x: nums) {
if(numberCount.get(x) != null){
return true;
}
numberCount.put(x, 1);
}
return false;
}
public static void main(String[] args) {
int[] nums = {3,3};
ContainsDuplicate contain = new ContainsDuplicate();
System.out.println(contain.solution(nums));
System.out.println("main");
}
}
// 2185. Counting Words With a Given Prefix
public class CountPrefix {
public int solution(String[] words, String prefix) {
int count = 0;
for(String s: words) {
if(s.startsWith(prefix)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
}
}
// 1295. Find Numbers with Even Number of Digits
public class EvenNumber {
public int solution(int[] nums) {
int answer = 0;
for (int x: nums) {
int count = 0;
while(x != 0) {
x = x / 10;
count++;
}
if(count % 2 == 0) {
answer++;
}
}
return answer;
}
public static void main(String[] args) {
int[] nums = {1,2,3,4,1111,1111,2234,112};
EvenNumber even = new EvenNumber();
System.out.println(even.solution(nums));
}
}
import java.util.Arrays;
// 1979. Find Greatest Common Divisor of Array
public class GreatestCommonDivisor {
public int solution(int[] nums) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int x: nums) {
if(x > max) {
max = x;
}
if(x < min) {
min = x;
}
}
int divisor = 1;
int answer = 1;
while(divisor <= min) {
if(min % divisor == 0 && max % divisor == 0) {
answer = divisor;
}
divisor++;
}
return answer;
}
public static void main(String[] args) {
int[] nums = {4,5,6};
GreatestCommonDivisor common = new GreatestCommonDivisor();
System.out.println(common.solution(nums));
System.out.println("main");
}
}
// 1450. Number of Students Doing Homework at a Given Time
public class HomeWorkAtGivenTime {
public int solution(int[] startTime, int[] endTime, int queryTime) {
int count = 0;
for (int i = 0; i < startTime.length; i++) {
if(startTime[i] <= queryTime && queryTime <= endTime[i]) {
count++;
}
}
return count;
}
public static void main(String[] args) {
System.out.println("Main");
}
}
import java.util.ArrayList;
import java.util.List;
// LeetCode: 1588. Sum of All Odd Length Sub-arrays.
public class SumOfSubArray {
public List<List<Integer>> solution(int[] arr) {
List<List<Integer>> myArray = new ArrayList<>();
int count = 1;
public int solution(int[] arr) {
int sum = 0;
int left = 0;
int right = 0;
while (true) {
if (left == right) {
sum += arr[left];
right += 2;
} else if (left < right && right < arr.length) {
for (int j = left; j < right + 1; j++) {
sum += arr[j];
}
right += 2;
} else if (right >= arr.length) {
left++;
right = left;
if (left >= arr.length) {
break;
}
int start = 0;
int length = 1;
while(length < arr.length+1 && start < arr.length) {
List<Integer> subArray = new ArrayList<>();
if(length == arr.length) {
length = ;
start++;
}
for (int j = start; j < length; j++) {
subArray.add(arr[j]);
}
length += 2;
myArray.add(subArray);
}
return myArray;
}
public static void main(String[] args) {
int[] arr = {1,4,2,5,3};
SumOfSubArray array = new SumOfSubArray();
List<List<Integer>> answer = array.solution(arr);
System.out.println(answer.toString());
return sum;
}
public static void main (String[]args){
int[] nums = {1, 2, 3, 4, 5};
SumOfSubArray array = new SumOfSubArray();
int answer = array.solution(nums);
System.out.println(answer);
}
}
import java.util.Arrays;
// 1304. Find N Unique Integers Sum up to Zero
public class SumToZero {
public int[] solution(int n) {
int start;
boolean isEven = n % 2 == 0;
if(isEven){
start = -1;
}
else {
start = 0;
}
int[] answer = new int[n];
int multiplier = -1;
for (int i = 0; i < n; i++) {
answer[i] = start;
if(start < 0) {
start = -1 * start;
}
else if(start > 0) {
start++;
start = -1 * start;
}
else if(start == 0) {
start++;
start = -1 * start;
}
}
return answer;
}
public static void main(String[] args) {
SumToZero sum = new SumToZero();
System.out.println(Arrays.toString(sum.solution(3)));
System.out.println("main");
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
// 242. Valid Anagram
public class ValidAnagram {
public boolean solution(String s, String t) {
Map<Character, Integer> firstCharCount = new HashMap<>();
Map<Character, Integer> secondCharCount = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char key = s.charAt(i);
firstCharCount.merge(key, 1, Integer::sum);
}
for (int i = 0; i < t.length(); i++) {
char key = t.charAt(i);
secondCharCount.merge(key, 1, Integer::sum);
}
Set<Character> charSet;
if(s.length() >= t.length()) {
charSet = firstCharCount.keySet();
}
else {
charSet = secondCharCount.keySet();
}
for(char c: charSet) {
if(!Objects.equals(firstCharCount.get(c), secondCharCount.get(c))){
return false;
}
}
return true;
}
public static void main(String[] args) {
String s = "a";
String t = "ab";
ValidAnagram valid = new ValidAnagram();
System.out.println(valid.solution(s, t));
System.out.println("main");
}
}
import java.util.ArrayList;
import java.util.List;
// 125. Valid Palindrome
public class ValidPalindrome {
public boolean solution(String s) {
List<Character> characterList = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char myChar = s.charAt(i);
if(String.valueOf(myChar).matches("[A-Za-z]") || String.valueOf(myChar).matches("[0-9]")){
if((int) myChar < 97) {
int charValue = (int) myChar + 32;
myChar = (char) charValue;
}
characterList.add(myChar);
}
}
int listSize = characterList.size() - 1;
int halfSize;
if(characterList.size() % 2 == 0) {
halfSize = characterList.size() / 2;
}
else {
halfSize = (characterList.size() / 2) - 1;
}
for (int i = 0; i < halfSize; i++) {
if(characterList.get(i) != characterList.get(listSize - i)){
return false;
}
}
return true;
}
public static void main(String[] args) {
String string = "abb";
ValidPalindrome valid = new ValidPalindrome();
System.out.println(valid.solution(string));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment