Skip to content

Instantly share code, notes, and snippets.

@wszdwp
wszdwp / fixTableViewTopMargin.m
Created December 6, 2018 17:14
fix ios 11 table extra top margin in an existed project
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad){
if (@available(iOS 11, *)) {
self.accountTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
else {
self.accountTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
}
}
@wszdwp
wszdwp / treeToGraph
Created November 24, 2018 16:06
binary tree to undirected graph
// binary tree to undirected graph
// treeToGraph(null, root);
public void treeToGraph(TreeNode parent, TreeNode child) {
if (parent != null) {
List<TreeNode> pNeighbors = g.getOrDefault(parent, new ArrayList<TreeNode>());
pNeighbors.add(child);
g.put(parent, pNeighbors);
List<TreeNode> cNeighbors = g.getOrDefault(child, new ArrayList<TreeNode>());
cNeighbors.add(parent);
@wszdwp
wszdwp / leetcode_summary.md
Created November 24, 2018 16:05
leetcode 题目归类总结

Tree

Path

543 Diameter of Binary Tree
687 Longest Univalue Path
863 All Nodes distance K in Binary Tree

Tree -> Undirected Graph BFS Graph find distance k

@wszdwp
wszdwp / Lt547FriendCircles2.java
Created November 18, 2018 19:41
check connected components in dynamic graph
// 2 union find, better for checking connected components in dymanic graph
public int findCircleNum(int[][] M) {
int[] id = new int[M.length];
for (int i = 0; i < id.length; i++) id[i] = i;
int[] count = new int[1];
count[0] = id.length;
for (int i = 0; i < M.length; i++) {
for (int j = i; j < M[0].length; j++) {
if (M[i][j] == 1 && i != j) union(i, j, count, id);
@wszdwp
wszdwp / Lt547FriendCircles.java
Created November 18, 2018 19:40
checking connected components in graph
// Lt547 friend circle
// 1 dfs, better for checking connected components in static graph
public int findCircleNum(int[][] M) {
if (M.length == 0 || M[0].length == 0) return 0;
int m = M.length;
int n = M[0].length;
boolean[] visited = new boolean[m];
int ans = 0;
for (int i = 0; i < m; i++) {
@wszdwp
wszdwp / runtimePermission.java
Created November 1, 2018 18:46
Android 6.0+ dangerous permissions runtime request
// if the device is running Android 6.0 (API level 23)
// and the app's targetSdkVersion is 23 or higher,
//the following system behavior applies when your app requests a dangerous permission:
// 1.
//If the app doesn't currently have any permissions in the permission group,
//the system shows the permission request dialog to the user describing the permission group
//that the app wants access to. The dialog doesn't describe the specific permission within
//that group. For example, if an app requests the READ_CONTACTS permission,
//the system dialog just says the app needs access to the device's contacts.
@wszdwp
wszdwp / OpenJDKUtiImpl.java
Created October 22, 2018 05:21
java open jdk
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
@wszdwp
wszdwp / AndroidGist.java
Created October 10, 2018 15:08
Request permissions at runtime in android
// check permissions
private void requestForGpsPermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_ACCESS_FINE_LOCATION);
}
}
@wszdwp
wszdwp / swift3CodeGist.swift
Last active August 6, 2018 14:21
Swift 3 code gist
// Delay 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
// Your code with delay
}
/*
* Copyright (C) 2014 skyfish.jy@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software