|
|
@ -2328,8 +2328,10 @@ $$
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
```js title="worst_best_time_complexity.js"
|
|
|
|
```js title="worst_best_time_complexity.js"
|
|
|
|
|
|
|
|
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
|
|
|
function randomNumbers(n) {
|
|
|
|
function randomNumbers(n) {
|
|
|
|
let nums = Array(n);
|
|
|
|
let nums = Array(n);
|
|
|
|
|
|
|
|
// 生成数组 nums = { 1, 2, 3, ..., n }
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
nums[i] = i + 1;
|
|
|
|
nums[i] = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -2343,6 +2345,7 @@ $$
|
|
|
|
return nums;
|
|
|
|
return nums;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 查找数组 nums 中数字 1 所在索引 */
|
|
|
|
function findOne(nums) {
|
|
|
|
function findOne(nums) {
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
if (nums[i] == 1) {
|
|
|
|
if (nums[i] == 1) {
|
|
|
@ -2352,6 +2355,7 @@ $$
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
function main() {
|
|
|
|
function main() {
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
let n = 100;
|
|
|
|
let n = 100;
|
|
|
@ -2368,8 +2372,10 @@ $$
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="worst_best_time_complexity.ts"
|
|
|
|
```typescript title="worst_best_time_complexity.ts"
|
|
|
|
|
|
|
|
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
|
|
|
function randomNumbers(n: number): number[] {
|
|
|
|
function randomNumbers(n: number): number[] {
|
|
|
|
let nums = Array(n);
|
|
|
|
let nums = Array(n);
|
|
|
|
|
|
|
|
// 生成数组 nums = { 1, 2, 3, ..., n }
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
nums[i] = i + 1;
|
|
|
|
nums[i] = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -2383,6 +2389,7 @@ $$
|
|
|
|
return nums;
|
|
|
|
return nums;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 查找数组 nums 中数字 1 所在索引 */
|
|
|
|
function findOne(nums: number[]): number {
|
|
|
|
function findOne(nums: number[]): number {
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
if (nums[i] == 1) {
|
|
|
|
if (nums[i] == 1) {
|
|
|
@ -2392,6 +2399,7 @@ $$
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
function main(): void {
|
|
|
|
function main(): void {
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
let n = 100;
|
|
|
|
let n = 100;
|
|
|
|