Skip to content

Instantly share code, notes, and snippets.

@edev90
edev90 / binSqrt.java
Last active September 3, 2024 20:32
Calculate square root in Java using binary search
public static double bin_sqrt(double num) {
double left = 0.0D, right = num;
if (num < 1.0D) {
if(num == 0) {
return 0;
}
else if (num < 0) {
// Handle these cases Like Math.sqrt() does it
return Double.NaN;
}
@edev90
edev90 / addBinary.js
Created April 1, 2024 19:39
Adds two binary strings in JavaScript and returns sum string
var addBinary = function(a, b) {
let aLen = a.length, bLen = b.length;
let maxLen = aLen;
if(bLen > aLen) {
maxLen = bLen;
}
let carryBit = 0;
let sumStr = "";
for(let i = 1; i <= maxLen || carryBit > 0; i++) {
let bitA = 0, bitB = 0;
@edev90
edev90 / addBinary.java
Created April 1, 2024 19:24
Adds two binary strings in Java and returns sum string
public String addBinary(String a, String b) {
int aLen = a.length(), bLen = b.length();
int maxLen = aLen;
if(bLen > aLen) {
maxLen = bLen;
}
int carryBit = 0;
String sumStr = "";
for(int i = 1; i <= maxLen || carryBit > 0; i++) {
int bitA = 0, bitB = 0;
@edev90
edev90 / addBinary.go
Last active April 1, 2024 19:25
Adds two binary strings in Go and returns sum string
func addBinary(a string, b string) string {
aLen, bLen := len(a), len(b)
maxLen := aLen
if bLen > aLen {
maxLen = bLen
}
carryBit := 0
sumStr := ""
for i := 1; i <= maxLen || carryBit > 0; i++ {
bitA, bitB := 0, 0
@edev90
edev90 / twoSumSolutionWithMap.go
Created November 9, 2023 22:01
TwoSum O(n) fast solution using a map - Go version
// TwoSum O(n) fast solution using a map - Go version
func twoSum(nums []int, target int) []int {
numsSeen := make(map[int]int)
for index := 0; index < len(nums); index++ {
otherNum := target - nums[index]
if indexOfOtherNum, isValidKey := numsSeen[otherNum]; isValidKey {
return []int{indexOfOtherNum, index}
}
numsSeen[nums[index]] = index
@edev90
edev90 / twoSumSolutionWithHashMap.java
Last active November 9, 2023 22:02
TwoSum O(n) fast solution using HashMap - Java version
// TwoSum O(n) fast solution using HashMap - Java version
public int[] twoSum_Fast(int[] nums, int target) {
Map<Integer, Integer> numsSeen = new HashMap();
for(int index = 0; index < nums.length; index++) {
int otherNum = target - nums[index];
if(numsSeen.containsKey(otherNum)) {
return new int[]{numsSeen.get(otherNum), index};
}
numsSeen.put(nums[index], index);
@edev90
edev90 / rotateSquareMatrix90degrees.java
Created October 19, 2023 16:14
Rotate NxN Square Matrix by 90 degrees
// Java code to rotate a square matrix by 90 degrees in place using diagonal swap and mirror image flip approach
// This example takes an input of a String[][] matrix - but can easily be converted to accept 2D arrays of other types
public static void rotateMatrix(String[][] matrix, boolean isClockwiseRotation) {
int mSize = matrix.length;
int mSizeMinusOne = mSize-1;
int mSizeMinusTwo = mSize-2;
// diag flip left triangle
for(int i = mSizeMinusOne; i >= 0; i--) {
@edev90
edev90 / JSColorWheel.html
Created July 30, 2021 13:22
JSColorWheel - 130 colors
<html>
<head>
<script type="text/javascript">
function genRandom(numIterations) {
c = document.getElementById("wheel").getContext("2d");
c.fillRect(0,0,500,500);
c.translate(250,250);
a = [1];
b = [];
end = 0;
@edev90
edev90 / lightspeed260pic.html
Last active July 30, 2021 03:21
Lightspeed 260 codegolf art (264 chars of JS + 70 of HTML)
<body onload="j=6240;g=document.getElementById('w').getContext('2d');g.translate(c=255,c);a=[1],b=[];e=0;while(j--){g.rotate(.001);for(i=-2;i<=e;i++)b.push(a[i]^(a[i+1]|a[i+2]));if(e%48<1)g.fillStyle='#'+c.toString(16).slice(-6);c=c*2|a[e/2];a=b;b=[];e+=2;g.fillRect(0,0,400,1)}"><canvas id="w"width="510"height="510"></canvas></body>
@edev90
edev90 / isPermOfPalindrome.java
Last active February 17, 2021 23:31
Checks if string is permutation of palindrome - java
public static boolean isPermOfPalindrome(String str) {
if(str == null || str.length() <= 1) return false;
HashMap<Character, Integer> counts = new HashMap();
int numOdd = 0;
for(Character ch : str.toCharArray()) {
int chCount = counts.containsKey(ch) ? counts.get(ch)+1 : 1;
counts.put(ch, chCount);
numOdd += counts.get(ch) % 2 != 0 ? 1 : -1;
}
return numOdd <= 1;