Leetcode in Java && Oracle
420 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
Channel photo updated
Welcome to channel "Leetcode Java".
In this channel I just want to provide my solutions of Leetcode problems in Java programming language.
#N1 Two sum
problem link =>https://leetcode.com/problems/two-sum/

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int a = 0, b = 0;
for (int i = 0; i < nums.size() - 1; i ++) {
for (int j = i + 1; j < nums.size(); j ++) {
if ( nums[i] + nums[j] == target) {
a = i;
b = j;
}
}
}
return {a, b};
}
};
#N26 Remove Duplicates from Sorted Array
problem link=>https://leetcode.com/problems/remove-duplicates-from-sorted-array/

class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for (int n : nums)
if (i == 0 || n > nums[i-1])
nums[i++] = n;
return i;
}
}
#N1431 Kids With the Greatest Number of Candies
problem link=>https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extra) {
ArrayList<Boolean> ans = new ArrayList<Boolean>();
int max=0;
for(int candy: candies){
max=Math.max(candy, max);
}
for(int candy: candies){
ans.add(candy+extra>=max);
}

return ans;
}
}
image_2021-10-13_18-18-36.png
43.5 KB
#N1920 Build Array from Permutation
problem link=>https://leetcode.com/problems/build-array-from-permutation/

class Solution {
public int[] buildArray(int[] nums) {
int[] ans=new int[nums.length];
for(int i=0; i<nums.length; i++){
ans[i]=nums[nums[i]];
}
return ans;
}
}
image_2021-10-13_18-21-48.png
45.1 KB
#N1929 Concatenation of Array
problem link=>https://leetcode.com/problems/concatenation-of-array/

class Solution {
public int[] getConcatenation(int[] nums) {
int[] ans=new int[nums.length * 2];
for(int i=0; i<nums.length; i++){
ans[i]=nums[i];
ans[i+nums.length]=nums[i];
}
return ans;
}
}
image_2021-10-15_01-29-34.png
44.7 KB
#N1313 Decompress Run-Length Encoded List
problem link=>https://leetcode.com/problems/decompress-run-length-encoded-list/

#solution
class Solution {
public int[] decompressRLElist(int[] nums) {
int size=0;
for(int i=0; i<nums.length; i+=2){
size+=nums[i];
}
int[] ans=new int[size];
for(int i=0, ix=0; i<nums.length; i+=2){
for(int j=0; j<nums[i]; j++){
ans[ix++]=nums[i+1];
}
}
return ans;
}
}
1
image_2021-10-15_03-31-56.png
48.9 KB
#N1773 Count Items Matching a Rule
problem link=>https://leetcode.com/problems/count-items-matching-a-rule/

#solution
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int key, count=0;
if(ruleKey.equals("type"))
key=0;
else if(ruleKey.equals("color"))
key=1;
else
key=2;

for(int i=0; i<items.size(); i++){
if(items.get(i).get(key).equals(ruleValue))
count++;
}

return count;
}
}
image_2021-10-15_03-57-31.png
33 KB
#N2006 Count Number of Pairs With Absolute Difference K
problem link=>https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/

#solution
class Solution {
public int countKDifference(int[] nums, int k) {
int count=0;
int[] helper=new int[101];
for (int n:nums)
helper[n]++;
for (int i=k+1; i<101; i++)
count+=helper[i]*helper[i-k];

return count;
}
}
Leetcode in Java && Oracle
image_2021-10-15_03-57-31.png
Best Solution ever for me)😃
image_2021-10-15_21-29-42.png
44.8 KB
#2011 Final Value of Variable After Performing Operations
problem link=>https://leetcode.com/problems/final-value-of-variable-after-performing-operations/

#solution
class Solution {
public int finalValueAfterOperations(String[] operations) {
int x=0;
for(int i=0; i<operations.length; i++){
if(operations[i].contains("++")){
x++;
}
if(operations[i].contains("--")){
x--;
}
}
return x;
}
}
image_2021-10-15_22-58-01.png
40.5 KB
#N1588 Sum of All Odd Length Subarrays
problem link=>https://leetcode.com/problems/sum-of-all-odd-length-subarrays/

#solution
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int res=0;
for(int i=0; i<arr.length; i++){
int start=arr.length-i;
int end=i+1;
if(start*end%2==1)
res+=(start*end+1)/2 * arr[i];
else
res+=(start*end)/2 * arr[i];
}
return res;
}
}
image_2021-10-15_23-08-17.png
43 KB
#N1662 Check If Two String Arrays are Equivalent
problem link=>https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/

#solution
class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
String str1="", str2="";
for(int i=0; i<Math.max(word1.length, word2.length); i++){
if(i<word1.length)
str1+=word1[i];
if(i<word2.length)
str2+=word2[i];
}
return str1.equals(str2);
}
}
image_2021-10-15_23-44-42.png
47.2 KB
#N1684 Count the Number of Consistent Strings
problem link=>https://leetcode.com/problems/count-the-number-of-consistent-strings/

#solution
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
int count = 0;
for (String s : words) {
boolean isValid = true;
for (char ch : s.toCharArray()) {
if (!allowed.contains(String.valueOf(ch))) {
isValid = false;
}
}
if (isValid) count++;
}
return count;
}
}
image_2021-10-15_23-57-39.png
55.5 KB
#N1913 Maximum Product Difference Between Two Pairs
problem link=>https://leetcode.com/problems/maximum-product-difference-between-two-pairs/

#solution
class Solution {
public int maxProductDifference(int[] nums) {
int largest = 0, largest2 = 0, smallest = Integer.MAX_VALUE, smallest2 = Integer.MAX_VALUE;
for(int i=0;i<nums.length;i++) {
if(nums[i]>=largest) {
largest2 = largest;
largest = nums[i];
} else if(nums[i]>largest2) largest2 = nums[i];
if(nums[i]<=smallest) {
smallest2 = smallest;
smallest = nums[i];
} else if(nums[i]<smallest2) smallest2 = nums[i];
}
return largest * largest2 - smallest * smallest2;
}
}
image_2021-10-16_00-24-46.png
39.4 KB
#N1816 Truncate Sentence
problem link=>https://leetcode.com/problems/truncate-sentence/

#solution
class Solution {
public String truncateSentence(String s, int k) {
s=s+" ";
String res="";
int start=0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i)==' '&&k>0){
res+=s.substring(start, i);
start=i;
k--;
}
}

return res;
}
}
image_2021-10-16_01-22-40.png
43 KB
#N1534 Count Good Triplets
problem link=>https://leetcode.com/problems/count-good-triplets/

#solution
class Solution {
public int countGoodTriplets(int[] arr, int a, int b, int c) {
int count=0;
for(int i=0; i<arr.length-2; i++){
for(int j=i+1; j<arr.length-1; j++){
if(Math.abs(arr[i]-arr[j])>a) continue;
for(int k=j+1; k<arr.length; k++){
if(Math.abs(arr[j]-arr[k])<=b&&Math.abs(arr[i]-arr[k])<=c)
count ++;
}
}
}

return count;
}
}