Skip to content

Instantly share code, notes, and snippets.

@hanshsieh
hanshsieh / web_crawler.go
Created January 19, 2019 14:18
Go Tour Exercise
package main
// Exercise: Web Crawler
// https://tour.golang.org/concurrency/10
import (
"fmt"
"sync"
)
type Fetcher interface {
@hanshsieh
hanshsieh / Solution.java
Created July 8, 2018 11:56
Finding longest palindromic string
import java.util.Objects;
/**
* https://leetcode.com/problems/longest-palindromic-substring/description/
* Run time: 10 ms
* Time complexity: O(N)
* Space complexity: O(N)
* Keyword: palindrome, Manacher’s Algorithm
*
* Ref: https://articles.leetcode.com/longest-palindromic-substring-part-ii/
@hanshsieh
hanshsieh / lcs.cpp
Created June 29, 2018 12:40
Longest common subsequence
// O(NM)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 100
char a[MAX],b[MAX],result[MAX];
int dp[MAX+1][MAX+1];
int getLcsLen(char a[], char b[])
{
@hanshsieh
hanshsieh / KMPSearch.java
Last active June 26, 2018 15:42
String searching
/**
* Implementation of Knuth-Morris-Pratt (KMP) Algorithm.
* Time complexity:
* (initialization) O(N)
* (query) O(N + M)
*
* Sample: LeetCode 686
*/
public class KMPSearch {
private final String target;
@hanshsieh
hanshsieh / Heapifier.java
Created June 17, 2018 11:42
Example code for heapify an array of integers
import java.util.Arrays;
import java.util.Random;
class Heapifier {
private int[] nums;
/**
* Rearrange the elements in the given array so that it satisfy min heap property.
* Time comlexity: O(N)
@hanshsieh
hanshsieh / VerifyIdToken.java
Last active June 10, 2020 00:22
Google Sign-in Example
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import java.util.Collections;
/**
* Hello world!
@hanshsieh
hanshsieh / gist:b97dd950ce2b7441187b
Last active August 29, 2015 14:24
Fix the problem that status bar overlaps the view
@implementation MyViewController
- (void)viewWillAppear:(BOOL)animated {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect viewBounds = [self.view bounds];
viewBounds.origin.y = 18;
viewBounds.size.height = viewBounds.size.height - 18;
self.view.frame = viewBounds;
}
// Some other code...
[super viewWillAppear:animated];
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
}