- 博客(56)
- 收藏
- 关注
原创 55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if yo...
2019-01-31 22:55:34 177
原创 402. Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.Note:The length of num is less than 10002 and will be ≥ k....
2019-01-31 21:20:58 222
原创 Java SE Eclipse中引入第三方jar
本文介绍Eclipse中引入protobuf-java-2.4.1.jar作为Reference Libraries。1.将jar文件直接拷贝到项目中 此时虽然已经能够看到protobuf-java-2.4.1.jar,但是并不能使用它,还需要构建Java Build Path。2.构建Build Path在项目名称上右键选择Build Path -- 选择Configu...
2019-01-30 20:20:21 440
原创 protobuf java代码生成及实例
1.下载工具 protoc-2.4.1-win32.zip protobuf-java-2.4.1.jar (注意两者的版本要相同,下载地址https://github.com/protocolbuffers/protobuf)2.proto文件的内容syntax = "proto2";option java_package = "com.netty.protobu...
2019-01-30 19:32:21 4270
原创 序列化协议Protobuf
1.序列化和反序列化 序列化: 将对象序列化为二进制数据(字节数组),一般也将序列化称为编码(Enccode),主要用于网络传输,数据持久化。 反序列化: 将从网络上,磁盘等读取的字节数组还原成原始对象,一般也将反序列化称为解码(Decode),主要用于网络传输对象的解码,以便完成远程调用。2.选择序列化协议时,需要考虑什么?序列化之后的码流大小(占用网络带宽) 序列...
2019-01-30 15:25:50 863
原创 376. Wiggle Subsequence
A sequence of numbers is called awiggle sequenceif the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either pos...
2019-01-29 22:13:06 252
原创 455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a coo...
2019-01-29 21:02:20 271
原创 76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"Note:I...
2019-01-27 22:27:51 159
原创 187. Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Write...
2019-01-27 17:57:37 329
原创 c++多线程(七) - 使用嵌套类释放单例对象
本文介绍了一种使用嵌套类释放单例对象的方法。#include<iostream>#include<thread>#include<mutex>#include<algorithm>using namespace std;mutex resource_mutex;class MyCAS{public: static M...
2019-01-27 17:29:43 617
原创 c++多线程(六) - 单例模式
单例模式是设计模式的一种。程序中有一些特殊的类,在程序运行期间只能有一个对象。1、单线程 该类的构造函数为私有,且包含一个静态的成员变量m_instance和成员函数GetInstance()。使用时通过GetInstance()返回该类的指针。#include<iostream>#include<thread>#include<mu...
2019-01-27 17:12:18 525
原创 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:...
2019-01-27 11:19:26 236
原创 49. Group Anagrams
Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]Note:All inputs will...
2019-01-26 10:25:03 424
原创 c++多线程(五)- unique_lock
unique_lock可以提供自动加锁、解锁功能,比lock_guard更加灵活,但比较耗时。unique_lock的常规用法与lock_guard相似,代码如下:bool outMsgProc(int &num){ unique_lock<mutex> guard(myMutex1); if (!myList.empty()) { num = myLis...
2019-01-26 09:43:25 859
原创 c++多线程(四)- try_to_lock
本文主要介绍c++多线程中try_to_lock的用法。首先,用一个实例说明,使用try_to_lock的必要性。 假设线程1 执行下例代码时,先锁定互斥量myMutex1,然后暂停了2s,才继续执行程序。线程1执行的代码如下:bool outMsgProc(int &num){ unique_lock<mutex> guard(myMutex1);...
2019-01-25 22:39:08 4966
原创 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]Example 2:Input: nums = [1], k = 1Output: [1]Note:You...
2019-01-25 17:00:31 227
原创 C++多线程(三) - 传局部变量给Thread
最近发现了一个有趣的现象。当传递局部变量给Thread时,即使线程的入口函数的参数为引用类型,Thread依然会拷贝对象。(这个我们的直觉不同,因为一般来讲,函数参数为引用时,它和实参地址相同)。 下例演示了这种现象。void myprint(const int &i){ cout << i << endl;}int ma...
2019-01-24 22:02:36 1168 1
原创 explicit 避免对象的隐式转换
最近项目中出现了隐式类型转换引起的BUG,使用关键字explicit解决了问题。本文使用简化后的例子说明该问题。 首先,定义一个分数类Fraction,它包含构造函数,将其转化为double类型的成员函数,并且重载了运算符+。 代码如下(该代码会有编译错误,先思考一下错在哪里)。#include<iostream>using namespace st...
2019-01-24 21:33:49 593
原创 C++多线程(二)- 死锁的模拟
本文用简单的程序模拟了死锁的形成。死锁是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象。#include<iostream>#include<thread>#include<mutex>#include<list>using namespace std;//保证两个互斥量,加锁的顺序相...
2019-01-24 14:19:43 668
原创 c++多线程(一)- 互斥量(mutex)
1.lock() / unlock() lock()锁定需要保护的代码区域,使之某一时刻只有一个线程访问。unlock()解除对代码区域的锁定,使其它线程可以访问。 lock()和unlock()必须成对使用。锁定后不解锁,或者重复解锁都会出问题。#include "stdafx.h"#include<iostream>#include<thread&g...
2019-01-23 18:38:30 940
原创 PostgreSQL中NpgsqlParameter的用法
本文主要介绍PostgreSQL数据库中NpgsqlParameter的用法。(MySql中MySqlParameter的用法与之类似) 首先,为什么要用NpgsqlParameter?那就要先分析直接执行SQL语句的风险了。 执行一个查询语句,目的是在表ModelInfo中查询ModelName等于某一值的行数目,该值由用户输入。代码如下:static void Main(...
2019-01-23 13:50:27 4182
原创 测试PostgreSQL的连接池
在ado.net中,每个应用程序都维护一个连接池。每次用户连接数据库时,会先尝试从连接池中获取连接,如果获取成功则直接使用,如果失败则新建连接。线程池减少了连接数据库的时间。注:函数close()并没有真正关闭连接,而是将对象放入连接池中。 下面使用简单的程序,测试线程池的作用。在两百次循环中,执行数据库的打开与关闭,在启用和禁用连接池的情况下,分别统计其执行时间。 首先,...
2019-01-23 09:45:43 6805 1
原创 876. Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list.If there are two middle nodes, return the second middle node.Example 1:Input: [1,2,3,4,5]Output: ...
2019-01-18 19:02:20 224
原创 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example 1:Input: 2Out...
2019-01-17 22:37:58 219
原创 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note:The given integer is guaranteed to fit within the range of a 3...
2019-01-17 21:53:20 128
原创 559. Maximum Depth of N-ary Tree
Given a n-ary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.For example, given a 3-ary tree: ...
2019-01-17 20:57:12 278
原创 238. Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Example:Input: [1,2,3,4]Output: ...
2019-01-17 12:11:10 266
原创 206. Reverse Linked List
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursi...
2019-01-17 10:16:01 123
原创 371. Sum of Two Integers
Calculate the sum of two integersaandb, but you arenot allowedto use the operator+and-.Example 1:Input: a = 1, b = 2Output: 3Example 2:Input: a = -2, b = 3Output: 1 异或运算(^)是...
2019-01-16 23:04:49 360
原创 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.Note:A leaf is a node with no children.Example:Given the below binary tree andsum = 22,...
2019-01-16 18:00:52 195
原创 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees of every node never diffe...
2019-01-16 16:41:23 232
原创 951. Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.A binary tree Xisflip equivalentto a binary tree Y if and only if we ca...
2019-01-16 11:52:15 338
原创 590. N-ary Tree Postorder Traversal
Given an n-ary tree, return thepostordertraversal of its nodes' values.For example, given a3-arytree:Return its postorder traversal as:[5,6,3,2,4,1].Note:Recursive solution is trivi...
2019-01-16 08:55:35 309
原创 589. N-ary Tree Preorder Traversal
Given an n-ary tree, return thepreordertraversal of its nodes' values.For example, given a3-arytree:Return its preorder traversal as:[1,3,5,6,2,4].Note:Recursive solution is trivial,...
2019-01-15 16:42:16 247
原创 872. Leaf-Similar Trees
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence. For example, in the given tree a...
2019-01-14 18:44:51 232
原创 617. Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree...
2019-01-14 17:33:13 132
原创 Nunit常用类(三) - CollectionAssert
CollectionAssert.AllItemsAreInstancesOfType判断集合中的各项是否是某类型的实例。List<int> nums = new List<int>() { 1, 3 };CollectionAssert.AllItemsAreInstancesOfType(nums, typeof(int)); //trueDic...
2019-01-14 16:25:50 614
原创 Nunit常用类(二) - StringAssert
StringAssert.Contains判断字符串中是否包含另一个字符串。string str = "April";StringAssert.Contains("pr", str); //trueStringAssert.StartsWith判断字符串是否以另一个字符串开头。string str = "April";StringAssert.StartsWith...
2019-01-14 15:36:01 525
原创 Nunit常用类(一) - Assert
Assert.AreEqual 测试两个参数是否相等。int num = 3;Assert.AreEqual(num, 3); //trueAssert.AreSame 测试两个参数所引用的对象是否一致。int a = 3, b = 3;Assert.AreSame(a, b); //falseAssert.Contains ...
2019-01-14 14:51:18 1029
原创 实现3D引擎(五)- 绘制五边形
本文绘制一个颜色随时间变化的五边形。 在片元着色器中设置uniform变量。uniform变量可以接收CPU中发送到着色器的数据,用以设置图形的颜色。片元着色器的定义如下:#version 330 coreuniform vec4 vertexColor; out vec4 color;void main(){ color = vertexColor;} 设置随着...
2019-01-12 18:37:54 981
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人