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 int MyAtoi(string s) { | |
sbyte sign = 1; | |
int runningNumber = 0; | |
bool isReadNumber = false; | |
bool isOverflow = false; | |
for(int i = 0; i < s.Length; i++) | |
{ | |
if(Char.IsWhiteSpace(s[i]) && !isReadNumber) continue; |
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
export function cycleInGraph(edges: number[][]) { | |
// 1. loop through all vertices of the graph | |
for (let v = 0; v < edges.length; v++) { | |
if (hasCycle(edges, v, new Set<Number>())) | |
return true; | |
} | |
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
// https://leetcode.com/problems/count-square-submatrices-with-all-ones/ | |
function countSquares(matrix: number[][]): number { | |
let count = 0; | |
for(let row = 0; row < matrix.length; row++) { | |
for(let column = 0; column < matrix[row].length; column++) { | |
let squareLength = 0; | |
while(squareLength < matrix.length) { | |
if(isOneMatrix(matrix, row, column, squareLength)) { | |
count++; | |
} |
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
// https://leetcode.com/problems/count-sorted-vowel-strings/ | |
function countVowelStrings(n: number): number { | |
return traverse(n, 0, 0);; | |
}; | |
const vowels: string[] = [ 'a', 'e', 'i', 'o', 'u' ]; | |
function traverse(n: number, runningCombination: number, runningIdx: number): number { | |
if(runningCombination == n) { | |
return 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
/** | |
* https://leetcode.com/problems/maximum-subarray/ | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var maxSubArray = function(nums) { | |
let maxSum = Number.MIN_SAFE_INTEGER; | |
let runningSum = 0; | |
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
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/ | |
var maxProfit = function(prices) { | |
let buyIndex = 0; | |
let lastProfit = 0; | |
for(let i = 1; i < prices.length; i++) { | |
let currentProfit = prices[i] - prices[buyIndex]; | |
// stock down |
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
// https://leetcode.com/problems/pascals-triangle-ii/ | |
public class Solution { | |
public IList<int> GetRow(int rowIndex) { | |
return PascalTriangle2(rowIndex); | |
} | |
public List<int> PascalTriangle2(int row) | |
{ | |
if(row == 0) | |
return new List<int>() { 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
// https://leetcode.com/problems/pascals-triangle/submissions/ | |
public class Solution { | |
public IList<IList<int>> Generate(int numRows) { | |
return PascalTriangle(numRows - 1,numRows - 1).Cast<IList<int>>().ToList(); | |
} | |
public List<List<int>> PascalTriangle(int row, int columns) | |
{ | |
if(row == 0) |
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
// https://leetcode.com/problems/divisor-game/ | |
public class Solution { | |
public enum Player { Alice = 1, Bob= 2 } | |
private const int X = 1; | |
public bool DivisorGame(int n) { | |
return WhoLooses(Player.Alice, n) != Player.Alice; | |
} | |
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 int[] CountBits(int n) { | |
if(n == 0) return new int[]{ 0 }; | |
int[] output = new int[n + 1]; | |
output[0] = 0; | |
output[1] = 1; | |
for(int i = 2; i < output.Length; i++) | |
{ |