Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -28,13 +28,16 @@ class BalancedBrackets {
*/
public static boolean isPaired(char leftBracket, char rightBracket) {
char[][] pairedBrackets = {
{'(', ')'},
{'[', ']'},
{'{', '}'},
{'<', '>'}
{ '(', ')' },
{ '[', ']' },
{ '{', '}' },
{ '<', '>' },
};
for (char[] pairedBracket : pairedBrackets) {
if (pairedBracket[0] == leftBracket && pairedBracket[1] == rightBracket) {
if (
pairedBracket[0] == leftBracket &&
pairedBracket[1] == rightBracket
) {
return true;
}
}
@ -63,7 +66,10 @@ class BalancedBrackets {
case ')':
case ']':
case '}':
if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) {
if (
bracketsStack.isEmpty() ||
!isPaired(bracketsStack.pop(), bracket)
) {
return false;
}
break;

View File

@ -8,6 +8,7 @@ package com.thealgorithms.datastructures.stacks;
import java.util.*;
public class CalculateMaxOfMin {
public static int calculateMaxOfMin(int[] a) {
int n = a.length;
int[] ans = new int[n];
@ -34,4 +35,4 @@ public class CalculateMaxOfMin {
/**
* Given an integer array. The task is to find the maximum of the minimum of the
* given array
*/
*/

View File

@ -24,10 +24,30 @@ public class DecimalToAnyUsingStack {
private static String convert(int number, int radix) {
if (radix < 2 || radix > 16) {
throw new ArithmeticException(
String.format("Invalid input -> number:%d,radius:%d", number, radix));
String.format(
"Invalid input -> number:%d,radius:%d",
number,
radix
)
);
}
char[] tables = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F',
};
Stack<Character> bits = new Stack<>();
do {

View File

@ -2,7 +2,7 @@ package com.thealgorithms.datastructures.stacks;
// 1. You are given a string exp representing an expression.
// 2. Assume that the expression is balanced i.e. the opening and closing brackets match with each other.
// 3. But, some of the pair of brackets maybe extra/needless.
// 3. But, some of the pair of brackets maybe extra/needless.
// 4. You are required to print true if you detect extra brackets and false otherwise.
// e.g.'
// ((a + b) + (c + d)) -> false
@ -25,7 +25,6 @@ public class DuplicateBrackets {
}
st.pop();
}
} else {
st.push(ch);
}
@ -40,5 +39,4 @@ public class DuplicateBrackets {
System.out.println(check(str));
sc.close();
}
}

View File

@ -10,7 +10,8 @@ public class InfixToPostfix {
assert "34+5*6-".equals(infix2PostFix("(3+4)*5-6"));
}
public static String infix2PostFix(String infixExpression) throws Exception {
public static String infix2PostFix(String infixExpression)
throws Exception {
if (!BalancedBrackets.isBalanced(infixExpression)) {
throw new Exception("invalid expression");
}
@ -27,7 +28,10 @@ public class InfixToPostfix {
}
stack.pop();
} else {
while (!stack.isEmpty() && precedence(element) <= precedence(stack.peek())) {
while (
!stack.isEmpty() &&
precedence(element) <= precedence(stack.peek())
) {
output.append(stack.pop());
}
stack.push(element);

View File

@ -1,33 +1,36 @@
package com.thealgorithms.datastructures.stacks;
import java.util.Stack;
/**
*
* @author mohd rameez github.com/rameez471
*/
public class LargestRectangle {
public static String largestRectanglehistogram(int[] heights) {
int n = heights.length, maxArea = 0;
Stack<int[]> st = new Stack<>();
for(int i=0;i<n;i++) {
int start = i;
while(!st.isEmpty() && st.peek()[1] > heights[i]) {
int[] tmp = st.pop();
maxArea = Math.max(maxArea, tmp[1]*(i - tmp[0]));
start = tmp[0];
}
st.push(new int[]{start, heights[i]});
}
while(!st.isEmpty()) {
int[] tmp = st.pop();
maxArea = Math.max(maxArea, tmp[1]*(n-tmp[0]));
}
return Integer.toString(maxArea);
}
public static void main(String[] args) {
assert largestRectanglehistogram(new int[]{2, 1, 5, 6, 2, 3}).equals("10");
assert largestRectanglehistogram(new int[]{2, 4}).equals("4");
}
}
package com.thealgorithms.datastructures.stacks;
import java.util.Stack;
/**
*
* @author mohd rameez github.com/rameez471
*/
public class LargestRectangle {
public static String largestRectanglehistogram(int[] heights) {
int n = heights.length, maxArea = 0;
Stack<int[]> st = new Stack<>();
for (int i = 0; i < n; i++) {
int start = i;
while (!st.isEmpty() && st.peek()[1] > heights[i]) {
int[] tmp = st.pop();
maxArea = Math.max(maxArea, tmp[1] * (i - tmp[0]));
start = tmp[0];
}
st.push(new int[] { start, heights[i] });
}
while (!st.isEmpty()) {
int[] tmp = st.pop();
maxArea = Math.max(maxArea, tmp[1] * (n - tmp[0]));
}
return Integer.toString(maxArea);
}
public static void main(String[] args) {
assert largestRectanglehistogram(new int[] { 2, 1, 5, 6, 2, 3 })
.equals("10");
assert largestRectanglehistogram(new int[] { 2, 4 }).equals("4");
}
}

View File

@ -97,10 +97,9 @@ public class MaximumMinimumWindow {
}
public static void main(String args[]) {
int[] arr = new int[]{10, 20, 30, 50, 10, 70, 30};
int[] target = new int[]{70, 30, 20, 10, 10, 10, 10};
int[] arr = new int[] { 10, 20, 30, 50, 10, 70, 30 };
int[] target = new int[] { 70, 30, 20, 10, 10, 10, 10 };
int[] res = calculateMaxOfMin(arr, arr.length);
assert Arrays.equals(target, res);
}
}

View File

@ -1,6 +1,8 @@
package com.thealgorithms.datastructures.stacks;
import java.util.Arrays;
import java.util.Stack;
/*
Given an array "input" you need to print the first grater element for each element.
For a given element x of an array, the Next Grater element of that element is the
@ -38,11 +40,9 @@ import java.util.Stack;
3. If elements are left in stack after completing while loop then their Next Grater element is -1.
*/
public class NextGraterElement {
public static int[] findNextGreaterElements(int[] array) {
public static int[] findNextGreaterElements(int[] array) {
if (array == null) {
return array;
}
@ -60,8 +60,7 @@ public class NextGraterElement {
return result;
}
public static void main(String[] args)
{
public static void main(String[] args) {
int[] input = { 2, 7, 3, 5, 4, 6, 8 };
int[] result = findNextGreaterElements(input);
System.out.println(Arrays.toString(result));

View File

@ -1,8 +1,8 @@
package com.thealgorithms.datastructures.stacks;
import java.util.Arrays;
import java.util.Stack;
/*
Given an array "input" you need to print the first smaller element for each element to the left side of an array.
For a given element x of an array, the Next Smaller element of that element is the
@ -38,8 +38,8 @@ import java.util.Stack;
*/
public class NextSmallerElement {
public static int[] findNextSmallerElements(int[] array)
{
public static int[] findNextSmallerElements(int[] array) {
// base case
if (array == null) {
return array;
@ -60,8 +60,7 @@ public class NextSmallerElement {
return result;
}
public static void main(String[] args)
{
public static void main(String[] args) {
int[] input = { 2, 7, 3, 5, 4, 6, 8 };
int[] result = findNextSmallerElements(input);
System.out.println(Arrays.toString(result));

View File

@ -50,8 +50,7 @@ public class NodeStack<Item> {
/**
* Constructors for the NodeStack.
*/
public NodeStack() {
}
public NodeStack() {}
private NodeStack(Item item) {
this.data = item;
@ -63,7 +62,6 @@ public class NodeStack<Item> {
* @param item : value to be put on the stack.
*/
public void push(Item item) {
NodeStack<Item> newNs = new NodeStack<Item>(item);
if (this.isEmpty()) {
@ -85,7 +83,6 @@ public class NodeStack<Item> {
* @return item : value that is returned.
*/
public Item pop() {
Item item = (Item) NodeStack.head.getData();
NodeStack.head.setHead(NodeStack.head.getPrevious());

View File

@ -18,11 +18,8 @@ import java.util.Stack;
public class PostfixToInfix {
public static boolean isOperator(char token)
{
switch(token)
{
public static boolean isOperator(char token) {
switch (token) {
case '+':
case '-':
case '/':
@ -34,43 +31,31 @@ public class PostfixToInfix {
return false;
}
public static boolean isValidPostfixExpression(String postfix)
{
public static boolean isValidPostfixExpression(String postfix) {
/* Postfix expression length should NOT be less than 3 */
if(postfix.length() < 3) return false;
if (postfix.length() < 3) return false;
/* First two characters should NOT be operators */
if(isOperator(postfix.charAt(0))) return false;
if(isOperator(postfix.charAt(1))) return false;
if (isOperator(postfix.charAt(0))) return false;
if (isOperator(postfix.charAt(1))) return false;
int operandCount = 0;
int operatorCount = 0;
/* Traverse the postfix string to check if --> Number of operands = Number of operators + 1 */
for(int i = 0; i < postfix.length(); i++)
{
for (int i = 0; i < postfix.length(); i++) {
char token = postfix.charAt(i);
if(isOperator(token))
{
if (isOperator(token)) {
operatorCount++;
if(operatorCount >= operandCount) return false;
}
else
{
if(operatorCount == 0)
{
if (operatorCount >= operandCount) return false;
} else {
if (operatorCount == 0) {
operandCount++;
continue;
}
if(operandCount != operatorCount + 1) return false;
if (operandCount != operatorCount + 1) return false;
/* Operand count is set to 2 because:-
*
@ -81,7 +66,6 @@ public class PostfixToInfix {
*/
operandCount = 2;
/* Reset operator count */
operatorCount = 0;
}
@ -90,17 +74,13 @@ public class PostfixToInfix {
return (operandCount == operatorCount + 1);
}
public static String getPostfixToInfix(String postfix)
{
public static String getPostfixToInfix(String postfix) {
String infix = "";
if(postfix.isEmpty()) return infix;
if (postfix.isEmpty()) return infix;
/* Validate Postfix expression before proceeding with the Infix conversion */
if(!isValidPostfixExpression(postfix))
{
if (!isValidPostfixExpression(postfix)) {
throw new IllegalArgumentException("Invalid Postfix Expression");
}
@ -110,13 +90,10 @@ public class PostfixToInfix {
String operandA, operandB;
char operator;
for(int index = 0; index < postfix.length(); index++)
{
for (int index = 0; index < postfix.length(); index++) {
char token = postfix.charAt(index);
if(!isOperator(token))
{
if (!isOperator(token)) {
stack.push(Character.toString(token));
continue;
}
@ -141,13 +118,12 @@ public class PostfixToInfix {
return infix;
}
public static void main(String args[])
{
public static void main(String args[]) {
assert getPostfixToInfix("ABC+/").equals("(A/(B+C))");
assert getPostfixToInfix("AB+CD+*").equals("((A+B)*(C+D))");
assert getPostfixToInfix("AB+C+D+").equals("(((A+B)+C)+D)");
assert getPostfixToInfix("ABCDE^*/-").equals("(A-(B/(C*(D^E))))");
assert getPostfixToInfix("AB+CD^/E*FGH+-^").equals("((((A+B)/(C^D))*E)^(F-(G+H)))");
assert getPostfixToInfix("AB+CD^/E*FGH+-^")
.equals("((((A+B)/(C^D))*E)^(F-(G+H)))");
}
}

View File

@ -11,9 +11,10 @@ import java.util.Stack;
public class ReverseStack {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements you wish to insert in the stack");
System.out.println(
"Enter the number of elements you wish to insert in the stack"
);
int n = sc.nextInt();
int i;
Stack<Integer> stack = new Stack<Integer>();
@ -28,7 +29,6 @@ public class ReverseStack {
System.out.print(stack.peek() + ",");
stack.pop();
}
}
private static void reverseStack(Stack<Integer> stack) {
@ -46,11 +46,9 @@ public class ReverseStack {
//Insert the topmost element to the bottom of the stack
insertAtBottom(stack, element);
}
private static void insertAtBottom(Stack<Integer> stack, int element) {
if (stack.isEmpty()) {
//When stack is empty, insert the element so it will be present at the bottom of the stack
stack.push(element);
@ -66,5 +64,4 @@ public class ReverseStack {
stack.push(ele);
}
}

View File

@ -9,7 +9,6 @@ import java.util.NoSuchElementException;
class StackOfLinkedList {
public static void main(String[] args) {
LinkedListStack stack = new LinkedListStack();
stack.push(1);
stack.push(2);
@ -24,7 +23,9 @@ class StackOfLinkedList {
assert stack.pop() == 5;
assert stack.pop() == 4;
System.out.println("Top element of stack currently is: " + stack.peek());
System.out.println(
"Top element of stack currently is: " + stack.peek()
);
}
}
@ -119,7 +120,9 @@ class LinkedListStack {
builder.append(cur.data).append("->");
cur = cur.next;
}
return builder.replace(builder.length() - 2, builder.length(), "").toString();
return builder
.replace(builder.length() - 2, builder.length(), "")
.toString();
}
/**