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

Let's Develop Together!
Download Telegram
Leetcode in Java && Oracle
image_2021-11-09_15-12-58.png
image_2021-11-09_15-29-43.png
57.8 KB
#updated

class Solution {
public String customSortString(String order, String s) {
StringBuilder res=new StringBuilder();
int[] arr=new int[26];

for(int i=0; i<s.length(); i++){
arr[s.charAt(i)-'a']++;
}

for(int i=0; i<order.length(); i++){
while(arr[order.charAt(i)-'a']>0){
res.append(order.charAt(i));
arr[order.charAt(i)-'a']--;
}
}

for(int i=0; i<26; i++){
while(arr[i]>0){
res.append((char) (i+'a'));
arr[i]--;
}
}

return res.toString();
}
}

p.s.: better runtime than previous one)
image_2021-11-09_18-00-09.png
60.1 KB
#N1122. Relative Sort Array
problem link

#solution
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] count=new int[1001];
int[] res=new int[arr1.length];

for(int i=0; i<arr1.length; i++){
count[arr1[i]]++;
}
int index=0;
for(int i=0; i<arr2.length; i++){
while(count[arr2[i]] > 0){
res[index]=arr2[i];
index++;
count[arr2[i]]--;
}
}

for(int i=0; i<count.length; i++){
if(count[i]!=0){
while(count[i]>0){
res[index]=i;
index++;
count[i]--;
}
}
}

return res;
}
}
image_2021-11-10_13-52-56.png
19.9 KB
#N441. Arranging Coins
problem link

#solution
class Solution {
public int arrangeCoins(int n) {
return ((int)(Math.pow((1+8*(long)n), 0.5))-1)/2;
}
}

p.s. Yeah, math really helps to solve this kind of problems😅
image_2021-11-11_21-24-38.png
27.7 KB
#N1413. Minimum Value to Get Positive Step by Step Sum
problem link

#solution
class Solution {
public int minStartValue(int[] nums) {
int sum=0, minValue=0;

for(int num:nums){
sum+=num;
minValue=Math.min(minValue, sum);
}

return 1-minValue;
}
}
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;
}
}