我似乎无法想出一个算法来解决以下问题,我尝试使用一系列for循环,但它变得太复杂了:
梯子有
n
台阶,人们可以使用1步或2步的任意组合爬上梯子.有多少种方法可以让人爬上梯子?
因此,例如,如果梯子有3个步骤,这些将是可能的路径:
并为4个步骤
任何关于如何做到这一点的见解将不胜感激.另外,我在Java工作.
编辑:我确实会使用较小的n
值,但知道如何管理较大的值肯定会很好.
我很惊讶地看到这种行为.
这是一个bug还是什么?
for(Object obj = new Object(), Integer = new Integer(300);
obj.toString().length()>3;
System.out.println("on object's loop")) {
} //causes an infinite loop (not foreach loop, of course)
Run Code Online (Sandbox Code Playgroud)
上面的代码编译并运行正常,没有任何引用new Integer(300)
.为什么这样?
我只想知道为什么Integer = new Integer(300);
没有任何参考可以.
在Java中我可以演员:
List<?> j = null;
List<Integer> j2 = (List<Integer>)j;
Run Code Online (Sandbox Code Playgroud)
那么为什么以下失败呢?
List<List<?>> i = null;
List<List<Integer>> i2 = (List<List<Integer>>)i;
Run Code Online (Sandbox Code Playgroud) 我正面临着这条线的问题(下面评论):
System.out.println("Using == ::"+s3==s4)
Run Code Online (Sandbox Code Playgroud)
哪个输出false
.
但是,System.out.println(s3==s4)
产出true
.
现在,我无法理解为什么我得到这个结果:
public class string {
public static void main(String[] args){
String s3="Shantanu";
String s4=s3;
String s1=new String("java");
String s2=new String("javaDeveloper");
System.out.println("Using Equals Method::"+s1.equals(s2));
System.out.println("Using Equals Method::"+s3.equals(s4));
System.out.println("Using == ::"+s3==s4);//Problem is here in this line
System.out.println(s1+"Directly printing the s2 value which is autocasted from superclass to string subclass ");
System.out.println("Directly printing the s1 value which is autocasted from superclass to string subclass "+s2);
System.out.println(s3);
}
}
Run Code Online (Sandbox Code Playgroud)
Output-Using Equals Method::false …
我要求用户输入1到100之间的一些数字并将它们分配到一个数组中.数组大小未初始化,因为它取决于用户输入数字的次数.我应该如何分配数组长度?如果用户输入5 6 7 8 9(5个数字),那么
int[] list;
Run Code Online (Sandbox Code Playgroud)
变
int[] list = new int[5];
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用循环,但它不会停止.
int[] integers;
int j = 0;
do {
integers = new int[j + 1];
integers[j] = in.nextInt();
j++;
} while((integers[j-1] >= 1) ||(integers[j-1]) <= 100);
Run Code Online (Sandbox Code Playgroud) 我只是有一个想法来测试一些东西并且它有效:
String[][] arr = new String[4][4];
arr[2] = new String[5];
for(int i = 0; i < arr.length; i++)
{
System.out.println(arr[i].length);
}
Run Code Online (Sandbox Code Playgroud)
输出显然是:
4
4
5
4
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
确定dict是否包含以特定字符串开头的键的最快方法是什么?我们能做得比线性好吗?当我们只知道密钥的开头时,我们如何才能实现O(1)操作?
这是当前的解决方案:
for key in dict.keys():
if key.start_with(str):
return True
return False
Run Code Online (Sandbox Code Playgroud) 我不明白以下代码导致的编译器错误.我定义了一个通用接口,参见Task,有两种方法:U doSomething(String value)
和List<Integer> getIDs()
.doSomething()方法实际上使用泛型类型作为其返回值的类型,但似乎不会导致问题.该getIDs()
方法返回一个List,它与Task的类型无关,但在使用for..each语句迭代返回值时会导致问题.发生以下编译器错误.
Run Code Online (Sandbox Code Playgroud)error: incompatible types for (Integer value : task.getIDs()){ required: Integer found: Object
似乎接口上的类型擦除导致编译器忘记第二个方法上的声明类型,这与泛型类型无关.或者换句话说,为什么接口上的泛型类型会影响编译器如何理解方法的返回值getIDs()
,特别是在for..each语句的上下文中?
显然,如果我引用for..each之外的列表没有问题,但不是直接的.
public class InterfaceTest {
public static void main(String[] args) {
Task task = new MyTask();
// no complaints about the type here
List<Integer> values = task.getIDs();
// getting a compiler error for this line
for (Integer value : task.getIDs()){
}
}
}
interface Task<U>{
U doSomething(String value);
List<Integer> getIDs();
}
Run Code Online (Sandbox Code Playgroud)
接口的实现不是必要的,以证明这一点,但我不想留下参考Task task …
你会得到一
n
对数字.在每对中,第一个数字总是小于第二个数字.当且仅当小于时,一对(c,d)
可以跟随.可以以这种方式形成对的链.找到形成的最长链对.(a,b)
b
c
我在接受亚马逊采访时得到了这个问题,但无法找到答案,只是它与LIS问题有关.
为什么我的断言声明没有产生任何结果?我认为第一个断言语句应该失败,但我没有看到Eclipse上显示任何内容.
我正在使用Eclipse来运行这个程序.
package java.first;
public class test {
public static void main(String[] args) throws Exception {
String s = "test1";
assert (s == "test");
s = "test";
assert (s == "test");
}
}
Run Code Online (Sandbox Code Playgroud)