Site updated: 2020-02-25 23:26:49

This commit is contained in:
Jimmy Xiang
2020-02-25 23:26:49 +08:00
parent 6bb5cfd83d
commit eaceb4754d
8 changed files with 234 additions and 0 deletions

View File

@@ -95,6 +95,19 @@
<div class="wrapper index-wrapper">
<ul class="container list-container">
<li class="post-i-wrapper">
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
<div class="wrapper cover">
<img src="/images/leetcode-0101.png" alt="LCCI 01.01.判定字符是否唯一">
</div>
<h3 class="wrapper title" title="LCCI 01.01.判定字符是否唯一">LCCI 01.01.判定字符是否唯一</h3>
<p class="wrapper subtitle">
LeetCode刷题计划
</p>
<div class="wrapper time">2020-02-25</div>
</a>
</li>
<li class="post-i-wrapper">
<a href="/article/python-leetcode-0000/" class="wrapper post-i-link" title="0000.为什么需要复杂度分析">
<div class="wrapper cover">

View File

@@ -95,6 +95,19 @@
<div class="wrapper index-wrapper">
<ul class="container list-container">
<li class="post-i-wrapper">
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
<div class="wrapper cover">
<img src="/images/leetcode-0101.png" alt="LCCI 01.01.判定字符是否唯一">
</div>
<h3 class="wrapper title" title="LCCI 01.01.判定字符是否唯一">LCCI 01.01.判定字符是否唯一</h3>
<p class="wrapper subtitle">
LeetCode刷题计划
</p>
<div class="wrapper time">2020-02-25</div>
</a>
</li>
<li class="post-i-wrapper">
<a href="/article/python-leetcode-0000/" class="wrapper post-i-link" title="0000.为什么需要复杂度分析">
<div class="wrapper cover">

View File

@@ -95,6 +95,19 @@
<div class="wrapper index-wrapper">
<ul class="container list-container">
<li class="post-i-wrapper">
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
<div class="wrapper cover">
<img src="/images/leetcode-0101.png" alt="LCCI 01.01.判定字符是否唯一">
</div>
<h3 class="wrapper title" title="LCCI 01.01.判定字符是否唯一">LCCI 01.01.判定字符是否唯一</h3>
<p class="wrapper subtitle">
LeetCode刷题计划
</p>
<div class="wrapper time">2020-02-25</div>
</a>
</li>
<li class="post-i-wrapper">
<a href="/article/python-leetcode-0000/" class="wrapper post-i-link" title="0000.为什么需要复杂度分析">
<div class="wrapper cover">

View File

