当前位置:网站首页>1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
2022-07-06 00:53:00 【PI Qiliang】
1791. Find the central node of the star graph 【 Simple questions 】
Ideas :
- Because each point does not repeat , and edges It represents the line connected by two endpoints , Then the central node of these lines will inevitably repeat in these segments , Only the central node will appear repeatedly , So as long as there is an endpoint in the center, at least 2 Time , Then this point must be the central node .
- Define a hash set Used to store the endpoints of these segments , As long as there is endpoint repetition , Then return to this node directly , Otherwise, add this endpoint into set Traverse the next line segment .
Code :
class Solution {
public int findCenter(int[][] edges) {
Set<Integer> set = new HashSet<>();
for (int[] edge : edges) {
if (!set.add(edge[0])){
return edge[0];
}
if (!set.add(edge[1])){
return edge[1];
}
}
return 0;
}
}
when :
At present, there is no official solution .
1790. Can performing only one string exchange make two strings equal 【 Simple questions 】
Ideas :
- Define a list list Used to count the character positions of two strings that are not equal .
- Because the two strings are the same length , So use ordinary for Loop can traverse two strings at the same time , If the characters of two strings are different in the same position , Add the current location to list in , After adding, if you find list The length of has been greater than 2 了 , Then it means that at this time, the two strings have at least 3 The positions of characters are different , It is impossible to make two strings equal by one string Exchange , So at this point, go straight back to false.
- After string traversal , If list The length of is still 0, Then it means that the two strings are exactly the same , Then I exchange characters in the same position of two strings at will , After that, the two strings must still be equal , Satisfy the question , So at this point, go straight back to true.
- If list The length of is 1, At this time, there is only one position character difference between the two strings , The characters in other positions are all the same and cannot be exchanged , At this time, it must be impossible to make the two strings equal through a string Exchange , So back false.
- Get rid of list The length of is 0, by 1 The situation of , that list The length of can only be 2 了 ( Greater than 2 The situation of 2 It has been eliminated when traversing two strings in one step , Can go to the end of traversal instructions list The length of must be less than or equal to 2 Of ), Now if s1 Of list【0】 The character of the position is the same as s2 Of list【1】 The characters of position are equal and s1 Of list【1】 Position character and s2 Of list【0】 Position characters are equal , Then it shows that the two unequal characters of these two strings can be exchanged to make the two strings equal , Satisfy the question , return true, Otherwise return to false.
Code :
class Solution {
public boolean areAlmostEqual(String s1, String s2) {
List<Integer> list = new ArrayList<>();
int len = s1.length();
for (int i = 0; i < len; i++) {
if (s1.charAt(i) != s2.charAt(i)){
list.add(i);
}
if (list.size()>2){
return false;
}
}
if (list.size() == 0){
return true;
}
if (list.size() == 1){
return false;
}
return (s1.charAt(list.get(0)) == s2.charAt(list.get(1))) && (s1.charAt(list.get(1)) == s2.charAt(list.get(0)));
}
}
when :
At present, there is no official solution .
notes :
I wrote today's daily question before , See
1189. “ balloon ” Maximum number of
The idea of rewriting today is not the same as last time , But the performance has not improved , I won't write it again in this article .
边栏推荐
- ubantu 查看cudnn和cuda的版本
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- Installation and use of esxi
- Recursive method to realize the insertion operation in binary search tree
- cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
- XML Configuration File
- Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
- 激动人心,2022开放原子全球开源峰会报名火热开启
- Study diary: February 13, 2022
- devkit入门
猜你喜欢

Ffmpeg captures RTSP images for image analysis

How to use the flutter framework to develop and run small programs
![[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)](/img/e4/a41fe26efe389351780b322917d721.jpg)
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)

MCU通过UART实现OTA在线升级流程
![[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)](/img/09/9076de099147b2d0696fe979a68ada.jpg)
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)

The relationship between FPGA internal hardware structure and code

Cf:c. the third problem

The population logic of the request to read product data on the sap Spartacus home page

Analysis of the combination of small program technology advantages and industrial Internet

Spark AQE
随机推荐
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
BiShe - College Student Association Management System Based on SSM
Spark获取DataFrame中列的方式--col,$,column,apply
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
【第30天】给定一个整数 n ,求它的因数之和
The inconsistency between the versions of dynamic library and static library will lead to bugs
Arduino六足机器人
95后CV工程师晒出工资单,狠补了这个,真香...
KDD 2022 | EEG AI helps diagnose epilepsy
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
Cannot resolve symbol error
Ffmpeg captures RTSP images for image analysis
2022-02-13 work record -- PHP parsing rich text
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
Curlpost PHP
Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
Recoverable fuse characteristic test
MySQL storage engine