0% found this document useful (0 votes)
22 views4 pages

Important Programs

The important Java programs

Uploaded by

Rajesh Padgilwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Important Programs

The important Java programs

Uploaded by

Rajesh Padgilwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Matrix rotation:-

public class Solution {


public void rotate(int[][] mat) {
int len = mat.length;
int width = mat[0].length;

int[][] arr = new int[width][len];


int[][] arr2 = new int[width][len];

for(int i = 0; i < len; i++){


for(int j = 0; j < width; j++){
arr[j][len - 1 - i] = mat[i][j];
arr2[width - 1 - j][i] = mat[i][j];
}
}
/*
for(int i = 0; i < width; i++){
for(int j = 0; j < len; j++){
System.out.print(arr2[i][j]);
}
System.out.println();
}*/

return;
}
}

Min Sum:-
public class Solution {
int min = Integer.MAX_VALUE;
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<Integer> temp = new ArrayList<>();
back(root, temp, 0);
System.out.println(min);

List<List<Integer>> odd = new ArrayList<>();


return odd;
}

public void back(TreeNode root, List<Integer> temp, int sum){


if(root == null) return;
if(root.left == null && root.right == null){
min = Math.min(sum + root.val, min);
return;
}
else{
temp.add(root.val);
back(root.left, temp, sum + root.val);
back(root.right, temp, sum + root.val);
temp.remove(temp.size() - 1);
}
return;
}
}

Reverse linked list in half:-

public class Solution {


public ListNode reverseList(ListNode head) {
ListNode newHead = null;
ListNode fast = null;
ListNode slow = null;
fast = head;
slow = head;
while(fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}

ListNode temp = slow.next;


while (temp != null) {
ListNode next = temp.next;
temp.next = newHead;
newHead = temp;
temp = next;
}
slow.next = newHead;
return head;
}
}

Valid Parenthesis:-
public class Solution {
public boolean isValid(String s) {
Stack<Character> st = new Stack<>();
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);

if(c == '(')
st.push(')');
else if(c == '[')
st.push(']');
else if(c == '{')
st.push('}');
else if(st.isEmpty() || c != st.pop())
return false;
}

return st.isEmpty();
}
}

You might also like