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的更多相关文章

  1. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  2. [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 ...

  3. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  4. LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. [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 ...

  6. Leetcode #9 Easy <Palindrome Number>

    题目如图,下面是我的解决方法: class Solution { public boolean isPalindrome(int x) { if(x < 0) //由题意可知,小于0的数不可能为 ...

  7. LeetCode Array Easy 88. Merge Sorted Array

    Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...

  8. 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 ...

  9. leetcode 492-543 easy

    492. Construct the Rectangle Input: 4 Output: [2, 2] Explanation: The target area is 4, and all the ...

随机推荐

  1. IDEA本地SBT项目上传到SVN

    需求 将本地创建的一个项目上到SVN 网上很多从SVN下载到idea,提交.更新.删除等操作. 但是少有从本地上传一个项目到svn管理的案例 本文参考https://blog.csdn.net/cao ...

  2. MyBatis - 输入和输出参数

    基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回值是一个对象的集合,@resultType中只能写该对 ...

  3. appscan如何扫描移动应用APP

    1.前置条件:让手机和电脑处于同一WIFI下 1打开appscan,选择手动探索/外部设备. 2在弹出的对话框页面点击右上角“记录代理配置”. 3在弹出的页面选择记录代理页签,设置Appscan代理端 ...

  4. highchart接收后台数据用法

    最近做数据分析的时候使用了highchart这个插件,从后台中接收数据的时候出了一些问题,记录下来免得以后忘了. $(function () { var list = {$weeklist}; var ...

  5. SVN 环境搭建

    安装配置 安装环境 #查看系统版本环境 [root@svn ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [root@svn ~]# u ...

  6. java关键字一览表

  7. iType.js仿输入文字效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. ajax传输数据

    AJAX向后台传输数//1 直接在url中传递 $.ajax({ type: "POST", url: "/testAjax/addUser?id=1&name= ...

  9. Python学习笔记之常用函数及说明

    Python学习笔记之常用函数及说明 俗话说"好记性不如烂笔头",老祖宗们几千年总结出来的东西还是有些道理的,所以,常用的东西也要记下来,不记不知道,一记吓一跳,乖乖,函数咋这么多 ...

  10. 桥接模式(Bridge、Implementor)(具体不同平台日志记录,抽象与实现分离)

    桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化.它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式 ...