leetcode 350 easy
350. Intersection of Two Arrays II
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> dict;
vector<int> res;
for(int i = ; i < (int)nums1.size(); i++) dict[nums1[i]]++;
for(int i = ; i < (int)nums2.size(); i++)
if(dict.find(nums2[i]) != dict.end() && --dict[nums2[i]] >= ) res.push_back(nums2[i]);
return res;
}
};
345. Reverse Vowels of a String
class Solution {
public:
string reverseVowels(string s) {
int i = , j = s.size() - ;
while (i < j) {
i = s.find_first_of("aeiouAEIOU", i);
j = s.find_last_of("aeiouAEIOU", j);
if (i < j) {
swap(s[i++], s[j--]);
}
}
return s;
}
};
387. First Unique Character in a String
Brute force solution, traverse string s times. First time, store counts of every character into the hash table, second time, find the first character that appears only once.
//遍历两次string
class Solution {
public:
int firstUniqChar(string s) {
unordered_map<char, int> m;
for (auto &c : s) {
m[c]++;
}
for (int i = ; i < s.size(); i++) {
if (m[s[i]] == ) return i;
}
return -;
}
};
if the string is extremely long, we wouldn't want to traverse it twice, so instead only storing just counts of a char, we also store the index, and then traverse the hash table.
//遍历一次string和一次map
class Solution {
public:
int firstUniqChar(string s) {
unordered_map<char, pair<int, int>> m;
int idx = s.size();
for (int i = ; i < s.size(); i++) {
m[s[i]].first++;
m[s[i]].second = i;
}
for (auto &p : m) {
if (p.second.first == ) idx = min(idx, p.second.second);
}
return idx == s.size() ? - : idx;
}
};
409. Longest Palindrome
Python: def longestPalindrome(self, s):
odds = sum(v & for v in collections.Counter(s).values())
return len(s) - odds + bool(odds)
C++: int longestPalindrome(string s) {
int odds = ;
for (char c='A'; c<='z'; c++)
odds += count(s.begin(), s.end(), c) & ; //如果是奇数则加1,偶数不加
return s.size() - odds + (odds > );
}
412. Fizz Buzz
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> ret_vec(n);
for(int i=; i<=n; ++i)
{
if( == i%)
{
ret_vec[i-] += "Fizz";
}
if( == i%)
{
ret_vec[i-] += "Buzz";
}
if(ret_vec[i-].empty())
{
ret_vec[i-] += to_string(i);
}
}
return ret_vec;
}
};
414. Third Maximum Number
class Solution {
public:
int thirdMax(vector<int>& nums) {
set<int> top3;
for (int num : nums) {
top3.insert(num);
if (top3.size() > )
top3.erase(top3.begin());
}
return top3.size() == ? *top3.begin() : *top3.rbegin();
}
};
415. Add Strings
class Solution {
public:
string addStrings(string num1, string num2) {
int i = num1.size() - ;
int j = num2.size() - ;
int carry = ;
string res = "";
while(i>= || j>= || carry){
long sum = ;
if(i >= ){sum += (num1[i] - '');i--;}
if(j >= ){sum += (num2[j] - '');j--;}
sum += carry;
carry = sum / ;
sum = sum % ;
res = res + to_string(sum);
}
reverse(res.begin(), res.end());
return res;
}
};
437. Path Sum III
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
if(!root) return ;
return dfs(root,,sum)+pathSum(root->left,sum)+pathSum(root->right,sum);
}
int dfs(TreeNode* root,int pre,int sum)
{
if(!root) return ;
int cur=pre+root->val;
return (cur==sum)+dfs(root->left,cur,sum)+dfs(root->right,cur,sum);
}
};
438. Find All Anagrams in a String
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int> pv(,), sv(,), res;
if(s.size() < p.size())
return res;
for(int i = ; i < p.size(); ++i)
{
++pv[p[i]];
++sv[s[i]];
}
if(pv == sv)
res.push_back();
for(int i = p.size(); i < s.size(); ++i)
{
++sv[s[i]];
--sv[s[i-p.size()]];
if(pv == sv)
res.push_back(i-p.size()+);
}
return res;
}
};
leetcode 350 easy的更多相关文章
- 前端与算法 leetcode 350. 两个数组的交集 II
目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- Leetcode #9 Easy <Palindrome Number>
题目如图,下面是我的解决方法: class Solution { public boolean isPalindrome(int x) { if(x < 0) //由题意可知,小于0的数不可能为 ...
- LeetCode Array Easy 88. Merge Sorted Array
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...
- LeetCode Arrary Easy 35. Search Insert Position 题解
Description Given a sorted array and a target value, return the index if the target is found. If not ...
- leetcode 492-543 easy
492. Construct the Rectangle Input: 4 Output: [2, 2] Explanation: The target area is 4, and all the ...
随机推荐
- linux查看系统内容
- Django的日常-视图层
目录 Django的日常-3 JsonResponse form表单上传文件 CBV的源码分析 视图层 模板传值 过滤器 标签 自定义过滤器和标签 Django的日常-3 JsonResponse 在 ...
- MVVM基础概念和理解
在MVVM模式中,View封装UI和UI逻辑,viewmodel封装presentation逻辑,model封装业务逻辑和数据. View类 View的责任是定义屏幕上的结构和外观,在完美的情况下,v ...
- springmvc-高级参数绑定-映射-异常-json数据交互-拦截器
1.1. 高级参数绑定 1.1.1. 复制工程 把昨天的springmvc-web工程复制一份,作为今天开发的工程 复制工程,如下图: 粘贴并修改工程名为web2,如下图: 工程右键点击,如下图: 修 ...
- 如何使用Junit进行单元测试
测试方法的要求: 必须是public 无返回值 无参数 @Testpublic void f1(){ .....} 在@Test上按下 Ctrl+1(快速锁定错误) 引入Junit包 在方法名上右键 ...
- [转]Expression Blend实例中文教程(8) - 动画设计快速入门StoryBoard
上一篇,介绍了Silverlight动画设计基础知识,Silverlight动画是基于时间线的,对于动画的实现,其实也就是对对象属性的修改过程. 而Silverlight动画分类两种类型,From/T ...
- CF集萃3
CF1118F2 - Tree Cutting 题意:给你一棵树,每个点被染成了k种颜色之一或者没有颜色.你要切断恰k - 1条边使得不存在两个异色点在同一连通块内.求方案数. 解:对每颜色构建最小斯 ...
- idea短信验证
idea短信验证(短信接口:阿里云:消息中间件:activeMQ) 主要分为两步:1.创建发消息 2.创建收消息 1.创建发消息 1.1 创建maven工程 1.2 pom.xml文件导入依赖 注意 ...
- [Day4] Nginx Http模块二
一. POST_READ阶段 1. 用户ip在http请求中的传递? 前提:Tcp连接四元组(src ip,src port,dst ip,dst port) HTTP头部 X-Formard ...
- python运~算~~符!!!!!!!!!!!
目录: 算术运算, 用于加减乘除等数学运算 赋值运算,用于接收运算符或方法调用返回的结果 比较运算, 用于做大小或等值比较运算 逻辑运算,用于做 与.或.非运算 位运算, 用于二进制运算 每种运算中所 ...