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. 这道题让我们求一系列字符串 ...
随机推荐
- System类StringBuilder小结
- CentOS 下 maven 安装
获取maven安装包 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bi ...
- 调用微软未公开ZwQueryInformationThread函数根据线程句柄获取线程ID
这段时间公司项目中为了支持XP系统同事代码中用到了 GetThreadId 这个微软的API 但是这个API最低支持版本是 Windows version Windows Vista [desktop ...
- 「2018-11-05模拟赛」T5 传送机 解题报告
5.传送机(sent.*) 问题描述: 黄黄同学要到清华大学上学去了.黄黄同学很喜欢清华大学的校园,每次去上课时总喜欢把校园里面的每条路都走一遍,当然,黄黄同学想每条路也只走一遍. 我们一般人很可能对 ...
- POJ 1458 Common Subsequence (动态规划)
题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...
- MySQL数据库(三)
前提要述:参考书籍<MySQL必知必会> 2.1 MySQL简介 2.1.1 什么是MySQL MySQL是一种关系数据库管理系统.负责数据库中数据的存储,检索,管理和处理. 2.1.2 ...
- Go Web 编程之 请求
概述 前面我们学习了处理器和处理器函数,如何编写和注册处理器.本文我们将学习如何从请求中获取信息. 请求的结构 通过前面的学习,我们知道处理器函数需要符合下面的签名: func (w http.Res ...
- 【转】[IT综合面试]牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
感谢IT面试群 S-北京-陈磊 的整理分享. 基础篇:操作系统.计算机网络.设计模式 提高篇:WIN32.MFC与Linux 算法篇:算法与数据结构 一:操作系 ...
- Jquery实现图片管理
这里实现的是一个图片的在线管理,类似于网络相册的图片管理. 效果图如下: 文件结构如下图: style2.css文件内容如下: @charset "utf-8"; *{;; } i ...
- LCA - 倍增法去求第几个节点
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...