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

Let's Develop Together!
Download Telegram
image_2021-11-03_01-45-54.png
31.8 KB
#N412. Fizz Buzz
problem link

#solution
class Solution {
public List<String> fizzBuzz(int n) {
ArrayList<String> ans = new ArrayList<String>();
for(int i=1; i<=n; i++){
if(i%15==0)
ans.add("FizzBuzz");
else if(i%3==0)
ans.add("Fizz");
else if(i%5==0)
ans.add("Buzz");
else
ans.add(""+i);
}

return ans;
}
}
image_2021-11-03_11-26-17.png
63.6 KB
#N1403. Minimum Subsequence in Non-Increasing Order
problem link=>https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/

#solution
class Solution {
public List<Integer> minSubsequence(int[] nums) {
ArrayList<Integer> ans=new ArrayList<Integer>();
int sum=0, sumSpecial=0, temp=0;

for(int i=0; i<nums.length; i++)
sum+=nums[i];

Arrays.sort(nums);
for(int i=0; i<nums.length/2; i++){
temp=nums[i];
nums[i]=nums[nums.length-i-1];
nums[nums.length-i-1]=temp;
}

for(int i=0; i<nums.length; i++){
sum-=nums[i];
sumSpecial+=nums[i];
if(sum>=sumSpecial)
ans.add(nums[i]);
else{
ans.add(nums[i]);
break;
}
}

return ans;
}
}
image_2021-11-03_14-27-07.png
79.6 KB
Leetcode in Java && Oracle
image_2021-11-03_14-27-07.png
class Solution {
public int countValidWords(String sentence) {
String[] words=sentence.split(" ");
int count=0;

for(String word:words){
if(!word.equals("")&&isWord(word))
count++;
}

return count;
}

boolean isWord(String word){
int hyphenCount=0;
for(int i=0; i<word.length(); i++){
if(Character.isDigit(word.charAt(i))||word.charAt(i)==' ') return false;

if(word.charAt(i)=='!'||word.charAt(i)=='.'||word.charAt(i)==',')
if(i!=word.length()-1) return false;

if(word.charAt(i)=='-')
if(i==0||i==word.length()-1) return false;
else{
if(word.charAt(i-1)>=97&&word.charAt(i-1)<=122&&word.charAt(i+1)>=97&&word.charAt(i+1)<=122)
hyphenCount++;
else return false;
}

}
if(hyphenCount>1) return false;
return true;
}
}

P.s.😢
image_2021-11-03_15-57-42.png
26 KB
#N977. Squares of a Sorted Array
problem link=>https://leetcode.com/problems/squares-of-a-sorted-array/

#solution
class Solution {
public int[] sortedSquares(int[] nums) {
int[] ans=new int[nums.length];
for(int i=0; i<nums.length; i++){
ans[i]=nums[i]*nums[i];
}

Arrays.sort(ans);
return ans;
}
}
image_2021-11-03_16-02-07.png
28.5 KB
#N852. Peak Index in a Mountain Array
problem link=>https://leetcode.com/problems/peak-index-in-a-mountain-array/

#solution
class Solution {
public int peakIndexInMountainArray(int[] arr) {
int index=-1;
for(int i=0; i<arr.length-1; i++){
if(arr[i]>arr[i+1]){
index=i;
break;
}
}

return index;
}
}
image_2021-11-03_17-41-51.png
27.6 KB
#N1217. Minimum Cost to Move Chips to The Same Position

click➡️ problem link

#solution
class Solution {
public int minCostToMoveChips(int[] position) {
int[] helper=new int[2];

for(int chip:position)
helper[chip%2]++;

return Math.min(helper[0], helper[1]);
}
}
image_2021-11-04_16-42-36.png
75 KB
#N1380. Lucky Numbers in a Matrix

problem link

#solution
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
ArrayList<Integer> ans=new ArrayList<Integer>();
int[] minRow=new int[matrix.length];
int[] maxCol=new int[matrix[0].length];
int min=0, max=0;
for(int i=0; i<minRow.length; i++){
min=matrix[i][0];
for(int j=0; j<matrix[0].length; j++){
min=Math.min(min, matrix[i][j]);
}
minRow[i]=min;
}
for(int i=0; i<maxCol.length; i++){
max=matrix[0][i];
for(int j=0; j<matrix.length; j++){
max=Math.max(max, matrix[j][i]);
}

maxCol[i]=max;
}
for(int i=0; i<maxCol.length; i++){
for(int j=0; j<minRow.length; j++){
if(maxCol[i]==minRow[j])
ans.add(maxCol[i]);
}
}
return ans;
}
}
image_2021-11-04_17-40-36.png
58.8 KB
#N821. Shortest Distance to a Character
problem link

