Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
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的更多相关文章
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- LeetCode记录之14——Longest Common Prefix
本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
- LeetCode之LCP(Longest Common Prefix)问题
这个也是简单题目.可是关键在于题意的理解. 题目原文就一句话:Write a function to find the longest common prefix string amongst an ...
- leetCode练题——14. Longest Common Prefix
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- leetcode第14题--Longest Common Prefix
Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【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 ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
随机推荐
- [梁山好汉说IT] 以水浒传为例讲解贝叶斯定理
0x00 摘要 看看呼延灼如何利用贝叶斯定理来判断 "自己是否是公明哥哥的心腹". 0x01 IT概念 1. 贝叶斯定理 贝叶斯定理是用来解决"逆概率"问题的, ...
- DEVOPS技术实践_21:Pipeline的嵌套以及流程控制的if和case语句
1 if控制语句 使用一个简单的If控制语句 pipeline { agent any stages { stage('flow control') { steps { script { == ) { ...
- spring cloud Gateway简单使用
一.引子 2年前有幸使用过一次Spring Cloud (1.5.9),那次用的是ZUUL做网关,没有使用Gateway做网关,一直是个小遗憾.终于在2年后的19年底再次使用Spring Cloud, ...
- nginx部署VUE跨域访问api
H5端配置跨域 nginx跨域配置 server { listen 80; charset utf-8; server_name you_dome_name;#location /tasklist.j ...
- 洛谷$P3756\ [CQOI2017]$老$C$的方块 网络流
正解:网络流 解题报告: 传送门$QwQ$ 看到不能出现给定的讨厌的图形,简单来说就,特殊边两侧的方格不能同时再连方格. 所以如果出现,就相当于是四种方案?就分别炸四个格子. 然后冷静分析一波之后发现 ...
- 阿里云函数计算 .NET Core 初体验
体验了一波阿里云函数计算, 已支持 .NET Core 2.1, 那么按照惯例, 来写个 "Hello World" 吧. 作者注: 开发环境 Windows 10 & V ...
- C++Primer第五版 3.2.3节练习
练习 3.6:编写一段程序,使用范围for语句将字符串内的所有字符用X代替. #include<iostream> #include<string> using namespa ...
- 【转】Java多线程面试问题集锦
如果你即将去一家从事大型系统研发的公司进行Java面试,不可避免的会有多线程相关的问题.下面是一些针对初学者或者新手的问题,如果你已经具备良好的基础,那么你可以跳过本文,直接尝试针对进阶水平的Java ...
- TensorFlow——MNIST手写数据集
MNIST数据集介绍 MNIST数据集中包含了各种各样的手写数字图片,数据集的官网是:http://yann.lecun.com/exdb/mnist/index.html,我们可以从这里下载数据集. ...
- FastOne专业计算平台助力生命科学研发
11月16日,由AWS主办的云计算行业沙龙在中油阳光酒店举行,速石科技CEO陈熹就高性能计算如何助力生命科学领域发表了精彩的演讲. 面临的问题及挑战 在算力及高性能领域,随着行业客户的业务需求量,数据 ...