Skip to content

Instantly share code, notes, and snippets.

public class Contains_Nearby_Dumplicate {
public boolean containsNearbyDuplicate(int[] nums, int k) {
// idea is to maintain a set of size k that obsorbing elements in the array
// from the head. From the k+1 th element, remove the i-k-1 th element that
// was previously added from the set then check if the newly added element is
// contained in the set by calling set.add() who return false if an element of
// the same value already exists.
Set<Integer> window = new HashSet<Integer>();
for(int i=0; i<nums.length; i++) {
if(i > k) window.remove(nums[i-k-1]);
public class MinStack {
private List<Integer> data;
private int top_idx;
/** initialize your data structure here. */
public MinStack() {
data = new ArrayList<Integer>();
top_idx = -1;
}
public class Solution {
public boolean isPowerOfTwo(int n) {
//return solutionOne(n);
//return solutionTwo(n);
return solutionThree(n);
}
private boolean solutionOne(int n) {
// check the result of divition by 2 iteratively
if(n == 0) return false;
public class Solution {
public boolean isPowerOfThree(int n) {
// special case
if(n == 0) return false;
while(n != 1) {
// check if n can be divided by 3, return false if not
if(n%3 != 0) return false;
// take the result as input of next check until it's 1 which means n is power of 3
n = n/3;
}
public class Solution {
// only for ASCII characters which takes one byte in storage
public boolean isAnagram(String s, String t) {
if(s == null && t == null)
return true;
if(s == null && t != null)
return false;
if(s != null && t == null)
return false;
if(s.length() != t.length())
import java.io.IOException;
import java.io.OutputStream;
import javafx.scene.control.TextArea;
public class TextAreaOutputStream extends OutputStream {
private TextArea textArea;
public TextAreaOutputStream(TextArea ta) {
@qingl97
qingl97 / stackoverflow-1.md
Last active August 29, 2015 14:18
Convert string to integer C

Alternative to atoi() there is better way with using strtol():

Defined in header <stdlib.h>

long strtol( const char *str, char **str_end, int base ); (until C99)
long strtol( const char *restrict str, char **restrict str_end, int base ); (since C99)
long long strtoll( const char *restrict str, char **restrict str_end, int base ); (since C99)

The call atoi(str) shall be equivalent to: (int) strtol(str, (char **)NULL, 10) except that the handling of errors may differ. For atoi(), if the value cannot be represented(not in the format below), the behavior is undefined.

@qingl97
qingl97 / gist:44419227ae5eac116ace
Last active June 14, 2024 23:17
Reset local repository to be just like remote repository HEAD

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work
@qingl97
qingl97 / gist:605b26bf7ee7716560bb
Created March 26, 2015 09:12
Alfresco content model
https://wiki.alfresco.com/wiki/Data_Dictionary_Guide
@qingl97
qingl97 / gist:d40150f7fa320051a785
Last active July 6, 2016 11:47
Alfresco Share doesn't show Admin Console page when running from Alfresco SDK 2.0