Skip to content

Instantly share code, notes, and snippets.

@wszdwp
wszdwp / string_leetcode.md
Created April 18, 2021 18:02
string leetcode summary
@wszdwp
wszdwp / DatabaseProvider.cs
Created April 8, 2020 15:09
Exercise 1 for coyote workshop
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 December 11, 2019 17:28
Demo for swipeRefreshLayout

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 November 2, 2019 17:59
prefix sum template
/**
* 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 October 7, 2019 00:04
some leetcode test cases

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 October 11, 2022 20:53
471. Encode String with Shortest Length
  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 September 5, 2019 14:17
Some code tricks
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 August 20, 2019 19:14
Some date time related gists
// 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;
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;