*勿以浮沙筑高台*

持续更新........     题目网址: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的更多相关文章

  1. UVA-11991 Easy Problem from Rujia Liu?

    Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...

  2. An easy problem

    An easy problem Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  3. 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, ...

  4. POJ 2826 An Easy Problem?!

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7837   Accepted: 1145 ...

  5. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  6. 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?

    UVa11991 Easy Problem from Rujia Liu?  思路:  构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...

  7. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

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

  9. 数据结构(主席树):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 ...

随机推荐

  1. 谁说码农不懂浪漫?(js写的'老婆生日快乐'特效)

    一直被老婆抱怨不懂浪漫,老婆的生日又来了,老婆指着闺蜜空间上贴的老公做的胡萝卜心形浪漫晚餐告诉我:必须送她一份用心的礼物.我绞尽脑汁想出这么一法子,还是得用我们码农的独特方式,经过一天多的努力,终于做 ...

  2. hdu 确定比赛名次(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    ...

  3. 动态获取UILabel的bounds

    在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需要的长度 CGSize labelBounds = [s ...

  4. win10系统下我的电脑右键没有属性

    1.右键点击系统左下角的开始,菜单中点击运行 2.在输入框中输入regeidt,点击确定打开系统的注册表编辑器 3.然后依次打开HKEY_CURRENT_USER\Software\Microsoft ...

  5. 通过BurpSuite和sqlmap配合对dvwa进行sql注入测试和用户名密码暴力破解

    0x1 工具和环境介绍 dvwa:渗透测试环境 BurpSuite:强大的WEB安全测试工具 sqlmap:强大的sql注入工具 以上工具和环境都在kali linux上安装和配置. 0x2 步骤说明 ...

  6. WCF REST 工作总结

    首先引用System.ServiceModel;System.ServiceModel;System.ServiceModel.Activation;命名空间 [ServiceContract] pu ...

  7. csv导入mysql提示错误[Error Code] 1290 - The MySQL server is running with the --secure-file-priv option 解决方法【转】

    解决方法: 1.进入mysql查看secure_file_prive的值 mysql>SHOW VARIABLES LIKE "secure_file_priv"; secu ...

  8. Flask:初次使用Blueprints

    Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2,Eclipse Oxygen.1a Release (4.7.1a),PyDev 6.3.2 本文为记录自己第一次使用 ...

  9. python中的多进程

    具体参考这个博客地址:http://www.cnblogs.com/lxmhhy/p/6052167.html

  10. delphi TComponent类(1)

    来自:http://blog.csdn.net/lailai186/article/details/7442383 ------------------------------------------ ...