image_2021-11-01_03-55-22.png
40.9 KB
#N1281. Subtract the Product and Sum of Digits of an Integer
problem link=>https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
#solution
problem link=>https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
#solution
class Solution {
public int subtractProductAndSum(int n) {
int product=1;
int sum=0;
int temp=0;
while(n>0){
temp=n%10;
product*=temp;
sum+=temp;
n/=10;
}
return product-sum;
}
}image_2021-11-01_03-58-29.png
35.9 KB
#N1342. Number of Steps to Reduce a Number to Zero
problem link=>https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
#solution
problem link=>https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
#solution
class Solution {
public int numberOfSteps(int num) {
int steps=0;
while(num>0){
if(num%2==0)
num/=2;
else
num--;
steps++;
}
return steps;
}
}image_2021-11-01_04-03-50.png
26.6 KB
#N1486. XOR Operation in an Array
problem link=>https://leetcode.com/problems/xor-operation-in-an-array/
#solution
problem link=>https://leetcode.com/problems/xor-operation-in-an-array/
#solution
class Solution {
public int xorOperation(int n, int start) {
int ans=0;
while(n-- > 0){
ans ^= start;
start+=2;
}
return ans;
}
}image_2021-11-01_04-22-39.png
46.9 KB
#medium
#N1828. Queries on Number of Points Inside a Circle
problem link=>https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/
#solution
#N1828. Queries on Number of Points Inside a Circle
problem link=>https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/
#solution
class Solution {
public int[] countPoints(int[][] point, int[][] circle) {
int insidePoints=0;
int[] arrOfInsidePoints=new int[circle.length];
for(int i=0; i<circle.length; i++){
for(int j=0; j<point.length; j++){
if(Math.pow(circle[i][0]-point[j][0], 2)+Math.pow(circle[i][1]-point[j][1], 2)<=Math.pow(circle[i][2], 2))
insidePoints++;
}
arrOfInsidePoints[i]=insidePoints;
insidePoints=0;
}
return arrOfInsidePoints;
}
}image_2021-11-01_15-39-39.png
24 KB
#N1688. Count of Matches in Tournament
problem link=>https://leetcode.com/problems/count-of-matches-in-tournament/
#solution
problem link=>https://leetcode.com/problems/count-of-matches-in-tournament/
#solution
class Solution {
public int numberOfMatches(int n) {
int matches=0;
while(n>1){
matches+=n/2;
n-=n/2;
}
return matches;
}
}image_2021-11-01_17-03-45.png
28.1 KB
#N1812. Determine Color of a Chessboard Square
problem link=>https://leetcode.com/problems/determine-color-of-a-chessboard-square/
#solution
problem link=>https://leetcode.com/problems/determine-color-of-a-chessboard-square/
#solution
class Solution {
public boolean squareIsWhite(String coordinates) {
int letter=coordinates.charAt(0)-97;
int num=coordinates.charAt(1)-49;
if((letter+num)%2==0)
return false;
return true;
}
}image_2021-11-01_17-30-46.png
34 KB
#N728. Self Dividing Numbers
problem link=>https://leetcode.com/problems/self-dividing-numbers/
#solution
problem link=>https://leetcode.com/problems/self-dividing-numbers/
#solution
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans=new ArrayList<>();
for (int i = left, num = 0; i <= right; i++) {
for (num = i; num > 0; num /= 10)
if (num % 10 == 0 || i % (num % 10) != 0)
break;
if (num == 0) ans.add(i);
}
return ans;
}
}image_2021-11-01_18-04-03.png
48.2 KB
#N1837. Sum of Digits in Base K
problem link=>https://leetcode.com/problems/sum-of-digits-in-base-k/
#solution
problem link=>https://leetcode.com/problems/sum-of-digits-in-base-k/
#solution
class Solution {
public int sumBase(int n, int k) {
int reverseBase=convertToBaseK(n, k);
int sum=0;
for(; reverseBase>0; reverseBase/=10){
sum+=reverseBase%10;
}
return sum;
}
//function to convert to Base K
int convertToBaseK(int n, int k){
int reverseBase=0;
for(; n>0; n/=k){
reverseBase=reverseBase*10+n%k;
}
return reverseBase;
}
}image_2021-11-01_18-31-53.png
25 KB
#N509. Fibonacci Number
problem link=>https://leetcode.com/problems/fibonacci-number/
#solution
problem link=>https://leetcode.com/problems/fibonacci-number/
#solution
class Solution {
public int fib(int n) {
return F(n);
}
int F(int n){
int sum=0;
if(n==0) return 0;
if(n==1) return 1;
return F(n-2)+F(n-1);
}
}image_2021-11-02_15-23-58.png
52.8 KB
#N551. Student Attendance Record I
problem link=>https://leetcode.com/problems/student-attendance-record-i/
#solution
problem link=>https://leetcode.com/problems/student-attendance-record-i/
#solution
class Solution {
public boolean checkRecord(String s) {
char[] ch=s.toCharArray();
int isLate=1;
int isAbsent=0;
for(int i=0; i<ch.length; i++){
if(ch[i]=='A'){
isAbsent++;
}
}
for(int i=0; i<ch.length-1;i++){
if(ch[i]=='L'&&ch[i+1]=='L'){
isLate++;
if(isLate==3)
return false;
}
else if(ch[i]!='L'&&ch[i+1]=='L'){
isLate=1;
}
}
return isAbsent<2 && isLate<3;
}
}image_2021-11-02_17-52-09.png
64.8 KB
#medium
#N969. Pancake Sorting
problem link=>https://leetcode.com/problems/pancake-sorting/
#solution
#N969. Pancake Sorting
problem link=>https://leetcode.com/problems/pancake-sorting/
#solution
class Solution {
public List<Integer> pancakeSort(int[] arr) {
ArrayList<Integer> ans = new ArrayList<Integer>();
int indexMax=0, max=arr.length, temp=0;
for(int i=0; i<arr.length-1; i++){
for(int j=0; j<arr.length-i; j++){
if(arr[j]==max) indexMax=j+1;
}
if(arr[0]!=max){
for(int k = 0; k < indexMax / 2; k++){
temp = arr[k];
arr[k] = arr[indexMax - k - 1];
arr[indexMax - k - 1] = temp;
}
ans.add(indexMax);
}
ans.add(max);
for(int k = 0; k < max / 2; k++){
temp = arr[k];
arr[k] = arr[max - k - 1];
arr[max - k - 1] = temp;
}
max--;
}
return ans;
}
}
Leetcode in Java && Oracle
image_2021-11-02_17-52-09.png
p.s. finally, accepted😊, after 2-hour trying. even 100% runtime and the Medium type)
the best solution ever for me😅
the best solution ever for me😅
image_2021-11-03_01-45-54.png
31.8 KB
#N412. Fizz Buzz
problem link
#solution
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
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
#N2047. Number of Valid Words in a Sentence
problem link=>https://leetcode.com/problems/number-of-valid-words-in-a-sentence/
#solution
problem link=>https://leetcode.com/problems/number-of-valid-words-in-a-sentence/
#solution
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
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
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
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
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;
}
}