1: sum of two numbers
topic:
Given an integer array nums and an integer target value target, please find the two integers whose sum is the target value target in the array, and return their array subscripts.
You can assume that there is only one answer for each input. However, the same element in the array cannot appear repeatedly in the answer.
You can return answers in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Code answer:
class Solution {
public int[] twoSum(int[] nums, int target) { //Introduce array and result object
int n=nums.length; //Get the length of the array
for(int i=0;i<n;i++){ //The index subscript of the first number, starting from itself and matching its next number,
// Because it cannot contain itself, the index range of the number is [0, length-1]
for(int j=i+1;j<n;j++){ //The second number is deduced from the order, +1 is because it is the next digit of the index of the previous number
if( nums[i]+nums[j]==target){ //Confirm whether the sum of the values corresponding to the indices of the two numbers is equal to the target target
return new int[]{i, j}; //output
} }}
return new int[0]; //return }}
2: Add two numbers
topic:
You are given two non-empty linked lists representing two non-negative integers. Each of them is stored in reverse order, and each node can only store one digit.
Please add two numbers and return a linked list representing the sum in the same form.
You can assume that neither number starts with a zero other than the number zero.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Code answer:
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(); //Create a virtual linked list to store the result linked list
ListNode curr = dummy; //virtual linked list
int carry = 0; // carry is 0
while (l1 != null || l2 != null) {
int x = l1 == null ? 0 : l1.val; //If the l1 linked list is 0, it is 0, otherwise let x be the specific value at the current position of the l1 linked list
int y = l2 == null ? 0 : l2.val; //similar
int total = x + y + carry; //What is the addition of the numbers at the corresponding positions on the two linked lists plus the carry from the previous one
curr.next = new ListNode(total % 10); //Find the remainder and place it in the next link of the current position
curr = curr.next;
carry = total / 10; //Seeking the quotient, as a carry for the next use
if (l1 != null) l1 = l1.next; //If the linked list is not null, keep going down
if (l2 != null) l2 = l2.next; //similar
}
if (carry != 0) curr.next = new ListNode(carry);
return dummy.next; //Return the linked list starting from the next bit of the created virtual linked list
}
}
3: the maximum value of no repeated string
topic:
Given a string s, please find the length of the longest substring that does not contain repeated characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: Since the longest substring without repeating characters is "abc", its length is 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: Since the longest substring without repeating characters is "b", its length is 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: Since the longest substring without repeating characters is "wke", its length is 3.
Note that your answer must be the length of the substring, "pwke" is a subsequence, not a substring.
Code answer:
/*
*designed by xiaoxiong
that Aug 11 2022
*/
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(); //Get the total length of the string
int ans = 0; //initialization result 0
Set<Character> set = new HashSet<>(); //use hash to store
int left = 0, right = 0; //Initialize left and right indexes
while(right<n){
//back the mark charstr, the index range is [0, char.length-1]
if(!set.contains(s.charAt(right))){ //As long as there is no duplicate, put it in the set
//Judge whether the right side of the intercepted string contains the one that has been slid on the left!
set.add(s.charAt(right)); //Not included, add this character to the length of the intercepted string
right++;
} else { //If it is included, that is, if it encounters a duplicate, calculate the longest result this time, and the window will slide back
ans = Math.max(ans, right - left);
set.remove(s.charAt(left));//clearthecharbetweentheleftandright
left++;
}
}
ans = Math.max(ans, set.size()); //There are no repeated characters from left to right or the length of the last string is the largest
return ans;
}
}
ps: For the sake of beauty, just put a picture of today's quiz!