@@ -0,0 +1,156 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<head><meta name="generator" content="Hexo 3.9.0">
<!-- Title -->
<meta charset="utf-8">
<meta name="applicable-device" content="pc,mobile">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0, viewport-fit=cover">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="author" content="flypython">
<meta name="designer" content="flypython">
<meta name="keywords" content="LCCI 01.01.判定字符是否唯一,FlyPython - 专业的Python学习社区,flypython, 飞蟒飞蟒PythonPython入门Python自动化Python日报">
<meta property="og:title" content="LCCI 01.01.判定字符是否唯一 | FlyPython - 专业的Python学习社区">
<meta property="og:site_name" content="http://www.flypython.com">
<meta property="og:type" content="article">
<meta property="og:url" content="http://www.flypython.com/article/python-leetcode-lcci0101/">
<meta property="og:image" content="http://www.flypython.com/images/leetcode-0101.png">
<meta property="og:description" content="LCCI 01.01.判定字符是否唯一--LeetCode刷题计划">
<meta name="description" content="LCCI 01.01.判定字符是否唯一--LeetCode刷题计划">
<meta name="rating" content="general">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="robots" content="index, follow">
<link rel="icon" href="/images/favicon.ico">
<title>LCCI 01.01.判定字符是否唯一 | FlyPython - 专业的Python学习社区</title>
<link rel="stylesheet" href="/css/f25.css">
<link rel="stylesheet" href="/css/highlight.css">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-147288599-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-147288599-1');
</script>
</head>
</head>
<body>
<header class="wrapper header-wrapper">
<div class="container header-nav-wrapper">
<div class="logo"><a href="/" title="FlyPython - 专业的Python学习社区"><h1 class="title">FlyPython</h1></a></div>
<nav class="nav-wrapper">
<a href="https://flypython.com/python" title="飞蟒微课堂">飞蟒微课堂</a>
<a href="https://flypython.com/flypython_daily" title="Python日报">Python日报</a>
<a href="https://flypython.com/PyCon/" title="PyCon">PyCon</a>
<a href="https://github.com/flypythoncom" title="Github">Github</a>
<a href="/article/about" title="关于">关于</a>
</nav>
<span class="btn-menu" id="J_header_menu">
<div class="inner">
<span class="line line-01"></span>
<span class="line line-02"></span>
<span class="line line-03"></span>
</div>
</span>
<div class="wrapper mb-nav-wrapper" id="J_header_menu_list">
<nav class="wrapper mb-nav-container">
<a href="https://flypython.com/python" title="飞蟒微课堂">飞蟒微课堂</a>
<a href="https://flypython.com/flypython_daily" title="Python日报">Python日报</a>
<a href="https://flypython.com/PyCon/" title="PyCon">PyCon</a>
<a href="https://github.com/flypythoncom" title="Github">Github</a>
<a href="/article/about" title="关于">关于</a>
</nav>
</div>
</div>
</header>
<section class="body-wrapper">
<section class="wrapper post-banner">
<div class="container post-banner-container">
<h2 class="wrapper title">LCCI 01.01.判定字符是否唯一</h2>
<div class="wrapper tips">
<span>Author</span><span>flypython</span> | <span>Date: </span><span>2020-02-25</span> | <span>Category</span><span><a href="/fly/飞蟒微课堂/" title="飞蟒微课堂">飞蟒微课堂</a><a href="/fly/飞蟒微课堂/LeetCode/" title="LeetCode">LeetCode</a></span>
</div>
</div>
</section>
<section class="wrapper main-wrapper">
<article class="sub-container post-content">
<h2 id="01-01-判定字符是否唯一"><a href="#01-01-判定字符是否唯一" class="headerlink" title="01.01.判定字符是否唯一"></a>01.01.判定字符是否唯一</h2><p>链接:<a href="https://leetcode-cn.com/problems/is-unique-lcci/" target="_blank" rel="noopener">https://leetcode-cn.com/problems/is-unique-lcci/</a><br>难度:简单</p>
<h4 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h4><blockquote>
<p>实现一个算法,确定一个字符串 s 的所有字符是否全都不同。</p>
<p>示例 1</p>
<p>输入: s = “leetcode”<br>输出: false<br>示例 2</p>
<p>输入: s = “abc”<br>输出: true</p>
<p>限制:</p>
<p>0 &lt;= len(s) &lt;= 100<br>如果你不使用额外的数据结构,会很加分。</p>
</blockquote>
<h4 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h4><p>首先,你得理解题意。不确定的东西,需要问或者尝试验证。</p>
<p>比如这里你可以问面试官字符串s是ASCII还是Unicode字符串如果是编程平台你可以写测试来判断。</p>
<p>在这里我们假定字符集为ASCII那如果字符串长度大于128那肯定是false不过有限制条件len(s)&lt;=100那这个判断就不必要了。限制里面还要求不使用额外的数据结构这就过滤掉了很多的方案。</p>
<p>比如使用set就一行代码解决</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def isUnique(self, astr: str) -&gt; bool:</span><br><span class="line"> return len(astr)== len(set(astr))</span><br></pre></td></tr></table></figure>
<p>我们很容易想到hash计数的方法经过测试字符串中只出现az26个小写字母那只需要一个bool数组直接统计出现的频率判断是否重复。</p>
<p>考虑到不使用额外的数据结构那我们可以尝试使用bitmap把数组换成一个整型数按照位上01来判断是否重复。</p>
<p>开始我们算出字符离最开始的字符a的距离然后1移动这个距离来表示这个数。后面用一个测试变量来测试p如果存在则返回False如果不存在则写入。</p>
<p>具体代码:</p>
<pre><code>def isUnique(self, astr: str) -&gt; bool:
t = 0
for c in astr:
if t &amp; (p := 1 &lt;&lt; (ord(c) - ord(&apos;a&apos;))):
return False
t |= p
return True</code></pre><p>在解答中使用了Python3.8的新特性:海象运算符,点击下面的链接,学习使用它。<br><a href="https://mp.weixin.qq.com/s/3xKdt-26guYHoFb3xP0muw" target="_blank" rel="noopener">https://mp.weixin.qq.com/s/3xKdt-26guYHoFb3xP0muw</a></p>
<h4 id="方案代码"><a href="#方案代码" class="headerlink" title="方案代码"></a>方案代码</h4><p>解法:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def isUnique(self, astr: str) -&gt; bool:</span><br><span class="line"> t = 0</span><br><span class="line"> for c in astr:</span><br><span class="line"> if t &amp; (p := 1 &lt;&lt; (ord(c) - ord(&apos;a&apos;))):</span><br><span class="line"> return False</span><br><span class="line"> t |= p</span><br><span class="line"> return True</span><br></pre></td></tr></table></figure>
<p>另外还有三种使用数据结构的解法</p>
<p>解法1:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def isUnique(self, astr: str) -&gt; bool:</span><br><span class="line"> return len(astr)== len(set(astr))</span><br></pre></td></tr></table></figure>
<p>解法2:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def isUnique(self, astr: str) -&gt; bool:</span><br><span class="line"> adict=&#123;&#125;</span><br><span class="line"> for value in astr:</span><br><span class="line"> if value in adict.keys():</span><br><span class="line"> return False</span><br><span class="line"> else:</span><br><span class="line"> adict[value]=0</span><br><span class="line"> return True</span><br></pre></td></tr></table></figure>
<p>解法3:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def isUnique(self, astr: str) -&gt; bool:</span><br><span class="line"> l=[]</span><br><span class="line"> for i in astr:</span><br><span class="line"> if i in l:</span><br><span class="line"> return False</span><br><span class="line"> else:</span><br><span class="line"> l.append(i)</span><br><span class="line"> return True</span><br></pre></td></tr></table></figure>
</article>
<div class="sub-container gitalk-wrapper" id="gitalk-container"></div>
</section>
<div class="tips-top-wrapper">
<span class="tip-top-container" onclick="scrollToWindowTop()">
<span class="l-bar"></span>
<span class="r-bar"></span>
</span>
</div>
<footer class="wrapper footer-wrapper">
<div class="container"><span class="copyright">&copy; 2020 FlyPython . All Rights Reserved.</span></div>
</footer>
</section>
<script src="/js/f25.js"></script>
</body>
</html>

