theash2473
theash2473
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 11/15/2024 in #java-help
Adding character/s to a string has two different variations.
Sample code to understand all cases.
String str = "eeksforGeeks";
String str2 = '[' + '[' + str;// first two operands is char so it becomes an int
System.out.println(str2);
str2 = '[' + str;// string gets concatenated with char and returns a string
System.out.println(str2);
str2 = 'a' + "" + 'b';// Trick to convert concatenated chars to a string where "" has to be put among the first two operands
System.out.println(str2);
str2 = 'a' + 'b' + "";// chars get converted to int instead because "" is not among the first two operands
System.out.println(str2);
str2 = str + 's' + 's';// concatenated successfully to string str
System.out.println(str2);
str2 = "" + 's' + 'a' + 'd';// Once it is converted to a string through "" we can add multiple chars to it in the same statement
System.out.println(str2);
String str = "eeksforGeeks";
String str2 = '[' + '[' + str;// first two operands is char so it becomes an int
System.out.println(str2);
str2 = '[' + str;// string gets concatenated with char and returns a string
System.out.println(str2);
str2 = 'a' + "" + 'b';// Trick to convert concatenated chars to a string where "" has to be put among the first two operands
System.out.println(str2);
str2 = 'a' + 'b' + "";// chars get converted to int instead because "" is not among the first two operands
System.out.println(str2);
str2 = str + 's' + 's';// concatenated successfully to string str
System.out.println(str2);
str2 = "" + 's' + 'a' + 'd';// Once it is converted to a string through "" we can add multiple chars to it in the same statement
System.out.println(str2);
14 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 11/15/2024 in #java-help
Adding character/s to a string has two different variations.
14 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 11/15/2024 in #java-help
Adding character/s to a string has two different variations.
Forgot to mention what I found. If first two operands has a string then it will become a string no matter how many oeprands. Otherwise it will be converted to an int.
14 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
Would someone like to take a look in this code and help me?
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
Something is missing always.
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
In this code, the edge cases are not able to be incorporated in the code. which is having (dividend = int_max , divisor=-1 ) or vice versa.
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
public static int divide(int dividend, int divisor) {
int answer = 0, negative = 0;
if ((dividend < 0) && (divisor > 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend - divisor > 0) {// we cannot do dividend>divisor for the dividend = int_min and divisor = 1
// case
int temp_divisor = divisor, counter = 0;
for (; (counter <= 31) && (dividend - temp_divisor >= 0); counter++) {
if (counter == 30) {
System.out.println("Edge case executed");
counter++;
temp_divisor = dividend + 1;
break;
}
temp_divisor = temp_divisor << 1;
System.out.println("temp_divisor is: " + temp_divisor + " power of 2 is: " +
(counter + 1) + " counter is: " + counter);
}
if (temp_divisor != Integer.MAX_VALUE) {
counter--;
temp_divisor = temp_divisor >> 1;
}
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
System.out.println("negated");
answer *= -1;
}
return answer;
}
public static int divide(int dividend, int divisor) {
int answer = 0, negative = 0;
if ((dividend < 0) && (divisor > 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend - divisor > 0) {// we cannot do dividend>divisor for the dividend = int_min and divisor = 1
// case
int temp_divisor = divisor, counter = 0;
for (; (counter <= 31) && (dividend - temp_divisor >= 0); counter++) {
if (counter == 30) {
System.out.println("Edge case executed");
counter++;
temp_divisor = dividend + 1;
break;
}
temp_divisor = temp_divisor << 1;
System.out.println("temp_divisor is: " + temp_divisor + " power of 2 is: " +
(counter + 1) + " counter is: " + counter);
}
if (temp_divisor != Integer.MAX_VALUE) {
counter--;
temp_divisor = temp_divisor >> 1;
}
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
System.out.println("negated");
answer *= -1;
}
return answer;
}
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
This is the current one I am having. It seems that the most optimized code is the only one which is the solution. No other code will be able to work.
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
I am not able to get the code right. I tried optimizing the code but I can't get to the correct solution.
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
The case is giving int_min when it multiplies int_max by 2 in the for loop which should be the condition at which it gets out of the loop but it is creating infinite loop instead.
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/18/2024 in #java-help
In the bitwise compliment operator there is confusion. Please help if you can.
Why do they not mention it int he articles.
17 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/18/2024 in #java-help
In the bitwise compliment operator there is confusion. Please help if you can.
Can you suggest one?
17 replies