single

Single Number leetcode java

五迷三道 提交于 2020-03-31 07:57:43
题目: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题解: 这道题运用位运算的异或。异或是相同为0,不同为1。所以对所有数进行异或,得出的那个数就是single number。初始时先让一个数与0异或,然后再对剩下读数挨个进行异或。 这里运用到异或的性质: 对于任何数x,都有x^x=0,x^0=x 代码如下: 1 public int singleNumber( int [] A) { 2 int result = 0; 3 for ( int i = 0; i<A.length;i++){ 4 result = result^A[i]; 5 } 6 return result; 7 } 同时异或还有性质: 交换律 A XOR B = B XOR A 结合律 A XOR B XOR C = A XOR (B XOR C) = (A XOR B) XOR C 自反性 A XOR B XOR B = A XOR 0 = A

类的单例代码实现

♀尐吖头ヾ 提交于 2020-02-01 04:28:09
single.h: #ifndef SINGLE_H #define SINGLE_H class single { private: static single *p; protected: single(); public: static single *makesignle(); static void releasesingle(); }; #endif // SINGLE_H single.cpp: #include "single.h" #include <iostream> single *single::p=NULL; single::single() { } single *single::makesignle() { if(p==NULL) p=new single;//如果p为空,就实例化d对象 return p; } void single::releasesingle() { delete p; } main.cpp: #include <iostream> #include "single.h" using namespace std; //实例化单例出来 int main() { single *p=single::makesignle(); single *p1=single::makesignle(); single::releasesingle(

Java-----关于单例设计模式

末鹿安然 提交于 2020-01-30 18:50:23
1. 单例模式DCL写法 单例设计模式中,有一种双重检查锁的写法, 也就是所谓的懒汉式 class Single{   private static Single sSingle;   private Single() {}   public static Single getInstance() {     if(sSingle == null) {       synchronized(Single.class) {         if(sSingle == null) {           sSingle = new Single();         }       }     }     return sSingle;   } } 2.DCL写法的问题 这种写法,如果使用sonar代码检测工具,会有警告提示,具体描述如下 Double-checked locking is the practice of checking a lazy-initialized object's state both before and after a synchronized block is entered to determine whether or not to initialize the object. It does not work reliably in a

单例模式

女生的网名这么多〃 提交于 2020-01-10 17:43:29
恶汉式 public class Monitor1 { private Monitor1 (){} //创建Monitor1的无参构造器 private static Monitor1 monitor=new Monitor1(); //先创建Monitor1对象 //待有需求的时候再调用 public static Monitor1 getMonitor(){ return monitor; } } 懒汉式 public class Single { private static volatile Single instance; private Single() {} public static Single getInstance() { if (instance == null) { synchronized (Single.class) { if (instance == null) { instance = new Single(); } } } return instance; } } 来源: https://www.cnblogs.com/jxxblogs/p/12176684.html

【leetcode 136】136. Single Number

寵の児 提交于 2019-12-26 02:12:34
要求:给定一个整数数组,除了其中1个元素之外,其他元素都会出现两次。找出这个只出现1次的元素。 例: array =[3,3,2,2,1] 找出元素1. 思路:最开始的想法是用两次for循环,拿数组自身和自身进行匹配查找,如果能匹配到相同的元素,则进行匹配下一个元素。    后来发现运用 ^异或 操作非常简单明了。 两个相应位上比特如果相异,则执行 异或 操作会得到1 比如二进制: 101 ^ 101 得 000 101 ^ 010 得 111 用此方法对数组元素遍历异或, 得出来得结果 就是单一没有配对得元素。 1 public class Solution { 2 public int singleNumber(int[] nums) { 3 int temp = 0; 4 for(int i = 0;i < nums.length;i++){ 5 temp = nums[i] ^ temp; 6 } 7 return temp; 8 } 9 } 来源: https://www.cnblogs.com/magicya/p/6704033.html

postgres dblink escape single quote

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Related Link: String literals and escape characters in postgresql Here is my error: ERROR: type "e" does not exist Here is my query: SELECT * FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword', E'SELECT field_1, CASE WHEN field_2 IS NOT NULL THEN \'inactive\' ELSE \'active\' END AS field_status FROM the_table ') AS linkresults(field_1 varchar(20),field_2 varchar(8)) If I use double quotes, remove the backslash escape for the single quotes and remove the E before the SELECT statement SELECT * FROM

并发编程之Executors(五)

匿名 (未验证) 提交于 2019-12-03 00:22:01
有了之前的ThreadPoolExecutor基础,本篇分析Executors就非常容易了。本文只讨论Executors类的几个创建线程池的方法,并对Fix和Single区别做了着重分析。 该类用于生成线程池,可以生成单线程线程池、固定线程数线程池等等。本科的好朋友让我分析下Fix和Single的区别,想必他也是忙的没时间,我尽量分析的透彻些。就从Single和Fix线程池开始吧。 public static ExecutorService newSingleThreadExecutor () { return new FinalizableDelegatedExecutorService //这里用到了装饰器模式,这是与Fix的唯一区别 ( new ThreadPoolExecutor ( 1 , 1 , 0 L , TimeUnit . MILLISECONDS , new LinkedBlockingQueue < Runnable >())); } public static ExecutorService newFixedThreadPool ( int nThreads ) { return new ThreadPoolExecutor ( nThreads , nThreads , 0 L , TimeUnit . MILLISECONDS , new