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 ...
随机推荐
- 用 scp 命令通过 SSH 互传文件
上传单个文件到远程服务器 命令格式 scp [/path/local_dir/filename] [username@servername:/path/remote_dir] 上传本地的 vimrc ...
- JS正则改变字符之间文字
var reg = /([[^[]*])/g; html = html.replace(reg, "<span class=\"bold\">$1</s ...
- 【转载】Ubuntu16.04安装最新版nodejs
安装最新版nodejs 更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties softw ...
- Android tess_two Android图片文字识别
文字识别一般都用的tesseract-ocr. GitHub:https://github.com/tesseract-ocr/tesseract 而Android对应的比较推荐的有个tess-two ...
- 运行JavaWeb项目报错Access denied for user 'root'@'localhost' (using password: YES)
问题重现:(以下讨论范围仅限Windows环境): C:\AppServ\MySQL> mysql -u root -p Enter password: ERROR 1045 (28000): ...
- POJ 1185 炮兵阵地 (状压DP,轮廓线DP)
题意: 给一个n*m的矩阵,每个格子中有'P'或者'H',分别表示平地和高原,平地可以摆放大炮,而大炮的攻击范围在4个方向都是2格(除了自身位置),攻击范围内不能有其他炮,问最多能放多少个炮?(n&l ...
- 洛谷 P1079 Vigenère 密码
题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南 ...
- thisnkphp添加二维码
Rcode二维码生成类QRcode.class.php实例演示 <?php //import('@.Org.QRcode');//thinkphp include_once('QRcode.cl ...
- EditPlus 3.7激活码注册码
EditPlus3.7激活教程以及EditPlus3.7激活码使用方法 EditPlus是一款功能齐全的文字编辑器,搭配其他的插件还可以实现很多的功能,还可以编辑和编译Java,调试程序等,主要用来打 ...
- JS实现全排列
https://www.jb51.net/article/39291.htm JavaScript全排列的六种算法 具体实现 算法一:交换(递归) 复制代码代码如下: <html xmlns=& ...