04、双指针练习题

01、平方数之和leetcode633

给定一个非负整数c,你要判断是否存在两个整数a和b,使得a*a + b*b = c

#include<iostream>
#include<vector>
using namespace std;

bool SumOfSquareNumbers(int& c, int& a, int& b) {
  bool ret = false;
  int left = 0;
  int right = sqrt(c);
  int sum = 0;
  while (left <= right) {
	sum = left * left + right * right;
	if (sum == c) {
	  ret = true;
	  break;
	}
	else if (sum < c) {
	  left++;
	}
	else {
	  right--;
	}
  }
  a = left;
  b = right;
  return ret;
}

void main() {
  int a = 0;
  int b = 0;
  int c = 18;
  if (SumOfSquareNumbers(c, a, b)) {
	cout << "the value is :" << a << "  and  " << b << endl;
  }
  else {
	cout << "the have no value!" << endl;
  }
}

02、给你一个字符串s,最多可以从中删除一个字符。请你判断s是否能够成为回文字符串:如果能返回true,否则,返回false。leetcode680

回文字符串指的是顺着读和倒着读内容都一样,

#include<iostream>
#include<vector>
#include<string>
using namespace std;


bool JudgePalindrome(string& s, int l, int r) {
  while (l < r) {
	if (s[l] != s[r]) {
	  return false;
	}
	l++;
	r--;
  }
  return true;
}

bool ValidPalindrome(string& s) {
  int l = 0; int r = s.length() - 1;
  while (l < r) {
    if (s[l] == s[r]) {
	  l++;
	  r--;
    }
    else {
	  return JudgePalindrome(s, l + 1, r) ||
	    JudgePalindrome(s, l, r - 1);
    }
  }
  return true;
}


void main() {
  string input = "abc";
  cout << "it is OK or NO ? :" << ValidPalindrome(input) << endl;

}

3、通国删除字母匹配到字典里最长的单词 leetcode524

给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary 中最长的字符串,该字符串可以通过删除 s 中的某些字符得到。

如果答案不止一个,返回长度最长且字母序最小的字符串。如果答案不存在,则返回空字符串。

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

bool IsSubSequence(string& s, string& t) {
  int s_len = s.length();
  int t_len = t.length();
  if (t_len > s_len) {
	return false;
  }
  int i = 0;
  for (char ch : t) {
	while (i < s_len && s[i] != ch) {
	  i++;
	}
	if(i> s_len){
	  return false;
	}
	i++;
  }
  return true;
}

bool compare(string &s1,string& s2) {
  return s1.length() > s2.length();
}

string FindLongestWord(string s, vector<string> input) {
  sort(input.begin(), input.end(), compare);
  for (auto t : input) {
	if (IsSubSequence(s, t)) {
	  return t;
	}
  }
  return "";
}

void main() {
  vector<string> input = { "ale","apple","monkey","plea" };
  string s = "abpcplea";
  cout << "the result is : " << FindLongestWord(s, input) << endl;
}

4、至多包含K个不同字符的最长子串 leetcode340

给定一个字符串s,找出至多包含k个不同字符的最长子串T

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
using namespace std;



int lengthOfLongestSubstringKDistinct(string s, int k) {
  unordered_map<char, int> map;
  if (k == 0) { return 0; }
  int n = s.size();
  int ans = 0;
  for (int l = 0, r = 0; r < n; r++) {
    map[s[r]]++;
    while (map.size() > k) {
      map[s[l]]--;
      if (map[s[l]] == 0) { map.erase(s[l]); }
      l++;
    }
    ans = max(ans, r - l + 1);
  }
  return ans;
}



void main() {
  vector<string> input = { "ale","apple","monkey","plea" };
 
  string s = "eccba"; int k = 2;
  lengthOfLongestSubstringKDistinct(s, k);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值