当前位置:网站首页>June brush question 01 - array
June brush question 01 - array
2022-07-06 09:38:00 【A Guang chasing dreams】
Brush questions in June 01—— Array
Today's brush topic content : Array
Preface
- Update the problem solution content of the problem brush every day
- Focus on personal understanding , Look at the difficulty and update the number of questions
- The title comes from Li Kou
- Try to work out at least one question every day
- Language java、python、c\c++
One 、 Today's topic
- 1588. The sum of all odd length subarrays
- 1848. Minimum distance to target element
- 1652. Dismantle the bomb
- 1640. Can you join to form an array
Two 、 Their thinking
1. 1588. The sum of all odd length subarrays
- Require the sum of all odd subsequences
- That is, start traversing from each element , Add the current element sum to the result when it is an odd number of elements
- Return the result
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int i = 0, j, count = 0, ans = 0;
int n = arr.length;
while( i < n){
j = i;
count = 0;
while(j < n){
count += arr[j];
if ((j - i + 1) % 2 == 1){
ans += count;
}
j++;
}
i++;
}
return ans;
}
}
2. 1848. Minimum distance to target element
- Find the subscript of the element , Maintain a minimum value
class Solution {
public int getMinDistance(int[] nums, int target, int start) {
int i;
int n = nums.length;
int ret = 10010;
for (i = 0; i < n; ++i){
if (nums[i] == target){
ret = Math.min(ret, Math.abs(i - start));
}
}
return ret;
}
}
3. 1652. Dismantle the bomb
- according to
kThere are three cases for the value of , For the sake of understanding , Directly mark the three situations- If
kby 0 Back to full 0 that will do- If
kGreater than or less than 0, There are two scenarios , The expansion here is larger than 0 Of
- If
i+kGreater than the rightmost subscript of the array , It will return to the first... From the beginning of the arrayi + k - nA place- If it is less than the rightmost subscript of the array , Then add it directly
- Less than 0 It's the same thing
class Solution {
public int[] decrypt(int[] code, int k) {
int i, n = code.length;
int t = k;
int[] ret = new int[n];
if (k == 0){
return ret;
}else if (k < 0){
for (i = 0; i < n; ++i){
int temp = 0;
while(k < 0){
if (i + k < 0){
temp += code[n + i + k];
}else{
temp += code[i + k];
}
k++;
}
k = t;
ret[i] = temp;
}
}else{
for (i = 0; i < n; ++i){
int temp = 0;
while(k > 0){
if (i + k > n - 1){
temp += code[i + k - n];
}else{
temp += code[i + k];
}
k--;
}
k = t;
ret[i] = temp;
}
}
return ret;
}
}
4. 1640. Can you join to form an array
There are two ways of thinking about this question
Train of thought
The first is to use hash table
1, Convert all subsets of the two-dimensional array into strings as
keyIn the hash table , Defaultvaluebyfalse
2. Traversing a one-dimensional array , If the current number exists in the... Of the hash tablekeyin , WillvalueSet astrue
3. Splice the current numbers in order , Judge whether there is inkeyin , If it exists, repeat step 2
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
// Define a hash table
HashMap<String, Boolean> map = new HashMap<>();
int i = 0, j, n = arr.length;
// Convert all subsets in the two-dimensional array into strings and store them in the hash table
for(int[] item: pieces){
String s = Integer.toString(item[0]);
for(int k = 1; k < item.length; k++){
s += Integer.toString(item[k]);
}
map.put(s, false); // By default value Set as false
}
while(i < n){
String s = Integer.toString(arr[i]);
// If it is a single subset ( There's only one number )
if (map.containsKey(s)){
map.put(s, true);
}else{
// Is a subset of multiple numbers
for(j = i+1; j < n; j++){
s += Integer.toString(arr[j]);
if (map.containsKey(s)){
map.put(s, true);
i = j;
break;
}
}
}
i++;
}
// Traverse to see if there is still false Value
for (Boolean b: map.values()){
if (!b) return false;
}
return true;
}
}
Train of thought two :
- Traversing a one-dimensional array , Use one flag Identify the return result
- Find an equal number from the second array , It must be arranged in order
- according to flag Judge the result
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
int i = 0, j, k;
boolean flag;
while (i < arr.length){
flag = false;
for(j = 0; j < pieces.length; j++){
// If the first is not equal to arr[i] Then skip
if (pieces[j][0] != arr[i]){
continue;
}
// First equals arr[i] when , Judge pieces[j] Whether the subset of and arr The next number corresponds to
for(k = 0; k < pieces[j].length; k++){
if (pieces[j][k] == arr[i]){
i++;
flag = true;
}else{
return false;
}
}
// If at this time i The traversal is complete
if (i == arr.length){
return true;
}
}
if(!flag){
return false;
}
}
return true;
}
}
边栏推荐
- 发生OOM了,你知道是什么原因吗,又该怎么解决呢?
- Mapreduce实例(五):二次排序
- 面试突击62:group by 有哪些注意事项?
- Scoped in webrtc_ refptr
- 基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
- QML type: overlay
- MySQL数据库优化的几种方式(笔面试必问)
- 068. Find the insertion position -- binary search
- Withdrawal of wechat applet (enterprise payment to change)
- Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
![[deep learning] semantic segmentation: paper reading: (2021-12) mask2former](/img/dd/fe2bfa3563cf478afe431ac87a8cb7.png)
[deep learning] semantic segmentation: paper reading: (2021-12) mask2former

MapReduce instance (x): chainmapreduce

Mapreduce实例(九):Reduce端join

Blue Bridge Cup_ Single chip microcomputer_ PWM output

O & M, let go of monitoring - let go of yourself
![《ASP.NET Core 6框架揭秘》样章发布[200页/5章]](/img/4f/5688c391dd19129d912a3557732047.jpg)
《ASP.NET Core 6框架揭秘》样章发布[200页/5章]

解决小文件处过多

Le modèle sentinelle de redis

Kratos战神微服务框架(一)
![[three storage methods of graph] just use adjacency matrix to go out](/img/79/337ee452d12ad477e6b7cb6b359027.png)
[three storage methods of graph] just use adjacency matrix to go out
随机推荐
leetcode-14. Longest common prefix JS longitudinal scanning method
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
Mathematical modeling 2004b question (transmission problem)
Connexion d'initialisation pour go redis
五月集训总结——来自阿光
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
[Yu Yue education] Wuhan University of science and technology securities investment reference
One article read, DDD landing database design practice
Activiti7工作流的使用
【深度學習】語義分割-源代碼匯總
Withdrawal of wechat applet (enterprise payment to change)
Redis之主从复制
QML control type: menu
[shell script] use menu commands to build scripts for creating folders in the cluster
DCDC power ripple test
Redis之性能指标、监控方式
Compilation of libwebsocket
Kratos战神微服务框架(二)
MapReduce instance (x): chainmapreduce
Kratos战神微服务框架(一)