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

Let's Develop Together!
Download Telegram
image_2021-10-26_02-13-29.png
38.2 KB
#N1389. Create Target Array in the Given Order
problem link=>https://leetcode.com/problems/create-target-array-in-the-given-order/

#solution
class Solution {
public int[] createTargetArray(int[] nums, int[] index) {
int[] target = new int[nums.length];
int i = 0, k = 0;

while (i < index.length) {

for (k = target.length - 1; k > index[i]; k--)
target[k] = target[k - 1];

target[index[i]] = nums[i];
i++;
}

return target;
}
}
‼️P.s. Without ArrayLists. It would be simple and no-logic with ArrayLists
image_2021-10-26_12-57-51.png
34.9 KB
#N1528. Shuffle String
problem link=>https://leetcode.com/problems/shuffle-string/

#solution
class Solution {
public String restoreString(String s, int[] indices) {
char[] ch=new char[s.length()];
for (int i = 0; i < s.length(); i++) {
ch[i] = s.charAt(i);
}
char[] newch=new char[ch.length];
for(int i=0; i<indices.length;i++){
newch[indices[i]]=ch[i];
}


return new String(newch);
}
}
image_2021-10-26_12-58-50.png
26.5 KB
#N1720. Decode XORed Array
problem link=>https://leetcode.com/problems/decode-xored-array/

#solution
class Solution {
public int[] decode(int[] encoded, int first) {
int[] arr=new int[encoded.length+1];
arr[0]=first;
for(int i=0; i<encoded.length; i++){
arr[i+1]=arr[i]^encoded[i];
}

return arr;
}
}
image_2021-10-26_17-48-37.png
26.6 KB
#N961. N-Repeated Element in Size 2N Array
problem link=>https://leetcode.com/problems/n-repeated-element-in-size-2n-array/

#solution
class Solution {
public int repeatedNTimes(int[] nums) {
int arr[]=new int[10000];
for(int i=0; i<nums.length; i++){
if(++arr[nums[i]]==2)
return nums[i];
}
return 0;
}
}
image_2021-10-26_18-02-16.png
36.4 KB
#N942. DI String Match
problem link=>https://leetcode.com/problems/di-string-match/

#solution
class Solution {
public int[] diStringMatch(String s) {
char[] ch=s.toCharArray();
int[] arr=new int[ch.length+1];
int I0=0, Dn=ch.length;
for(int i=0; i<ch.length; i++){
if(ch[i]=='I')
arr[i]=I0++;
if(ch[i]=='D')
arr[i]=Dn--;
}

arr[arr.length-1]=I0;

return arr;
}
}
image_2021-10-27_11-17-19.png
36.7 KB
#N1748. Sum of Unique Elements
problem link=>https://leetcode.com/problems/sum-of-unique-elements/

#solution
class Solution {
public int sumOfUnique(int[] nums) {
int countFrequency[]=new int[101];
for(int num:nums){
++countFrequency[num];
}

int sum=0;

for(int i=0; i<countFrequency.length; i++){
if(countFrequency[i]==1)
sum+=i;
}

return sum;
}
}
image_2021-10-27_15-23-19.png
35.2 KB
#N9. Palindrome Number
problem link=>https://leetcode.com/problems/palindrome-number/

#solution
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
int num=x;
int palindrome=0;
int temp=0;
while(true){
temp=x%10;
x/=10;
palindrome=palindrome*10+temp;
if(x==0)
break;
}

return num==palindrome;
}
}
image_2021-10-27_17-05-48.png
56.5 KB
#N13. Roman to Integer
problem link=>https://leetcode.com/problems/roman-to-integer/

#solution
class Solution {
public int romanToInt(String s) {
int nums[]=new int[s.length()];
int temp=0;
int num=0;

for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='I') nums[i]=1;
if(s.charAt(i)=='V') nums[i]=5;
if(s.charAt(i)=='X') nums[i]=10;
if(s.charAt(i)=='L') nums[i]=50;
if(s.charAt(i)=='C') nums[i]=100;
if(s.charAt(i)=='D') nums[i]=500;
if(s.charAt(i)=='M') nums[i]=1000;
}

for(int i=0; i<nums.length-1; i++){
if(nums[i]<nums[i+1]){
num-=nums[i];
}else
num+=nums[i];
}
num+=nums[nums.length-1];
return num;
}
}
image_2021-10-28_01-48-53.png
41.1 KB
#N67. Add Binary
problem link=>https://leetcode.com/problems/add-binary/

#solution
class Solution {
public String addBinary(String a, String b) {
int lena = a.length();
int lenb = b.length();
int i =0, qarz = 0;
String res = "";
while(i<lena || i<lenb || qarz!=0){
int x = (i<lena) ? Integer.parseInt(""+a.charAt(lena - 1 - i)) : 0;
int y = (i<lenb) ? Integer.parseInt(""+b.charAt(lenb - 1 - i)) : 0;
res = (x + y + qarz)%2 + res;
qarz = (x + y + qarz)/2;
i++;
}

return res;
}
}
1
image_2021-10-28_02-50-24.png
35.6 KB
#N69. Sqrt(x)
problem link=>https://leetcode.com/problems/sqrtx/

