[LeetCode C++实现]443. String Compression

刷完上一道题目字符串压缩,看到leetcode中的443题,基本上与字符串压缩这道题一致。

class Solution {

public:

int compress(vector<char>& chars) {

int n = chars.size();

//location to write and character count

int loc = 0,cnt = 1;

if(n < 2) return n;

for(int i = 1 ;i < n ;i++)

{

if(chars[i] == chars[i-1])

{

cnt++;

}else{......

[程序员面试金典 C++实现]面试题 01.06. 字符串压缩

老规矩直接根据最直观的方法进行求解,遇到不通过的用例分析存在的问题,AC代码如下:

class Solution {

public:

string compressString(string S) {

int cnt = 1,n = S.size();

if(n <= 2) return S;

string res;

for(int i = 1;i < n;i++)

{

if(S[i] == S[i-1])

{

cnt += 1;

if(i == n-1)

{

res += S[i-1];

res += to_string(cnt);

}

} else if(S[i] !=......

[程序员面试金典 C++实现]面试题01.05 一次编辑

打开力扣看一道leetcode题目描述,发现力扣中集成了程序面试金典中的题目,一口气刷了4道,直到遇见这道面试题01.05.一次编辑.

暴力求解

我本以为这道题应该也能一遍AC,结果却卡了半个多小时,未能AC的代码如下:

class Solution {

public:

bool oneEditAway(string first, string second) {

int m = first.size(),n = second.size();

//相等返回true

if(first == second) return true;

//长度相差大于1

if(abs(m - n) &......

[LeetCode C++实现]209. Minimum Size Subarray Sum

老规矩先使用暴力破解方法,穷举每一种可能的起点,然后累加直到大于等于target。

class Solution {

public:

int minSubArrayLen(int target, vector<int>& nums) {

int n = nums.size(),ans = INT_MAX;

if(n == 0) return 0;

for(int i = 0;i < n;i++)

{

int sum = 0;

for(int j = i;j < n;j++)

{

sum += nums[j];

if(sum >= target)

{

......

[LeetCode C++实现]3. Longest Substring Without Repeating Characters

通过unordered_set来判断字符是否出现过,通过两个指针维护无重复字符子串,如果字符没有出现过,则将字符插入unorder_set,否则不断的移除左指针指向的元素,直到右指针指向的元素不在unordered_set中。AC代码如下:

class Solution {

public:

int lengthOfLongestSubstring(string s) {

unordered_set<char> hash_set;

int i = 0,j = 0,maxlen = 0,n = s.size();

while(i<n && j<n){

......

[LeetCode C++实现]718. Maximum Length of Repeated Subarray

这道题时最长公共子序列的一个变种,对这道题目印象非常的深,原因是以前面试的时候被这道题目卡住了,老规矩我们先尝试暴力破解方法。

class Solution {

public:

int findLength(vector<int>& A, vector<int>& B) {

int maxlen = 0;

for(int i = 0;i < A.size();i++)

{

for(int j = 0;j < B.size();j++)

{

if(A[i] == B[j])

{

int k = 1;

while((i + k < A......

[LeetCode C++实现]34. Find First and Last Position of Element in Sorted Array

经典的二分查找题目,以前刷题的时候知道一个小技巧,计算mid的时候不是相加除以2,而是l + (r-l)/2这样可以避免l+r溢出,还有针对数组{1,2,2,2,3,4,5}这种,查询最左边的2或者最右边的2.AC的解法参考了评论区大神代码,自己实现了stl里的lower_bound,用于查找第一个不小于给定元素的位置。如果存在相同元素当然命中的是最左边的元素。AC解法巧妙的使用lower_bound来实现upper_bound.

class Solution {

public:

vector<int> searchRange(vector<int>& n......

[LeetCode C++实现]752. Open the Lock

BFS经典套路题目,AC code如下:

class Solution {

public:

int openLock(vector<string>& deadends, string target) {

unordered_set<string>deadset(deadends.begin(),deadends.end());

unordered_set<string>visited;

visited.insert("0000");

queue<string> q;

q.push("0000");

int step=0;

whi......

[LeetCode C++实现]583. Delete Operation for Two Strings

class Solution {

public:

int minDistance(string word1, string word2) {

int m = word1.size(),n = word2.size();

int l = lcs(word1,word2,m,n);

return m + n -2 * l;

}

private:

int lcs(string word1,string word2,int m,int n) {

if(m == 0 || n == 0) return 0;

if(word1[m - 1] == word2[n - 1]) {

return 1 + l......

[LeetCode C++实现]1663. Smallest String With A Given Numeric Value

贪心算法,从最后向前填充构造字典序列最小值,字符串初始为n个'a',这样在保证了字符串长度,如果不预先从k中减去n个a,那么在我们计算过程中,有可能会生成小于n个字符的结果,处理起来比较麻烦。

class Solution {

public:

//计算生成字典序最小字符串,贪心算法,从后面开始先使用最大z

string getSmallestString(int n, int k) {

string res(n,'a');

k -= n;

int i = n - 1;

while(k > 0) {

int fill = min(k,25);

k -= 25;

re......