当前位置:网站首页>Numbers that only appear once < difficulty coefficient > & Yang Hui triangle < difficulty coefficient >

Numbers that only appear once < difficulty coefficient > & Yang Hui triangle < difficulty coefficient >

2022-06-23 11:16:00 Hua Weiyun

1、 A number that appears only once < The difficulty coefficient >

Title Description : Given an array of non-empty integers , Except that an element only appears once , Each of the other elements occurs twice . Find the element that only appears once .

explain : Your algorithm should have linear time complexity . Can you do this without using extra space ?

Example 1:

Input : [2,2,1]
Output : 1

Example 2:

Input : [4,1,2,1,2]
Output : 4

🧷 platform :Visual studio 2017 && windows

The core idea : Use the XOR operator ^ —— Same as 0, Dissimilarity is 1

leetcode The original title is

class Solution {public:    int singleNumber(vector<int>& nums) {        int ret = 0;        //1、operator[]        /*for(size_t i = 0; i < nums.size(); ++i) { ret ^= nums[i]; }*/        //2、 iterator         /*vector<int>::iterator it = nums.begin(); while(it != nums.end()) { ret ^= *it; ++it; }*/        //3、 Range for        for(auto e : nums)        {            ret ^= e;        }        return ret;    }};
2、 Yang hui triangle < The difficulty coefficient >

Title Description : Given a nonnegative integer numRows, Generate 「 Yang hui triangle 」 Before numRows That's ok . stay 「 Yang hui triangle 」 in , Each number is the sum of the numbers at the top left and right of it .

 Please add a picture description
Example 1:

Input :numRows = 5
Output :[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]

Example 2:

Input :numRows = 1
Output :[ [1] ]

Tips :1 <= numRows <= 30

🧷 platform :Visual studio 2017 && windows

The core idea : You need to be a Yanghui triangle , The first and last of each line are 1, The rest are set to 0, If it is 0, You need to calculate . Here we can find the law :1 = 1 + (1 - 1), Here, take the first value to be calculated as an example , And the number here represents the subscript of —— The first 3 Line to 1 The value for the subscript position is equal to the second 2 Line to 1 Add the... To the value of the subscript 2 Line to 1 - 1 Is the value of the subscript .

leetcode The original title is

class Solution {public:    //vector<vector<int>> It's a two-dimensional array , Here it is vector When simulating the implementation, I will also talk about it in detail     vector<vector<int>> generate(int numRows) {        vector<vector<int>> vv;        vv.resize(numRows);        // Generate         for(size_t i = 0; i < vv.size(); ++i)        {        	// How many per line , And initialize to 0            vv[i].resize(i + 1, 0);            // The first and last values of each line are assigned to 1            /*vv[i].front() = 1; vv[i].back() = 1;*/            vv[i][0] = 1;            vv[i][vv[i].size() - 1] = 1;         }        // Traverse         for(size_t i = 0; i < vv.size(); ++i)        {            for(size_t j = 0; j < vv[i].size(); ++j)            {                if(vv[i][j] == 0)// Need to deal with                 {                    vv[i][j] = vv[i - 1][j] + vv[i - 1][j - 1];                }            }        }        return vv;    }};

Add
 Insert picture description here
If this question is in C Words written in language , We need to dynamically open up a two-dimensional array , There is relatively no C++ The comfort of .

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231022198154.html