Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc7c3352

Browse files
committed
task 08
1 parent7badb94 commitc7c3352

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

‎src/leetcode/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import task_a03 from './task-a03';
44
importtask_a04from'./task-a04';
55

66
// import run from './task-a06';
7-
importrunfrom'./task-a07';
7+
importrunfrom'./task-a08';
88

99
exportdefaultrun;

‎src/leetcode/task-a08.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,64 @@
77
88
c) A function that determines the number of elements k that precede the first negative element. If all elements are non-negative, k is equal to the number of elements in the array.
99
10+
*
1011
*/
1112

13+
import{formRandomArray}from'./task-formArray';
14+
15+
functionchangeSignA(array:number[]):void{
16+
array.forEach((element,index)=>{
17+
if(index%2===0)array[index]=-element;
18+
});
19+
}
20+
21+
functiondetermineEvenElements(array:number[]):number{
22+
letcounter=0;
23+
array.forEach((element,index)=>{
24+
if(index%2!==0&&element%2===0)counter+=1;
25+
});
26+
returncounter;
27+
}
28+
29+
functionelementsReturned(array:number[]):number{
30+
letcounter=0;
31+
for(leti=0;i<array.length;i++){
32+
if(array[i]<0)break;
33+
elsecounter+=1;
34+
}
35+
returncounter;
36+
}
37+
38+
functionelementsReturned2(array:number[]):number{
39+
for(leti=0;i<array.length;i++){
40+
if(array[i]<0)returni;
41+
}
42+
returnarray.length;
43+
}
44+
45+
functionelementsReturned3(array:number[]):number{
46+
leti=0;
47+
while(array[i]>0)i+=1;
48+
returni;
49+
}
50+
51+
functionrun():void{
52+
console.log(' Run 08 ');
53+
constarray:number[]=formRandomArray(15,-30,90);
54+
console.log(' array: ',array);
55+
// changeSignA(array);
56+
// console.log(' array: ', array);
57+
console.log(
58+
'Count even elements odd indencies ',
59+
determineEvenElements(array),
60+
);
61+
console.log('Elements returned: ',elementsReturned(array));
62+
console.log('Elements returned: ',elementsReturned2(array));
63+
console.log('Elements returned: ',elementsReturned3(array));
64+
}
65+
66+
exportdefaultrun;
67+
1268
/***
1369
*
1470

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp