自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(115)
  • 收藏
  • 关注

原创 Leetcode : 375. Guess Number Higher or Lower II

Guess Number Higher or Lower IIURL : https://leetcode.com/problems/guess-number-higher-or-lower-ii/解题思路:递归的思路是,我可以猜1~n中任意一个数x,然后递归的去猜[1,x) 和(x,n]两段,取他们最大的值。动态规划的思路,自底向上,把长度为[1,2,…,n]的子数组算出来,dp[1]...

2019-06-07 08:36:40 168

原创 Leetcode: 992. Subarrays with K Different Integers

URL : https://leetcode.com/problems/subarrays-with-k-different-integers/思路:该题其实代码量不多,但是思路不好想,我是看了discuss才恍然大悟。下面给出代码。数组中最多出现K个不同的数字的组合数为N, 数组中最多出现K-1个不同的数字的组合数为M,则我们想要的结果是出现K个不同的组合数N-M.最多出现K个不同的数字...

2019-05-26 10:25:32 367

原创 Leetcode: Longest Substring Without Repeating Characters

URL : https://leetcode.com/problems/longest-substring-without-repeating-characters/思路:思路就是滑动窗口,当前字符在之前没有出现过,则窗口左端和右端都有效,如果出现重复,则移动窗口的左端直到当前位置的字符在字符串[left, current-1]中没有出现过,在变化的过程中记录最大的长度。下面给出代码。代码:...

2019-05-26 10:11:16 171

原创 Leetcode: 315. Count of Smaller Numbers After Self

Url : https://leetcode.com/problems/count-of-smaller-numbers-after-self/You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i...

2019-03-31 20:00:24 160

原创 Leetcode:297. Serialize and Deserialize Binary Tree

url https://leetcode.com/problems/serialize-and-deserialize-binary-tree/本题使用层次遍历就行了,下面给出代码/** * Definition for a binary tree node. */ static public class TreeNode { int val;...

2019-03-11 23:50:16 107

原创 Leetcode 295 Find Median from Data Stream

题目链接:https://leetcode.com/problems/find-median-from-data-stream/解题思路既然是是找到中位数,我们就使用两个堆,一个是大顶堆,一个小顶堆,保证两个堆满足以下条件:大顶堆的第一个元素需要小于小顶堆的第一个元素;大顶堆的元素数量不能小于小顶堆且最多只能多一个元素;满足以上条件,需要找中位数的时候,判断大顶堆是否比小顶堆元素多,如...

2019-03-02 13:24:19 126

原创 Leetcode: 241. Different Ways to Add Parentheses

题目链接https://leetcode.com/problems/different-ways-to-add-parentheses/描述Given a string of numbers and operators, return all possible results from computing all the different possible ways to group nu...

2019-02-11 20:06:01 154

原创 Golang goroutine pool 的实现

Golang 语言很好的支持高并发场景,goroutine相比java的thread开销更小。但是大量的goroutine会带来内存开销,如果无限的创建goroutine则会出现内存溢出的灾难,所以萌生出了goroutine pool 的想法,仿照java中的ThreadPoolExecutor实现一个简单的Golang 版本的协程池。代码如下:package goroutine_pooli...

2019-02-11 16:50:05 804 1

原创 218. The Skyline Problem

题目链接 : https://leetcode.com/problems/the-skyline-problem/解题思路先定义下:对于三元组(left,right,high)我们可以认为(left,high)为开始的先计为(left,high,true)(right,high) 认为结束的点,计为(right,high,false),把数据整理为如上格式之后,把输入的点处理一下,保证排顺...

2019-01-19 21:51:44 131

原创 Leetcode 289. Game of Life

题目描述Game of LifeMedium647131FavoriteShareAccording to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John ...

2019-01-06 11:52:14 343

原创 Leetcode 284. Peeking Iterator

题目描述Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be ...

2019-01-06 10:18:41 145

原创 Leetcode 287. Find the Duplicate Number

题目描述Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate numbe...

2019-01-06 10:15:59 163 1

原创 Tensorflow:Mac下的环境安装和helloworld

Tensorflow安装官网上建议virtualenv安装我们建议使用 virtualenv 安装。virtualenv 是一个和其它 Python 项目开发隔离的虚拟 Python 环境,在同一台机器上不会干扰也不会被其它程序影响。virtualenv 安装过程中,你不仅仅安装了 TensorFlow 还有它的所有依赖包。(事实上这很简单)要开始使用 TensorFlow,你需要“启动” v...

2019-01-04 17:17:08 260

原创 Leetcode 324 Wiggle Sort II

题目描述Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]…Example 1:Input: nums = [1, 5, 1, 1, 6, 4]Output: One possible answer is [1, 4, 1, 5, 1, 6].E...

2018-12-30 16:58:17 126

原创 Leetcode:239. Sliding Window Maximum

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window m...

2018-12-06 09:19:37 138

原创 Coding interview:数组中未出现的最小正整数

题目描述给定一个无序整型数组array,找出数组中未出现的最小正整数.例如:[-1,2,3,4] 返回1[1,2,3,4] 返回5解题思路对于给定数组长度为N,我们希望数组中的数据为1,2,3,4…,N,这样最小的正整数则为N+1,但是事实情况并非我们所期待的这样,所以我们对数组进行一个划分,索引 i 左边的数组是从1开始逐个递增的,相差为1。索引 j 右边的数据是“无效数据”,“无效...

2018-10-28 14:40:58 279

原创 Flink:word count demo

flink 安装单机安装flink非常简单,在官网下载flink,并执行安装包中bin文件下的start-cluster.sh即可,运行成功之后访问http://localhost:8081,会出现flink 的管理页面Wordcout 代码import org.apache.flink.api.common.functions.FlatMapFunction;import org.ap...

2018-10-21 21:23:58 552

原创 Coding interview:位运算2

找出正数中二进制1出现的次数public int count1(int n) { int res = 0; while (n!=0){ res += n&1; n>>>=1; } return res; }其他数都出现偶数次的数组中,找到唯一个出现...

2018-10-06 16:08:25 177

原创 Coding interview:位运算

位运算总结1.交换两个数2.选择两个数中较大的一个3.位运算进行加减乘除下面给出代码//交换两个数public void swap(int a,int b) { a = a^b; b = a^b; a = a^b; System.out.println("a: " + a + " , b:" + b); }//...

2018-10-05 20:45:38 226

原创 leetcode:best time buy and sell stock系列

best time buy and sell stock系列最近把leetcode上“best time buy and sell stock”系列一共5个问题整理了一下,题目就补贴出来了,在leetcode上面就能找到。best time buy and sell stock 1public int maxProfit(int[] prices) { if (prices=...

2018-10-02 22:42:25 177

原创 Coding interview:添加最少字符使字符串整体为回文字符串

题目描述给定一个字符串str,如果可以在str的任意位置添加字符,请返回添加字符最少的情况下,让str整体都是回文字符串的一种结果。解题思路本题可以使用动态规划的思想,dp[i][j]表示子字符串(i,j)变成回文字符串需要添加的字符数,则有如下公式:1. 如果str[i]==str[j]时,dp[i][j] = dp[i+1][j-1];2. 如果str[I]!=str[j]时...

2018-09-08 23:38:45 534

原创 Code interview 删除字符串中连续出现K个'0'

思路很简单,遍历数组遇到连续出现K个‘0’的情况就将其移除,就是用一个变量pos,其左侧是需要保留的字符(包括pos的位置)。 注意如果连续出现K+m 个0,m>0,这种情况是不需要移除的。下面给出代码:public String removeZero(String str,int k) { int len = str.length(); if(l...

2018-07-19 09:05:19 179

原创 Mac Grpc 环境安装 + Demo

安装和配置环境变量参考: https://blog.csdn.net/liangguangchuan/article/details/53582152这里没有使用brew 进行安装,而是选择在官网下载安装包进行安装,我下载了最新的go1.10.2.darwin-amd64.pkg版本,直接点击安装,安装提示继续就行啦。 安装的路径为: /usr/local/go 之后进行环境变量配...

2018-06-16 12:12:35 5533 1

原创 Mac 安装Brew

安装和配置环境变量参考: https://blog.csdn.net/liangguangchuan/article/details/53582152这里没有使用brew 进行安装,而是选择在官网下载安装包进行安装,我下载了最新的go1.10.2.darwin-amd64.pkg版本,直接点击安装,安装提示继续就行啦。 安装的路径为: /usr/local/go 之后进行环境变量配...

2018-06-16 10:56:56 559

原创 Coding interview: 找到二叉树中符合搜索二叉树条件的最大拓扑结构

题目描述给定一颗二叉树的头节点Head,已知所有节点的值都不一样,返回最大的且符合搜索二叉树的最大拓扑结构的大小。解题思路利用先序遍历,每一次可以限定子树中最大值和最小值,符合条件则计数+1,否则直接返回,无需遍历其子树。package com.codeinterview.tree;/** * 找到二叉树中符合搜索二叉树条件的最大拓扑结构 */public class...

2018-05-27 14:33:16 399 1

原创 Coding Interview:找到最大的搜索二叉子树

题目描述给定一颗二叉树的头节点,找到含有节点最多的搜索二叉子树,并返回这个树的头节点。解题思路后续遍历这个树,当前节点的值如果大于等于左子树中的最大值,并且小于等于右子树的最小值,并且左子树是搜索二叉树,并且右子树是搜索二叉树,则当前节点是搜索二叉树。否则,选择左,右子树中最大数量的即可。代码/** * 找到二叉树中最大的搜索二叉树 */public class Bi...

2018-05-26 23:27:06 462

原创 Coding Interview:打印二叉树的边界节点

问题描述: 给定一颗二叉树的头节点,按照如下规则逆时针打印边界节点。 规则: 1.头节点为边界节点 2.叶节点为边界节点 3.如果节点在其所在的层中是最左或者最右的,也是边界节点。解题思路使用层次遍历,如果是一层开始的节点则直接打印,如果是一层中最后一个节点,则直接暂存起来,如果是叶子节点则直接打印,最后在逆序打印出暂存的节点。static class Node{ ...

2018-05-19 13:15:36 666

原创 Coding Interview:最长子数组问题(2)

1.未排序的数组中累加和为小于或者等于给定值的最长子数组长度2.在二叉树中找到累加和为指定值的最长路径长度问题1令Subsum[j] 等于array{0…j}的累加和,则从0至j这部分中,最长子数组长度应为len = j-k; K为最先出现的SubSum[k]>=SubSum[j]-target 的位置。求最先出现的位置,我们需要辅助数组Fe.令Fe[i] = Max{Fe[i-...

2018-05-19 12:21:09 167

原创 Coding Interview:最长子数组问题(1)

1.未排序正数数组中累加和为给定值的最长子数组长度2.未排序数组中累加和为给定值的最长子数组长度(包含正数和负数)3.给定数组中包含正数、负数、0,求正数与负数相等的最长子数组的长度4.给定数组只包含1、0,求所有子数组中0、1个数相等的最长子数组的长度问题1使用left,right分别表示当前数组的开始和结束位置,累加和为Sum; case: sum>target 则...

2018-05-18 09:11:28 172

原创 Coding interview:字符串反转

首先对整个句子反转,然后对其中每一个单词进行反转。void swap(char[]sentence,int i,int j){ char c = sentence[i]; sentence[i] = sentence[j]; sentence[j] = c; } void reverse(char[]sentence,int begin,

2018-05-05 23:24:50 139

原创 Coding interview :第一个没有重复的字符

思路:两次遍历,第一次遍历记录字符出现的次数,第二次找到第一个没有出现的字符。 代码如下:public char firstNotRepeat(char[]str){ if(str==null || str.length==0){ return ' '; } if(str.length==1){ retur

2018-05-05 23:08:58 106

原创 Coding interview:替换字符串中的空格

这里利用一个技巧,首先统计出字符串中出现的空格数量,计算出替换之后总的数组长度,最后一定注意从后向前遍历进行替换。public String replaceSpace(String str,String replaceStr){ char[]origin = str.toCharArray(); char[] replaceChar = replaceStr.toCha

2018-05-05 21:15:28 126

原创 Coding interview:字符串的全排列

使用递归来解决问题,下面给出代码。void permutation(char[]str,int i){ if(i==str.length){ System.out.println(str); return; } for(int k=i;k<str.length;k++){ swap(s

2018-05-05 19:44:55 160

原创 Coding interview :逆序对

结合归并排序的原理计算逆序对数,下面给出代码public int inversePair(int[]array){ if(array==null || array.length <= 1) return 0; int[]array2 = new int[array.length]; return inversePair(array,0,array.len

2018-05-04 14:53:10 117

原创 Code interview: 连续数组最大和 (二维数组)

思路与这边文章一致: https://blog.csdn.net/zhumingyuan111/article/details/80192729 因为是二维数组可以做一个预处理,可以使得在O(1)的时间复杂度获得到Array[low..hight][col]的值。 下面给出代码public void getMaxSubArray(int[][]array){ int row =

2018-05-04 13:01:52 175

原创 Code interview: 连续数组最大和

void maxSubArray(int[] array){ if(array==null || array.length == 0) return; int max = 0,sum = 0,b = -1,e = -1, bt = -1; int len = array.length; for(int i=0;i<l

2018-05-04 11:32:40 163

原创 Code interview:获取数组中最小的K个数

获取数组中最小的K个数,利用堆完成。public class MinKNum { public void createHeap(int[]heap){ int len = heap.length; int i = len >> 1; while(i >= 1){ sink(heap,i); i--;

2018-05-04 10:56:29 125

原创 Coding interview: 二叉树的遍历(递归和非递归)

二叉树的非递归遍历就是使用数据结构栈来实现的,下面直接给出代码。前序遍历//递归static void recursionPreIterator(Node root){ if(root == null) { return; } System.out.println(root.v); recursionPreIt

2018-03-23 09:14:20 109

原创 Coding interview : 链表排序(选择,插入,快排,归并)

本文章分别给出链表排序的几种方法,对于学习如何操作链表很有帮助。

2018-03-18 21:59:45 165

原创 Coding intervew: 链表反转

单向链表反转static class Node { Node next; int val; Node(int v){ this.val = v; } } public static Node reverse(Node head) { Node pre = null;

2018-03-12 08:15:32 137

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除