14. Longest Common Prefix

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

注:这题竟然连个示例都没有,说明特殊情况并不多,就是要找出所有字符串的最长公共前缀。他应该不会超过所有字符串中最短的那个,可以试着找出最短长度n,然后每个都读取和比较n个,并根据情况不断减小那个n.\

 if (strs.size() == )
{
return "";
}
int n{int(strs.front().size())};
if (n == )
{
return "";
}
for (vector<string>::iterator str_i = strs.begin()+; str_i != strs.end(); str_i++)
{
if ((*str_i).size() < n)
{
n = (*str_i).size();
}
int temp{n};//感觉有点儿多余
for (int i = ; i < n; i++)
{
if ((*str_i).at(i) != (*(str_i - )).at(i))
{
temp = i;
break;
}
}
n = temp;
} string longest_prefix(strs.front(),,n);
return longest_prefix;

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.

注:给出一个链表,把倒数第n个元素移除。听起来很常用的技术,了解一下链表的结构,用处 ,优势,再来做这个题。

今天关于链表的部分没有完成,明天继续

    int i{  };
ListNode *temp,*fronter ,*backer,*curNode;
temp = head;
//fronter从第一个节点开始向后移动,直到第n个,或者到了结尾
for (fronter = temp; i < n && !(fronter->next==NULL); fronter = fronter->next,i++);//(*fronter).next)
if (fronter->next == NULL)//如果是因为到了结尾而结束的循环,那么说明要删除的点在head
{
ListNode *tmp = head;
if (head->next==NULL)//整个链表只有一个节点
head= ;
else
head = head->next;
return head;
}
//如果是因为fornter已经过去n个了,那么这个时候给backer赋值
curNode = head;
//二者同时继续向后遍历,直到fronter走到结尾时,删除backer之后的那个节点
for (; fronter->next != NULL; fronter = fronter->next){ backer=curNode;
curNode = curNode->next;
}
//删除backer后面的节点
backer->next = curNode->next;
return head;

Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List的更多相关文章

  1. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  2. LeetCode记录之14——Longest Common Prefix

    本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...

  3. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

  4. LeetCode之LCP(Longest Common Prefix)问题

    这个也是简单题目.可是关键在于题意的理解. 题目原文就一句话:Write a function to find the longest common prefix string amongst an ...

  5. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  6. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

  7. 【LeetCode算法-14】Longest Common Prefix

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

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

随机推荐

  1. TCP/IP Basic

    1.概述 TCP/IP起源于60年代美国政府遮住的一个分组交换网络项目,在当今被定义为互联网通信接口,TCP/IP主要分为4层,每一层负责不同的通信功能,这促成了一个协议族的诞生,而TCP/IP是一组 ...

  2. IE框架表单遍历

    // HtmlWeb.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <atlbase.h> #include ...

  3. .net 异步编程总结

    异步的方式,就是,先发起IO.CPU密集工作等,然后函数返回,在IO.CPU密集工作等完成了以后——某个不确定的时刻,再执行后续的代码.   所以,如果使用异步代码,必须注意代码的执行顺序. 所以,异 ...

  4. 1.2 UML带来了什么(学习笔记)

    需求->需求分析->设计->开发 uml 编号 uml元素 对于语言理解 1 元模型 基本词汇 2 表示法或视图 语法 3 RUP 方法(统一软件开发过程)  方法 4 控制类 定语 ...

  5. 【DDD】持久化领域对象的方法实践

    [toc] 概述 在实践领域驱动设计(DDD)的过程中,我们会根据项目的所在领域以及需求情况捕获出一定数量的领域对象.设计得足够好的领域对象便于我们更加透彻的理解业务,方便系统后期的扩展和维护,不至于 ...

  6. 基于CentOS 7 部署MySQL 5.7的基本操作

    关闭selinux # sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config重启后生效# sestatus 修改提示符配置# vi / ...

  7. POJ 2318 TOYS(叉积+二分)

    题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...

  8. 简简单单之Linux命令入门

    show me the code and talk to me,做的出来更要说的明白 GitHub 项目JavaHouse同步收录 我是布尔bl,你的支持是我分享的动力! 引入 作为一名合格的后端开发 ...

  9. MySQL快速回顾:更新和删除操作

    前提要述:参考书籍<MySQL必知必会> 6.1 更新数据 为了更新(修改)表中的数据,可使用UPDATE语句.可采用两种方式使用UPDATE: 更新表中特定的行: 更新表中所有的行. U ...

  10. 什么是aPaas?aPaas与低代码又是如何促进应用程序开发现代化的?

    从软件即服务(SaaS)到基础设施即服务(IaaS),云计算的兴起使“一切皆服务”(XaaS)模型得以泛滥,而aPaaS可能是这些模型中最鲜为人知的模型.随着aPaaS市场预计将从2018年的近90亿 ...