Leetcode Python Solution(continue update)
leetcode python solution
1. two sum (easy)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
来源: https://leetcode.com/articles/two-sum/
solution
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
tmp_dict = {}
for i in range(len(nums)):
if target - nums[i] in tmp_dict:
return [tmp_dict[target - nums[i]], i]
else:
tmp_dict[nums[i]] = i
2. Reverse String (easy)
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
来源: https://leetcode.com/problems/reverse-string/
solution
class Solution(object):
def reverseString(self, s):
""
:type s: str
:rtype: str
"""
return s[::-1]
3. Nim Game(easy)
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
Example:
if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
来源: https://leetcode.com/problems/nim-game/
solution
class Solution(object):
def canWinNim(self, n):
"""
:type n: int
:rtype: bool
"""
return (n % 4 != 0)
Leetcode Python Solution(continue update)的更多相关文章
- Python之 continue继续循环和多重循环
Python之 continue继续循环 在循环过程中,可以用break退出当前循环,还可以用continue跳过后续循环代码,继续下一次循环. 假设我们已经写好了利用for循环计算平均分的代码: L ...
- Python字典(Dictionary)update()方法
原文连接:https://www.runoob.com/python/att-dictionary-update.html Python字典(dictionary)update()函数把字典dict2 ...
- LeetCode python实现题解(持续更新)
目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...
- Python break/continue - Python零基础入门教程
目录 一.break 二.continue 三.重点总结 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 在 Python wh ...
- Leetcode OJ : Compare Version Numbers Python solution
Total Accepted: 12400 Total Submissions: 83230 Compare two version numbers version1 and version2 ...
- Leetcode OJ : Repeated DNA Sequences hash python solution
Total Accepted: 3790 Total Submissions: 21072 All DNA is composed of a series of nucleotides abb ...
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
随机推荐
- 归纳决策树ID3(Java实现)
先上问题吧,我们统计了14天的气象数据(指标包括outlook,temperature,humidity,windy),并已知这些天气是否打球(play).如果给出新一天的气象指标数据:sunny,c ...
- Spring的annotation用在set方法上 hibernate的annotation用get方法上
1.Spring的annotation用在set方法上 2.hibernate的annotation用在get方法上
- Etcd学习(二)集群搭建Clustering
1.单个etcd节点(测试开发用) 之前我一直开发测试一直是用的一个Etcd节点,然后启动命令一直都是直接打一个etcd(我已经将etcd安装目录的bin目录加入到PATH环 境变量中),然后启动信息 ...
- BZOJ_1833_[ZJOI2010]_数字计数_(数位dp)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1833 统计\(a~b\)中数字\(0,1,2,...,9\)分别出现了多少次. 分析 数位dp ...
- Linux Kernel‘ieee80211_radiotap_iterator_init()’函数拒绝服务漏洞
漏洞名称: Linux Kernel‘ieee80211_radiotap_iterator_init()’函数拒绝服务漏洞 CNNVD编号: CNNVD-201312-041 发布时间: 2013- ...
- puppy 制作linux
经过一段时间的使用以后,我们每个人电脑里的Puppy Linux都是独一无二的,我们可以通过简单的方法将自己电脑上的Puppy制作成iso或Live-CD,成为自己玩的“Only You”Puppy ...
- sharepoint 2010 隐藏左边菜单left menu样式脚本
转:http://www.cfanz.cn/?c=article&a=read&id=60536 在v4.master中,<head></head>标签中,加入 ...
- (转载)php array_merge 和 两数组相加区别
(转载)http://www.cnblogs.com/shistou/archive/2013/03/16/2963586.html PHP中两个数组合并可以使用+或者array_merge,但之间还 ...
- Android中TextView输入字数统计和限制
在Android开发应用的时候,文本编辑框中最多输入140个字,经常会显示还剩多少字以限制用户输入的字数, EditText content;//定义一个文本输入框 TextView hasnum;/ ...
- bootstrap基本标签总结[转]
文件头: DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...