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的更多相关文章

  1. 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 ...

  2. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  3. [链表]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, ...

  4. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  5. [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( ...

  6. [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 ...

  7. [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. 这道 ...

  8. [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 ...

  9. 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( ...

  10. [Leetcode] Merge Sorted Array (C++)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...

随机推荐

  1. 关于EasyML的使用

    一.安装IntelliJ Idea 具体安装过程比较简单.但是遇到一个问题,如今LInux版本的IntelliJ的安装需要jdk1.8及以上版本的支持,但是EasyML目前仅支持jdk1.7的环境. ...

  2. CSS3 - CheakBox 开关效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. openstack安装newton版本Glance部署(二)

    一.部署Glance 1.Glance 安装 [root@linux-node1 ~]#yum install openstack-glance python-glance python-glance ...

  4. 前端开发如何做好SEO优化的工作

    前端开发工程师不仅需要要跟视觉设计师.交互式设计师配合,完美还原设计图稿,编写兼容各大浏览器.加载速度快.用户体验好的页面.现在还需要跟SEO人员配合,调整页面的代码结构和标签. 一些成熟的平台,在开 ...

  5. 编译安装php容易出现的问题以及解决办法

    http://crybit.com/20-common-php-compilation-errors-and-fix-unix/

  6. java容器集合

    java 集合分为 Collection 和 Map 两大类 Collection 是 Java 集合框架的顶层接口,它是对容器类进行增.删.改.查的定义,同时继承了 Iterable 接口,具有对集 ...

  7. Spring Cloud--搭建Eureka注册中心服务

    使用RestTemplate远程调用服务的弊端: Eureka注册中心: Eureka原理: 搭建Eureka服务 引pom 启动类: 启动类上要加上@EnableEurekaServer注解: 配置 ...

  8. (四)我的JavaScript系列:原型链

    夜深风竹敲秋韵,万叶千声皆是恨. 原型链对于JavaScript来说是个很核心的概念.JavaScript不是基于类模板的面向对象语言:反而,它的面向对象机制是基于原型的.我们不可能说某个对象属于什么 ...

  9. Windows Azure 配置Active Directory 主机(2)

    前一篇概况给大家介绍了,在云端部署一台DC 需要满足一些条件,接下来进入正题,云端VM安装域控制器具体步骤. 步骤1 :验证 主DC 的静态 IP 地址 1.登录到 Corp 网络上的 主DC. 2. ...

  10. python2含有中文路径报错解决办法[\xe4\xbf\xa1\xe6\x81\xaf]

    如图所示 百度的解决办法大多数是针对python3版本的,在脚本开头加# -*- coding:utf-8 -*-,但是python2版本加了编码格式,还是报错,具体解决办法是:path =unico ...