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. ...
随机推荐
- win7系统自带截图工具快捷键是什么?怎么设置快捷键
win7自带的截图工具很好,很强大,比从网上下载的截图工具好用多了,很少会出现问题.但是它能不能像QQ截图工具一样可以使用快捷键呢?今天小编和大家分享下心得,希望能够给你的工作带来快捷. 工具/原料 ...
- Linux中ifreq 结构体分析和使用 及其在项目中的简单应用
[基础知识说明] 结构原型: /* * Interface request structure used for socket * ioctl's. All interface ioctl's mu ...
- GIS数据格式:Geodatabase
转自:http://www.cnblogs.com/quansixiang/archive/2010/09/17/1829286.html 1 Geodatabase概念 Geodatabase是A ...
- Yii框架-Smarty-整合
一.搭建yii框架 一.首先你得下个YII框架的源码 :下载地址:http://www.yiiframework.com/download/ 二.把下载到的源码解压放到一个PHP可访问的位置:如我的 ...
- Codeforces 302D
思路:最短路,map[i][j] = d*(|x[i]-x[j]| + |y[i]-y[j]|) - add[i] #include<iostream> #include<cstdi ...
- 用java api读取HDFS文件
import java.io.IOException; import java.io.InputStream; import java.security.PrivilegedExceptionActi ...
- 给Sublime Text 2安装CTags插件
以Windows操作系统为例介绍安装过程: 安装ctags应用程序. 到CTags的官方网站下载最新版本,解压后将ctags.exe文件放到系统的搜索路径中. 安装Sublime Text 2的Pac ...
- oracle 对象上锁,不能插入或删除情况
ora-00054:resource busy and acquire with nowait specified解决方法 当某个数据库用户在数据库中插入.更新.删除一个表的数据,或者增加一个表的主键 ...
- Spring笔记(四)SpingAOP
需要的Jar包(String3.2) com.springsource.net.sf.cglib-2.2.0.jar // 作用于cglib方式的动态代理 com.springsource.org.a ...
- 关于Eclispse连接Mysql的Jdbc
1.在Eclipse中新建Java工程 2.引入JDBC库(在bulid path 的extenrnal里) 3. 1)导入sql包(import java.sql.*) 2)加载(注册)mysql ...