当前位置:网站首页>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 .

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 
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 .
边栏推荐
猜你喜欢

DevEco Device Tool 助力OpenHarmony设备开发

最简单DIY基于STM32F407探索者开发板的MPU6050陀螺仪姿态控制舵机程序

Deveco device tool helps openharmony device development

新派科技美学、原生物联网操作系统重塑全屋智能

A child process is created in the program, and then the parent and child processes run independently. The parent process reads lowercase letters on the standard input device and writes them to the pip

The simplest DIY pca9685 steering gear control program based on the integration of upper and lower computers of C # and 51 single chip microcomputer

直播带货app源码搭建中,直播CDN的原理是什么?

“互联网+”大赛命题火热对接中 | 一图读懂百度38道命题

Esp32-cam, esp8266, WiFi, Bluetooth, MCU, hotspot create embedded DNS server

一年多时间时移世易,中国芯片不断突破,美国芯片却难以卖出
随机推荐
Noi OJ 1.2 06: round floating point numbers to zero
Picture storage -- Reference
证券开户网上安全度高吗
【云舟说直播间】-数字安全专场明天下午正式上线
[use of Matlab graphical user interface controls]
Is it difficult to register stocks and open accounts online? Is it safe to open an account online now?
1154. 一年中的第几天
“互联网+”大赛命题火热对接中 | 一图读懂百度38道命题
Deep dive kotlin synergy (XIV): problems of shared state
最简单DIY基于51单片机、PCA9685、IIC、云台的舵机集群控制程序
华为云·维享会交流平台简介
为什么指针做形参没有改变对应的值
圖片存儲--引用
Maui uses Masa blazor component library
1154. day of the year
UWA上新|真人真机测试新增海外机型专区
Implement common C language string processing functions
Noi OJ 1.3 14: elephant drinking water C language
How to write a literature review? What should I do if I don't have a clue?
Groovy之Map操作