小学网站建设及使用,售卖链接,沈阳做网站软件,淄川响应式网站建设目录
034:删除公共字符串
035:两个链表的第一个公共节点
036:mari和shiny 034:删除公共字符串
删除公共字符_牛客题霸_牛客网 (nowcoder.com) 题解:
用哈希记录好第二个字符串中的字符#xff0c;再遍历一遍第一个字符串#xff0c;只将没有记录的字符加在结果字符串上。…目录
034:删除公共字符串
035:两个链表的第一个公共节点
036:mari和shiny 034:删除公共字符串
删除公共字符_牛客题霸_牛客网 (nowcoder.com) 题解:
用哈希记录好第二个字符串中的字符再遍历一遍第一个字符串只将没有记录的字符加在结果字符串上。
#include iostream
#includestring
using namespace std;int main()
{string s;string t;getline(cin,s);getline(cin,t);bool hash[300]{0};for(auto ch:t) hash[ch]true;string ret;for(auto ch:s){if(hash[ch]false){retch;}}coutretendl;return 0;}035:两个链表的第一个公共节点 两个链表的第一个公共结点_牛客题霸_牛客网 (nowcoder.com)
题目: 题解
1.计数:从两个头节点遍历一遍计算长度差长的链表先走len步再同时向后遍历当两节点相同时到达第一个公共节点。
2.等量关系:两个头节点遍历双头链表的全部节点是等价的所以两个头节点同时开始向后遍历到结尾后到另一个头节点继续遍历最终会在公共相交节点相遇此时刚好均遍历完全部节点。 class Solution {
public:ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {//计数ListNode* cur1pHead1,* cur2pHead2;int count10,count20;while(cur1 || cur2){if(cur1){count1;cur1cur1-next;}if(cur2){count2;cur2cur2-next;}}int len0;ListNode* longList;ListNode* shortList;if(count1count2){longListpHead1;shortListpHead2;lencount1-count2;}else{longListpHead2;shortListpHead1;lencount2-count1;}cur1longList;cur2shortList;for(int i0;ilen;i){cur1cur1-next;}while(cur1 cur2){if(cur1cur2) return cur1;cur1cur1-next;cur2cur2-next;}return NULL;}
};class Solution {
public:ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {//等量关系ListNode* cur1pHead1,* cur2pHead2;while(cur1!cur2){cur1cur1!NULL?cur1-next:pHead2;cur2cur2!NULL?cur2-next:pHead1;}return cur1;}
};036:mari和shiny
mari和shiny (nowcoder.com)
题目: 题解简单线性dp维护i位置之前⼀共有多少个s,sh然后更新shy的个数。
#includeiostream
#includestring
using namespace std;
int main()
{string S;int n0;cinn;cinS;long long s0,h0,y0;for(auto ch : S){if(chs) s;else if(chh) hs;else if(chy) yh;}coutyendl;return 0;
}