View File

@@ -95,6 +95,19 @@
<div class="wrapper index-wrapper">
<ul class="container list-container">
<li class="post-i-wrapper">
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
<div class="wrapper cover">
<img src="/images/leetcode-0101.png" alt="LCCI 01.01.判定字符是否唯一">
</div>
<h3 class="wrapper title" title="LCCI 01.01.判定字符是否唯一">LCCI 01.01.判定字符是否唯一</h3>
<p class="wrapper subtitle">
LeetCode刷题计划
</p>
<div class="wrapper time">2020-02-25</div>
</a>
</li>
<li class="post-i-wrapper">
<a href="/article/python-leetcode-0000/" class="wrapper post-i-link" title="0000.为什么需要复杂度分析">
<div class="wrapper cover">

View File

@@ -95,6 +95,19 @@
<div class="wrapper index-wrapper">
<ul class="container list-container">
<li class="post-i-wrapper">
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
<div class="wrapper cover">
<img src="/images/leetcode-0101.png" alt="LCCI 01.01.判定字符是否唯一">
</div>
<h3 class="wrapper title" title="LCCI 01.01.判定字符是否唯一">LCCI 01.01.判定字符是否唯一</h3>
<p class="wrapper subtitle">
LeetCode刷题计划
</p>
<div class="wrapper time">2020-02-25</div>
</a>
</li>
<li class="post-i-wrapper">
<a href="/article/python-leetcode-0000/" class="wrapper post-i-link" title="0000.为什么需要复杂度分析">
<div class="wrapper cover">

BIN
images/leetcode-0101.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 497 KiB

View File

@@ -95,6 +95,19 @@
<div class="wrapper index-wrapper">
<ul class="container list-container">
<li class="post-i-wrapper">
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
<div class="wrapper cover">
<img src="/images/leetcode-0101.png" alt="LCCI 01.01.判定字符是否唯一">
</div>
<h3 class="wrapper title" title="LCCI 01.01.判定字符是否唯一">LCCI 01.01.判定字符是否唯一</h3>
<p class="wrapper subtitle">
LeetCode刷题计划
</p>
<div class="wrapper time">2020-02-25</div>
</a>
</li>
<li class="post-i-wrapper">
<a href="/article/python-leetcode-0000/" class="wrapper post-i-link" title="0000.为什么需要复杂度分析">
<div class="wrapper cover">