Welcome to channel "Leetcode Java".
In this channel I just want to provide my solutions of Leetcode problems in Java programming language.
In this channel I just want to provide my solutions of Leetcode problems in Java programming language.
Forwarded from Leetcode in Java && Oracle
#N1 Two sum
problem link =>https://leetcode.com/problems/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};
}
};Forwarded from Leetcode in Java && Oracle
#N26 Remove Duplicates from Sorted Array
problem link=>https://leetcode.com/problems/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;
}
}Forwarded from Leetcode in Java && Oracle
#N1431 Kids With the Greatest Number of Candies
problem link=>https://leetcode.com/problems/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;
}
}Forwarded from Leetcode in Java && Oracle
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/
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;
}
}Forwarded from Leetcode in Java && Oracle
image_2021-10-13_18-21-48.png
45.1 KB
#N1929 Concatenation of Array
problem link=>https://leetcode.com/problems/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
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
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;
}
}
