Site updated: 2020-02-27 11:48:06
This commit is contained in:
@@ -95,6 +95,19 @@
|
||||
<div class="wrapper index-wrapper">
|
||||
<ul class="container list-container">
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0102/" class="wrapper post-i-link" title="LCCI 01.02.判定是否互为字符重排">
|
||||
<div class="wrapper cover">
|
||||
<img src="/images/leetcode-0102.png" alt="LCCI 01.02.判定是否互为字符重排">
|
||||
</div>
|
||||
<h3 class="wrapper title" title="LCCI 01.02.判定是否互为字符重排">LCCI 01.02.判定是否互为字符重排</h3>
|
||||
<p class="wrapper subtitle">
|
||||
LeetCode刷题计划
|
||||
</p>
|
||||
<div class="wrapper time">2020-02-26</div>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
|
||||
<div class="wrapper cover">
|
||||
|
||||
@@ -95,6 +95,19 @@
|
||||
<div class="wrapper index-wrapper">
|
||||
<ul class="container list-container">
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0102/" class="wrapper post-i-link" title="LCCI 01.02.判定是否互为字符重排">
|
||||
<div class="wrapper cover">
|
||||
<img src="/images/leetcode-0102.png" alt="LCCI 01.02.判定是否互为字符重排">
|
||||
</div>
|
||||
<h3 class="wrapper title" title="LCCI 01.02.判定是否互为字符重排">LCCI 01.02.判定是否互为字符重排</h3>
|
||||
<p class="wrapper subtitle">
|
||||
LeetCode刷题计划
|
||||
</p>
|
||||
<div class="wrapper time">2020-02-26</div>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
|
||||
<div class="wrapper cover">
|
||||
|
||||
@@ -95,6 +95,19 @@
|
||||
<div class="wrapper index-wrapper">
|
||||
<ul class="container list-container">
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0102/" class="wrapper post-i-link" title="LCCI 01.02.判定是否互为字符重排">
|
||||
<div class="wrapper cover">
|
||||
<img src="/images/leetcode-0102.png" alt="LCCI 01.02.判定是否互为字符重排">
|
||||
</div>
|
||||
<h3 class="wrapper title" title="LCCI 01.02.判定是否互为字符重排">LCCI 01.02.判定是否互为字符重排</h3>
|
||||
<p class="wrapper subtitle">
|
||||
LeetCode刷题计划
|
||||
</p>
|
||||
<div class="wrapper time">2020-02-26</div>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
|
||||
<div class="wrapper cover">
|
||||
|
||||
161
article/python-leetcode-lcci0102/index.html
Normal file
161
article/python-leetcode-lcci0102/index.html
Normal file
@@ -0,0 +1,161 @@
|
||||
<!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.02.判定是否互为字符重排,FlyPython - 专业的Python学习社区,flypython, 飞蟒,飞蟒Python,Python入门,Python自动化,Python日报">
|
||||
<meta property="og:title" content="LCCI 01.02.判定是否互为字符重排 | 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-lcci0102/">
|
||||
<meta property="og:image" content="http://www.flypython.com/images/leetcode-0102.png">
|
||||
<meta property="og:description" content="LCCI 01.02.判定是否互为字符重排--LeetCode刷题计划">
|
||||
<meta name="description" content="LCCI 01.02.判定是否互为字符重排--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.02.判定是否互为字符重排 | 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.02.判定是否互为字符重排</h2>
|
||||
<div class="wrapper tips">
|
||||
<span>Author:</span><span>flypython</span> | <span>Date: </span><span>2020-02-26</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-02-判定是否互为字符重排"><a href="#01-02-判定是否互为字符重排" class="headerlink" title="01.02.判定是否互为字符重排"></a>01.02.判定是否互为字符重排</h2><p>链接:<a href="https://leetcode-cn.com/problems/check-permutation-lcci/" target="_blank" rel="noopener">https://leetcode-cn.com/problems/check-permutation-lcci/</a><br>难度:简单</p>
|
||||
<h4 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h4><blockquote>
|
||||
<p>给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。</p>
|
||||
<p>示例 1:</p>
|
||||
<p>输入: s1 = “abc”, s2 = “bca”<br>输出: true<br>示例 2:</p>
|
||||
<p>输入: s1 = “abc”, s2 = “bad”<br>输出: false</p>
|
||||
<p>说明:<br>0 <= len(s1) <= 100<br>0 <= len(s2) <= 100</p>
|
||||
</blockquote>
|
||||
<h4 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h4><p>开始,我们还是先理解题意,确定是否区分大小写,是否忽略空白字符等。有了01.01题的hash计数0,1判断的经历,在这里很容易想到使用hash计数。</p>
|
||||
<p>因为字符可以重复,需要对字符进行计数,这个时候我们会想到collections库中的Counter。于是有了下面两行代码解决了问题。</p>
|
||||
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">from collections import Counter</span><br><span class="line">return Counter(s1) == Counter(s2)</span><br></pre></td></tr></table></figure>
|
||||
|
||||
<p>但是上面的方法严格来说算作弊,那我们手写吧。对每个字符进行计数操作并测试是否相等,不相等返回False,全部相等返回True。</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><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> if len(s1) != len(s2):</span><br><span class="line"> return False</span><br><span class="line"> </span><br><span class="line"> base = set(s1+s2)</span><br><span class="line"> for b in base:</span><br><span class="line"> if s1.count(b) != s2.count(b):</span><br><span class="line"> return False</span><br><span class="line"> return True</span><br></pre></td></tr></table></figure>
|
||||
|
||||
<p>上面的代码,最容易出问题的是<code>base = set(s1+s2)</code>,需要以两个字符串集的唯一字符来进行计数,但这里引入了set。</p>
|
||||
<p>我们测试了字符范围,发现是’a’到’z’,那么代码可以改成:</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><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> if len(s1) != len(s2):</span><br><span class="line"> return False</span><br><span class="line"> </span><br><span class="line"> base = [chr(i) for i in range(ord("a"),ord("z")+1)]</span><br><span class="line"> for b in base:</span><br><span class="line"> if s1.count(b) != s2.count(b):</span><br><span class="line"> return False</span><br><span class="line"> return True</span><br></pre></td></tr></table></figure>
|
||||
|
||||
<p>再进一步,为了直观,我们可以引入string包,代码如下:</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><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> import string </span><br><span class="line"> if len(s1) != len(s2):</span><br><span class="line"> return False</span><br><span class="line"> </span><br><span class="line"> for b in string.ascii_lowercase:</span><br><span class="line"> if s1.count(b) != s2.count(b):</span><br><span class="line"> return False</span><br><span class="line"> return True</span><br></pre></td></tr></table></figure>
|
||||
|
||||
<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></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> return sorted(s1) == sorted(s2)</span><br></pre></td></tr></table></figure>
|
||||
|
||||
<p>空间复杂度O(nlogn),相比hash计数复杂度要高一些,但是hash计数有空间复杂度。</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><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">class Solution {</span><br><span class="line"> public boolean CheckPermutation(String s1, String s2) {</span><br><span class="line"> if(s1.length() != s2.length()) {</span><br><span class="line"> return false;</span><br><span class="line"> }</span><br><span class="line"> int s1Sum = 0, s2Sum = 0;</span><br><span class="line"> for(int i = 0; i < s1.length(); i++) {</span><br><span class="line"> s1Sum += s1.charAt(i);</span><br><span class="line"> s2Sum += s2.charAt(i);</span><br><span class="line"> }</span><br><span class="line"> return s1Sum == s2Sum;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||||
|
||||
<p>此解法主要是计算s1,s2字符和,比较字符和。这种解法的问题就是忽略了和相同,但数字并不同的情况,比如 3+7 == 2+8。</p>
|
||||
<p>除了计算s1,s2的字符和外,计算相对位移和,加s1字符减s2字符等方法都有此问题。</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><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> if len(s1) != len(s2):</span><br><span class="line"> return False</span><br><span class="line"> </span><br><span class="line"> base = set(s1+s2)</span><br><span class="line"> for b in base:</span><br><span class="line"> if s1.count(b) != s2.count(b):</span><br><span class="line"> return False</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><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> from collections import Counter</span><br><span class="line"> return Counter(s1) == Counter(s2)</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></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> return sorted(s1) == sorted(s2)</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><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class Solution:</span><br><span class="line"> def CheckPermutation(self, s1: str, s2: str) -> bool:</span><br><span class="line"> import string</span><br><span class="line"> if len(s1) != len(s2):</span><br><span class="line"> return False</span><br><span class="line"> </span><br><span class="line"> for b in string.ascii_lowercase:</span><br><span class="line"> if s1.count(b) != s2.count(b):</span><br><span class="line"> return False</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">© 2020 FlyPython . All Rights Reserved.</span></div>
|
||||
</footer>
|
||||
</section>
|
||||
<script src="/js/f25.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -95,6 +95,19 @@
|
||||
<div class="wrapper index-wrapper">
|
||||
<ul class="container list-container">
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0102/" class="wrapper post-i-link" title="LCCI 01.02.判定是否互为字符重排">
|
||||
<div class="wrapper cover">
|
||||
<img src="/images/leetcode-0102.png" alt="LCCI 01.02.判定是否互为字符重排">
|
||||
</div>
|
||||
<h3 class="wrapper title" title="LCCI 01.02.判定是否互为字符重排">LCCI 01.02.判定是否互为字符重排</h3>
|
||||
<p class="wrapper subtitle">
|
||||
LeetCode刷题计划
|
||||
</p>
|
||||
<div class="wrapper time">2020-02-26</div>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
|
||||
<div class="wrapper cover">
|
||||
|
||||
@@ -95,6 +95,19 @@
|
||||
<div class="wrapper index-wrapper">
|
||||
<ul class="container list-container">
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0102/" class="wrapper post-i-link" title="LCCI 01.02.判定是否互为字符重排">
|
||||
<div class="wrapper cover">
|
||||
<img src="/images/leetcode-0102.png" alt="LCCI 01.02.判定是否互为字符重排">
|
||||
</div>
|
||||
<h3 class="wrapper title" title="LCCI 01.02.判定是否互为字符重排">LCCI 01.02.判定是否互为字符重排</h3>
|
||||
<p class="wrapper subtitle">
|
||||
LeetCode刷题计划
|
||||
</p>
|
||||
<div class="wrapper time">2020-02-26</div>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
|
||||
<div class="wrapper cover">
|
||||
|
||||
BIN
images/leetcode-0102.png
Normal file
BIN
images/leetcode-0102.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 405 KiB |
13
index.html
13
index.html
@@ -95,6 +95,19 @@
|
||||
<div class="wrapper index-wrapper">
|
||||
<ul class="container list-container">
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0102/" class="wrapper post-i-link" title="LCCI 01.02.判定是否互为字符重排">
|
||||
<div class="wrapper cover">
|
||||
<img src="/images/leetcode-0102.png" alt="LCCI 01.02.判定是否互为字符重排">
|
||||
</div>
|
||||
<h3 class="wrapper title" title="LCCI 01.02.判定是否互为字符重排">LCCI 01.02.判定是否互为字符重排</h3>
|
||||
<p class="wrapper subtitle">
|
||||
LeetCode刷题计划
|
||||
</p>
|
||||
<div class="wrapper time">2020-02-26</div>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="post-i-wrapper">
|
||||
<a href="/article/python-leetcode-lcci0101/" class="wrapper post-i-link" title="LCCI 01.01.判定字符是否唯一">
|
||||
<div class="wrapper cover">
|
||||
|
||||
Reference in New Issue
Block a user