Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the realization method of C # moving zero-sum stair climbing?

Shulou Source: shulou.com Published: 2022-06-01 20:50:36 10月03日 Update

This article mainly explains "what is the realization method of C# mobile zero-sum staircase climbing". The content of the article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the realization method of C# mobile zero-sum staircase climbing"?

Given an array nums, write a function to move all zeros to the end of the array while maintaining the relative order of non-zero elements.

Example:

Input: [0meme1pence0pence3pr 12]

Output: [1, 3, 12, 0, 0]

Description:

Must operate on the original array, and additional arrays cannot be copied.

Minimize the number of operations.

Public void MoveZeroes (int [] nums)

{

/ / solution 1: deal with those that are not 0, and then process those that are 0

If (nums = = null | | nums.Length = = 0)

Return

Int index = 0

For (int I = 0; I

< nums.Length; i++) { if (nums[i] != 0) nums[index++] = nums[i]; } while (index < nums.Length) { nums[index++] = 0; } // 解法2:遇到不为0的 互换位置 int j = 0; for (int i = 0; i < nums.Length; i++) { if (nums[i] != 0) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; j++; } } }

The official website of the topic links to https://leetcode-cn.com/problems/climbing-stairs/

seventy。 Climb the stairs

Suppose you are climbing the stairs. You need step n to get to the roof.

You can climb one or two steps at a time. How many different ways do you have to climb to the roof?

Note: given n is a positive integer.

Example 1:

Enter: 2

Output: 2

Explanation: there are two ways to climb to the roof.

1. Order 1 + 1

2. Order 2

Example 2:

Enter: 3

Output: 3

Explanation: there are three ways to climb to the roof.

1. 1 order + 1 order + 1 order

2. Order 1 + 2

3. Order 2 + 1

Public int ClimbStairs (int n)

{

/ / solution 1: recursion (memory search)

/ / int [] memo = new int [n + 1]

/ / return helper (n, memo)

/ solution 2: dynamic programming

/ / if (n = = 1)

/ / return 1

/ / int [] dp = new int [n + 1]

/ / dp [1] = 1

/ / dp [2] = 2

/ / for (int I = 3; I

Tags: Methods stairs movement solution caching arrays roofs examples learning input output content problem solving processing interpretation differences positions elements functions dynamics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Microsoft Huawei Shulou Technology