This file contains 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 static partial class DynamicProgramming | |
{ | |
public static int KnapSack_01(Tuple<int, int>[] valueWeightTuple, int maxWeight) | |
{ | |
int[,] dp = new int[valueWeightTuple.Length + 1, maxWeight + 1]; | |
for (int i = 0; i <= maxWeight; ++i) dp[0, i] = 0; | |
for (int i = 1; i <= valueWeightTuple.Length; i++) | |
{ |
This file contains 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 RotateArray { | |
public void rotate(int[] nums, int k) { | |
if(k > nums.length) k = k % nums.length; | |
reverse(nums, 0, nums.length); | |
reverse(nums, 0, k); | |
reverse(nums, k, nums.length); | |
} |
This file contains 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
//From OJ handbook | |
public class StringToint{ | |
public int atio(String str){ | |
int i = 0; | |
int n = str.length(); | |
while(i < n && Character.isWhiteSpace(str.charAt(i))) i++; | |
int sign = 1; |
This file contains 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 PrintSpiralMatrix { | |
public List<Integer> spiralOrder(int[][] matrix) { | |
List<Integer> elements = new ArrayList<>(); | |
if (matrix.length == 0) return elements; | |
int m = matrix.length; //rows | |
int n = matrix[0].length; //col |
This file contains 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 MergeIntervals { | |
public List<Interval> merge(List<Interval> intervals) { | |
List<Interval> mergedIntervals = new ArrayList<>(); | |
if(intervals == null || intervals.size() == 0) | |
return mergedIntervals; | |
int n = intervals.size(); |
This file contains 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 Pow { | |
public double myPow(double x, int n) { | |
boolean isNegative = n < 0; | |
if(isNegative) | |
n = n * -1; | |
double result = innerPow(x, n); |
This file contains 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 searchMatrix(int[][] matrix, int target) { | |
int rows = matrix.length; | |
if(rows <= 0) return false; | |
int cols = matrix[0].length; | |
if(cols <= 0) return false; |
This file contains 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 KthLargest{ | |
public int findKthLargestPQ(int[] nums, int k) { | |
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); | |
int n = nums.length; | |
for(int i = 0; i < n; ++i){ | |
This file contains 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 Search2DMatrix{ | |
public boolean searchMatrix(int[][] matrix, int target) { | |
int rows = matrix.length; | |
if(rows <= 0) return false; | |
int cols = matrix[0].length; | |
This file contains 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 SymmetricTree{ | |
public boolean isSymmetric(TreeNode root) { | |
return isSymmetric(root, root); | |
} | |
private boolean isSymmetric(TreeNode p, TreeNode q){ | |
if(p == null && q == null) return true; |
NewerOlder