Skip to content

Instantly share code, notes, and snippets.

@wszdwp
wszdwp / DatabaseProvider.cs
Created Apr 8, 2020
Exercise 1 for coyote workshop
View DatabaseProvider.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Coyote.Tasks;
namespace TinyService
{
using Document = Dictionary<string, string>;
using Collection = Dictionary<string, Dictionary<string, string>>;
@wszdwp
wszdwp / SwipeRefreshLayoutDemo.md
Created Dec 11, 2019
Demo for swipeRefreshLayout
View SwipeRefreshLayoutDemo.md

How to use swipeRefreshLayout

Steps

  1. Add SwipeRefreshLayout as parent view to your listview
  2. Implement SwipeRefreshLayout.OnRefreshListener() by setOnRefreshListener
  3. Dimiss the refresh activity indicator

In layout (Step 1)

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
@wszdwp
wszdwp / ArrayUtil.java
Created Nov 2, 2019
prefix sum template
View ArrayUtil.java
/**
* Prefix sums of nums, preSum[i] = sum of range [0, j)
* sum[i, j) = preSum[j] - preSum[i]
*
* @param nums
* @return preSum array
*/
public static int[] getPrefixSum(int[] nums) {
int[] preSum = new int[nums.length + 1];
for (int i = 1; i <= nums.length; i++) {
@wszdwp
wszdwp / leet_test_cases.md
Created Oct 7, 2019
some leetcode test cases
View leet_test_cases.md

734 sentense similarity

["this","summer","thomas","get","actually","actually","rich","and","possess","the","actually","great","and","fine","vehicle","every","morning","he","drives","one","nice","car","around","one","great","city","to","have","single","super","excellent","super","as","his","brunch","but","he","only","eat","single","few","fine","food","as","some","fruits","he","wants","to","eat","an","unique","and","actually","healthy","life"] ["this","summer","thomas","get","very","very","rich","and","possess","the","very","fine","and","well","car","every","morning","he","drives","a","fine","car","around","unique","great","city","to","take","any","really","wonderful","fruits","as","his","breakfast","but","he","only","drink","an","few","excellent","breakfast","as","a","super","he","wants","to","drink","the","some","and","extremely","healthy","life"] [["good","nice"],["good","excellent"],["good","well"],["good","great"],["fine","nice"],["fine","excellent"],["fine","well"],["fine","great"],["wonderful","nice"]

@wszdwp
wszdwp / LC471EncodeStringWithShortesLength.md
Last active Oct 6, 2019
471. Encode String with Shortest Length
View LC471EncodeStringWithShortesLength.md
  1. Encode String with Shortest Length

Given a non-empty string, encode the string such that its encoded length is the shortest.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times.

Note:

k will be a positive integer and encoded string will not be empty or have extra space. You may assume that the input string contains only lowercase English letters. The string's length is at most 160.

@wszdwp
wszdwp / CodeTricks.md
Created Sep 5, 2019
Some code tricks
View CodeTricks.md
int[] arr = {5, 3, 5, 7, 8, 9, 9, 10};
int[] res = new int[2];   //res[0] sum of even idx elems in arr, res[1] sum of odd idx elems
...
// normal way
if (i % 2 == 0) {
   res[0] += arr[i];
} else {
   res[1] += arr[i];
}
@wszdwp
wszdwp / DateTimeGist.java
Last active Aug 20, 2019
Some date time related gists
View DateTimeGist.java
// Check leap year
public boolean isLeapYear(int year) {
boolean leapYear = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if ( year % 400 == 0) {
leapYear = true;
}
} else {
leapYear = true;
View binary_seach.java
public int binarySearch(int[] arr, int target) {
int l = 0, r = arr.length;
while (l < r) {
int m = l + (r - l) / 2;
if (arr[m] == target) return m;
else if (arr[m] > target) {
r = m;
}
else {
l = m + 1;