Skip to content

Instantly share code, notes, and snippets.

View AbhiAgarwal192's full-sized avatar

Abhi Agarwal AbhiAgarwal192

View GitHub Profile
@AbhiAgarwal192
AbhiAgarwal192 / mlnet-recommender2-load.cs
Last active July 4, 2019 15:53 — forked from mdfarragher/mlnet-recommender2-load.cs
This code is part of a Medium article.
/// <summary>
/// The main program class.
/// </summary>
class Program
{
// filename for dataset
private static string dataPath = Path.Combine(Environment.CurrentDirectory, "amazon0302.txt");
/// <summary>
/// The main entry point of the program.
@AbhiAgarwal192
AbhiAgarwal192 / TwoSum.cs
Created August 20, 2020 11:36
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
public class Solution {
public int[] TwoSum(int[] nums, int target) {
//Get length of the array
int len = nums.Length;
//Create a dictionary of array elements
var map = new Dictionary<int,int>();
for(int i=0;i<len;i++){
if(map.ContainsKey(nums[i])){
@AbhiAgarwal192
AbhiAgarwal192 / ReverseInteger.cs
Created August 20, 2020 14:11
Given a 32-bit signed integer, reverse digits of an integer.
public class Solution {
public int Reverse(int x) {
if(x<=Int32.MinValue){
return 0;
}
int temp = x;
long rev = 0;
temp = Math.Abs(temp);
while(temp>0){
@AbhiAgarwal192
AbhiAgarwal192 / RemoveDuplicates.cs
Created August 21, 2020 03:43
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
public class Solution {
public int RemoveDuplicates(int[] nums) {
int len = nums.Length;
if(len == 0 || len == 1){
return len;
}
int j = 0;
int i=1;
@AbhiAgarwal192
AbhiAgarwal192 / RemoveElement.cs
Created August 21, 2020 04:34
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
public class Solution {
public int RemoveElement(int[] nums, int val) {
int len = nums.Length;
int i = 0;
int j = 0;
while(i<len){
while(i<len && nums[i] == val){
i++;
}
if(i<len){
@AbhiAgarwal192
AbhiAgarwal192 / PalindromeNumber.cs
Created August 21, 2020 04:51
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
public class Solution {
public bool IsPalindrome(int x) {
if(x<0){
return false;
}
int rev = 0;
int temp = x;
while(temp>0){
@AbhiAgarwal192
AbhiAgarwal192 / RomanToInteger.cs
Created August 21, 2020 04:59
Given a roman numeral, convert it to an integer.
public class Solution {
public int RomanToInt(string s) {
int len = s.Length;
int num = 0;
int i=0;
while(i<len){
if(i-1>=0 && s[i] == 'V' && s[i-1] == 'I'){
num = num + 4;
}
@AbhiAgarwal192
AbhiAgarwal192 / LongestCommonPrefix.cs
Created August 21, 2020 05:09
Write a function to find the longest common prefix string amongst an array of strings.
public class TrieNode{
public bool isEndOfWord;
public TrieNode[] children;
public TrieNode(){
isEndOfWord = false;
children = new TrieNode[26];
for(int i=0;i<26;i++){
children[i] = null;
}
}
@AbhiAgarwal192
AbhiAgarwal192 / ValidParentheses.cs
Created August 21, 2020 05:31
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
public class Solution {
public bool IsValid(string s) {
int len = s.Length;
if(len == 0){
return true;
}
var stack = new Stack<char>();
@AbhiAgarwal192
AbhiAgarwal192 / MergeTwoLists.cs
Created August 21, 2020 06:18
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }