This file contains hidden or 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 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; | |
} |
This file contains hidden or 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
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; |
This file contains hidden or 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 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; |
This file contains hidden or 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
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 |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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); |
This file contains hidden or 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
// 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--) { |
This file contains hidden or 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
<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; |
This file contains hidden or 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
<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> |
This file contains hidden or 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 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; |
NewerOlder