Skip to content

Instantly share code, notes, and snippets.

@wszdwp
wszdwp / CursorRecyclerAdapter.java
Created July 11, 2018 18:11 — forked from Shywim/CursorRecyclerAdapter.java
A custom Adapter for the new RecyclerView, behaving like the CursorAdapter class from previous ListView and alike. Now with Filters and updated doc.
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Matthieu Harlé
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@wszdwp
wszdwp / caculateDate.swift
Created May 8, 2018 20:20
date calculation in swift
// one month
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let today = dateFormatter.date(from: calenderDate)!
let startOfMonth = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: Calendar.current.startOfDay(for: today)))!
let endOfMonth = Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: startOfMonth)!
let startOfMonString = dateFormatter.string(from: startOfMonth)
let endOfMonString = dateFormatter.string(from: endOfMonth)
NSLog(dateFormatter.string(from: startOfMonth))
NSLog(dateFormatter.string(from: endOfMonth))
public int rob(TreeNode root) {
if (root == null) return 0;
int notRobAmt = rob(root.left, false) + rob(root.right, false);
int robAmt = rob(root.left, true) + rob(root.right, true) + root.val;
return Math.max(notRobAmt, robAmt);
}
private int rob(TreeNode root, boolean robbed) {
if (root == null) return 0;
if (robbed) {
public class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
int[] s = new int[nums.length];
s[0] = nums[0];
s[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
s[i] = Math.max(s[i-2] + nums[i], s[i-1]);
@wszdwp
wszdwp / textToSpeech.swift
Created August 10, 2017 15:34
Text to speech in iOS
func textToSpeech(text: String) {
let synthesizer:AVSpeechSynthesizer = AVSpeechSynthesizer()
let utterance:AVSpeechUtterance = AVSpeechUtterance(string: text)
utterance.rate = 0.1
synthesizer.speakUtterance(utterance)
}
@wszdwp
wszdwp / hideSoftKeyBoard.java
Created July 3, 2017 15:16
Hide soft keyboard in android
private void hideSoftKeyBoard() {
final View myCurrentFocusView = getCurrentFocus();
myCurrentFocusView.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myCurrentFocusView.getWindowToken(), 0);
}
},100);
public boolean wordPattern(String pattern, String str) {
if (pattern == null || str == null) return false;
String[] words = str.split(" ");
if (pattern.length() != words.length) return false;
HashMap<String, Character> sMap = new HashMap<String, Character>();
HashMap<Character, String> cMap = new HashMap<Character, String>();
for (int i = 0; i < words.length; i++) {
@wszdwp
wszdwp / string_util.py
Last active April 12, 2017 13:24
Python gist
# mirror sentence
# eg: This is my test. -> .tset ym si sihT
" ".join(map(lambda x: x[::-1], s.split()))
# reverse string
# eg: test -> tset
"test"[::-1]
@wszdwp
wszdwp / StringReverse.java
Created March 21, 2017 02:38
345. Reverse Vowels of a String
/**
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
@wszdwp
wszdwp / Duplicate.java
Created March 21, 2017 01:56
442. Find All Duplicates in an Array
/**
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
Input:
[4,3,2,7,8,2,3,1]