『LeetCode』练习第六弹_算法9题

9. Palindrome Number

判断回文数

 1 class Solution(object):
 2     def isPalindrome(self, x):
 3         """
 4         :type x: int
 5         :rtype: bool
 6         """
 7         if x<0:
 8             return False
 9         y = int(str(abs(x))[::-1])
10         if x == y:
11             return True
12         else:
13             return False

结果:Accepted,虽然本来就是easy难度的,但仍然感觉用python写好赖皮

时间: 2024-08-05 23:14:22

『LeetCode』练习第六弹_算法9题的相关文章

『LeetCode』练习第四弹_算法6题

6. ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by l

『PyTorch』第十二弹_nn.Module和nn.functional

大部分nn中的层class都有nn.function对应,其区别是: nn.Module实现的layer是由class Layer(nn.Module)定义的特殊类,会自动提取可学习参数nn.Parameter nn.functional中的函数更像是纯函数,由def function(input)定义. 由于两者性能差异不大,所以具体使用取决于个人喜好.对于激活函数和池化层,由于没有可学习参数,一般使用nn.functional完成,其他的有学习参数的部分则使用类.但是Droupout由于在训

『PyTorch』第六弹_最小二乘法的不同实现手段(待续)

PyTorch的Variable import torch as t from torch.autograd import Variable as V import matplotlib.pyplot as plt from IPython import display # 指定随机数种子 t.manual_seed(1000) def get_fake_data(batch_size=8): x = t.rand(batch_size,1)*20 y = x * 2 + 3 + 3*t.ran

『MXNet』第六弹_数据处理API(待续)

一.Gluon数据加载 图片数据(含标签)加载函数:gluon.data.vision.ImageFolderDataset 给出ImageFolderDataset类的描述, Init signature: mxnet.gluon.data.vision.datasets.ImageFolderDataset(root, flag=1, transform=None) Source: class ImageFolderDataset(dataset.Dataset): """

『LeetCode』练习第二弹

3. Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", t

『TensorFlow』队列&amp;多线程&amp;TFRecod文件_我辈当高歌

TF数据读取队列机制详解 TFR文件多线程队列读写操作: TFRecod文件写入操作: import tensorflow as tf def _int64_feature(value): # value必须是可迭代对象 # 非int的数据使用bytes取代int64即可 return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) num_shards = 2 instance_perPshard = 2 for i

『cs231n』作业2选讲_通过代码理解卷积层&amp;池化层

卷积层 卷积层向前传播示意图: def conv_forward_naive(x, w, b, conv_param): """ A naive implementation of the forward pass for a convolutional layer. The input consists of N data points, each with C channels, height H and width W. We convolve each input w

『cs231n』作业2选讲_通过代码理解优化器

1).Adagrad一种自适应学习率算法,实现代码如下: cache += dx**2 x += - learning_rate * dx / (np.sqrt(cache) + eps) 这种方法的好处是,对于高梯度的权重,它们的有效学习率被降低了:而小梯度的权重迭代过程中学习率提升了.要注意的是,这里开根号很重要.平滑参数eps是为了避免除以0的情况,eps一般取值1e-4 到1e-8. 2).RMSpropRMSProp方法对Adagrad算法做了一个简单的优化,以减缓它的迭代强度: ca

『PyTorch』第十四弹_torch.nn.Module深入分析

nn.Module基类的构造函数: def __init__(self): self._parameters = OrderedDict() self._modules = OrderedDict() self._buffers = OrderedDict() self._backward_hooks = OrderedDict() self._forward_hooks = OrderedDict() self.training = True 其中每个属性的解释如下: _parameters: