Skip to content

Instantly share code, notes, and snippets.

View hnaoto's full-sized avatar
💭
"What I cannot create, I do not understand." -- Richard Feynman

Trinity Xia hnaoto

💭
"What I cannot create, I do not understand." -- Richard Feynman
View GitHub Profile
class Solution {
public int minPathSum(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
//dp[i][j] stores the value of minimum path sum at the [i][j]
int[][] dp = new int[row][col];
//initilize the value in dp[][]
dp[0][0] = grid[0][0];

interface vs abstract class

  • An interface is a description of the behaviour an implementing class will have. The implementing class ensures, that it will have these methods that can be used on it. It is basically a contract or a promise the class has to make.

  • An abstract class is a basis for different subclasses that share behaviour which does not need to be repeatedly created. Subclasses must complete the behaviour and have the option to override predefined behaviour (as long as it is not defined as final or private).

Interface:
import scala.collection.mutable.ArrayBuffer
import java.util.concurrent._
class Producer(queue :BlockingQueue[String]) extends Runnable {
val str = "item"
def run(): Unit = {
while(true){
try {
//For Java, the compiler generates a .java file with a class for each message type,
//as well as a special Builder classes for creating message class instances.
syntax = "proto2";
//package declarations, helps to prevent naming conflicts between diffrent projects
package tutorial;
//in what java package name your generated class should live
@hnaoto
hnaoto / context_processors.py
Last active October 9, 2021 02:57
Write your own context processors ("global variable" in Django templates)
#Reference: https://docs.djangoproject.com/en/1.9/ref/templates/api/#writing-your-own-context-processors
#This file is in yourapp/ directory
+from .models import Foo
+
+def foo(request):
# you might need this line for unit tests
if request.user.is_authenticated and request.user.is_active:
+ count = len(Foo.objects.filter(user = request.user))
+ return {"foo_count" :count}
@hnaoto
hnaoto / BooksActivity.java
Last active November 2, 2021 21:52
Make HTTP request and parse JSON data from Android by using HttpURLConnection
//The original sample is from here: http://syntx.io/how-to-send-an-http-request-from-android-using-httpurlconnection/
//Deprecated funcitons and non-working code are modeifed.
//For more reference, please visit: http://developer.android.com/reference/java/net/HttpURLConnection.html
//I am using Google Books APIs for this sample.
//The returned book objects might have different format according to the language/country. (The second book obejct in data.json is the Japanese version)
//execute the AsyncTask some place in the UI thread
//new CatalogClient().execute("https://www.googleapis.com/books/v1/mylibrary/bookshelves/3/volumes?country=US");