|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="zh-CN"> |
| 3 | +<head> |
| 4 | + <head><meta name="generator" content="Hexo 3.9.0"> |
| 5 | + <!-- Title --> |
| 6 | + |
| 7 | + <meta charset="utf-8"> |
| 8 | + <meta name="applicable-device" content="pc,mobile"> |
| 9 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0, viewport-fit=cover"> |
| 10 | + <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> |
| 11 | + <meta name="author" content="flypython"> |
| 12 | + <meta name="designer" content="flypython"> |
| 13 | + <meta name="keywords" content="LCCI 01.01.判定字符是否唯一,FlyPython - 专业的Python学习社区,flypython, 飞蟒,飞蟒Python,Python入门,Python自动化,Python日报"> |
| 14 | + <meta property="og:title" content="LCCI 01.01.判定字符是否唯一 | FlyPython - 专业的Python学习社区"> |
| 15 | + <meta property="og:site_name" content="http://www.flypython.com"> |
| 16 | + |
| 17 | + <meta property="og:type" content="article"> |
| 18 | + <meta property="og:url" content="http://www.flypython.com/article/python-leetcode-lcci0101/"> |
| 19 | + <meta property="og:image" content="http://www.flypython.com/images/leetcode-0101.png"> |
| 20 | + <meta property="og:description" content="LCCI 01.01.判定字符是否唯一--LeetCode刷题计划"> |
| 21 | + <meta name="description" content="LCCI 01.01.判定字符是否唯一--LeetCode刷题计划"> |
| 22 | + |
| 23 | + <meta name="rating" content="general"> |
| 24 | + <meta name="apple-mobile-web-app-capable" content="yes"> |
| 25 | + <meta name="apple-mobile-web-app-status-bar-style" content="black"> |
| 26 | + <meta name="format-detection" content="telephone=yes"> |
| 27 | + <meta name="mobile-web-app-capable" content="yes"> |
| 28 | + <meta name="robots" content="index, follow"> |
| 29 | + <link rel="icon" href="/images/favicon.ico"> |
| 30 | + <title>LCCI 01.01.判定字符是否唯一 | FlyPython - 专业的Python学习社区</title> |
| 31 | + <link rel="stylesheet" href="/css/f25.css"> |
| 32 | + <link rel="stylesheet" href="/css/highlight.css"> |
| 33 | + |
| 34 | + |
| 35 | +<!-- Global site tag (gtag.js) - Google Analytics --> |
| 36 | +<script async src="https://www.googletagmanager.com/gtag/js?id=UA-147288599-1"></script> |
| 37 | +<script> |
| 38 | + window.dataLayer = window.dataLayer || []; |
| 39 | + function gtag(){dataLayer.push(arguments);} |
| 40 | + gtag('js', new Date()); |
| 41 | + |
| 42 | + gtag('config', 'UA-147288599-1'); |
| 43 | +</script> |
| 44 | + |
| 45 | +</head> |
| 46 | +</head> |
| 47 | +<body> |
| 48 | + <header class="wrapper header-wrapper"> |
| 49 | + <div class="container header-nav-wrapper"> |
| 50 | + <div class="logo"><a href="/" title="FlyPython - 专业的Python学习社区"><h1 class="title">FlyPython</h1></a></div> |
| 51 | + <nav class="nav-wrapper"> |
| 52 | + |
| 53 | + <a href="https://flypython.com/python" title="飞蟒微课堂">飞蟒微课堂</a> |
| 54 | + |
| 55 | + <a href="https://flypython.com/flypython_daily" title="Python日报">Python日报</a> |
| 56 | + |
| 57 | + <a href="https://flypython.com/PyCon/" title="PyCon">PyCon</a> |
| 58 | + |
| 59 | + <a href="https://github.com/flypythoncom" title="Github">Github</a> |
| 60 | + |
| 61 | + <a href="/article/about" title="关于">关于</a> |
| 62 | + |
| 63 | + </nav> |
| 64 | + <span class="btn-menu" id="J_header_menu"> |
| 65 | + <div class="inner"> |
| 66 | + <span class="line line-01"></span> |
| 67 | + <span class="line line-02"></span> |
| 68 | + <span class="line line-03"></span> |
| 69 | + </div> |
| 70 | + </span> |
| 71 | + <div class="wrapper mb-nav-wrapper" id="J_header_menu_list"> |
| 72 | + <nav class="wrapper mb-nav-container"> |
| 73 | + |
| 74 | + <a href="https://flypython.com/python" title="飞蟒微课堂">飞蟒微课堂</a> |
| 75 | + |
| 76 | + <a href="https://flypython.com/flypython_daily" title="Python日报">Python日报</a> |
| 77 | + |
| 78 | + <a href="https://flypython.com/PyCon/" title="PyCon">PyCon</a> |
| 79 | + |
| 80 | + <a href="https://github.com/flypythoncom" title="Github">Github</a> |
| 81 | + |
| 82 | + <a href="/article/about" title="关于">关于</a> |
| 83 | + |
| 84 | + </nav> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | +</header> |
| 88 | + <section class="body-wrapper"> |
| 89 | + <section class="wrapper post-banner"> |
| 90 | + <div class="container post-banner-container"> |
| 91 | + <h2 class="wrapper title">LCCI 01.01.判定字符是否唯一</h2> |
| 92 | + <div class="wrapper tips"> |
| 93 | + <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> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | +</section> |
| 97 | +<section class="wrapper main-wrapper"> |
| 98 | + <article class="sub-container post-content"> |
| 99 | + <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> |
| 100 | +<h4 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h4><blockquote> |
| 101 | +<p>实现一个算法,确定一个字符串 s 的所有字符是否全都不同。</p> |
| 102 | +<p>示例 1:</p> |
| 103 | +<p>输入: s = “leetcode”<br>输出: false<br>示例 2:</p> |
| 104 | +<p>输入: s = “abc”<br>输出: true</p> |
| 105 | +<p>限制:</p> |
| 106 | +<p>0 <= len(s) <= 100<br>如果你不使用额外的数据结构,会很加分。</p> |
| 107 | +</blockquote> |
| 108 | +<h4 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h4><p>首先,你得理解题意。不确定的东西,需要问或者尝试验证。</p> |
| 109 | +<p>比如:这里你可以问面试官字符串s是ASCII还是Unicode字符串,如果是编程平台你可以写测试来判断。</p> |
| 110 | +<p>在这里我们假定字符集为ASCII,那如果字符串长度大于128,那肯定是false,不过有限制条件len(s)<=100,那这个判断就不必要了。限制里面还要求不使用额外的数据结构,这就过滤掉了很多的方案。</p> |
| 111 | +<p>比如,使用set就一行代码解决</p> |
| 112 | +<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) -> bool:</span><br><span class="line"> return len(astr)== len(set(astr))</span><br></pre></td></tr></table></figure> |
| 113 | + |
| 114 | +<p>我们很容易想到hash计数的方法,经过测试,字符串中只出现’a’到’z’,26个小写字母,那只需要一个bool数组,直接统计出现的频率,判断是否重复。</p> |
| 115 | +<p>考虑到不使用额外的数据结构,那我们可以尝试使用bitmap,把数组换成一个整型数,按照位上0,1来判断是否重复。</p> |
| 116 | +<p>开始我们算出字符离最开始的字符’a’的距离,然后1移动这个距离来表示这个数。后面用一个测试变量来测试p,如果存在则返回False,如果不存在则写入。</p> |
| 117 | +<p>具体代码:</p> |
| 118 | +<pre><code>def isUnique(self, astr: str) -> bool: |
| 119 | + t = 0 |
| 120 | + for c in astr: |
| 121 | + if t & (p := 1 << (ord(c) - ord('a'))): |
| 122 | + return False |
| 123 | + t |= p |
| 124 | + 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> |
| 125 | +<h4 id="方案代码"><a href="#方案代码" class="headerlink" title="方案代码"></a>方案代码</h4><p>解法:</p> |
| 126 | +<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) -> bool:</span><br><span class="line"> t = 0</span><br><span class="line"> for c in astr:</span><br><span class="line"> if t & (p := 1 << (ord(c) - ord('a'))):</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> |
| 127 | + |
| 128 | +<p>另外还有三种使用数据结构的解法</p> |
| 129 | +<p>解法1:</p> |
| 130 | +<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) -> bool:</span><br><span class="line"> return len(astr)== len(set(astr))</span><br></pre></td></tr></table></figure> |
| 131 | + |
| 132 | +<p>解法2:</p> |
| 133 | +<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) -> bool:</span><br><span class="line"> adict={}</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> |
| 134 | + |
| 135 | +<p>解法3:</p> |
| 136 | +<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) -> 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> |
| 137 | + |
| 138 | + |
| 139 | + </article> |
| 140 | + <div class="sub-container gitalk-wrapper" id="gitalk-container"></div> |
| 141 | +</section> |
| 142 | + |
| 143 | +<div class="tips-top-wrapper"> |
| 144 | + <span class="tip-top-container" onclick="scrollToWindowTop()"> |
| 145 | + <span class="l-bar"></span> |
| 146 | + <span class="r-bar"></span> |
| 147 | + </span> |
| 148 | +</div> |
| 149 | +<footer class="wrapper footer-wrapper"> |
| 150 | + <div class="container"><span class="copyright">© 2020 FlyPython . All Rights Reserved.</span></div> |
| 151 | +</footer> |
| 152 | + </section> |
| 153 | + <script src="/js/f25.js"></script> |
| 154 | + |
| 155 | +</body> |
| 156 | +</html> |
0 commit comments