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-11_21-35-59.png
23.6 KB
#N136. Single Number
problem link

#solution
class Solution {
public int singleNumber(int[] nums) {
int ans = 0;
int len = nums.length;
for(int i = 0; i != len; i++)
ans ^= nums[i];

return ans;

}
}

p.s. good problem to understand XOR operator better)
image_2021-11-12_01-47-18.png
30.7 KB
#N1925. Count Square Sum Triples
problem link

#solution
class Solution {
public int countTriples(int n) {
int count = 0;
for (int i = 1; i < n; i++){
for (int j = i + 1; j < n; j++){
int square = i * i + j * j;
int c = (int) Math.sqrt(i * i + j * j);
if (c <= n && c * c == square) count++;
}
}

return count*2;
}
}
image_2021-11-12_02-06-19.png
31.8 KB
#N1323. Maximum 69 Number
problem link

#solution
class Solution {
public int maximum69Number (int num) {
int first6=-1;
int number=num;

for(int i=1; num>0; i++){
if(num%10==6){
first6=i;
}
num/=10;
}

return number+3*(int)Math.pow(10, first6-1);
}
}
image_2021-11-12_15-55-09.png
61.3 KB
#N66. Plus One
problem link

#solution
class Solution {
public int[] plusOne(int[] digits) {
int count=0;
for(int i=0;i<digits.length; i++){
if(digits[i]==9) count++;
}
if(digits[digits.length-1]!=9){
digits[digits.length-1]=digits[digits.length-1]+1;
return digits;
}
if(count!=digits.length){
for(int i=digits.length-1; i>=0; i--){
if(digits[i]>=9){
digits[i]=0;
}else{
digits[i]=digits[i]+1;
break;
}
}
}else{
int[] arr=new int[digits.length+1];
arr[0]=1;
for(int i=1; i<arr.length; i++){
arr[i]=0;
}
return arr;
}
return digits;
}
}
image_2021-11-13_20-10-24.png
50.7 KB
#N1290. Convert Binary Number in a Linked List to Integer
problem link

#solution
class Solution {
public int getDecimalValue(ListNode head) {
int num = head.val;
while (head.next != null) {
num = num * 2 + head.next.val;
head = head.next;
}
return num;

}
}
image_2021-11-13_20-43-54.png
51.6 KB
#N876. Middle of the Linked List
problem link

#solution
class Solution {
public ListNode middleNode(ListNode head) {
ListNode temp=head;
int length=0;
while(temp!=null){
temp=temp.next;
length++;
}

for(int i=1; i<=length/2; i++)
head=head.next;

return head;
}
}
image_2021-11-14_00-07-22.png
30.1 KB
#N237. Delete Node in a Linked List
problem link

#solution
class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}
p.s.: but the question is totally wrong), because the head node is not given
image_2021-11-14_00-34-17.png
38.2 KB
#N206. Reverse Linked List
problem link

#solution
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null) return head;
ListNode prev=null, curr=head, after=head.next;

while(curr.next.next!=null){
curr.next=prev;
prev=curr;
curr=after;
after=after.next;
}

curr.next=prev;
after.next=curr;

return after;
}
}
image_2021-11-14_00-55-39.png
49.4 KB
#N21. Merge Two Sorted Lists
problem link

#solution
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode();
ListNode temp=head;

while(l1!=null&&l2!=null){
if(l1.val>=l2.val){
temp.next=l2;
l2=l2.next;
}
else{
temp.next=l1;
l1=l1.next;
}
temp=temp.next;
}

temp.next=(l1==null)?l2:l1;

return head.next;
}
}
image_2021-11-14_01-18-29.png
22.2 KB
#N1716. Calculate Money in Leetcode Bank
problem link

#solution
class Solution {
public int totalMoney(int n) {
return (n+1-5*(n/7))*(n-7*(n/7))/2+(49+(n/7)*7)*(n/7)/2;
}
}
#statistics
Till this time, 118 in total,
109 easy;
9 medium;
type of problem solutions have been posted
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;
}
}