当前位置:网站首页>Daily question brushing record (11)
Daily question brushing record (11)
2022-07-03 06:37:00 【Unique Hami melon】
List of articles
- The first question is : The finger of the sword Offer II 005. Maximum product of word length
- The second question is : The finger of the sword Offer II 007. Array and 0 Three numbers of
- Third question : The finger of the sword Offer II 008. And greater than or equal to target The shortest subarray of
- Fourth question : The finger of the sword Offer II 009. The product is less than K Subarray
- Fifth question : The finger of the sword Offer II 010. And for k Subarray
- Sixth question : The finger of the sword Offer II 011. 0 and 1 The same number of subarrays
The first question is : The finger of the sword Offer II 005. Maximum product of word length
LeetCode: The finger of the sword Offer II 005. Maximum product of word length
describe :
Given an array of strings words, Please calculate when two strings words[i] and words[j] Does not contain the same characters , The maximum of the product of their lengths . Suppose that the string contains only English lowercase letters . If there is no pair of strings that do not contain the same characters , return 0.
Their thinking :
- The length used here is
words.lengthArray ofretTo record every word String to binary format .
for example :ac===>101;ad===>1001;bd===>1010- The method of converting string to binary , Is to traverse every character , Give Way
ret[i] |= (1<<words[i].charAt(j)), It means : The current character appears , Then record its corresponding bit in binary form , Because the characters area~z26 individual , From right to left , As long as there is 1, It means that the current element appears .- Traverse , in pairs
Andoperation , As long as the result is equal to 0 , It means that the two do not contain the same elements . here , Record the length product of the largest two strings that do not contain the same element .
Code implementation :
class Solution {
public int maxProduct(String[] words) {
int[] ret = new int[words.length];
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words[i].length(); j++) {
// As long as this character appears , Just turn this position into 1
ret[i] = ret[i] | (1 << (words[i].charAt(j) - 'a'));
}
}
int ans = 0;
for(int i = 0; i < words.length-1; i++) {
for(int j = i + 1; j < words.length; j++) {
// Pairwise and operation
if((ret[i] & ret[j]) == 0) {
// Record the maximum product
ans = Math.max(ans, words[i].length() * words[j].length());
}
}
}
return ans;
}
}
The second question is : The finger of the sword Offer II 007. Array and 0 Three numbers of
LeetCode: The finger of the sword Offer II 007. Array and 0 Three numbers of
describe :
Given an inclusion n Array of integers nums, Judge nums Are there three elements in a ,b ,c , bring a + b + c = 0 ? Please find all and for 0 And No repetition Triple of .
Their thinking :
- So let's sort it first ,
- First of all, get number i individual Elements , Give Way left = i+1, right = len - 1;
- From the remaining elements , Fetch , as long as
nums[left] + nums[right] == targetJust put this 3 Put elements into the result set , Note here , After joining , To the current left The value of the subscript and right The value of the subscript is de duplicated , In order to avoid adding- Pay attention every time you take elements , When
i > 0When . Be careful to take the same elements repeatedly .nums[i] = nums[i-1]
Code implementation :
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
// Sort
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < nums.length - 2;i++) {
// Here, it is forbidden to take the same elements repeatedly
if(i != 0 && nums[i] == nums[i-1]) continue;
// Because I got it nums[i] To add up to 0, You need to find and be -nums[i] Of
int target = -nums[i];
int left = i + 1;
int right = nums.length-1;
while (left < right) {
if (nums[left] + nums[right] == target) {
// The number of two subscripts here can meet the requirements , Just add these three elements into the set
List<Integer> ret = new ArrayList<>();
ret.add(nums[i]);
ret.add(nums[left]);
ret.add(nums[right]);
ans.add(ret);
// It's here to prevent left and right Repeat the subscript to get the same value , Deduplication
int tmp = nums[left];
while (left < right && nums[left] == tmp){
left++;
}
tmp = nums[right];
while (left < right && nums[right] == tmp) {
right--;
}
}else if (nums[left] + nums[right] < target) {
left++;
}else {
right--;
}
}
}
return ans;
}
}
Third question : The finger of the sword Offer II 008. And greater than or equal to target The shortest subarray of
LeetCode: The finger of the sword Offer II 008. And greater than or equal to target The shortest subarray of
describe :
Given a containing n An array of positive integers and a positive integer target .
Find the sum of the array ≥ target The smallest length of Continuous subarray [numsl, numsl+1, ..., numsr-1, numsr] , And return its length . If there is no sub array that meets the conditions , return 0 .
Their thinking :
- Here we use the sliding window
- Give Way left = 0, right =0, The initial window size is 0
- use total Record nums[right] Value . Judge the present total and target value
- If at present
total >= target, Record the current Subscript distance ( Finally, only the smallest ) , At this time lettotal -= nums[left],left++,- Loop each time
right++- Sliding window size is always maintained
[left,right]- Returns the minimum subscript distance
Code implementation :
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int start = 0;
int end = 0;
int ans = Integer.MAX_VALUE;
int total = 0;
// The size is [start,end] Sliding window of
while (end < nums.length) {
total += nums[end];
while (total >= target) {
ans = Math.min(ans, end-start+1);
total -= nums[start];
start++;
}
end++;
}
return ans==Integer.MAX_VALUE?0:ans;
}
}
Fourth question : The finger of the sword Offer II 009. The product is less than K Subarray
LeetCode: The finger of the sword Offer II 009. The product is less than K Subarray
describe :
Given an array of positive integers nums And integer k , Please find out that the product in the array is less than k The number of successive subarrays of .
Their thinking :
- Use the sliding window to solve problems
- Record the current product each time
ret *= nums[j], Judge whether it is greater than k
- If this is greater than k, Let the left edge of the window move (i++), Record the current subscript
- Return all possible times
Code implementation :
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int i = 0;
int ret = 1;
int ans = 0;
// The sliding window [i,j]
for (int j = 0; j < nums.length; j++) {
ret *= nums[j];
while(i <= j && ret >= k) {
ret /= nums[i];
i++;
}
// This method is used here to calculate and prevent double counting
ans += j - i + 1;
}
return ans;
}
}
Fifth question : The finger of the sword Offer II 010. And for k Subarray
LeetCode: The finger of the sword Offer II 010. And for k Subarray
describe :
Given an array of integers and an integer k , Please find the array with k The number of consecutive subarrays of .
Their thinking :
- Here we use hash table , Record the value of the current sum ret.
- Judge Is there an element
ret-k, This adds up to k, Record the current value value- Note here k = nums[i] The situation of , You need to add in advance 0 The situation of .
Code implementation :
class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<>();
int ans = 0;
map.put(0,1);
int ret = 0;
for(int i = 0; i < nums.length; i++) {
ret += nums[i];
if (map.containsKey(ret - k)) {
ans += map.get(ret - k);
}
map.put(ret,map.getOrDefault(ret,0)+1);
}
return ans;
}
}
Sixth question : The finger of the sword Offer II 011. 0 and 1 The same number of subarrays
LeetCode: The finger of the sword Offer II 011. 0 and 1 The same number of subarrays
describe :
Given a binary array nums , Find the same number of 0 and 1 The longest continuous subarray of , And returns the length of the subarray .
Their thinking :
- First, let all in the array be 0 The element becomes -1, In this way, we only need to judge whether the sum is 0, We can know 0 and 1 Whether the number of is the same .
- If the current element does not exist in the hash table , Is added to the hash table ,value The value is the current subscript
- If the current element exists in the hash table , Record the current maximum subscript difference
Code implementation :
class Solution {
public int findMaxLength(int[] nums) {
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 0) nums[i] = -1;
}
Map<Integer,Integer> map = new HashMap<>();
map.put(0,-1);
int ans = 0;
int target = 0;
for(int i = 0; i < nums.length; i++) {
target += nums[i];
if (map.containsKey(target)) {
ans = Math.max(ans,i-map.get(target));
}else {
map.put(target,i);
}
}
return ans;
}
}
边栏推荐
- The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
- ODL framework project construction trial -demo
- Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样
- Know flex box
- ROS+Pytorch的联合使用示例(语义分割)
- 【code】偶尔取值、判空、查表、验证等
- “我为开源打榜狂”第一周榜单公布,160位开发者上榜
- Support vector machine for machine learning
- 輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
- 致即将毕业大学生的一封信
猜你喜欢

Install VM tools

Advanced technology management - do you know the whole picture of growth?

Summary of UI module design and practical application of agent mode

ssh链接远程服务器 及 远程图形化界面的本地显示

Use selenium to climb the annual box office of Yien

有意思的鼠標指針交互探究

Local rviz call and display of remote rostopic

Project summary --04

輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
![[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle](/img/f8/0e3fbfd13bf06291a73200552ff17a.png)
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
随机推荐
Kubesphere - build Nacos cluster
Print time Hahahahahaha
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
Local rviz call and display of remote rostopic
Derivation of variance iteration formula
Example of joint use of ros+pytoch (semantic segmentation)
Common interview questions
[untitled] 5 self use history
Some thoughts on machine learning
Learning notes -- principles and comparison of k-d tree and IKD tree
2022 CISP-PTE(三)命令执行
Printer related problem record
冒泡排序的简单理解
Use selenium to climb the annual box office of Yien
论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
JMeter performance automation test
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
Summary of the design and implementation of the weapon system similar to the paladin of vitality
Project summary --2 (basic use of jsup)
Yolov3 learning notes
