当前位置:网站首页>Huawei machine test question: longest continuous subsequence
Huawei machine test question: longest continuous subsequence
2022-07-05 01:33:00 【JOEL-T99】
Scala Realization
Title Description :
- Yes N A sequence of positive integers , Given an integer sum
- Find the longest continuous subsequence so that their sum equals sum
- Returns the length of the secondary subsequence , If there is no sequence that meets the requirements return -1
remarks :
- The input sequence consists only of numbers and English commas , Numbers are separated by English commas
- Sequence length 1<=N<=200, The input sequence does not consider exceptions
- The title ensures that the input sequence meets the requirements
Example
Input :
- 1,2,3,4,2
- 6
Output :
- 3
analysis :
- 1,2,3 and 4,2 Both sequences can meet the requirements , So the longest continuous sequence is 1,2,3 So the result is 3
Input :
- 1,2,3,4,2
- 20
Output :
- -1
explain :
- There are no subsequences that meet the requirements , return -1
Code :
def main(args: Array[String]): Unit = {
// Get input
val elems = StdIn.readLine().split(",").map(_.toInt).toList
val sum = StdIn.readLine().toInt
// Declare variables
var tmp = 0
var maxLen = -1
// Calculation maxLen
for (i <- elems.indices) {
tmp += elems(i)
// Continuously increase the following number , If it is greater than sum Is out of , If it is equal to sum Reset maxLen
breakable {
for (j <- i + 1 until elems.length) {
tmp += elems(j)
// Out of the loop
if (tmp > sum) break
// Reset maxLen
if (tmp == sum) {
maxLen = Math.max(maxLen, j - i + 2)
break
}
// If you can't find it, set it to -1
maxLen = -1
}
}
// Reset tmp
tmp = 0
}
// Output results
println(maxLen)
}
边栏推荐
- PHP Joseph Ring problem
- Discrete mathematics: reasoning rules
- 【海浪建模1】海浪建模的理论分析和matlab仿真
- Global and Chinese market of veterinary thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
- 流批一體在京東的探索與實踐
- [CTF] AWDP summary (WEB)
- Nebula Importer 数据导入实践
- 微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
- MySQL regexp: Regular Expression Query
- Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
猜你喜欢
Five ways to query MySQL field comments!
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
微信小程序;胡言乱语生成器
流批一體在京東的探索與實踐
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
What sparks can applet container technology collide with IOT
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
【CTF】AWDP总结(Web)
Expansion operator: the family is so separated
Armv8-a programming guide MMU (3)
随机推荐
[wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
[wave modeling 3] three dimensional random real wave modeling and wave generator modeling matlab simulation
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Database postragesq role membership
[OpenGL learning notes 8] texture
MySQL REGEXP:正则表达式查询
增量备份 ?db full
Classification of performance tests (learning summary)
I was beaten by the interviewer because I didn't understand the sorting
Discrete mathematics: propositional symbolization of predicate logic
batchnorm. Py this file single GPU operation error solution
If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
[swagger]-swagger learning
Query for Boolean field as "not true" (e.g. either false or non-existent)
PHP 基础篇 - PHP 中 DES 加解密详解
Database postragesql client connection default
JS implementation determines whether the point is within the polygon range
【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
DOM basic syntax