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. ...
随机推荐
- 在Sublime Text 3中配置编译和运行C++程序
下载解压MinGW至目标目录,本次安装的解压目录为C:\MinGW 设置环境变量.右击我的电脑,属性-->高级-->环境变量. 在系统环境变量PATH里添加C:\MinGW\bin(如果里 ...
- Using innodb_large_prefix to avoid ERROR #1071,Specified key was too long; max key length is 1000 bytes
Using innodb_large_prefix to avoid ERROR 1071 单列索引限制上面有提到单列索引限制767,起因是256×3-1.这个3是字符最大占用空间(ut ...
- jdbcTemplate 获取数据表结构
jdbcTemplate 操作方法 /** *1.方法一: */ String sql = "select * from "+ tableName; //RowCountCallb ...
- 结构体page_cur_t
/** Type of the index page */ typedef byte page_t; /** Index page cursor */ typedef struct page_cur_ ...
- 从头开始编写一个Orchard网上商店模块(4) - 创建ProductPart
原文地址:http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-par ...
- HTML特殊字符大全2
HTML的特殊字符我们并不常用,但是有的时候却要在页面中用到这些字符,甚至有时候还需要用这些字符来实现某种特殊的视觉效果.现在,国外的设计师Neal Chester整理了一份很全的特殊字符集,我觉得这 ...
- C# 日期转换为中文大写
/// <summary> /// 日期转换为中文大写 /// </summary> public class UpperConvert { public UpperConve ...
- Java笔记(十一)……单例设计模式
设计模式 解决某一类问题最行之有效的方法 Java中有23中设计模式 单例设计模式 解决一个类在内存中只存在一个对象 思路 将构造函数私有化 在类中创建一个本类对象 提供一个方法可以获取到对象 两种方 ...
- java 小结3 hashcode和equals I/o问题
我需要把星期天看的一些东西记录下来,要不然会忘记. hashCode.equals: 1)每个java对象都有hashCode和equals方法. java的终极类是object类,那么object类 ...
- HW4.6
public class Solution { public static void main(String[] args) { final double MILES_PER_KILOGRAM = 1 ...