邯郸网站建设效果好,wordpress 新页面打开,怎样做平台网站,做网站为什么要用php框架目录
1. 搜索插入位置 #x1f31f;
2. 结合两个字符串 #x1f31f;
3. 同构字符串 #x1f31f;
#x1f31f; 每日一练刷题专栏 #x1f31f;
Golang每日一练 专栏
Python每日一练 专栏
C/C每日一练 专栏
Java每日一练 专栏 1. 搜索插入位置
给定一个排序数…
目录
1. 搜索插入位置
2. 结合两个字符串
3. 同构字符串 每日一练刷题专栏
Golang每日一练 专栏
Python每日一练 专栏
C/C每日一练 专栏
Java每日一练 专栏 1. 搜索插入位置
给定一个排序数组和一个目标值在数组中找到目标值并返回其索引。如果目标值不存在于数组中返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。
示例 1:
输入: [1,3,5,6], 5
输出: 2
示例 2:
输入: [1,3,5,6], 2
输出: 1
示例 3:
输入: [1,3,5,6], 7
输出: 4
示例 4:
输入: [1,3,5,6], 0
输出: 0
代码
#include bits/stdc.h
using namespace std;class Solution
{
public:int searchInsert(vectorint nums, int target){int lo -1;int hi nums.size();while (lo 1 hi){int mid lo (hi - lo) / 2;if (target nums[mid]){lo mid;}else{hi mid;}}return hi;}
};int main()
{Solution s;vectorint nums {1,3,5,6};cout s.searchInsert(nums, 5) endl;cout s.searchInsert(nums, 2) endl;cout s.searchInsert(nums, 7) endl;cout s.searchInsert(nums, 0) endl;return 0;
}
输出
2 1 4 0
二分查找其它写法
class Solution { public: int searchInsert(vectorint nums, int target) { int left 0, right nums.size() - 1; while (left right) { int mid left (right - left) / 2; if (nums[mid] target) { return mid; } else if (nums[mid] target) { left mid 1; } else { right mid - 1; } } return left; } };
完整代码
#include bits/stdc.h
using namespace std;class Solution {
public:int searchInsert(vectorint nums, int target) {int left 0, right nums.size() - 1;while (left right) {int mid left (right - left) / 2;if (nums[mid] target) {return mid;} else if (nums[mid] target) {left mid 1;} else {right mid - 1;}}return left;}
};int main()
{Solution s;vectorint nums {1,3,5,6};cout s.searchInsert(nums, 5) endl;cout s.searchInsert(nums, 2) endl;cout s.searchInsert(nums, 7) endl;cout s.searchInsert(nums, 0) endl;return 0;
} 2. 结合两个字符串
写一个结合两个字符串的方法从第一个字符串中取出一个字符然后从第二个字符串中取出一个字符以此类推。一旦一个字符串没有字符它就应该继续使用另一个字符串
输入两个字符串如s1day和s2time
输出一个结果字符串对于上面的输入情况它将是“dtaiyme”。
出处
https://edu.csdn.net/practice/23719159
代码
#include iostream
#include string
using namespace std;string StrCon(const string a, const string b)
{string c;int n a.size(), m b.size();if (0 n) return a;if (0 m) return b;int i, j;for (i 0, j 0; i n j m; i, j){c a[i];c b[i];}while (i n)c a[i];while (j m)c b[j];return c;
}int main()
{string s day, t time;cout StrCon(s, t) endl;system(pause);return 0;
}
输出
dtaiyme 3. 同构字符串
给定两个字符串 s 和 t判断它们是否是同构的。
如果 s 中的字符可以按某种映射关系替换得到 t 那么这两个字符串是同构的。
每个出现的字符都应当映射到另一个字符同时不改变字符的顺序。不同字符不能映射到同一个字符上相同字符只能映射到同一个字符上字符可以映射到自己本身。
示例 1:
输入s egg, t add
输出true示例 2
输入s foo, t bar
输出false
示例 3
输入s paper, t title
输出true提示
可以假设 s 和 t 长度相同。
出处
https://edu.csdn.net/practice/23719160
代码
#include bits/stdc.h
using namespace std;class Solution
{
public:bool isIsomorphic(string s, string t){vectorint m(128, -1);for (int i 0; i s.size(); i){if (m[s[i]] ! -1){if (m[s[i]] ! t[i])return false;}else{for (auto v : m){if (v t[i])return false;}m[s[i]] t[i];}}return true;}
};int main()
{Solution sol;string s egg, t add;cout (sol.isIsomorphic(s, t) ? true : false) endl;s foo, t bar;cout (sol.isIsomorphic(s, t) ? true : false) endl;s paper, t title;cout (sol.isIsomorphic(s, t) ? true : false) endl;return 0;
}
输出
true false true 每日一练刷题专栏
✨ 持续努力奋斗做强刷题搬运工 点赞你的认可是我坚持的动力 收藏你的青睐是我努力的方向
✎ 评论你的意见是我进步的财富 Golang每日一练 专栏 Python每日一练 专栏 C/C每日一练 专栏 Java每日一练 专栏