盡可能使字符串相等
給你兩個長度相同的字符串,s 和 t。
將 s 中的第 i 個字符變到 t 中的第 i 個字符需要 |s[i] - t[i]| 的開銷(開銷可能為 0),也就是兩個字符的 ASCII 碼值的差的絕對值。
用于變更字符串的最大預算是 maxCost。在轉化字符串時,總開銷應當小于等于該預算,這也意味著字符串的轉化可能是不完全的。
如果你可以將 s 的子字符串轉化為它在 t 中對應的子字符串,則返回可以轉化的最大長度。
如果 s 中沒有子字符串可以轉化成 t 中對應的子字符串,則返回 0。
示例 1:
輸入:s = “abcd”, t = “bcdf”, cost = 3
輸出:3
解釋:s 中的 “abc” 可以變為 “bcd”。開銷為 3,所以最大長度為 3。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/get-equal-substrings-within-budget
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
int equalSubstring(char * s
, char * t
, int maxCost
){int start
= 0;int cnt
= 0;int len
= strlen(s
);int i
;for(i
=0;i
<len
;i
++){cnt
+= abs(s
[i
]-t
[i
]);if(cnt
>maxCost
){cnt
-= abs(s
[start
]-t
[start
]);start
++;}}return len
-start
;
}
刪除字符串中的所有相鄰重復項 II
給你一個字符串 s,「k 倍重復項刪除操作」將會從 s 中選擇 k 個相鄰且相等的字母,并刪除它們,使被刪去的字符串的左側和右側連在一起。
你需要對 s 重復進行無限次這樣的刪除操作,直到無法繼續為止。
在執行完所有刪除操作后,返回最終得到的字符串。
本題答案保證唯一。
示例 1:
輸入:s = “abcd”, k = 2
輸出:“abcd”
解釋:沒有要刪除的內容。
示例 2:
輸入:s = “deeedbbcccbdaa”, k = 3
輸出:“aa”
解釋:
先刪除 “eee” 和 “ccc”,得到 “ddbbbdaa”
再刪除 “bbb”,得到 “dddaa”
最后刪除 “ddd”,得到 “aa”
char * removeDuplicates(char * s
, int k
){int len
;int start
;int i
,j
;char flag
= 0;len
= strlen(s
);start
= 0;for(i
=0;i
<len
;){for(j
=i
;j
<i
+k
;j
++){if((j
+k
)>len
) break;if(s
[j
]!=s
[i
]){break;}}if(j
==i
+k
){i
= j
;flag
= 1;}else{s
[start
++]=s
[i
++];}} s
[start
]='\0';if(flag
== 0){return s
;}return removeDuplicates(s
,k
);
}
char * removeDuplicates(char * s
, int k
){int len
;int cnt
=0;int i
,j
;int di
;char flag
= 0;len
= strlen(s
);for(di
=0,i
=0;i
<len
;i
++,di
++){s
[di
] = s
[i
];if(di
==0){cnt
= 1;}else if(s
[di
-1]!=s
[di
]){cnt
= 1;}else{cnt
++;}if(cnt
==k
){di
= di
-k
;cnt
=1;flag
=1;}}s
[di
]='\0';if(flag
== 0) return s
;return removeDuplicates(s
,k
);
}
形成目標數組的子數組最少增加次數
給你一個整數數組 target 和一個數組 initial ,initial 數組與 target 數組有同樣的維度,且一開始全部為 0 。
請你返回從 initial 得到 target 的最少操作次數,每次操作需遵循以下規則:
在 initial 中選擇 任意 子數組,并將子數組中每個元素增加 1 。
答案保證在 32 位有符號整數以內。
示例 1:
輸入:target = [1,2,3,2,1]
輸出:3
解釋:我們需要至少 3 次操作從 intial 數組得到 target 數組。
[0,0,0,0,0] 將下標為 0 到 4 的元素(包含二者)加 1 。
[1,1,1,1,1] 將下標為 1 到 3 的元素(包含二者)加 1 。
[1,2,2,2,1] 將下表為 2 的元素增加 1 。
[1,2,3,2,1] 得到了目標數組。
int minNumberOperations(int* target
, int targetSize
){int n
= targetSize
;int i
,d
=0;int t
=target
[0];for(i
=1;i
<n
;i
++){d
= target
[i
]-target
[i
-1];if(d
>0)t
+= d
;}return t
;
}
最小覆蓋子串
給你一個字符串 s 、一個字符串 t 。返回 s 中涵蓋 t 所有字符的最小子串。如果 s 中不存在涵蓋 t 所有字符的子串,則返回空字符串 “” 。
注意:如果 s 中存在這樣的子串,我們保證它是唯一的答案。
示例 1:
輸入:s = “ADOBECODEBANC”, t = “ABC”
輸出:“BANC”
示例 2:
輸入:s = “a”, t = “a”
輸出:“a”
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/minimum-window-substring
char * minWindow(char * s
, char * t
){int tc
['z'-'A'+1]={0};int tlen
=0;int i
;for(i
=0;t
[i
]!='\0';i
++){tc
[t
[i
]-'A'] += 1;tlen
+= 1;}char index
;int left
=0;int minleft
=0,minright
=-1;for(i
=0;s
[i
]!='\0';i
++){index
= s
[i
]-'A';if(tc
[index
]>0){tlen
--;}tc
[index
]--;if(tlen
==0){while(tc
[s
[left
]-'A']<0){tc
[s
[left
]-'A']++;left
++;}if((minright
==-1) || (i
-left
< minright
-minleft
)){minleft
= left
;minright
= i
;}tc
[s
[left
]-'A']++;tlen
++;left
++;}}s
[minright
+1]='\0';return s
+minleft
;
}
替換子串得到平衡字符串
有一個只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四種字符,且長度為 n 的字符串。
假如在該字符串中,這四個字符都恰好出現 n/4 次,那么它就是一個「平衡字符串」。
給你一個這樣的字符串 s,請通過「替換一個子串」的方式,使原字符串 s 變成一個「平衡字符串」。
你可以用和「待替換子串」長度相同的 任何 其他字符串來完成替換。
請返回待替換子串的最小可能長度。
如果原字符串自身就是一個平衡字符串,則返回 0。
示例 1:
輸入:s = “QWER”
輸出:0
解釋:s 已經是平衡的了。
示例 2:
輸入:s = “QQWE”
輸出:1
解釋:我們需要把一個 ‘Q’ 替換成 ‘R’,這樣得到的 “RQWE” (或 “QRWE”) 是平衡的。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/replace-the-substring-for-balanced-string
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
int balancedString(char * s
){int cn
['W'-'E'+1]={0};int i
,j
;int left
=0;int len
=0;int avg
= 0;for(i
=0;s
[i
]!='\0';i
++){printf("%c",s
[i
]);cn
[s
[i
]-'E']++;len
++;}avg
= len
>>2;int min
= len
;for(i
=0;s
[i
]!='\0';i
++){cn
[s
[i
]-'E']--;while((cn
['Q'-'E']<=avg
) && (cn
['W'-'E']<=avg
) &&(cn
['E'-'E']<=avg
) && (cn
['R'-'E']<=avg
) ){if(i
-left
+1 < min
){min
= i
-left
+1;}if(left
>i
){break;}cn
[s
[left
]-'E']++;left
++;}}return min
;
}
int balancedString(char * s
){int cn
['W'-'E'+1]={0};int i
;int left
=0;int len
=0;int avg
= 0;for(i
=0;s
[i
]!='\0';i
++){printf("%c",s
[i
]);cn
[s
[i
]-'E']++;len
++;}avg
= len
>>2;int tlen
= 0;for(i
='E';i
<='W';i
++){if(cn
[i
-'E']>avg
){tlen
= tlen
+cn
[i
-'E']-avg
;}}int min
= len
;for(i
=0;s
[i
]!='\0';i
++){if(cn
[s
[i
]-'E'] > avg
){tlen
--;}cn
[s
[i
]-'E']--;if(tlen
==0){while((left
<=i
) && (cn
[s
[left
]-'E']<avg
)){cn
[s
[left
]-'E']++;left
++;}if(i
-left
+1<min
){min
= i
-left
+1;}if(left
<=i
){cn
[s
[left
]-'E']++;tlen
++;left
++;}}}return min
;
}
非遞減數列
給你一個長度為 n 的整數數組,請你判斷在 最多 改變 1 個元素的情況下,該數組能否變成一個非遞減數列。
我們是這樣定義一個非遞減數列的: 對于數組中所有的 i (0 <= i <= n-2),總滿足 nums[i] <= nums[i + 1]。
示例 1:
輸入: nums = [4,2,3]
輸出: true
解釋: 你可以通過把第一個4變成1來使得它成為一個非遞減數列。
bool
checkPossibility(int* nums
, int numsSize
){int i
=0;int n
= numsSize
;int cnt
=0;for(i
=1;i
<numsSize
;i
++){if(nums
[i
-1]>nums
[i
]){cnt
++;if(cnt
>1){return false
;}if(i
==1 || i
==numsSize
-1){continue;}if(nums
[i
-2]>nums
[i
] && nums
[i
-1]>nums
[i
+1]){return false
;}}}return true
;
}
數組的度
給定一個非空且只包含非負數的整數數組 nums,數組的度的定義是指數組里任一元素出現頻數的最大值。
你的任務是在 nums 中找到與 nums 擁有相同大小的度的最短連續子數組,返回其長度。
示例 1:
輸入:[1, 2, 2, 3, 1]
輸出:2
解釋:
輸入數組的度是2,因為元素1和2的出現頻數最大,均為2.
連續子數組里面擁有相同度的有如下所示:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
最短連續子數組[2, 2]的長度為2,所以返回2.
int cnt
[50000];int findShortestSubArray(int* nums
, int numsSize
){int i
;int ret
=numsSize
;int maxcnt
=0;int r
=0;int l
=0;memset(cnt
,0,sizeof(cnt
));for(i
=0;i
<numsSize
;i
++){cnt
[nums
[i
]]++;maxcnt
= fmax(maxcnt
, cnt
[nums
[i
]]);}memset(cnt
,0,sizeof(cnt
));for(r
=0;r
<numsSize
;r
++){cnt
[nums
[r
]]++;while(cnt
[nums
[r
]]==maxcnt
){ret
=fmin(ret
,r
-l
+1);cnt
[nums
[l
]]--;l
++;}}return ret
;
}
檢查兩個字符串數組是否相等
給你兩個字符串數組 word1 和 word2 。如果兩個數組表示的字符串相同,返回 true ;否則,返回 false 。
數組表示的字符串 是由數組中的所有元素 按順序 連接形成的字符串。
示例 1:
輸入:word1 = [“ab”, “c”], word2 = [“a”, “bc”]
輸出:true
解釋:
word1 表示的字符串為 “ab” + “c” -> “abc”
word2 表示的字符串為 “a” + “bc” -> “abc”
兩個字符串相同,返回 true
bool
arrayStringsAreEqual(char ** word1
, int word1Size
, char ** word2
, int word2Size
){int i1
=0,i2
=0;int j1
=0,j2
=0;bool ret
= false
;while(i1
<word1Size
&& i2
<word2Size
){if(word1
[i1
][j1
]!=word2
[i2
][j2
]){break;}j1
++;if(word1
[i1
][j1
]=='\0'){i1
++;j1
=0;}j2
++;if(word2
[i2
][j2
]=='\0'){i2
++;j2
=0;}}if(i1
==word1Size
&& i2
==word2Size
){ret
= true
;}return ret
;
}
具有給定數值的最小字符串
小寫字符 的 數值 是它在字母表中的位置(從 1 開始),因此 a 的數值為 1 ,b 的數值為 2 ,c 的數值為 3 ,以此類推。
字符串由若干小寫字符組成,字符串的數值 為各字符的數值之和。例如,字符串 “abe” 的數值等于 1 + 2 + 5 = 8 。
給你兩個整數 n 和 k 。返回 長度 等于 n 且 數值 等于 k 的 字典序最小 的字符串。
注意,如果字符串 x 在字典排序中位于 y 之前,就認為 x 字典序比 y 小,有以下兩種情況:
x 是 y 的一個前綴;
如果 i 是 x[i] != y[i] 的第一個位置,且 x[i] 在字母表中的位置比 y[i] 靠前。
示例 1:
輸入:n = 3, k = 27
輸出:“aay”
解釋:字符串的數值為 1 + 1 + 25 = 27,它是數值滿足要求且長度等于 3 字典序最小的字符串。
char * getSmallestString(int n
, int k
){char *res
;int rest
,bound
;res
= (char *)malloc(100000);memset(res
,0,100000);for(rest
=n
;rest
>=1;rest
--){bound
= k
-26*(rest
-1);if(bound
>0){res
[n
-rest
] = bound
+'a'-1;k
-= bound
;}else{res
[n
-rest
] = 'a';k
-=1;}}return res
;
}
char res
[100000]={0};char * getSmallestString(int n
, int k
){int i
;int zs
;int lev
;memset(res
,'a',n
);res
[n
]='\0';zs
= (k
-n
)/25;lev
= (k
-n
)%25;for(i
=1;i
<=zs
;i
++){res
[n
-i
]='z';}if(lev
!=0){res
[n
-i
] += lev
;}return res
;
}
生成平衡數組的方案數
給你一個整數數組 nums 。你需要選擇 恰好 一個下標(下標從 0 開始)并刪除對應的元素。請注意剩下元素的下標可能會因為刪除操作而發生改變。
比方說,如果 nums = [6,1,7,4,1] ,那么:
選擇刪除下標 1 ,剩下的數組為 nums = [6,7,4,1] 。
選擇刪除下標 2 ,剩下的數組為 nums = [6,1,4,1] 。
選擇刪除下標 4 ,剩下的數組為 nums = [6,1,7,4] 。
如果一個數組滿足奇數下標元素的和與偶數下標元素的和相等,該數組就是一個 平衡數組 。
請你返回刪除操作后,剩下的數組 nums 是 平衡數組 的 方案數 。
示例 1:
輸入:nums = [2,1,6,4]
輸出:1
解釋:
刪除下標 0 :[1,6,4] -> 偶數元素下標為:1 + 4 = 5 。奇數元素下標為:6 。不平衡。
刪除下標 1 :[2,6,4] -> 偶數元素下標為:2 + 4 = 6 。奇數元素下標為:6 。平衡。
刪除下標 2 :[2,1,4] -> 偶數元素下標為:2 + 4 = 6 。奇數元素下標為:1 。不平衡。
刪除下標 3 :[2,1,6] -> 偶數元素下標為:2 + 6 = 8 。奇數元素下標為:1 。不平衡。
只有一種讓剩余數組成為平衡數組的方案。
int waysToMakeFair(int* nums
, int numsSize
){int h1
=0,h2
=0;int t1
=0,t2
=0;int i
;int cnt
=0;for(i
=0;i
<numsSize
;i
++){if((i
&1)==0){t2
+=nums
[i
];}else{t1
+=nums
[i
];}}for(i
=0;i
<numsSize
;i
++){if((i
&1)==0){t2
-=nums
[i
];if(h2
+t1
==h1
+t2
){cnt
++;}h2
+=nums
[i
];}else{t1
-=nums
[i
];if(h2
+t1
==h1
+t2
){cnt
++;}h1
+=nums
[i
];}}return cnt
;
}
總結
以上是生活随笔為你收集整理的编程练习的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。