leetcode easy problem set
*勿以浮沙筑高台*
持续更新........ 题目网址:https://leetcode.com/problemset/all/?difficulty=Easy
1. Two Sum [4ms]
2. Reverse Integer [12ms]
题意:将一个32bit signed integer反转输出,如果反转之后超出32位补码范围 [-2^31,2^31-1],则输出0
思路:边取模边算结果,结果存longlong判界

3. Palindrome Number [112ms]
题意:判断数字回文,且不要将数字转为字符串
思路:和第2题一样,非负反转判等

4. Roman to Integer [52ms]
题意:罗马数字串转为数字
方法:字符串hash
class Solution {
private:
int case_(int ch)
{
switch (ch)
{
case 'I': return ;
case 'V': return ;
case 'X': return ;
case 'L': return ;
case 'C': return ;
case 'D': return ;
case 'M': return ;
case 'I' * + 'V': return ;
case 'I' * + 'X': return ;
case 'X' * + 'L': return ;
case 'X' * + 'C': return ;
case 'C' * + 'D': return ;
case 'C' * + 'M': return ;
default:return ;
}
}
public:
int romanToInt(string str) {
register int x = , i;
for (i = ; i < str.size(); ++i)
{
register int t = case_(str[i - ] * + str[i]);
if (t)x += t, i++;
else
x += case_(str[i-]);
}
if (i == str.size())x += case_(str[i - ]);
return x;
}
};
5. Longest Common Prefix [4ms]
题意:一个字符串数组中所有元素的最长公共前缀
思路:暴力,注意数组可能为空,可能数组只含有一个空串

6. Valid Parentheses [4ms]
题意:括号合法匹配
方法:栈基本操作
class Solution {
public:
bool isValid(string s) {
char arr[] = { '#' };
unordered_map<char, char> P{ {'(',')'},{'{','}'},{'[',']'} };
int index = ;
for (auto i : s)
{
if (i != P[arr[index - ]])arr[index++] = i;
else index--;
}
return index == ;
}
};
7. Merge Two Sorted Lists [8ms]
题意:合并两个已序链表
方法:模拟
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1)return l2;
if (!l2)return l1;
ListNode* res, *cur, *newNode;
if (l1->val < l2->val)
{
res = new ListNode(l1->val);
res->next = NULL;
l1 = l1->next;
}
else
{
res = new ListNode(l2->val);
res->next = NULL;
l2 = l2->next;
}
cur = res;
while (l1 != NULL && l2 != NULL)
{
if (l1->val < l2->val)
{
newNode = new ListNode(l1->val);
newNode->next = NULL;
cur->next = newNode;
l1 = l1->next;
}
else
{
newNode = new ListNode(l2->val);
newNode->next = NULL;
cur->next = newNode;
l2 = l2->next;
}
cur = cur->next;
}
while (l1 != NULL)
{
newNode = new ListNode(l1->val);
newNode->next = NULL;
cur->next = newNode;
l1 = l1->next;
cur = cur->next;
}
while (l2 != NULL)
{
newNode = new ListNode(l2->val);
newNode->next = NULL;
cur->next = newNode;
l2 = l2->next;
cur = cur->next;
}
return res;
}
};
8. Remove Duplicates from Sorted Array [16ms]
题意:序列去重
.· 方法:STL-unique

9. Remove Element [4ms]
题意:移除序列中指定元素
方法:STL-remove_if

10. Implement strStr() [4ms]
题意:返回b串在a串中出现的首位置
方法:KMP
class Solution {
int next[];
void GetNext(string p) {
next[] = -;
int k = -;
for (int q = ; q <= (int)p.size() - ; q++)
{
while (k > - && p[k + ] != p[q])
k = next[k];
if (p[k + ] == p[q])
k = k + ;
next[q] = k;
}
}
public:
int strStr(string s, string p) {
if (p.empty())return ;
GetNext(p);
register int i = , j = ;
int k = -;
for (int i = ; i < s.size(); i++)
{
while (k >- && p[k + ] != s[i])
k = next[k];
if (p[k + ] == s[i])
k = k + ;
if (k == p.size() - )
return i - p.size() +;
}
return -;
}
};
11. Divide Two Integers [12ms]
题意:两个32bit signed int 做除法,如果结果越界那么输出2^31-1
方法:存longlong,然后判界输出

12. Search Insert Position [4ms]
题意:已序序列中找一个数,如果存在,返回index,如果不存在返回插入后使序列仍有序的插入位置
方法:STL-lower_bound

13. Maximum Subarray [8ms]
题意:给定一个数字串,找出其中各个数字相加之和最大的一个子串,输出最大和。
方法:dp[ ],dp[i]代表前 i 位的最优解,则转移方程为 dp[i] = max(dp[i] + nums[i], nums[i]);

