领扣[LeetCode]从零开始[使用C++][1,10]
0.序
以后不做后端开发是不是就用不到C++了?真香。话不多说,我已经躺倒在第一题上了。不贴题目了,持续更新。
1.两数之和
原文:https://www.cnblogs.com/grandyang/p/4130379.html
解1:
搬过来是搬过来了,理解了才是自己的。但是理解也要好久啊o(╥﹏╥)o。
class Solution {
public: //公有,谁都能调用这里面的东西
vector<int> twoSum(vector<int>& nums, int target) { //vector<int>是返回值,twoSum是函数名
unordered_map<int, int> m; //哇,m是一个哈希表,元素无序但是搜索的时间复杂度是O(1),然后空间复杂度高了呗,但是LeetCode里谁快谁是大哥
vector<int> res;
for (int i = ; i < nums.size(); ++i) { //由值得到下标
m[nums[i]] = i;
}
for (int i = ; i < nums.size(); ++i) {
int t = target - nums[i];
if (m.count(t) && m[t] != i) { //t存在且不是它自己,count()统计了t出现的次数
res.push_back(i); //把i放进一个叫res的容器
res.push_back(m[t]); //知道了t的下标,把它放进容器
break; //跳出for循环
}
}
return res;
}
};
解2:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) { //这里加一个&到底是干嘛的呢
unordered_map<int, int> m; //又是一个哈希表
for (int i = ; i < nums.size(); ++i) { //喵的,上面两个for循环这里直接就一个了,原理都是一样的
if (m.count(target - nums[i])) {
return {i, m[target - nums[i]]};
}
m[nums[i]] = i;
}
return {};
}
};
解3:
好的,接下来让我自己来写一个时间复杂度超高的解。emmm,执行时间108ms,是上面两个解的13.5倍。要不要先判断非空呀?
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i=; i<nums.size(); i++)
for (int j=i+; j<nums.size(); j++)
if (target == nums[i]+nums[j]) return{i,j};
return {};
}
};
2018-12-6 提交
2.两数相加
原文:https://blog.csdn.net/qq_32805671/article/details/79883391
解:
链表的知识,在数据结构里学过,看了下面的代码才想起来怎么用,哭。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {} //带参数的构造函数
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *result = new ListNode(); //给0分配内存,作为头结点
ListNode *tmp = result; //temporal,临时指针tmp
int sum = ;
while(l1 || l2){
if(l1){
sum += l1->val;
l1 = l1->next;
}
if(l2){
sum += l2->val;
l2 = l2->next;
}
tmp->next = new ListNode(sum%);
sum /= ; //如果sum的值大于等于10的话是要进1位的
tmp = tmp->next;
} //while
if(sum)
tmp->next = new ListNode();
return result->next;
} //addTwoNumbers
}; //class Solution
2018-12-7 提交
3. 无重复字符的最长子串
原文:https://www.cnblogs.com/ariel-dreamland/p/8668286.html
解1:
class Solution
{
public:
int lengthOfLongestSubstring(string s){
int m[] = {}, res = , left = ;//ASCII有256个字符
for (int i = ; i < s.size(); ++i){
if(m[s[i]]==||m[s[i]]<left){//字符串中第i个元素以前没出现过,或出现在很左边的位置
res = max(res, i-left+);
}else{
left=m[s[i]];
}
m[s[i]]=i+;//为了和缺省的0区分,所以要+1吧
}
return res;
}
};
enmm,花了一个小时才大概搞懂一题的一个解哟,还没完呢。从sublime复制过来的注释是绿色的耶。
2018-12-8 提交
解2:
解1的简化。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> m(, -);
int res = , left = -;
for (int i = ; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};
解3:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
set<char> t;
int res = , left = , right = ;
while (right < s.size()) {
if (t.find(s[right]) == t.end()) {
t.insert(s[right++]);
res = max(res, (int)t.size());
} else {
t.erase(s[left++]);
}
}
return res;
}
};
解4:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res = , left = , i = , n = s.size();
unordered_map<char, int> m;
for (int i = ; i < n; ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i + ;
res = max(res, i - left + );
}
return res;
}
};
2018-12-9 提交
-
领扣[LeetCode]从零开始[使用C++][1,10]的更多相关文章
- 领扣(LeetCode)第三大的数 个人题解
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...
- 领扣(LeetCode)删除链表中的节点 个人题解
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 - ...
- 【力扣leetcode】-787. K站中转内最便宜的航班
题目描述: 有 n 个城市通过一些航班连接.给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 p ...
- 领扣-754 到达终点数字 Reach a Number MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 领扣-1/167 两数之和 Two Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数
最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...
- 领扣-5 最长回文子串 Longest Palindromic Substring MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 领扣-两数之和-Python实现
领扣每日一题 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- 领扣-121/122/123/188 最佳买卖时机 Best Time to Buy and Sell MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
随机推荐
- 使用transfor让图片旋转
材料:Transform,onmouseout,onmouseover css: html: js:
- SpringBoot非官方教程 | 第十二篇:springboot集成apidoc
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-apidoc/ 本文出自方志朋的博客 首先声明下 ...
- Python常用模块之os和sys
1.OS常用方法 os.access(path, mode) # 检验权限模式 os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirn ...
- view添加毛玻璃效果两种方法
第一种方法: UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectV ...
- PHP如何实现99乘法表?
看到这个问题,可能大家更多的是考虑到用for循环,个人觉得使用for循环太影响程序性能.推荐使用递归处理. /** * Title : 递归实现99乘法表 * Author : Bruceqi * ...
- java服务端项目开发规范
更新内容 2015-03-13 (请先更新svn的mybatis.xml.BaseMapper.java.Pager.java文件) 加入测试类规范 加入事物控制规范 加入mapper接口规则 ...
- python应用:爬虫实例(静态网页)
爬取起点中文网某本小说实例: # -*-coding:utf8-*- import requests import urllib import urllib2 from bs4 import Beau ...
- LocalDate相关方法
getYear() int 获取当前日期的年份 getMonth() Month 获取当前日期的月份对象 getMonthValue() int 获取当前日期是第几月 getDayOfWeek() D ...
- asp.net core mvc简介
MVC 通常而言,我们使用.NET Core MVC 构建网页应用与 API,MVC是使用模型-视图-控制器(Model-View-Controller)设计模式. 创建项目 使用如下命令创建一个名称 ...
- Git更改远程仓库地址
最近在开发一个公司内部的公共组件库.老大整理了git仓库里的一些项目,其中就包括这个项目. 项目git地址变了,于是我本地的代码提交成功后push失败. 查看远程地址 git remote -v 更改 ...