287寻找重复数;6Z 字形变换;142环形链表 II;148排序链表;208实现 Trie (前缀树)
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
示例 1:
输入: [1,3,4,2,2]
输出: 2
示例 2:
输入: [3,1,3,4,2]
输出: 3
说明:
不能更改原数组(假设数组是只读的)。
只能使用额外的 O(1) 的空间。
时间复杂度小于 O(n2) 。
数组中只有一个重复的数字,但它可能不止重复出现一次。
class Solution {
public:int findDuplicate(vector<int>& nums) {//寻找循环入口点int nSize=nums.size();if(nSize==2)return nums[1];int lowIndex=0;int fastIndex=0;do{ lowIndex=nums[lowIndex];fastIndex=nums[nums[fastIndex]];}while(lowIndex!=fastIndex);int pos=0;while(pos!=lowIndex){pos=nums[pos];lowIndex=nums[lowIndex];}return pos;}
};
将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。
比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:
L C I R
E T O E S I I G
E D H N
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。
请你实现这个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"
示例 2:
输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:
L D R
E O E I I
E C I H N
T S G
class Solution {
/*根据下标对应关系,i行
diff=(numRows<<1)-2;
(1):j+=diff;
(2):j=j+diff-(i<<1); j+=(i<<1);
*/
public:string convert(string s, int numRows) {int sSize=s.size();if(sSize<=1||numRows==1)return s;int diff=(numRows<<1)-2;//int cIndex=0;char c[sSize+1];c[sSize]='\0';for(int i=0;i<numRows;++i)if(i==0||i==numRows-1)//for(int j=i;j<sSize;j+=diff){c[cIndex++]=s[j];} elsefor(int j=i;j<sSize;j+=(i<<1)){c[cIndex++]=s[j];j=j+diff-(i<<1);//if(j>=sSize)break;c[cIndex++]=s[j];} return c;}
};
class Solution {//char二维数组->string一位数组
public:string convert(string s, int numRows) {int sSize=s.size();if(sSize<=1||numRows==1)return s;int diff=(numRows<<1)-2;vector<string> temp(numRows);bool toDown=false;for(int i=0,rowIndex=0;i<sSize;++i){if(rowIndex==0||rowIndex==numRows-1)toDown=!toDown;if(toDown)temp[rowIndex++]+=s[i];elsetemp[rowIndex--]+=s[i];}string res;for(auto i:temp)res+=i;return res;}
};
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
说明:不允许修改给定的链表。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:tail connects to node index 1
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0
输出:tail connects to node index 0
解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:
输入:head = [1], pos = -1
输出:no cycle
解释:链表中没有环。
进阶:
你是否可以不用额外空间解决此题?
class Solution {
public:ListNode *detectCycle(ListNode *head) {if(!head||head->next==head)return head;if(!head->next)return nullptr;ListNode *low=head,*fast=head;do{low=low->next;fast=fast->next->next;}while(fast&&fast->next&&low!=fast);if(!fast||!fast->next)return nullptr;fast=head;while(low!=fast){low=low->next;fast=fast->next;}return low;}
};
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
示例 1:
输入: 4->2->1->3
输出: 1->2->3->4
示例 2:
输入: -1->5->3->4->0
输出: -1->0->3->4->5
class Solution {//二路归并
public:ListNode* sortList(ListNode* head) {if(!head||!head->next)return head;ListNode *mid=head,*last=head,*pre=nullptr;while(last&&last->next){//找中间点pre=mid;mid=mid->next;last=last->next->next;}pre->next=nullptr;ListNode *left=sortList(head);//分两路ListNode *right=sortList(mid);ListNode *LN=new ListNode(0);ListNode *res=LN;while(left&&right){//合并两路if(left->val<right->val){res->next=left;left=left->next;res=res->next;}else{res->next=right;right=right->next;res=res->next;}}if(left)res->next=left;if(right)res->next=right;res=LN->next;delete LN;return res;}
};
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
示例:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true
说明:
你可以假设所有的输入都是由小写字母 a-z 构成的。
保证所有输入均为非空字符串。
class Trie {
public:/** Initialize your data structure here. */struct TT{char c;bool exist;unordered_map<char,TT*>m;TT(char c,bool e):c(c),exist(e){};};TT *t=new TT('#',true);Trie() {;} /** Inserts a word into the trie. */void insert(string word) {TT *p=t;for(auto i:word)if(p->m.count(i))p=p->m[i];elsep->m[i]=new TT(i,false), p=p->m[i]; p->exist=true;} /** Returns if the word is in the trie. */bool search(string word) {TT *p=t;for(auto i:word)if(p->m.count(i))p=p->m[i];elsereturn false;return p->exist;} /** Returns if there is any word in the trie that starts with the given prefix. */bool startsWith(string prefix) {TT *p=t;for(auto i:prefix)if(p->m.count(i))p=p->m[i];elsereturn false;return true;}
};/*** Your Trie object will be instantiated and called as such:* Trie* obj = new Trie();* obj->insert(word);* bool param_2 = obj->search(word);* bool param_3 = obj->startsWith(prefix);*/
class Trie {
public:/** Initialize your data structure here. */bool exist;vector<Trie *>next;Trie() {exist=false;next.resize(26);} /** Inserts a word into the trie. */void insert(string word) {Trie *p=this;for(auto i:word){int index=i-'a';if(!p->next[index])p->next[index]=new Trie();p=p->next[index];} p->exist=true;} /** Returns if the word is in the trie. */bool search(string word) {Trie *p=this;for(auto i:word){int index=i-'a';if(!p->next[index])return false;p=p->next[index];} return p->exist;} /** Returns if there is any word in the trie that starts with the given prefix. */bool startsWith(string prefix) {Trie *p=this;for(auto i:prefix){int index=i-'a';if(!p->next[index])return false;p=p->next[index];} return true;}
};
287寻找重复数;6Z 字形变换;142环形链表 II;148排序链表;208实现 Trie (前缀树)相关推荐
- 【LeetCode】【HOT】287. 寻找重复数(抽象环形链表)
[LeetCode][HOT]287. 寻找重复数 文章目录 [LeetCode][HOT]287. 寻找重复数 package hot;public class Solution287 {publi ...
- python【力扣LeetCode算法题库】6-Z 字形变换
Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C ...
- LeetCode【5--最长的回文子串】 LeetCode【6--Z字形变换】
最长的回文子串 题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 解题思路 可以跟无重复的最长子串一样,用一个滑动窗口,只不过这个窗口的右边界往右,左 ...
- 287. 寻找重复数
寻找重复数 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. class So ...
- 【leetcode】287. 寻找重复数
题目链接:传送门 题目描述: 给定一个数组 nums 包含 n + 1 个整数,每个整数在 1 到 n 之间,包括 1 和 n.现在假设数组中存在一个重复的数字,找到该重复的数字. 注意 不能修改数组 ...
- 【LeetCode笔记】287. 寻找重复数(Java、快慢指针、原地、链表)
文章目录 题目描述 思路 & 代码 更新 题目描述 可以理解成数组版本的 环形链表 II 更多详细思路可见以上超链接. 思路 & 代码 如何转化成逻辑上的链表? nums[i] 是 第 ...
- leetcode题库6-- Z 字形变换
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...
- leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)
题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序, ...
- 【leetcode】287 寻找重复数(查找)
题目链接:https://leetcode-cn.com/problems/find-the-duplicate-number/ 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 ...
最新文章
- 美国防部官员讨论量子科学、5G和定向能的发展
- 属性为nil的时候测试是否crash nil是不会崩溃的
- 第 21 章 —— 单例模式
- Github 标星 13K+!这可能是最好的 Java 博客系统
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
- android 浮层菜单弹出,Android PopupWindow实现微信右上角的弹出菜单
- [zz]ASP.net中新建Web网站与新建Web应用程序的区别
- RSS导入功能已完成
- 基于图像的三维模型重建 ——双视角SFM
- 你觉得让Android 开发所向往的高薪岗位有哪些?
- java访问邮箱 apache,关于apache common-mail发邮件,部分邮箱无法接收有关问题
- 修复图片音频全新升级带特效喝酒神器小游戏微信小程序源码下载-多种游戏支持流量主
- 深度强化学习算法研究中的常用对比试验及作图技巧
- ctfshow萌新赛web
- 【从零开始vnpy量化投资】三. 手动安装vnpy环境
- 微软牵手大疆打造先进无人机技术
- PostGIS 入门
- 简单工厂和抽象工厂有什么区别?
- 《赢在中国》精彩评语
- 用摄像头实现远程监控咋搞不定呢
热门文章
- 使用Spring Security Oauth2 和 JWT保护微服务--Uaa授权服务器的编写
- C# 读取PDF文本和图片
- 矩阵的相似与特征值和特征向量定义
- python 同花顺thstrader_GitHub - hooddonkey/THSTrader: 量化交易。最新版通用版同花顺客户端的python API。(Python3)...
- Form表单, 4种常见的表单提交方式
- KeyPress 和KeyDown KeyPress之间的区别
- keydown、keypress与keyup用法及区别详解
- unittest 测试框架
- C语言数组初始化及逆向输出
- Sping Cloud Alibaba / netflix