numb

[LeetCode 解题报告]260. Single Number III

蓝咒 提交于 2020-02-11 06:37:11
Given an array of numbers nums , in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note : The order of the result is not important. So in the above example, [5, 3] is also correct. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity? class Solution { public: vector<int> singleNumber(vector<int>& nums) { if (nums.empty()) return {}; int diff = 0; for (int i = 0; i < nums.size(); i ++) diff ^= nums[i

设计模式--简单工厂模式

巧了我就是萌 提交于 2019-12-23 02:12:37
简单工厂模式 (Simple Factory Pattern) 定义: 又叫静态工厂方法模式 (Static FactoryMethod Pattern), 是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。 类型:创建型模式 UML图 简单工厂模式中所包含的角色及其相应职责如下: 工厂 (Creator) 角色 : 简单工厂模式的核心,由它负责创建所有类的内部逻辑。当然工厂类必须能被外界调用,创建所需要的产品对象。 抽象产品 (Product) 角色 : 简单工厂模式所创建的所有对象的父类,注意,这里的父类可以是接口也可以是抽象类,它负责描述所有实例所共有的公共接口。 具体产品 (Concrete Product) 角色 :简单工厂所创建的具体实例对象,这些具体的产品往往都拥有共同的父类。 使用场景及代码实现 使用 Java 来编写一个计算器控制台程序,要求输入两个数和运算符号,得到结果 public abstract class Operation { protected double numA; protected double numB; public double getNumA() { return numA; } public void setNumA(double numA) { this.numA = numA; } public

java泛型

◇◆丶佛笑我妖孽 提交于 2019-12-07 05:11:19
泛型 1)在编译时,由编译器约束放入集合中的类型 2)在运行时,编译器会擦除原泛型类型 3)泛型二边要么都不使用约束,要么二边约束一致类型,同时二边必须使用引用类型 4)为了与JDK1.4兼容,泛型可以一边有约束,一边无约束 思考:不利用中间变量,将二个数交互 5)当一个类中出大量的泛型方式或属性/字段,此时可以将该类作成泛型类 6)如果使用泛型通配符,只能获取与泛型具体类型无关的信息,例如:长度。 7)有二个类,初学者一定要注意:Collections和Arrays `` public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("liwen"); list.add("jiayou"); show(list); } public static void show(List list) { for(Object obj:list) { String s = (String) obj; System.out.println(s + "\t"); } } ` public static void main(String[] args) { Boolean numA = false; Boolean numB = true; System.out

设计模式之简单工厂模式

这一生的挚爱 提交于 2019-12-05 06:27:45
本博客例子均来自 大话设计模式一书! 1.首先定一个计算器: 实现简单的加减乘除 public class Compute1 { public static void main(String args[]) { try { Scanner sc = new Scanner(System.in); String result = ""; System.out.println("请输入一个数字A"); int numA = sc.nextInt(); System.out.println("请输入一个数字B"); int numB = sc.nextInt(); sc.nextLine(); System.out.println("请输入运算符+-*/"); String Opration = sc.nextLine(); switch (Opration) { case "+": result = String.valueOf(numA + numB); break; case "-": result = String.valueOf(numA - numB); break; case "*": result = String.valueOf(numA * numB); break; case "/": if (numB == 0) { result = "除数不能为0";

PAT乙级题库“傻瓜”题解之计算谱半径

孤街醉人 提交于 2019-12-04 14:31:14
在数学中,矩阵的“谱半径”是指其特征值的模集合的上确界。换言之,对于给定的 n 个复数空间的特征值 { a ​ 1 ​​ + b ​ 1 ​​ i , ⋯ , a ​ n ​​ + b ​ n ​​ i },它们的模为实部与虚部的平方和的开方,而“谱半径”就是最大模。 现在给定一些复数空间的特征值,请你计算并输出这些特征值的谱半径。 输入格式: 输入第一行给出正整数 N( ≤ 10 000)是输入的特征值的个数。随后 N 行,每行给出 1 个特征值的实部和虚部,其间以空格分隔。注意:题目保证实部和虚部均为绝对值不超过 1000 的整数。 输出格式: 在一行中输出谱半径,四舍五入保留小数点后 2 位。 输入样例: 5 0 1 2 0 -1 0 3 3 0 -3 输出样例: 4.24 1 #include<iostream> 2 #include<cmath> 3 #include<stdio.h> 4 using namespace std; 5 int main() 6 { 7 int n; 8 cin>>n; 9 int numb[n][2]; 10 double max=0; 11 for(int i=0;i<n;i++) 12 { 13 cin>>numb[i][0]>>numb[i][1]; 14 double x=numb[i][0]*numb[i][0]+numb[i]

Propositional Logic - Reducing the Number of Operations

匿名 (未验证) 提交于 2019-12-03 07:36:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In short, I'm wondering if, given two propositional formulas, whether there is a standard method for finding the shortest sequence of operations that still have the same output as the two formulas. For example if we have the following formulas: and we can reduce the number of operations by introducing a new proposition: and then Q becomes: This reduced the number of operations (unary and binary) from 19 to 14 . The new logic circuit for Q is: Ideally I would like there to be only negations and disjunctions. Is there an algorithm for

Oracle Sequence nextval is jumping number back and forth

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I created a new table and a new sequence, I have two C# web services trying to insert records into this table using same query utilizing mySequence.nextval (and yes I checked it many times, they both use mySequence.nextval ). The two web services are inserting rows to the table, but the mySequence.nextval is returning numbers out of sequence Here is how the records were created, showing PrimaryKey which gets its value from mySequence.nextval 1 21 22 23 2 3 24 25 4 27 28 5 So far no duplicates but why is mySequence.nextval jumping

SQL: avoiding hard-coding or magic numbers

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Question: What are some other strategies on avoiding magic numbers or hard-coded values in your SQL scripts or stored procedures? Consider a stored procedure whose job is to check/update a value of a record based on its StatusID or some other FK lookup table or range of values. Consider a Status table where the ID is most important, as it's a FK to another table: The SQL scripts that are to be avoided are something like : DECLARE @ACKNOWLEDGED tinyint SELECT @ACKNOWLEDGED = 3 --hardcoded BAD UPDATE SomeTable SET CurrentStatusID =

Why does this sed command to match number not work?

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My command is like this: echo "12 cats" | sed 's/[0-9]+/Number/g' (I'm using the sed in vanilla Mac) I expect the result to be: Number cats However, the real result is: 12 cats Does anyone have ideas about this? Thanks! 回答1: Expanding the + modifier works for me: echo "12 cats" | sed 's/[0-9][0-9]*/Number/g' Also, the -E switch would make the + modifier work, see choroba’s answer. 回答2: + must be backslashed to get its special meaning. echo "12 cats" | sed 's/[0-9]\+/Number/g' 回答3: Use perl, this will work on unix or linux. echo "12 cats" |

Efficiently compute pairwise squared Euclidean distance in Matlab

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 选择语言 中文(简体) 日语 英语 中文(繁体) 由 翻译 强力驱动 问题: Given two sets of d -dimensional points. How can I most efficiently compute the pairwise squared euclidean distance matrix in Matlab? Notation: Set one is given by a (numA,d) -matrix A and set two is given by a (numB,d) -matrix B . The resulting distance matrix shall be of the format (numA,numB) . Example points: d = 4 ; % dimension numA = 100 ; % number of set 1 points numB = 200 ; % number of set 2 points A = rand ( numA , d ); % set 1 given as matrix A B = rand ( numB , d ); % set 2 given as matrix B 回答1