多态以及 LeetCode 每日一题
1 多态
1.1 多态性
Java 引用变量有两个类型:一个是编译时类型,一个是运行时类型。前者是代码中声明这个变量时的类型,后者是由实际对象的类型决定的。当编译类型和运行类型不一样时,产生多态。
class BaseClass{
public int book = 6;
public void base(){
System.out.println("Father loves you!");
}
public void test(){
System.out.println("Father's method is covered");
}
}
public class SubClass extends BaseClass{
public String book = "Marvel comic book";
public void test(){
System.out.println("Son's method covers Father's");
}
public void sub(){
System.out.println("Son");
}
public static void main(String[] args){
//多态
BaseClass nc = new SubClass();
System.out.println(nc.book);//输出6,对象的实例变量不具备多态性
nc.test();//输出Son's method covers father's ,执行的是SubClass中重写的方法
nc.base();//Father loves you,subclass继承的方法
//nc.sub(); 会报错,因为BaseClass 不具备sub方法
}
}
这个例子中 nc 的编译时类型是 BaseClass , 运行类型是 SubClass,在调用 nc 的方法时,总是显现除 SubClass 的行为特征。但是注意,实例变量不具有多态性。
可以这么理解,子类对象建立时其实也创建了一个父类类型的对象,若编译时类型是父类,运行时类型是子类,该变量运行子类和父类拥有的共同的方法依然保持子类的特征(依然使用子类重写的方法),无法使用子类独有的方法,而且只能使用父类的实例变量。
引用变量在编译时,只能调用其编译时类型所具有的方法,但运行时则执行它运行时类型所具有的方法;在访问其包含的实例变量时,系统总是访问它编译时类型所定义的成员变量,而不是运行时类型。
1.2 强制转换
引用类型的转换只能发生在具有继承关系的两个类型之间,如果试图把一个父类的实例转换成子类类型时,则这个对象必须实际上是子类实例才行(运行时类型是子类)。子类转换成父类总是可以成功的。但父类转换成子类不一定。
类型转换之前,最好先通过 instanceof 运算符判断。
if (obj1 instanceof Type2){
Type2 obj2 = (Type2)obj1;
}
1.3 关于继承和多态的个人理解
1.3.1 继承
在学习继承和多态的过程中,发现了一个现象,当子类声明了一个和父类同名的变量时,子类的实例其实同时拥有了这两个实例变量,只是默认情况下父类的那个变量会被隐藏,接下来用代码测试展示。
class Father {
int i = 0; public int getI() {
return i;
} public void setI(int i) {
this.i = i;
} public void print() {
System.out.println("i:" + i);
}
} public class Son extends Father {
int i = 1; public void print() {
System.out.println(i);
} public static void main(String[] args) {
Son son = new Son();
son.setI(2);
son.print();
System.out.println(son.i);
System.out.println(son.getSuper());
son.superPrint();
} public int getSuper() {
return super.i;
}
public void superPrint() {
super.print();
}
}
在这个程序里,父类声明 i 时赋值为 0,子类也声明了一个 i 赋初值为1,子类重写了父类的 print()方法,为表示区分,父类的print()方法加了字符串“i:”。
- main 方法中,新建了一个子类对象后,从子类对象调用继承来的 setter 方法改变 i 的值为 2,接着调用子类重写的 print()方法。打印的值依然为 “1”;
- 用常规的 println()方法打印 son.i 的值,也是“1”;
- 用子类的 getSuper() 方法获得父类的 i 的值,并将其打印,打印的值为 “2”;
- 用子类的 superPrint()方法,调用父类的 print()方法,打印值为“i: 2”;
由我之前写的一批文章中可知,当子类重写了父类的方法后,父类的方法其实只是被覆盖了,可以用 super 限定调用,其实实例变量也是一样的。
另外值得提出的是,系统在执行方法时,查找某一变量的顺序:
- 查找该方法中是否有名为 a 的局部变量;
- 查找当前类中是否包含名为 a 的成员变量;
- 查找 a 的直接父类中是否有名为 a 的成员变量,依次上溯到 a 的所有父类,这种查找只会往继承树的上方找。
所以我们可以解释上述程序的现象了。当我们调用 setter 方法时,setter 在其当前类也就是父类中找到了成员变量 i ,于是改变其值为 2;调用 son 的 print()方法时,在其当前类也就是子类找到了 成员变量 i ,所以打印值为 1;用常规方法打印 son.i 时,父类的 i 已经被覆盖,所以打印值也是 1;用 getSuper()方法获得父类的 i 并将其打印发现,其值确实被修改成了 2;用 superPrint()方法调用父类的 print()方法,该方法在当前类找到了变量 i,于是将其打印,结果为:“i: 2”。
在继承的学习中,我们知道,当我们创建一个子类实例时,系统会从继承树的顶端开始,依次往下初始化类,接着创建各个类的实例。根据上文的例子,我的理解是,与其说继承后子类“拥有”了父类的属性和方法,倒不如说是“借用”。当我们由子类实例调用父类的方法或访问父类的成员变量(子类中没有重写的方法\没有再次声明的成员变量),系统在子类下没有找到同名的方法或变量,于是由下往上在其父类中寻找并调用\访问,这就造成了子类“拥有”了父类的成员变量及方法的假象。一旦子类中有同名的成员,父类成员就被覆盖。
1.3.2 多态
利用上面继承所说的“覆盖”的特性,也可以解释一些多态的现象。
class Father{
int i = 1;
public void print(){
System.out.println("Father");
}
public void printNum(){
System.out.println(i);
}
}
public class Son extends Father{
int i = 2; public void print(){
System.out.println("Son");
}
public void printNum(){
System.out.println(i);
}
public static void main(String[] args) {
Father val = new Son();
val.print();
System.out.println(val.i);
val.printNum();
}
}
程序的输出是:
第一条和第二条我们由上文“多态性”中不难理解,总结一句话就是:“方法有多态性,实例变量没有多态性”,寻找变量时系统总是由编译时类型出发,而寻找方法时系统总是由运行时类型出发。而第三条输出结果,我们就可以借由上文所说的“隐藏”特性以及“多态性”一同理解:当 val 调用 printNum()方法时,系统由其运行时类型出发,找到了子类的重写的 printNum()方法,该方法又由当前类(子类)出发寻找变量 i ,于是找到了被隐藏了的值为 2 的变量 i 。
2 LeetCode
38.报数
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1
被读作 "one 1"
("一个一"
) , 即 11
。11
被读作 "two 1s"
("两个一"
), 即 21
。21
被读作 "one 2"
, "one 1"
("一个二"
, "一个一"
) , 即 1211
。
给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。
注意:整数顺序将表示为一个字符串。
示例 1:
输入: 1
输出: "1"
示例 2:
输入: 4
输出: "1211"
题目比较晦涩难懂。。。(外国人的脑洞)反正就是数组第一项是1,之后每一项都是把前一项像报数一样念出来,再将念出来的数变成数字序列。(好像我也没说清楚。。)
我的回答是:
class Solution {
public String countAndSay(int n) {
String[] array = new String[n];
array[0] = "1";
StringBuilder tmp = new StringBuilder();
int count = 1;
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < array[i].length(); j++){
if(j < array[i].length() - 1 && array[i].charAt(j) == array[i].charAt(j + 1)){
count++;
continue;
}
tmp.append(count);
tmp.append(array[i].charAt(j));
count = 1;
}
array[i + 1] = tmp.toString();
tmp.delete(0, tmp.length());
}
return array[n-1];
}
}
另附评论找到的最快方法(手动滑稽),其实是方便大家理解题意:
class Solution {
public String countAndSay(int n) {
switch(n){
case 1:
return "1";
case 2:
return "11";
case 3:
return "21";
case 4:
return "1211";
case 5:
return "111221";
case 6:
return "312211";
case 7:
return "13112221";
case 8:
return "1113213211";
case 9:
return "31131211131221";
case 10:
return "13211311123113112211";
case 11:
return "11131221133112132113212221";
case 12:
return "3113112221232112111312211312113211";
case 13:
return "1321132132111213122112311311222113111221131221";
case 14:
return "11131221131211131231121113112221121321132132211331222113112211";
case 15:
return "311311222113111231131112132112311321322112111312211312111322212311322113212221";
case 16:
return "132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211";
case 17:
return "11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221";
case 18:
return "31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211";
case 19:
return "1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221";
case 20:
return "11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211";
case 21:
return "311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311122122111312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221131112311311121321122112132231121113122113322113111221131221";
case 22:
return "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113213221133122112231131122211211131221131112311332211211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211";
case 23:
return "111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121113222123112221221321132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221";
case 24:
return "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211";
case 25:
return "132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132132211231232112311321322112311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312111312212231131122211311123113322112111312211312111322111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113213221132213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121132211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221";
case 26:
return "1113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123211211131211121311121321123113111231131122112213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122113221122112133221121113122113121113222123211211131211121311121321123113213221121113122113121113222113221113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211";
case 27:
return "31131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322123211211131211121332211231131122211311122122111312211213211312111322211231131122211311123113322112111331121113112221121113122113111231133221121113122113121113222123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221123113112221131112311332111213122112311311123112111331121113122112132113311213211321222122111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311123113322113223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331221122311311222112111312211311123113322112132113213221133122211332111213112221133211322112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121122132112311321322112111312211312111322211213111213122112132113121113222112132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212321121113121112133221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213212312311211131122211213211331121321122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311222113111221221113122112132113121113222112132113213221133122211332111213322112132113213221132231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221";
case 28:
return "13211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221232112111312211312113211223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321322113311213212322211322132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212311322123123112111321322123122113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132132211331221122311311222112111312211311123113322112111312211312111322212311322123123112112322211211131221131211132221132213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211";
case 29:
return "11131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221231122212213211321322112311311222113311213212322211211131221131211132221232112111312111213322112131112131221121321131211132221121321132132212321121113121112133221121321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122111213122112311311222113111221131221221321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311222112111331121113112221121113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212321121113121112133221132211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112132132112211131221131211132221121321132132212321121113121112133221123113112221131112311332111213211322111213111213211231131211132211121311222113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131211131221223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112211213322112312321123113213221123113112221133112132123222112311311222113111231132231121113112221121321133112132112211213322112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212311222122132113213221123113112221133112132123222112311311222113111231133211121321132211121311121321122112133221123113112221131112311332211322111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221";
case 30:
return "3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321223112111311222112132113213221133122211311221122111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331222113321112131122211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112112322211322311311222113111231133211121312211231131112311211232221121113122113121113222123211211131221132211131221121321131211132221123113112211121312211231131122113221122112133221121321132132211331121321231231121113121113122122311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312211322311211133112111312211213211311123113223112111321322123122113222122211211232221121113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123112221221321132132211231131122211331121321232221121113122113121113222123211211131211121332211213111213122112132113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231131112311311221122132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132122311211131122211213211321222113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111312211322311211133112111312212221121123222112132113213221133112132123222113223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211";
}
}
}
多态以及 LeetCode 每日一题的更多相关文章
- 【js】Leetcode每日一题-制作m束花所需的最少天数
[js]Leetcode每日一题-制作m束花所需的最少天数 [题目描述] 给你一个整数数组 bloomDay,以及两个整数 m 和 k . 现需要制作 m 束花.制作花束时,需要使用花园中 相邻的 k ...
- 【JavaScript】Leetcode每日一题-在D天内送包裹的能力
[JavaScript]Leetcode每日一题-在D天内送包裹的能力 [题目描述] 传送带上的包裹必须在 D 天内从一个港口运送到另一个港口. 传送带上的第 i 个包裹的重量为 weights[i] ...
- 【js】Leetcode每日一题-完成所有工作的最短时间
[js]Leetcode每日一题-完成所有工作的最短时间 [题目描述] 给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间. 请你将这些工作分配给 k 位工人.所有工 ...
- 【js】Leetcode每日一题-数组异或操作
[js]Leetcode每日一题-数组异或操作 [题目描述] 给你两个整数,n 和 start . 数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == ...
- 【js】Leetcode每日一题-解码异或后数组
[js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...
- 【JavaScript】Leetcode每日一题-青蛙过河
[JavaScript]Leetcode每日一题-青蛙过河 [题目描述] 一只青蛙想要过河. 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有). 青蛙可以跳上石子 ...
- 【JavaScript】Leetcode每日一题-平方数之和
[JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...
- 【JavaScript】Leetcode每日一题-二叉搜索树的范围和
[JavaScript]Leetcode每日一题-二叉搜索树的范围和 [题目描述] 给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和. 示例1: 输入: ...
- 【JavaScript】Leetcode每日一题-递增顺序搜索树
[JavaScript]Leetcode每日一题-递增顺序搜索树 [题目描述] 给你一棵二叉搜索树,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没 ...
随机推荐
- 【BAT面试题系列】面试官:你了解乐观锁和悲观锁吗?
前言 乐观锁和悲观锁问题,是出现频率比较高的面试题.本文将由浅入深,逐步介绍它们的基本概念.实现方式(含实例).适用场景,以及可能遇到的面试官追问,希望能够帮助你打动面试官. 目录 一.基本概念 二. ...
- C#中使用Bogus创建模拟数据
原文:CREATING SAMPLE DATA FOR C# 作者:Bruno Sonnino 译文:C#中使用Bogus创建模拟数据 译者: Lamond Lu 背景 在我每次写技术类博文的时候,经 ...
- mip-link 组件功能升级说明
背景描述 某个页面被多少页面引用(在其他页面上有指向这个页面的 a 标签),是搜索引擎判断这个页面价值的其中一个因子.这里的搜索引擎不只是指百度,还包括国内外其他的搜索引擎. MIP 在最初设计 MI ...
- python中的shutil模块
目录 python中的shutil模块 目录和文件操作 归档操作 python中的shutil模块 shutil模块对文件和文件集合提供了许多高级操作,特别是提供了支持文件复制和删除的函数. 目录和文 ...
- Python调用ansible API系列(四)动态生成hosts文件
方法一:通过最原始的操作文件的方式 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 通过操作文件形式动态生成ansib ...
- Hibernate内容详解
一:引入Hibernate的jar包 http://t.cn/EioD1xk 二:配置Hibernate的核心配置文件hibernate.cfg.xml <!DOCTYPE hibernate- ...
- Scrapy爬虫遇到 ‘Forbidden by robots.txt’的问题
今天在爬知乎精华时,出现了‘Forbidden by robots.txt’的问题 了解到到scrapy在爬取设定的url之前,它会先向服务器根目录请求一个txt文件,这个文件规定了爬取范围 scra ...
- Java设置PDF有序、无序列表
文档中的设置有序或无序列表是一种反应内容上下级关系或者内容相同属性的方式,与单纯的文字叙述相比,它能有效增强文档内容的条理性,突出重点.因此,本文将分享通过Java编程在PDF文档中设置有序或无序列表 ...
- MongoDB分片群集的部署(用心描述,详细易懂)!!
概念: MongoDB分片是使用多个服务器存储数据的方法,以支持巨大的数据存储和对数据进行存储 优势: 1.减少了每个分片需啊哟处理的请求数,群集可以提高自己的存储容量和吞吐量 2.减少了每个分片存储 ...
- jqGrid 常用 总结 -2
这次的总结是针对于一次bug,先说下我们遇到的问题,就是后台人员告诉我们添加数据到100条数据的时候,101条就看不到,当时我觉得就是没有分页的原因,所以我就以为在jqgrid中设置一个loadonc ...