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

Let's Develop Together!
Download Telegram
image_2021-11-15_01-17-38.png
32.5 KB
#N868. Binary Gap
problem link

#solution
class Solution {
public int binaryGap(int n) {
int res=0, d=-30;

while(n>0){
if(n%2==1){
res=Math.max(res, d);
d=0;
}
n=n/2;
d++;
}

return res;
}
}
image_2021-11-15_01-30-54.png
34.3 KB
#N1518. Water Bottles
problem link

#solution
class Solution {
public int numWaterBottles(int bottle, int exchange) {
int count=0;

while(bottle>0){
count+=bottle-bottle%exchange;
if(bottle>=exchange)
bottle=bottle/exchange+bottle%exchange;
else
break;
}

return count+bottle;
}
}
image_2021-11-15_01-43-45.png
30.7 KB
#medium

#N1833. Maximum Ice Cream Bars
problem link

#solution
class Solution {
public int maxIceCream(int[] costs, int coins) {
int count=0;
Arrays.sort(costs);

for(int i=0; i<costs.length&&coins>0; i++){
if(costs[i]<=coins){
coins-=costs[i];
count++;
}
}
return count;
}
}
image_2021-11-16_02-57-00.png
52.3 KB
#N2032. Two Out of Three
problem link

#solution
class Solution {
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
ArrayList<Integer> ans=new ArrayList<Integer>();
int[] helper1=new int[101];
int[] helper2=new int[101];
int[] helper3=new int[101];

for(int num:nums1)
helper1[num]=1;

for(int num:nums2)
helper2[num]=1;

for(int num:nums3)
helper3[num]=1;

for(int i=0; i<helper1.length; i++){
if(helper1[i]+helper2[i]+helper3[i]>1)
ans.add(i);
}

return ans;
}
}
image_2021-11-16_17-30-33.png
33.6 KB
#N83. Remove Duplicates from Sorted List
problem link

#solution
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null) return head;
ListNode curr=head;
while(curr.next!=null){
if(curr.val==curr.next.val){
curr.next=curr.next.next;
}
else
curr=curr.next;
}

return head;
}
}
image_2021-11-17_11-35-49.png
39.3 KB
#N160. Intersection of Two Linked Lists
problem link

#solution
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode currA=headA, currB=headB;

while(currA!=currB){
if(currA==null)
currA=headB;
else
currA=currA.next;

if(currB==null)
currB=headA;
else
currB=currB.next;
}

return currA;

}
}
image_2021-11-17_11-53-16.png
39 KB
#N234. Palindrome Linked List
problem link

#solution
class Solution {
public boolean isPalindrome(ListNode head) {
List<Integer> arr=new ArrayList<>();
ListNode curr=head;

while(curr!=null){
arr.add(curr.val);
curr=curr.next;
}

for(int i=0; i<arr.size()/2; i++){
if(arr.get(i)!=arr.get(arr.size()-i-1))
return false;

}

return true;
}
}
image_2021-11-17_15-59-40.png
35.3 KB
#N203. Remove Linked List Elements
problem link

#solution
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode curr=head;
if(head==null) return head;
while(curr.next!=null){
if(curr.next.val==val)
curr.next=curr.next.next;
else
curr=curr.next;
}
if(head.val==val) return head.next;

return head;
}
}
image_2021-11-18_14-37-00.png
38.3 KB
#N448. Find All Numbers Disappeared in an Array
problem link

#solution
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res=new ArrayList<>();
int[] count=new int[nums.length+1];

for(int num:nums)
count[num]++;

for(int i=1; i<count.length; i++){
if(count[i]==0)
res.add(i);
}

return res;
}
}
image_2021-11-19_01-10-08.png
45.5 KB
#N1854. Maximum Population Year
problem link

#solution
class Solution {
public int maximumPopulation(int[][] logs) {
int[] count=new int[101];
int year=0, max=0;

for(int i=0; i<logs.length; i++){
for(int j=logs[i][0]; j<logs[i][1]; j++)
count[j-1950]++;
}

for(int i=count.length-1; i>=0; i--){
if(count[i]>=max){
max=count[i];
year=i+1950;
}
}

return year;
}
}
image_2021-11-19_01-20-32.png
39.1 KB
#N258. Add Digits
problem link

