#83 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,

Given 1->1->2, return 1->2.

Given 1->1->2->3->3, return 1->2->3.

删除单链表中反复元素的节点。要考虑链表是否为空,和下一个节点是否存在的特殊情况

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
struct ListNode *p,*temp;
if (head)
{
p = head;
while (p->next)
{
if (p->val != p->next->val)
p = p->next;
else
{
temp = p->next;
p->next = p->next->next;
free(temp);
}
}
}
return head;
}

#88 Merge Sorted Array

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

Note:

You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1and nums2 are m and n respectively.

合并两个有序数组---前面#21合并两个有序单链表类似---结果保存在nums1(如果空间足够大)

//0ms
void merge(int* nums1, int m, int* nums2, int n) {
int index = m + n -1, i = m - 1, j = n - 1;
while(j>=0)
{
if(i < 0 || nums1[i] < nums2[j])
nums1[index--] = nums2[j--];
else
nums1[index--] = nums1[i--];
}
}

#100 Same Tree

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

//0ms
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if(p==NULL || q==NULL)
return p == q;
if(p->val == q->val)
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
else
return false;
}

#101 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:

    1
/ \
2 2
\ \
3 3

递归写法

//4ms
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ bool dfs(struct TreeNode* root1, struct TreeNode *root2)
{
if(root1 == NULL||root2 == NULL)
return root1 == root2;
if(root1->val != root2->val )
return false;
else
return dfs(root1->left,root2->right) && dfs(root1->right,root2->left);
} bool isSymmetric(struct TreeNode* root) {
if(!root || (!root->left && !root->right))//空树||仅仅有根结点
return true;
else
return dfs(root->left,root->right);
}

Leetcode--easy系列5的更多相关文章

  1. hdu 2049 不easy系列之(4)——考新郎

    不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. LeetCode——single-number系列

    LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...

  3. HDU 2045不easy系列之三LELE的RPG难题(趋向于DP的递推)

    不easy系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  4. hdu1465不easy系列之中的一个(错排)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/37512659 转载请注明出 ...

  5. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  6. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  7. leetcode easy problem set

     *勿以浮沙筑高台* 持续更新........     题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...

  8. [Leetcode] Sum 系列

    Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...

  9. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  10. 决战Leetcode: easy part(51-96)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

随机推荐

  1. Vue结合原生js实现自定义组件自动生成

    就目前三大前端主流数据驱动框架(vue,ng,react)而言,均具有创建自定义组件的api,但都是必须先做到事先写好挂载点,这个挂载点可以是原有静态元素标签也可以是自定义模板:对于多种组件通过同一数 ...

  2. 微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)

    首先先补习下企业库的Caching Application Block的相关知识: 1.四大缓存方式,在Caching Application Block中,主要提供以下四种保存缓存数据的途径,分别是 ...

  3. 计蒜客 A2232.程序设计:蒜厂年会-单调队列(双端队列(STL deque)实现)滑窗维护最小前缀和

    程序设计:蒜厂年会 问答问题反馈 只看题面 16.79% 1000ms 262144K   在蒜厂年会上有一个抽奖,在一个环形的桌子上,有 nn 个纸团,每个纸团上写一个数字,表示你可以获得多少蒜币. ...

  4. 洛谷P2713 罗马游戏

    题目传送门 分析: 好吧,其实没什么好分析的,左偏树裸题. Code: #include<cstdio> #include<cstring> #include<cstdl ...

  5. CodeForces - 995B Suit and Tie

    题面在这里! 明明可以出成n<=1e5但是因为拒绝写数据结构而只出到n<=100,,,出题人真的很棒棒.. 一个显然的贪心就是,把和当前序列最左端的数匹配的数移到它的右边,这样迭代下去总是 ...

  6. 【其它算法】Face The Right Way

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5278   Accepted: 246 ...

  7. Ui Automator Test Through Command

    问题描述: 通过adb shell uiautomator runtest 命令,直接运行java测试例. 命令解析: adb shell uiautomator runtest <JARS&g ...

  8. MYSQL复习笔记12-视图

    Date: 20140223Auth: Jin参考:http://blog.sina.com.cn/s/blog_436732df0100e768.html 一.介绍1.概念视图是从一个或几个基本表( ...

  9. GG同步到sqlserver报错一例 Invalid date format

    在将Oracle表同步到sqlserver时,在sqlserver端应用数据时,可能会遇到这个报错. 2014-05-17 17:20:24 WARNING OGG-01154 SQL error - ...

  10. Jquery 一次处理多个ajax请求的代码

    Jquery 一次处理多个ajax请求的代码,需要的朋友可以参考下.   复制代码代码如下: $(document).ready(function () { $('#getsetgo').click( ...