14.Length of Last Word [4ms]
题意:给定一个字符串,里面的空格符将之分割为(0个或一个或)多个子字符串,求最后一个子串的长度
方法:利用字符串流将其顺序读出,返回长度

15. Plus One [0 ms]
题意:给定一个数组,这个数组代表一个数,比如【1,2,3】:123,让代表的数+1,然后返回新的数组,比如:【1,2,4】
解法:从后往前数,第一个不是9的数,让其+1,是9的,变为0

16. Add Binary [4ms]
题意:两个二进制字符串相加
方法:先反转两个字符串,使得低位对齐,然后遍历,进位标记做好即可
class Solution {
public:
string addBinary(string a, string b) {
string c = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int siz = min(a.size(), b.size()), key = , i;
for (i = ; i < siz; ++i)
{
if (a[i] != b[i]) key ? c += '' : c += '';
else
{
if (key) c += '', key = false;
else c += '';
if (a[i] == '')key++;
}
}
auto fun = [&](string& a) {
if (a.size() - siz)
{
if (key)
for (i = siz; i < a.size(); ++i)
if (a[i] == '')c += '';
else
{
a[i] = '';
key = false;
break;
}
if (key)c += '', key = false;
else c += string(a.begin() + i, a.end());
}
};
fun(a);
fun(b);
if (key)c += '';
reverse(c.begin(), c.end());
return c;
}
};
17. Sqrt(x)
题意:求取一个整数的根号取下整
正解:【0,x】二分答案!!
18. Remove Duplicates from Sorted List [8ms]
题意:删除已序链表重复元素
解法:遍历删除

19. Same Tree [0ms]
题意:给定两棵二叉树的根节点,判定两棵树的结构是否相同
解题:中序遍历,一边遍历一边结构判同

20. Symmetric Tree [4ms]
题意:判定一颗二叉树是否左右对称
解题:同上一题思路,一边遍历,一遍判定结构是否相同。但是,有一个需要注意的地方。
遍历顺序,中-左-右,中-右-左,判定值序列是否相同,如果为null,记录值为0 !!,切不可省略不存储值!
class Solution {
vector<int> left,right;
public:
void trans(TreeNode* root, bool ispre)
{
if(root == NULL)
{
if(ispre)left.push_back();
else right.push_back();
return;
}
if(ispre)left.push_back(root->val);
else right.push_back(root->val);
if(ispre)trans(root->left, ispre);
trans(root->right, ispre);
if(!ispre)trans(root->left, ispre);
}
bool isSymmetric(TreeNode* root) {
if(root == NULL)return true;
trans(root,true);
trans(root,false);
return left == right;
}
};
leetcode easy problem set的更多相关文章
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
- An easy problem
An easy problem Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
- 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?
UVa11991 Easy Problem from Rujia Liu? 思路: 构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...
- HDU 5475 An easy problem 线段树
An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- UVA 11991 Easy Problem from Rujia Liu?(vector map)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (J ...
随机推荐
- Postgresql获取所有schema
Postgresql 连接方式_连接五要素_psql: https://blog.csdn.net/u011402596/article/details/38510547 postgresql的sho ...
- bzoj 5085: 最大——结论题qwq
Description 给你一个n×m的矩形,要你找一个子矩形,价值为左上角左下角右上角右下角这四个数的最小值,要你最大化矩形 的价值. Input 第一行两个数n,m,接下来n行每行m个数,用来描述 ...
- Python读取Excel中的数据并导入到MySQL
""" 功能:将Excel数据导入到MySQL数据库 """ import xlrd import MySQLdb # Open the w ...
- TCP/IP 网络编程的理解
一.网络各个协议:TCP/IP.SOCKET.HTTP等 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被称作媒体层,是网络工程 ...
- Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
问题: 当我们打开数据库,即use dbname时,要预读数据库信息,当使用-A参数时,就不预读数据库信息. 解决方法:mysql -hhostname -uusername -ppassword - ...
- React Native新手入门
前言 React Native是最近非常火的一个话题,想要学习如何使用它,首先就要知道它是什么. 好像面对一个新手全面介绍它的文章还不多,我就归纳一下所有的资料和刚入门的小伙伴一起来认识它~ 将从以下 ...
- IDL界面程序直接调用envi菜单对应功能
参考自http://blog.sina.com.cn/s/blog_764b1e9d010115qu.html 参考文章的方法是构建一个button控件,通过单击实现,这种方法比较复杂,不是我们经常能 ...
- 缓存数据库-redis介绍
一:Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的 ...
- Shell编程学习1--基础了解
"#!path"告诉系统path所指的程序为用来解释此脚本文件的Shell程序: 如#!/bin/sh, #!/bin/bash Shell Script的后缀名为.sh; ech ...
- TF-tf.nn.dropout介绍
官方的接口是这样的 tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) 根据给出的keep_prob参数,将输入te ...