leetcode

parent 3d7d60fd
import java.util.List;
import java.util.Objects;
public class CountItem {
public int solution(List<List<String>> items, String ruleKey, String ruleValue){
int index = 0;
int count = 0;
if(Objects.equals(ruleKey, "type")) index = 0;
else if (Objects.equals(ruleKey, "color")) index = 1;
else if (Objects.equals(ruleKey, "name")) index = 2;
for(List<String> s: items) {
if(Objects.equals(s.get(index), ruleValue)) count++;
}
return count;
}
public static void main(String[] args) {
CountItem count = new CountItem();
// test cases too long. code is working fine.
System.out.println();
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DecompressList {
public int[] solution(int[] nums) {
int i = 0;
List<Integer> answer = new ArrayList<>();
while(i < nums.length) {
int count = nums[i];
int value = nums[i+1];
i += 2;
for (int j = 0; j < count; j++) {
answer.add(value);
}
}
return answer.stream().mapToInt(x->x).toArray();
}
public static void main(String[] args) {
int[] nums = {1,2,3,4};
DecompressList list = new DecompressList();
int[] answer = list.solution(nums);
System.out.println(Arrays.toString(answer));
int[] x = {1,2};
System.out.println(Arrays.toString(x.clone()));
System.out.println("main");
}
}
...@@ -2,6 +2,7 @@ import java.util.ArrayList; ...@@ -2,6 +2,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
//leetcode 1431
public class GreatestNumberOfCandies { public class GreatestNumberOfCandies {
List<Boolean> solution(int[] candies, int extraCandies) { List<Boolean> solution(int[] candies, int extraCandies) {
List<Boolean> answer = new ArrayList<>(); List<Boolean> answer = new ArrayList<>();
......
import java.util.Arrays;
//leetcode 1365
public class SmallerThanCurrent {
public int[] solution(int[] nums) {
int count;
int index = 0;
int[] answer = new int[nums.length];
for(int x: nums) {
count = 0;
for(int y: nums) {
if(x > y) count++;
}
answer[index] = count;
index++;
}
return answer;
}
public static void main(String[] args) {
int[] nums = {8,1,2,2,3};
SmallerThanCurrent smaller = new SmallerThanCurrent();
int[] answer = smaller.solution(nums);
System.out.println(Arrays.toString(answer));
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;
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());
}
}
//import java.util.HashMap; import java.util.HashMap;
//import java.util.Map; import java.util.Map;
//
//public class Testing { public class Testing {
// public static void main(String[] args) { public static void main(String[] args) {
// Map<Integer, Integer> myMap = new HashMap<>(); Map<Integer, Integer> myMap = new HashMap<>();
// System.out.println(myMap.get(1)); System.out.println(myMap.get(1));
// } if(true) {
//} int x = 5;
}
}
}
// LeetCode: 1816. Truncate Sentence
import java.util.Arrays;
public class TruncateSentence {
public String solution(String s, int k) {
String[] stringArray = s.split(" ");
String[] answer = new String[k];
System.out.println(Arrays.toString(stringArray));
for (int i = 0; i < k; i++) {
answer[i] = stringArray[i];
}
return String.join(" ", answer);
}
public static void main(String[] args) {
TruncateSentence truncate = new TruncateSentence();
System.out.println(truncate.solution("This is A a sentence and this is incomplete.", 4));
String[] testing = new String[5];
System.out.println(Arrays.toString(testing));
}
}
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