给定一个排序的数字列表,我需要找到大于给定数字的最小数字.考虑这个清单:
arr=[1,2,3,5,7,11,101,131,151,181,191,313,353,373,383]
Run Code Online (Sandbox Code Playgroud)
假设指定的数字是320.然后,我的方法应该返回353,因为353是大于320的最小数字.
我试图使用略微修改的二进制搜索形式; 但是在执行时,程序进入无限循环.
def modBinarySearch(arr,x):
l=len(arr)
mid=l/2
if arr[mid]>=x and arr[mid-1]<x:
return arr[mid]
elif arr[mid]>x and arr[mid-1]>x:
modBinarySearch(arr[mid:l],x)
else:
modBinarySearch(arr[0:mid],x)
N=int(raw_input())
arr=[1,2,3,5,7,11,101,131,151,181,191,313,353,373,383]
print modBinarySearch(arr,N)
Run Code Online (Sandbox Code Playgroud)
有人可以指出我做错了什么吗?
我正在阅读关于链表的几个基本操作,我看到主要使用两种类型的循环
struct node {
int data;
struct node *next;
}*start=NULL,*tmp;
Run Code Online (Sandbox Code Playgroud)
第一个循环是形式
for(tmp=start;tmp->next!=NULL;tmp=tmp->next);
Run Code Online (Sandbox Code Playgroud)
使用上面的循环,现在tmp指针指向列表中的最后一个节点
第二个循环是形式
tmp=start;
while(tmp!=NULL)
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
我认为他们都做同样的工作,但我不确定.有什么区别吗?
编写一个程序,用于在表达式中查找重复的括号.例如 :
(( a + b ) + (( c + d ))) = a + b + c + d
(( a + b ) * (( c + d ))) = (a + b) * (c + d)
Run Code Online (Sandbox Code Playgroud)
我所知道的一种方法涉及以下两个步骤:
我不想完成从一个表示转换为另一个表示的整个过程,然后将其转换回来.
我想使用堆栈,但只需一次通过.可能吗 ?
请建议算法或共享代码.
我正在重新审视我的笔记Dynamic Programming.它基本上是一种记忆递归技术,它将解决方案存储到较小的子问题中,以便以后在相对较大的子问题的计算解决方案中重用.
我的问题是,为了将DP应用于递归问题,它必须具有最佳子结构.这基本上需要对问题的最佳解决方案包含对子问题的最佳解决方案.
否则可能吗?我的意思是你见过一个问题的最佳解决方案不包含子问题的最佳解决方案的情况.
如果您想加深我的理解,请分享一些例子.
我这里有一点烦人的情况; 其中我无法正确接受输入.我总是接受输入Scanner,而不习惯BufferedReader.
输入格式
First line contains T, which is an integer representing the number of test cases.
T cases follow. Each case consists of two lines.
First line has the string S.
The second line contains two integers M, P separated by a space.
Run Code Online (Sandbox Code Playgroud)
例
Input:
2
AbcDef
1 2
abcabc
1 1
Run Code Online (Sandbox Code Playgroud)
我的代码到目前为止:
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
int T= Integer.parseInt(inp.readLine());
for(int i=0;i<T;i++) { …Run Code Online (Sandbox Code Playgroud) 我最近遇到了在Python中定义函数的想法.我有这个代码,它给出了错误:
def f1(a):
def f2(x):
return a+x
return 2*a
Run Code Online (Sandbox Code Playgroud)
错误:在通话时 f2(5)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
f2(5)
NameError: name 'f2' is not defined
Run Code Online (Sandbox Code Playgroud)
我有一些困难,了解全局变量跨职能甚至在递归调用中使用的方式.我真的很感激,如果有人会指出我的错误,并且可以帮助我前进的道路.提前致谢 !!
我需要nCr mod p有效地计算.现在,我已经编写了这段代码,但它超出了时间限制.请建议更优化的解决方案.
对于我的情况, p = 10^9 + 7 and 1 ? n ? 100000000
我还必须确保没有溢出,因为nCr mod p保证适合32位整数,但n!可能超出限制.
def nCr(n,k):
r = min(n-k,k)
k = max(n-k,k)
res = 1
mod = 10**9 + 7
for i in range(k+1,n+1):
res = res * i
if res > mod:
res = res % mod
res = res % mod
for i in range(1,r+1):
res = res/i
return res
Run Code Online (Sandbox Code Playgroud)
PS:我认为我的代码可能不完全正确.但是,它似乎适用于小的n正确.如果错了,请指出来!
我正在转向Python,而且对pythonic方法仍然相对较新.我想编写一个带字符串和列表的函数,如果列表中的所有元素都出现在字符串中,则返回true.
这似乎很简单.但是,我面临一些困难.代码如下:
def myfun(str,list):
for a in list:
if not a in str:
return False
return True
Run Code Online (Sandbox Code Playgroud)
Example : myfun('tomato',['t','o','m','a']) should return true
myfun('potato',['t','o','m','a']) should return false
myfun('tomato',['t','o','m']) should return true
Run Code Online (Sandbox Code Playgroud)
此外,我希望有人可以在这里提出可能的正则表达式方法.我也在试试他们.
我想检查变量的类型是否是Python中的特定类型。例如-我想检查 varx是否是 int 。
>>x=10
>>type(x)
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
但我如何比较它们的类型。我尝试过这个,但似乎不起作用。
if type(10)== "<type 'int'>":
print 'yes'
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?
我正在尝试将Set转换为数组.
Set<String> s = new HashSet<String>(Arrays.asList("mango","guava","apple"));
String[] a = s.toArray(new String[0]);
for(String x:a)
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)
它工作正常.但我不理解其意义new String[0]在String[] a = s.toArray(new String[0]);.
我的意思是最初我在尝试String[] a = c.toArray();,但它不会工作.为什么需要new String[0].