#solution
class Solution {
public int addDigits(int num) {

return add(num);
}

int add(int num){
if(num<10) return num;
int rem=0, sum=0;
while(num>0){
rem=num%10;
num/=10;
sum+=rem;
}

num=sum;
return add(num);

}
}
image_2021-11-19_01-32-08.png
28 KB
#N976. Largest Perimeter Triangle
problem link

#solution
class Solution {
public int largestPerimeter(int[] nums) {
int max=0;
Arrays.sort(nums);
for(int i=nums.length-3; i>=0; i--){
if(nums[i]+nums[i+1]>nums[i+2])
return nums[i]+nums[i+1]+nums[i+2];
}

return 0;
}
}
image_2021-11-19_01-41-26.png
31.8 KB
#N268. Missing Number
problem link

#solution
class Solution {
public int missingNumber(int[] nums) {
int count[]=new int[nums.length+1];

for(int num:nums)
count[num]++;

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

return 0;
}
}
image_2021-11-19_01-54-18.png
35.9 KB
#N141. Linked List Cycle
problem link

#solution
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null) return false;
ListNode curr=head, ahead=head;

while(ahead.next!=null&&ahead.next.next!=null){
curr=curr.next;
ahead=ahead.next.next;

if(curr==ahead)
return true;
}

return false;
}
}
image_2021-11-19_14-54-18.png
29 KB
#N461. Hamming Distance
problem link

#solution
class Solution {
public int hammingDistance(int x, int y) {
int num=x^y;
int count=0;
while(num>0){
if(num%2==1)
count++;

num/=2;
}

return count;
}
}
image_2021-11-19_15-28-32.png
56.6 KB
#medium

#N1721. Swapping Nodes in a Linked List
problem link

#solution
class Solution {
public ListNode swapNodes(ListNode head, int k) {
int length=0, k1=k;
ListNode curr=head;

while(curr!=null){
curr=curr.next;
length++;
}

ListNode first=head, second=head;

while(k>1){
first=first.next;
k --;
}

while(length-k1>0){
second=second.next;
length--;
}

int temp=first.val;
first.val=second.val;
second.val=temp;

return head;

}
}
image_2021-11-19_16-28-32.png
50.6 KB
#N1021. Remove Outermost Parentheses
problem link

#solution
class Solution {
public String removeOuterParentheses(String s) {
StringBuilder res=new StringBuilder();
Stack stack=new Stack();
int size=0;
for(char ch:s.toCharArray()){
if(ch=='('){
stack.push(ch);
size++;
if(size!=1)
res.append(ch);
}

if(ch==')'){
stack.pop();
size--;
if(size!=0)
res.append(ch);
}
}

return res.toString();
}
}
image_2021-11-19_17-27-59.png
35.1 KB
#N1475. Final Prices With a Special Discount in a Shop
problem link

#solution
class Solution {
public int[] finalPrices(int[] prices) {
Stack<Integer> stack=new Stack<>();
for(int i=0; i<prices.length; i++){
while(stack.size()>0&&prices[stack.peek()]>=prices[i]){
prices[stack.peek()]-=prices[i];
stack.pop();
}
stack.push(i);
}

return prices;
}
}
image_2021-11-19_17-58-20.png
48.5 KB
#N1047. Remove All Adjacent Duplicates In String
problem link

#solution
class Solution {
public String removeDuplicates(String s) {
StringBuilder res=new StringBuilder();
Stack<Character> stack=new Stack<>();
char[] letters=s.toCharArray();

for(int i=letters.length-1; i>=0; i--){
if(!stack.isEmpty()&&letters[i]==stack.peek())
stack.pop();
else
stack.push(letters[i]);
}

while(!stack.isEmpty()){
res.append(stack.pop());
}

return res.toString();
}
}
image_2021-11-22_11-40-28.png
37 KB
#N1700. Number of Students Unable to Eat Lunch
problem link

#solution
class Solution {
public int countStudents(int[] students, int[] sandwiches) {
int[] count=new int[2];
int i=0;

for(int student:students)
count[student]++;

for(; i<sandwiches.length && count[sandwiches[i]]!=0; i++)
count[sandwiches[i]]--;

return sandwiches.length-i;
}
}