Skip to content

Instantly share code, notes, and snippets.

@kulkarnism
kulkarnism / SortColors
Last active August 29, 2015 14:21
Sort Colors such that all 0s 1s and 2s are together
void sortColors(vector<int>& nums) {
int zero = 0,one = 0,two = 0;
for(int j=0;j<nums.size();j++) {
if(nums[j]==0)zero++;
else if(nums[j] == 1)one++;
else two++;
}
int i,j,k;
@kulkarnism
kulkarnism / SlidingRDD.java
Created May 9, 2015 20:07
Apache Spark + JAVA + Sliding RDD
package edu.sjsu.cs.sparkdemo;
import java.util.Arrays;
import java.util.regex.Pattern;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
@kulkarnism
kulkarnism / LonComPref
Created March 31, 2015 21:54
Find Longest Common Prefix from Array of Strings
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.empty()) return "";
string lpref = strs[0];
for(int i = 1; i<strs.size();i++)
{
@kulkarnism
kulkarnism / RemDupLL
Created March 31, 2015 21:13
Remove Duplicates From Sorted Linked List
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@kulkarnism
kulkarnism / gist:1fe3180f6ae5513e1bc5
Created March 9, 2015 01:09
LeetCode : Search for a range
public class Solution {
public int[] searchRange(int[] A, int target) {
int left = findE(A, 0, A.length-1, target);
int right = findR(A, 0, A.length-1, target);
return new int[]{left,right};
}
public int findE(int[] arr, int low, int high, int elem)
{
if(high<low)return -1;
if(arr[low]==elem)return low;