数据结构算法数据科学与R语言数据分析

数据结构:两数之和(三)

2016-12-05  本文已影响14人  Bioconductor

本文首发为CSDN博客,地址为:http://blog.csdn.net/xxzhangx/article/details/53433519

欢迎关注,谢谢!引用转载请注明作者和地址!

题目:设计一个类,包含如下两个成员函数:
Save(int input)
插入一个整数到一个整数集合里。
Test(int target)
检验是否存在两个数和为输入值。如果存在这两个数,则返回true;否则返回false。

注意:允许有相同的元素。

伪代码:

这里写代码片

R语言:

two_numbers_3<-function(a)
{
  s= unique(a)
  for (i in 1:length(s))
  {
    if(is.element(new_input-s[i],s))
    {
      k = k +1
      res = append(res,TRUE)
    }
    else
    {
      k = k +1
      res =append(res,FALSE)
    }
  }
  if(sum(res) > 0)
  {
    print ("true")
  }
  else
  {
    print ("false")
  }
}


> res=c()
> new_input<-scan()
1: 6
2: 
Read 1 item
> k=0
> a=c(1,4,9,23)
> two_numbers_3(a)
[1] "false"


> a=c(1:6,2:9)
> two_numbers_3(a)
[1] "true"

python:

这里写代码片
上一篇 下一篇

猜你喜欢

热点阅读