leetcode-25-exercise_string&array
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
解题思路:
以strs[0]为标准,LCP的长度必不超过strs[0]的长度,从前向后遍历。内层遍历所有的串,如果在所有串中都有strs[0][i],
就把它放到result中,否则返回。
string longestCommonPrefix(vector<string>& strs) {
int len = strs.size();
string result = "";
if (len == 0)
return result;
if (len == 1)
return strs[0];
if (strs[0] == "")
return strs[0];
for (int i = 0; i < strs[0].length(); i++) {
for (int j = 1; j < len; j++) {
if (strs[j].length() == i || strs[0][i] != strs[j][i])
return result;
}
result.push_back(strs[0][i]);
}
return result;
}
219. Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
解题思路:
使用unordered_map存储<nums[i], first_i>。如果当前i-first_i <= k,那就找到了。
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (k == 0)
return false;
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); i++) {
if (m.find(nums[i]) != m.end() && i - m[nums[i]] <= k)
return true;
else
m[nums[i]] = i;
}
return false;
}
leetcode-25-exercise_string&array的更多相关文章
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [链表]LeetCode 25 K组一个翻转链表
LeetCode 25 k组一个翻转链表 TITLE 示例 1: 输入:head = [1,2,3,4,5], k = 2 输出:[2,1,4,3,5] 示例 2: 输入:head = [1,2,3, ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [LeetCode] Sort Transformed Array 变换数组排序
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- [LeetCode] Product of Array Except Self 除本身之外的数组之积
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- Leetcode: Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- [Leetcode] Merge Sorted Array (C++)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...
随机推荐
- Jenkins权威指南
https://jenkins.io/doc/ ---官网 https://www.cnblogs.com/leefreeman/p/4226978.html
- 074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[ [1, 3, ...
- POJ 3735 Training little cats 矩阵快速幂
http://poj.org/problem?id=3735 给定一串操作,要这个操作连续执行m次后,最后剩下的值. 记矩阵T为一次操作后的值,那么T^m就是执行m次的值了.(其实这个还不太理解,但是 ...
- (转)Centos7安装配置NFS服务和挂载
Centos7安装配置NFS服务和挂载 原文:https://www.u22e.com/601.html NFS简介 NFS(Network File System)即网络文件系统,是FreeBSD支 ...
- 微信支付(java版本)_统一下单
最近工作接触到微信支付,刚开始解决微信支付很神秘,接触之后发现并没有那么神秘,就是有很多坑,在开发的时候需要注意,整理出来: 1.准备工作 首先需要登录微信支付公众平台阅读接口文档,地址:https: ...
- 个人博客 attack.cf
新开了个emlog搭的博客 地址:attack.cf 主要分享一下网络安全方面的东西和一些精品资源 欢迎来访
- PHP超全局变量、魔术变量和魔术方法
PHP超全局变量 $_GET 通过get方式传递的值(通过 URL 参数传递给当前脚本的变量的数组.) $_POST 通过post形式传递的值(当 HTTP POST 请求的 Content-Type ...
- IM云通信行业步入快车道,谁将成为代表中国的全球IM“独角兽”?
2016年,Twilio的成功上市,以及抢眼的股价表现,拓宽了全球云通信行业的想象空间,行业内公司估值水平也集体上调. 在中国,IM云通信行业也从2016年开始进入了一个“黄金发展时期”,一批如融云. ...
- IM云服务领域,融云因何得以登上浪潮之巅?
每一次技术驱动下的商业变革都是以浪潮的形式到来,每一次的涨潮都会带着赶上浪潮的企业登上新的巅峰.随着移动互联网的融合发展,短短几年期间,IM通讯云服务从早期的虚无飘渺到如今已经成为现代企业的标配,引发 ...
- UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)
读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...