#solution
class Solution {
public int[] shortestToChar(String s, char ch) {
int[] ans=new int[s.length()];
int left=-1, right=-1;

for(int i=0; i<ans.length; i++){
if(s.charAt(i)!=ch){
for(int j=i+1; j<ans.length; j++){
if(s.charAt(j)==ch){
right=j;
break;
}
}
if(left==-1)
ans[i]=right-i;
else if(right==-1)
ans[i]=i-left;
else
ans[i]=Math.min(right-i, i-left);
}
else{
left=i;
right=-1;
}
}

return ans;
}
}
image_2021-11-04_17-50-48.png
40.4 KB
#N922. Sort Array By Parity II
problem link

#solution
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int[] ans=new int[nums.length];
int evenIndex=0, oddIndex=1;

for(int i=0; i<nums.length; i++){
if(nums[i]%2==0){
ans[evenIndex]=nums[i];
evenIndex+=2;
}
else{
ans[oddIndex]=nums[i];
oddIndex+=2;
}
}

return ans;
}
}
image_2021-11-05_17-43-17.png
33.9 KB
#N1441. Build an Array With Stack Operations

problem link

#solution
class Solution {
public List<String> buildArray(int[] target, int n) {
ArrayList<String> stack=new ArrayList<String>();
int x=0;
for(int i=1;i<=n && x<target.length; i++){
stack.add("Push");
if(target[x]==i) x++;
else stack.add("Pop");
}

return stack;
}
}
image_2021-11-07_00-54-43.png
27.2 KB
#N1108. Defanging an IP Address
problem link

#solution
class Solution {
public String defangIPaddr(String address) {
String res="";
for(char ch:address.toCharArray()){
if(ch=='.')
res+="[.]";
else
res+=ch;
}

return res;
}
}
image_2021-11-07_01-01-04.png
27.2 KB
#N771. Jewels and Stones
problem link

#solution
class Solution {
public int numJewelsInStones(String jewels, String stones) {
int count=0;
for(char j:jewels.toCharArray()){
for(char s:stones.toCharArray()){
if(j==s) count++;
}
}

return count;
}
}
image_2021-11-07_03-38-55.png
42.6 KB
#N1678. Goal Parser Interpretation
problem link

#solution
class Solution {
public String interpret(String command) {
String res="";
for(int i=0; i<command.length()-1; i++){
if(command.charAt(i)=='('&&command.charAt(i+1)==')')
res+="o";
else if(command.charAt(i)=='('&&command.charAt(i+1)=='a')
res+="al";
else if(command.charAt(i)=='G')
res+="G";
}
if(command.charAt(command.length()-1)=='G') res+="G";

return res;
}
}
image_2021-11-07_04-12-12.png
31.4 KB
#N1221. Split a String in Balanced Strings
problem link

#solution
class Solution {
public int balancedStringSplit(String s) {
int count=0, balance=0;
for(char ch:s.toCharArray()){
if(ch=='R')
balance--;
if(ch=='L')
balance++;
if(balance==0)
count++;
}

return count;
}
}
image_2021-11-07_04-26-24.png
44 KB
#N1859. Sorting the Sentence
problem link

#solution
class Solution {
public String sortSentence(String s) {
String[] words=s.split(" ");
String[] sorted=new String[words.length];
String res="";

for(String word:words){
sorted[Character.getNumericValue(word.charAt(word.length()-1))-1]=word.substring(0, word.length()-1);
}

for(int i=0; i<sorted.length; i++){
res+=sorted[i];
if(i!=sorted.length-1)
res+=" ";
}

return res;
}
}
image_2021-11-07_04-36-36.png
30.3 KB
#N1614. Maximum Nesting Depth of the Parentheses
problem link

#solution
class Solution {
public int maxDepth(String s) {
int count=0, depth=0;
for(char ch:s.toCharArray()){
if(ch=='(')
count++;
if(ch==')'){
depth=Math.max(depth, count--);
}
}

return depth;
}
}
image_2021-11-07_04-41-37.png
32.4 KB
#N1832. Check if the Sentence Is Pangram
problem link

#solution
class Solution {
public boolean checkIfPangram(String sentence) {
int[] alphabet=new int[26];

for(char ch:sentence.toCharArray()){
alphabet[ch-'a']++;
}

for(int num:alphabet)
if(num==0)
return false;

return true;
}
}
#statistics
Till this time, 91 in total,
86 easy;
5 medium;
type of problem solutions have been posted
image_2021-11-07_16-32-18.png
27.9 KB
#N709. To Lower Case
problem link

#solution
class Solution {
public String toLowerCase(String s) {
char[] res=s.toCharArray();

for(int i=0; i<res.length; i++){
if(res[i]>='A'&&res[i]<='Z')
res[i]=(char)(res[i]+32);
}

return String.valueOf(res);
}
}
or another option, using the String method toLowerCase(), just write return s.toLowerCase(); .