LeetCode(15) 3Sum】的更多相关文章

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so…
题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i > 0 and nums[i] == nums[i-1]: continue l, r = i+1, len(nums)-1 while l < r: s = nums[i] + nums[l] + nums[r] if s < 0: l +=1 elif s > 0: r -= 1 el…
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ] 自己的思路:如果追求思路简单,就是暴力的去求解每三个字符的和是否为0,但是有三层循环,时间复杂度太高,提交,超出时间限制. (1) 排序.…
Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ] 解题思路: 这道题让我们求三数之和,比之前那道Two Sum要复杂一些,考虑过先fix一个数,然后另外两个数使…
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1…
hard! 题目描述: 给出一个链表,每 k 个节点为一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定这个链表:1->2->3->4->5 当 k = 2 时,应当返回: 2->1->4->3->5 当 k = 3 时,应当返回: 3->2->1->4->5 说明 : 你的算法只能使用常数的额外空间. 你不能只是单纯的改变…
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, return its bottom-up level order traversal as: 分析 与…
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GRE/VxLAN 虚拟网络 (4)Neutron OVS OpenFlow 流表 和 L2 Population (5)Neutron DHCP Agent (6)Neutron L3 Agent (7)Neutron LBaas (8)Neutron Security Group (9)Neutro…
(15)剪切文件夹 ① 通过rename($oldname,$newname) 函数实现剪切文件夹的操作 ② 需要检测目标文件夹是否存在,如果存在还要检测目标目录中是否存在同名文件夹,如果不存在则剪切 dir.function.php 添加: //剪切文件夹 function cutFolder($src,$dst){ if(!file_exists($dst)){ return '目标目录不存在'; }else{ if(!is_dir($dst)){ return '不是目录'; }else{…
Maven学习系列二(1-5) 本文转自 QuantSeven 博客,讲解精炼易懂,适合入门,链接及截图如下 http://www.cnblogs.com/quanyongan/category/471332.html…
C语言的本质(15)--C语言的函数接口 函数的调用者和其实现者之间存在一个协议,在调用函数之前,调用者要为实现者提供某些条件,在函数返回时,实现者完成调用者需要的功能. 函数接口通过函数名,参数和返回值来描述这个协议,只要函数名和参数名命名合理,参数和返回值的类型定义的准确,调用者仅仅通过函数接口就能知道函数的用法.当函数接口不能表达函数的全部语义时,文档就起了重要的补充作用,函数文档的写法我们可以参照Linux下的Man Page或MSDN. 下面通过分析C标准库函数来说明函数接口: 在Li…
原文:Windows Phone开发(15):资源 活字印刷术是我国"四大发明"之一,毕昇在发明活字印刷术之后,他很快发现一个问题,随着要印刷资料的不断增加,要用到的汉字数目越来越多,于是,他必须寻找一种有效的办法去管理那些刻有汉字的立方体(暂且就叫立方体,其实的确是个立方体),所以,他就和助手们一起努力,为这些立方体进行记录,有标识地放好,在印刷过程中用到哪些字,就直接取出来,不用了就放回去,既环保又方便. 这就是资源,水.空气.阳光也是资源,煤.铁矿物也是资源,只不过有些可再生,有…
原文:Android菜鸟的成长笔记(15)-- Android中的状态保存探究(下) 在上一篇中我们简单了解关于Android中状态保存的过程和原理,这一篇中我们来看一下在系统配置改变的情况下保存数据及恢复数据的过程. 下面我们先来看一个现象:(代码在 Android中状态保存探究(上)中) 先启动应用如下: 打印的Log 再翻转屏幕 打印的Log如下 可以看到每翻转一次屏幕实际上系统会停止原理的activity并销毁然后重新启动一次,在这个过程中会调用onSaveInstanceState方法…
原文:MEF框架在Silverlight中应用(15) MEF框架在Silverlight中应用 代码下载 MEF框架是微软提供的一个插件框架.应用概括为一句话:输入,输出,组合.下面是具体在Silverlight中应用中的关键代码. 步骤 1.Client端建立3个Silverlight项目.   MEFSilverLight4(主项目,组装部件)   SilverlightApplication1(部件)   SilverlightApplication2(部件) 2.Server建立一个测…
redis入门(15)redis的数据备份和恢复…
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GRE/VxLAN 虚拟网络 (4)Neutron OVS OpenFlow 流表 和 L2 Population (5)Neutron DHCP Agent (6)Neutron L3 Agent (7)Neutron LBaas (8)Neutron Security Group (9)Neutro…
Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public class Originator { private int number; private File file = null; public Originator(){} // 创建一个Memento public Memento getMemento(){ return new Memento(this)…
程序1 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: import os def countfile(path): dict1 = {} # 定义一个字典 all_files = os.listdir(path) for each_file in all_files: if os.path.isdir(os.path.join(path,each_file)): dict1.setdefault('文件夹', 0) # setdefault:如果字典中包含有给定键, # 则返…
[译]MVC3 20个秘方-(15)使用CAPTCHA去防止恶意软件自动提交评论(防灌水)   问题 有种不太幸运的情况,有人用自动程序去提交表单,在整个互联网中造成大量的垃圾.为了防止这种情况的方法之一,是使用一个验证码---CAPTCHA:全自动区分计算机和人类的图灵测试,这迫使用户把生成的文字输入到文本框. (译者:CAPTCHA是一种更人性化的验证码,可以通过视觉和听觉来区分post的请求是人类还是计算机发出的) 解决方案 从NuGet安装ASP.NET Web Helpers Libr…
leecode刷题(15)-- 验证回文字符串 验证回文字符串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 思路: 刚开始和上一题一样,我也没理解"回文字符串"是什么意思,后来想了下,&quo…
Qt 学习之路 2(15):标准对话框 QMessageBox  豆子  2012年9月18日  Qt 学习之路 2  40条评论 所谓标准对话框,是 Qt 内置的一系列对话框,用于简化开发.事实上,有很多对话框都是通用的,比如打开文件.设置颜色.打印设置等.这些对话框在所有程序中几乎相同,因此没有必要在每一个程序中都自己实现这么一个对话框. Qt 的内置对话框大致分为以下几类: QColorDialog:选择颜色: QFileDialog:选择文件或者目录: QFontDialog:选择字体:…
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 分析 同LeetCode(274)H-Index第二个版本,给定引用数量序列为递增的:这就省略了我们的第一个排序步骤: O(n)的时间复杂度,遍历一次即可. AC代码 class Solution { public: int hIndex(vector<int>…
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 分析 题目描述:给定一个整数序列,查找是否存在两个下标分别为i和j的元…
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh…
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times).…
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially,…
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 分析 本题目与上一题LeetCode(112) Path Sum虽然类型相同,但是需要在以前的基础上,多做处理一些: 与Path Sum相比,本题是求出路径,所以,在找到满足的路…
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 分析 给定两个有序序列,要求两个序列综合后的中位数.关键:算法复杂度T(n)=O(log(m+n)) 该问题的关键就在于复杂度的限制,有了这个限制,就…
在前面学习ActiveMQ时,看到ActiveMQ可以是队列消息模式,也可以是订阅发布模式. 同样,在RocketMQ中,也存在两种消息模式,即是集群消费模式和广播消费模式. 1. 集群消费模式 跟AciiveMQ一样,当存在多个消费者时,消息通过一定负载均衡策略,将消息分发到多个consumer中. 如图: 在RockeMQ中,通过ConsumeGroup的机制,实现了天然的消息负载均衡!通俗点来说,RocketMQ中的消息通过ConsumeGroup实现了将消息分发到C1/C2/C3/……的…
如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! 今天给大家讲一下MaterialApp应用组件及routes路由详解,我会着重说一下routes路由的使用及解释,因为会经常用到,前面我写的一些demo所涉及到的组件,都是遵循着Material Design设计风格,所谓的Material Design是由Goodle推出的全新的设计语言,这种设…