当前位置:网站首页>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 .
边栏推荐
- UART的奇偶校验
- Google Earth Engine(GEE)——用不同方法计算slope对比案例分析
- Noi OJ 1.2 integer data type storage space size
- 安卓安全/逆向面试题
- Noi OJ 1.4 01: positive and negative C language
- 【黄金分割点】与【斐波那契数列】
- 智慧园区效果不满意?请收下ThingJS这份秘籍
- Torch weight to mindspore
- Not satisfied with the effect of the smart park? Please accept this secret script of thingjs
- 从0到1,IDE如何提升端侧研发效率?| DX研发模式
猜你喜欢

ESP32-CAM、ESP8266、WIFI、蓝牙、单片机、热点创建嵌入式DNS服务器

Picture storage -- Reference

深潜Kotlin协程(十四):共享状态的问题
[Architect (Part 40)] connecting mongodb database developed by server

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file

如何用 Redis 实现一个分布式锁

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

Design and implementation of stm32f103zet6 single chip microcomputer dual serial port mutual sending program

TTY drive frame

The simplest DIY actuator cluster control program based on 51 single chip microcomputer, pca9685, IIC and PTZ
随机推荐
【MATLAB 图形用户界面控件使用】
New technology aesthetics and original biological networking operating system reshape the whole house intelligence
Simplest DIY remote control computer system based on STM32 ② (wireless remote control + key control)
Over a year, time has changed. Chinese chips have made breakthroughs, but American chips are difficult to sell
Noi OJ 1.3 14: elephant drinking water C language
STM32F103ZET6单片机双串口互发程序设计与实现
L'outil de périphérique deveco aide au développement de périphériques openharmony
为什么指针做形参没有改变对应的值
Force buckle 1319 Number of connected network operations
UWA new | real person real machine test new overseas model zone
Flush recommended? Is it safe to open a mobile account?
最简单DIY基于蓝牙、51单片机和舵机的钢铁爱国者机关枪控制器
web技术分享| 【高德地图】实现自定义的轨迹回放
Win10 微软输入法(微软拼音) 不显示 选字栏(无法选字) 解决方法
[use of Matlab graphical user interface controls]
六张图详解LinkedList 源码解析
php 正则表达式
智慧园区效果不满意?请收下ThingJS这份秘籍
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
MAUI使用Masa blazor组件库