#solution
class Solution {
public int mySqrt(int x) {
if (x == 0) return 0;
int start = 1, end = x;
while (start < end) {
int mid = (start + end) / 2;
if (mid <= x / mid && (mid + 1) > x / (mid + 1))
return mid;
else if (mid > x / mid)
end = mid;
else
start = mid + 1;
}
return start;
}
}
image_2021-10-28_03-22-49.png
26 KB
#N70. Climbing Stairs
problem link=>https://leetcode.com/problems/climbing-stairs/

#solution
class Solution {
public int climbStairs(int n) {
int fib=0, x=1, y=1;
for(int i=1; i<n; i++){
fib=y;
y=x+y;
x=fib;
}

return y;
}
}
image_2021-10-29_01-38-15.png
30.7 KB
#N2037. Minimum Number of Moves to Seat Everyone
problem link=>https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/

#solution
class Solution {
public int minMovesToSeat(int[] seats, int[] students) {
Arrays.sort(seats);
Arrays.sort(students);
int balance=0;
for(int i=0; i<seats.length; i++){
balance+=Math.abs(students[i]-seats[i]);
}

return balance;
}
}
image_2021-10-29_01-40-43.png
37.5 KB
#N905. Sort Array By Parity
problem link=>https://leetcode.com/problems/sort-array-by-parity/

#solution
class Solution {
public int[] sortArrayByParity(int[] nums) {
int temp=0;
for(int i=0; i<nums.length-1; i++){
for(int j=0; j<nums.length-i-1; j++){
if(nums[j]%2==1&&nums[j+1]%2==0){
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}

return nums;
}
}
image_2021-10-29_01-46-19.png
24.2 KB
#N561. Array Partition I
problem link=>https://leetcode.com/problems/array-partition-i/

#solution
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int sum=0;
for(int i=0; i<nums.length; i=i+2){
sum+=nums[i];
}

return sum;
}
}
image_2021-10-29_14-53-36.png
39.9 KB
#N704. Binary Search
problem link=>https://leetcode.com/problems/binary-search/

#solution
class Solution {
public int search(int[] nums, int target) {
int left=0, right=nums.length-1, mid;
while(left<=right){
mid=(left+right)/2;
if(nums[mid]<target){
left=mid+1;
}
if(nums[mid]>target){
right=mid-1;
}

if(nums[mid]==target){
return mid;
}
}

return -1;
}
}

‼️Implementation of Bineary Search to the problems
image_2021-10-29_15-02-21.png
33.8 KB
#N1346. Check If N and Its Double Exist
problem link=>https://leetcode.com/problems/check-if-n-and-its-double-exist/

#solution
class Solution {
public boolean checkIfExist(int[] arr) {
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr.length; j++){
if(arr[i]==2*arr[j]&&i!=j)
return true;
}
}

return false;
}
}
image_2021-10-31_00-21-17.png
67.8 KB
#N1154. Day of the Year
problem link=>https://leetcode.com/problems/day-of-the-year/

#solution
class Solution {
public int dayOfYear(String date) {
int[] days={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year=Integer.parseInt(date.substring(0, 4));
int month=Integer.parseInt(date.substring(5, 7));
int isLeapYear=0;

int ans=0;

for(int i=0; i<month; i++){
ans+=days[i];
}

ans+=Integer.parseInt(date.substring(8, 10));

if(year%4!=0)
isLeapYear=0;
else if(year%100!=0)
isLeapYear=1;
else if(year%400!=0)
isLeapYear=0;
else
isLeapYear=1;

if(month<=2)
isLeapYear=0;

return ans+isLeapYear;
}
}
image_2021-10-31_02-54-09.png
52 KB
#N1556. Thousand Separator
problem link=>https://leetcode.com/problems/thousand-separator/

#solution
class Solution {
public String thousandSeparator(int n) {
String str=String.valueOf(n);
int xonalar=str.length();
String ans="";

if(xonalar<=3) return str;

if(xonalar>3&&xonalar%3!=0)
ans+=str.substring(0, xonalar%3)+".";

int index=xonalar%3+2;
while(index<=xonalar){
ans+=str.substring(index-2, index+1)+".";
index+=3;
}

return ans.substring(0, ans.length()-1);
}
}
image_2021-10-31_04-26-08.png
41.5 KB
#N1051. Height Checker
problem link=>https://leetcode.com/problems/height-checker/

#solution
class Solution {
public int heightChecker(int[] heights) {
int[] expected=new int[heights.length];
for(int i=0; i<expected.length; i++){
expected[i]=heights[i];
}
int count=0;

Arrays.sort(expected);

for(int i=0; i<heights.length; i++){
if(heights[i]!=expected[i])
count++;
}

return count;
}
}
image_2021-10-31_04-30-00.png
31.1 KB
#N1460. Make Two Arrays Equal by Reversing Sub-arrays
problem link=>https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/

#solution
class Solution {
public boolean canBeEqual(int[] target, int[] arr) {
Arrays.sort(target);
Arrays.sort(arr);

for(int i=0; i<arr.length; i++){
if(arr[i]!=target[i])
return false;
}
return true;
}
}