当前位置:网站首页>Rehabilitation type force deduction brush question notes D1
Rehabilitation type force deduction brush question notes D1
2022-07-05 06:34:00 【Silent moon cold moon】
Catalog
Code section
topic 1
The difficulty is simple 13081 Switch to English to receive dynamic feedback
Given an array of integers
numsAnd an integer target valuetarget, Please find... In the array And is the target valuetargetthe Two Integers , And return their array subscripts .You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Self solution :
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0 ;i < nums.length ; i++)
{
int flag;
flag = target - nums[i];
for(int j = i+1 ;j < nums.length ; j++)
{
if(nums[j] == flag) return new int[] {i,j};
else continue;
}
}
return null;
}
} Double cycle , Time complexity
. Bubble like thinking .
topic 2
Medium difficulty 7312 Switch to English to receive dynamic feedback
Here are two for you Non empty The linked list of , Represents two nonnegative integers . Each of them is based on The reverse Stored in , And each node can only store a Numbers .
Please add up the two numbers , And returns a linked list representing sum in the same form .
You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Self solution :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode root = new ListNode();
ListNode result = root;
int andre , catchre = 0 , nextre = 0;
while(l1 != null || l2 != null || catchre == 1)
{
int l1val = (l1 != null)? l1.val:0;
int l2val = (l2 != null)? l2.val:0;
if(l1 ==null && l2 != null)
{
andre = l2val + catchre;
nextre = andre + (-10)*((andre >= 10)?1:0);
catchre = ((andre >= 10)?1:0);
}
else if(l2 ==null && l1 != null)
{
andre = l1val + catchre;
nextre = andre + (-10)*((andre >= 10)?1:0);
catchre = ((andre >= 10)?1:0);
}
else if(l1 == null &&l2 == null && catchre == 1)
{
andre = catchre;
nextre = andre + (-10)*((andre >= 10)?1:0);
catchre = ((andre >= 10)?1:0);
}
else
{
andre = l1val + l2val + catchre;
nextre = andre + (-10)*((andre >= 10)?1:0);
catchre = ((andre >= 10)?1:0);
}
ListNode Next = new ListNode (nextre);
result.next = Next;
result = Next;
if(l1 != null) l1 = l1.next;
if(l2 != null) l2 = l2.next;
}
return root.next;
}
}Use unit adder thinking . Set up andre Direct addition result ,catchre Carry mark ( The carry flag bit is cf, Should be carry flag, I remember wrong ),nextre The value left after processing .
The core code snippet is as follows , That is, the processing of addition results and the generation of carry flag bits .
andre = l1val + l2val + catchre;
nextre = andre + (-10)*((andre >= 10)?1:0);
catchre = ((andre >= 10)?1:0);topic 3
3. Longest substring without repeating characters
Medium difficulty 6697 Switch to English to receive dynamic feedback
Given a string
s, Please find out that there are no duplicate characters in it Longest substrings The length of .
Learn from the code of the great God , A lot of comments are added :
class Solution {
public int lengthOfLongestSubstring(String s) {
int [] last =new int[128]; // Where the character last appeared
for(int i = 0 ; i < 128 ; i++)
{
last[i] = -1; // initialization
}
int res = 0; // Output value
int start = 0; // Starting position
char [] S = s.toCharArray(); //string Turn array
for(int i = 0; i < s.length() ; i++) // Is a loop over the original string
{
int index = S[i]; // Take out the character to be positioned
start = Math.max (start , last[index] + 1);
//start The value is current. start Value and the last occurrence position of the character plus one to get the maximum value
res = Math.max (res , i - start + 1);
//res The value is current. res The value of the space between the value and the character is the maximum
last[index] = i; // Character last The value is updated to the current character position
}
return res;
}
}According to the original code comments , Using the idea of window . It can be understood that there is a box always in the search position , When a duplicate character appears, the box is updated to the next digit after the current character .
java Small knowledge part
topic 1
Take the array length method : Array .length.
topic 2
To operate on a linked list, you need to first operate on an original linked list (root) Define and allocate memory , Then define a linked list to be operated (result), Finally, return to the original linked list (root).
ListNode root = new ListNode();
ListNode result = root;return root.next;The linked list operation needs to generate new nodes , Then link to the existing list . Then let the pointer of the linked list climb backward .
ListNode Next = new ListNode (nextre);
result.next = Next;
result = Next;boolean Type numbers cannot participate in operations , Need to use “(andre >= 10)?1:0” The method is transformed into 1 or 0, Here 1、0 It can also be replaced with other numbers .
Carry out linked list node = List nodes .next when , First judge whether it is empty , namely “if( node != null) node = node .next;”.
topic 3
String turn char Array :
char [] S = s.toCharArray();Take the maximum function :Math.max.
边栏推荐
- [QT] QT multithreading development qthread
- vsCode创建自己的代码模板
- 阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
- [2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
- 5.Oracle-錶空間
- 1.手动创建Oracle数据库
- 【LeetCode】Easy | 20. Valid parentheses
- Find the combination number acwing 888 Find the combination number IV
- How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
- C Primer Plus Chapter 15 (bit operation)
猜你喜欢

Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter

博弈论 AcWing 892. 台阶-Nim游戏

Single chip computer engineering experience - layered idea

confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)

将webApp或者H5页面打包成App

安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel

Paper reading report

Game theory acwing 893 Set Nim game

MySQL advanced part 2: SQL optimization

Vscode configures the typera editor for MD
随机推荐
时间很快,请多做有意义的事情
SolidWorks template and design library are convenient for designers to call
Leetcode divide and conquer / dichotomy
RecyclerView的应用
Game theory acwing 894 Split Nim game
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
Redis-02. Redis command
Design specification for mobile folding screen
There are three kinds of SQL connections: internal connection, external connection and cross connection
【高德地图POI踩坑】AMap.PlaceSearch无法使用
博弈论 AcWing 892. 台阶-Nim游戏
7. Oracle table structure
[QT] QT multithreading development qthread
2022 winter vacation training game 5
博弈论 AcWing 891. Nim游戏
如何正确在CSDN问答进行提问
[Gaode map POI stepping pit] amap Placesearch cannot be used
‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
Record the process of configuring nccl and horovod in these two days (original)