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 ZigZag{ | |
public static void main(String []args){ | |
String str = "thisisazigzag"; | |
System.out.println(str); | |
printZigZag(str, 4); | |
} | |
public static void printZigZag(String input, int levels) { |
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
class Solution { | |
public List<List<Integer>> permute(int[] nums) { | |
return permute(nums, 0, nums.length - 1); | |
} | |
private List<List<Integer>> permute(int[] nums, int low, int high) { | |
List<List<Integer>> result = new ArrayList<List<Integer>>(); | |
if (low == high) { | |
ArrayList<Integer> temp = new ArrayList<Integer>(); | |
temp.add(nums[low]); |
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
class Solution { | |
public int maxAreaOfIsland(int[][] matrix) { | |
int rows = matrix.length; | |
int cols = matrix[0].length; | |
// return if empty matrix | |
if (cols == 0) return 0; | |
int[][] visited = new int[rows][cols]; | |
int maxArea = 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
public int nfibonacci(int n) { | |
if (n == 1 || n == 2) return 1; | |
if (n < 1) throw new RuntimeException("Invalid parameter. n must be greater than 0"); | |
int i = 1; | |
int j = 1; | |
int result = 0; | |
for(int k = 3; k <= n; k++) { | |
result = i + j; | |
j = i; | |
i = result; |
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
class Solution { | |
public int[][] reshapeMatrix(int[][] matrix, int r, int c) { | |
int rows = matrix.length; | |
int cols = matrix[0].length; | |
if ((rows * cols) != (r * c)) throw new RuntimeException("Invalid operation"); | |
int[][] resultMatrix = new int[r][c]; | |
int resultRow = 0; | |
int resultCol = 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
class Solution { | |
public void merge(int[] nums1, int m, int[] nums2, int n) { | |
int i = m-1; | |
int j = n-1; | |
int k = nums1.length - 1; | |
while(i >= 0 && j >= 0) { | |
if (nums1[i] > nums2[j]) { | |
nums1[k] = nums1[i]; | |
i -= 1; | |
k -= 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/remove-duplicates-from-sorted-array/description/ | |
public void removeDuplicates(int[] arr) { | |
if (arr.length <= 1) return arr.length; | |
int p1 = 1; | |
for(int i = 1; i < arr.length; i++) { | |
if (arr[i] != arr[i-1]) { | |
arr[p1] = arr[i]; | |
p1 += 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
# @param {Integer[][]} board | |
# @return {Void} Do not return anything, modify board in-place instead. | |
def game_of_life(board) | |
m = board.size | |
n = board[0].size | |
meta_data = {} | |
(0...m).each do |row| | |
(0...n).each do |col| | |
living_neighbours = live_neighbours(board, row, col) | |
# meta-data format -> [row:col] => [old_value, num_of_live_neighbours] |
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
def longest_nonrepeating_substring(str) | |
max_len = 0 | |
look_up = {} | |
len = 0 | |
last_repeating_index = 0 # indicates beginning of unique string | |
(0...str.size).each do |index| | |
if (p = look_up[str[index]]) # repeat char found at index | |
if p > last_repeating_index | |
len = index - p | |
last_repeating_index = p |
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
# Given an input array and target sum, find all possible ways the elements of the array can be added upto get target | |
# sub_set_sum([10, 7, 5, 18, 12, 20, 15], 35) => [ | |
# [10, 7, 18], | |
# [10, 5, 20] | |
# [5, 18, 12], | |
# [20, 15] | |
# ] | |
require 'set' | |
def subset_sum(arr, sum, curr_arr = [], result = Set.new) | |
return if arr.nil? |
NewerOlder