This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
int counter=0; | |
public int totalNQueens(int n) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int col[]=new int[n]; | |
placeQueens(col, 0, n); | |
return counter; | |
} | |
void placeQueens(int [] col, int row, int n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public ArrayList<String[]> solveNQueens(int n) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int col[]=new int[n]; | |
ArrayList<String[]> results=new ArrayList<String[]>(); | |
placeQueens(results, col, 0, n); | |
return results; | |
} | |
void placeQueens(ArrayList<String[]> results, int [] col, int row, int n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int maxSubArray(int[] A) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(A.length==0) return 0; | |
int max=A[0]; | |
int sum=A[0]; | |
for(int i=1; i<A.length; i++) | |
{ | |
sum=Math.max(A[i], A[i]+sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public boolean canJump(int[] A) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int length=A.length; | |
if(length==0) return false; | |
int max=0; | |
for(int i=0; i<length; i++) | |
{ | |
if(i<=max) max=Math.max(max, A[i]+i); else break; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(newInterval==null) return intervals; | |
if(intervals.size()==0) {intervals.add(newInterval); return intervals;} | |
Collections.sort(intervals, ORDER_INTERVAL); | |
ArrayList<Interval> results=new ArrayList<Interval>(); | |
for(int i=0; i<intervals.size(); i++) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for an interval. | |
* public class Interval { | |
* int start; | |
* int end; | |
* Interval() { start = 0; end = 0; } | |
* Interval(int s, int e) { start = s; end = e; } | |
* } | |
*/ | |
public class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public ArrayList<Integer> spiralOrder(int[][] matrix) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
ArrayList<Integer> result=new ArrayList<Integer>(); | |
if(matrix.length==0) return result; | |
spiralOrder(result, matrix, 0, matrix.length-1, 0, matrix[0].length-1); | |
return result; | |
} | |
void spiralOrder(ArrayList<Integer> result, int[][] matrix, int upBound, int lowBound, int leftBound, int rightBound) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int lengthOfLastWord(String s) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(s.equals("")) return 0; | |
String [] results=s.split(" "); | |
int length=results.length; | |
if(length==0) return 0; | |
return results[length-1].length(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int[][] generateMatrix(int n) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int[][] matrix=new int [n][n]; | |
spiralOrder(matrix, 0, n-1, 0, n-1, 1); | |
return matrix; | |
} | |
void spiralOrder(int[][] matrix, int upBound, int lowBound, int leftBound, int rightBound, int counter) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |