Category Archives: algorithm

First Missing Number

leetcode 41. Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. Solution. I learned the solution from here. The idea is try as much as possible to rearrange array like [1, 2,… Read More »

Sudoku

I came across the sudoku problem in leetcode 37. Here I wrote a page javascript version to solve this problem. Check my code on github.

Bulls and Cows

leetcode 299. You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both… Read More »

Metric for bloom filter

Assume totally has 1M record. We want test is 1% wrong rate. 7 hash functions. In this way, it totally need around 1.14MB bits for the bloom filter. Bloom filter block those which doesn’t exist. For those bloom filter think the value exist, there is 1% chance wrong. Refer bloom filter calculator. http://hur.st/bloomfilter?n=1000000&p=0.01

strStr

leetcode 28. Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Solution. This problem is asking to implement source.indexOf(target) method. A brilliant solution is from here. public int strStr(String source, String target) { for (int i = 0; true; i++) { for… Read More »

Isomorphic Strings

leetcode 205. Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but… Read More »

Reverse Nodes in k-Group

leetcode 25. Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes… Read More »

4Sum

leetcode 18. Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. For example, given… Read More »

First Unique Character in a String

leetcode 387 Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. Examples: s = “leetcode” return 0. s = “loveleetcode”, return 2. Solution. From g4g. During the first time scan, 1. Count the frequency of each char. 2. Store the position of each char where… Read More »

Shuffling

Assuming we have an integer array, we want to shuffle(randomly reset elements in array). One approach is like this: iterate each element from [0, …, n – 1]. For each element i, swap i with a random position within [0, …, i]. public void shuffle(int[] nums) { Random random = new Random(); for (int i… Read More »