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
module Main where | |
-- This is how you write a comment in Haskell | |
import Prelude hiding (length) -- Hide length function to define our own later | |
import Data.List () | |
import Data.Char -- required for toUpper | |
-- Defining your own data types | |
data MyBool = MyTrue |
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
/** | |
* LeetCode 92. Reverse Linked List II (Problem: https://leetcode.com/problems/reverse-linked-list-ii) | |
* Reverse a linked list from position m to n. Do it in-place and in one-pass. | |
* For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. | |
* Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. | |
*/ | |
/** | |
* Definition for singly-linked list. |
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
/** | |
* LeetCode 2. Add Two Numbers (Problem Link: https://leetcode.com/problems/add-two-numbers) | |
* You are given two linked lists representing two non-negative numbers. | |
* The digits are stored in reverse order and each of their nodes contain a single digit. | |
* Add the two numbers and return it as a linked list. | |
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 | |
*/ | |
/** | |
* Definition for singly-linked list. |
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
/** | |
* LeetCode 338. Counting Bits | |
* Counting number of set bits in all numbers from 0 to num (Problem Link: https://leetcode.com/problems/counting-bits) | |
* Given a non negative integer number num. For every number i in the range 0 ≤ i ≤ num, calculate and store the number of 1's | |
* in the binary representation of i and return results as an array. | |
* Example: For num = 5, program should return [0,1,1,2,1,2]. | |
*/ | |
public class CountingBits { | |
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
/** | |
* LeetCode 151. Reverse Words in a String (Problem Link: https://leetcode.com/problems/reverse-words-in-a-string) | |
* Given an input string, reverse the string word by word. | |
* For example, given s = "the sky is blue", return "blue is sky the". | |
* NOTES: | |
* 1. A sequence of non-space characters constitutes a word. | |
* 2. Remove all leading and trailing spaces. | |
* 3. Reduce multiple spaces between words into a single space. | |
* | |
*/ |
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
/** | |
* LeetCode 29. Divide Two Integers (Problem Link: https://leetcode.com/problems/divide-two-integers) | |
* Divide two integers without using multiplication, division and mod operator. | |
* If it is overflow, return MAX_INT. (Integer.MAX_VALUE in Java) | |
*/ | |
public class DivideTwoIntegers { | |
public int divide(int dividend, int divisor) { | |
// Arrr...gh! Divide by zero | |
if(divisor == 0) return Integer.MAX_VALUE; |
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
/** | |
* | |
* Thread-safe singleton with lazy initialization. (Bill Pugh's approach) | |
* | |
* NOTES - | |
* When BillPughSingleton class is loaded, SingletonHelper class is not loaded into memory. Only when someone calls | |
* getInstance() method, does the SingletonHelper class gets loaded and creates a BillPughSingleton instance. | |
* | |
* The advantage comes from the lazy initialization. BillPughSingleton instance, which might be potentially consuming | |
* very high memory, will only be created when getInstance is called the first time. |
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
/** | |
* LeetCode 394. Decode String - (Problem Link - https://leetcode.com/contest/3/problems/decode-string/) | |
* | |
* Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string | |
* inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. | |
* You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. | |
* Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat | |
* numbers, k. For example, there won't be input like 3a or 2[4]. | |
* | |
* Examples: |
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
/** | |
* | |
LeetCode 200. Number of Islands | |
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. | |
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. | |
You may assume all four edges of the grid are all surrounded by water. | |
Example 1: | |
11110 | |
11010 | |
11000 |
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
/** | |
LeetCode 130. Surrounded Regions | |
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. | |
A region is captured by flipping all 'O's into 'X's in that surrounded region. | |
For example, | |
X X X X | |
X O O X | |
X X O X | |
X O X X |
OlderNewer