当前位置:网站首页>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)
}
边栏推荐
- Chia Tai International Futures: what is the master account and how to open it?
- Kibana installation and configuration
- Es uses collapsebuilder to de duplicate and return only a certain field
- Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
- Can financial products be redeemed in advance?
- Async/await you can use it, but do you know how to deal with errors?
- 当产业互联网时代真正发展完善之后,将会在每一个场景见证巨头的诞生
- es使用collapseBuilder去重和只返回某个字段
- 19. Delete the penultimate node of the linked list
- 流批一體在京東的探索與實踐
猜你喜欢
增量备份 ?db full
Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
A simple SSO unified login design
Yyds dry goods inventory kubernetes management business configuration methods? (08)
[OpenGL learning notes 8] texture
Nebula importer data import practice
Yyds dry inventory swagger positioning problem ⽅ formula
PHP wechat official account development
[wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
Expansion operator: the family is so separated
随机推荐
LeetCode周赛 + AcWing周赛(T4/T3)分析对比
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #
【CTF】AWDP总结(Web)
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
Chia Tai International Futures: what is the master account and how to open it?
Global and Chinese market of optical densitometers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of radiation linear accelerators 2022-2028: Research Report on technology, participants, trends, market size and share
Database postragesq role membership
Can financial products be redeemed in advance?
pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
How to use words to describe breaking change in Spartacus UI of SAP e-commerce cloud
What sparks can applet container technology collide with IOT
Database performance optimization tool
Main window in QT application
Global and Chinese market of nutrient analyzer 2022-2028: Research Report on technology, participants, trends, market size and share
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
【微处理器】基于FPGA的微处理器VHDL开发
【海浪建模2】三维海浪建模以及海浪发电机建模matlab仿真