#14 Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

这个题求多个字符串的公共前缀,除了要考虑串空外,假设存在公共前缀,那么必然也是第一个串的前缀。

所以能够以第一个串为基准,比較其它串的前缀是否与第一个串同样。

char* longestCommonPrefix(char** strs, int strsSize) {
char *s0,*si;
int index;//指示比較位置
if (strsSize <= 0 || strs == NULL) return strs;
if (strsSize == 1) return strs[0];
s0 = strs[0];//以第一个字符串为基准
for (int i = 1; i < strsSize; ++i)
{
si = strs[i];
index = 0;//每一个字符串从0位置開始比較
while (true)
{
if (s0[index] != si[index] || s0[index] == NULL || si[index] == NULL)
{
s0[index] = '\0';//对于不相等的位置,必然不是公共前缀
break;
}
index++;
}
}
return strs[0];
}

#19 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:

Given n will always be valid.

Try to do this in one pass.

//0ms
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
int len=0,j=0;
struct ListNode *p,*q;
p = head;//指向第一个元素 没有头结点
//求链表长度
while(p!=NULL)
{
p = p->next;
len++;
}
//边界条件
if(len-n<0)
return NULL;
if(len==1 &&n==1)
return NULL;
p = head;//指向第一个元素
q=p;
//删除的元素是第一个位置,不存在q全部先讨论
if(n==len)
{
p =head->next;
return p;
}
//一般条件---待删除元素位于第len-n+1个位置,倒数第n个位置
for(j=1;j<=len-n;j++)
{
q = p;//q指向待删除元素前一个元素第len-n个元素
p = p->next;//p指向待删除元素---第len-n+1个元素
}
q->next = p->next;
return head;
}

在leetcode discuss里面又看到第二种写法,一次遍历得到待删除节点位置

struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
struct ListNode* front = head;
struct ListNode* behind = head;
//front后移len长度,behind后移len-n长度。behind->next即为待删除节点
while (front != NULL)
{
front = front->next;
if (n-- < 0)
behind = behind->next;
}
if (n == 0)//len==n
head = head->next;
else
behind->next = behind->next->next;
return head;
}

#20 Valid Parentheses

Given a string containing just the characters '('')''{''}''[' and ']',
determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are
all valid but "(]" and "([)]" are
not.

括号匹配,首先想到栈。

bool isValid(char* s) {
int i,top,len;
char *a;
char ch;
len = strlen(s);
a = (char *)malloc(sizeof(char)*len);
top =-1;
for(i=0;i<len;i++)
{
ch = s[i];
if(ch=='{'||ch=='('||ch=='[')
a[++top] = ch;
if(ch==']'||ch==')'||ch=='}')
{
if(top==-1)
return false;
else if(ch==']'&& a[top]=='[')
top--;
else if(ch=='}'&& a[top]=='{')
top--;
else if(ch==')'&& a[top]=='(')
top--;
else
return false;
}
}
if(top==-1)
return true;
else
return false;
}

#21 Merge Two Sorted Lists

Merge
two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

合并两个有序链表---首先想到了归并排序最后的合并两个有序数组,写法全然类似。查了下面discuss 发现一种递归的写法。

/**4ms
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *p,*head;
head = p;
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
while(l1&&l2)
{
if(l1->val <= l2->val)
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
if(l1)
p->next = l1;
if(l2)
p->next = l2;
return head->next;
}
/**4ms
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode* l3;
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
if(l1->val < l2->val)
{
l3 = l1;
l3->next = mergeTwoLists(l1->next,l2);
}
else
{
l3 = l2;
l3->next = mergeTwoLists(l1,l2->next);
}
return l3;
}

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

  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. 【POJ 2286】 The Rotation Game

    [题目链接] http://poj.org/problem?id=2286 [算法] IDA* [代码] #include <algorithm> #include <bitset& ...

  2. 通过ip获取地址

    <?php /** * IP 地理位置查询类 * * @author 马秉尧 * @version 1.5 * @copyright 2005 CoolCode.CN */ class IpLo ...

  3. RT-Thread 设备驱动SPI浅析及使用

    OS版本:RT-Thread 4.0.0 测试BSP:STM32F407 SPI简介 SPI总线框架其实和I2C差不多,可以说都是总线设备+从设备,但SPI设备的通信时序配置并不固定,也就是说控制特定 ...

  4. Mybatis中resultMap的作用-解决实体类属性名和数据库字段不一致

    解决实体类属性名和数据库字段不一致

  5. python基本数据类型之元祖tuple

    元祖tuple 是对列表的二次加工,书写格式为括号(),里面放元素 元组的一级元素不可被修改,且不能被增加和删除 一般写元组的时候,推荐在最后加入逗号,  能加则加 创建元组 ? 1 tu = (11 ...

  6. POJ 3275 Floyd传递闭包

    题意:Farmer John想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛(1 ≤ N ≤ 1,000).FJ通过比较,已经知道了M(1 ≤ M ≤ 10,000)对相对关系.每一对关系表示为&q ...

  7. POJ 2492 A Bug's Life 带权并查集

    题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...

  8. SQLServer inner join,left join,right join,outer join 备忘备忘

    LEFT JOIN LEFT JOIN 关键字会从左表那里返回所有的行,即使在右表中没有匹配的行. 即LEFT JOIN 的 ON 条件不会对数据行造成影响 RIGHT JOIN RIGHT JOIN ...

  9. hiho一下 第174周

    题目1 : Dice Possibility 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice ...

  10. 1) 十分钟学会android--建立第一个APP,创建android项目

    一个Android项目包含了所有构成Android应用的源代码文件. 本小节介绍如何使用Android Studio或者是SDK Tools中的命令行来创建一个新的项目. Note:在此之前,我们应该 ...