search
Homephp教程php手册面试题之算法集锦,试题算法集锦

面试题之算法集锦,试题算法集锦

思路1:<span>
把A去重得到A1</span>,B去重得到B1,然后对A1,B1分别进行排序,然后遍历较短的字符串的每个字符是否存在于较长的字符串中,<span>存在则输出
问题</span>:
1.思路很简单,基本大家都会这么考虑,<span>但是面试的时候就没有亮点了
思路2</span>:<span>
假设AB串只包含小写(其实无所谓)</span>,那么创建一个数组,数组的key为a->z,<span>value都是0;
</span><?<span>php
    </span><span>function</span> stringToChar(<span>$str</span>,<span>$num</span>=1,<span>$tmp</span>=<span>null</span><span>){
        </span><span>if</span>(<span>empty</span>(<span>$tmp</span><span>)){
</span><span>$tmp</span>=<span>array</span>('a'=>0,'b'=>0,'c'=>0,'d'=>0,'e'=>0,'f'=>0,'g'=>0,'h'=>0,'i'=>0,'j'=>0,'k'=>0,'l'=>0,'m'=>0,'n'=>0,'o'=>0,'p'=>0,'q'=>0,'r'=>0,'s'=>0,'t'=>0,'u'=>0,'v'=>0,'w'=>0,'x'=>0,'y'=>0,'z'=>0<span>);
        }
        </span><span>$arr_temp</span>=<span>str_split</span>(<span>$str</span>,1<span>);
        </span><span>foreach</span>(<span>$arr_temp</span> <span>as</span> <span>$v</span><span>){
            </span><span>if</span>(<span>$tmp</span>[<span>$v</span>]<<span>$num</span><span>){
                </span><span>$tmp</span>[<span>$v</span>]+=<span>$num</span><span>;
            }
        }
        </span><span>return</span> <span>$tmp</span><span>;
    }
    </span><span>function</span> getStringIntersect(<span>$str1</span>, <span>$str2</span><span>){
        </span><span>$temp</span>=stringToChar(<span>$str1</span>,1<span>);
        </span><span>//</span><span>$str2的$num用2 就是为了区分 stemp中的原来的1 是 $str1中设置的</span>
        <span>$temp</span>=stringToChar(<span>$str2</span>,2,<span>$temp</span><span>);
        </span><span>$result</span>=''<span>;
        </span><span>foreach</span> (<span>$temp</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) {
            </span><span>if</span>(<span>$value</span>===3<span>){
                </span><span>$result</span>.=<span>$key</span><span>;
            }
        }
        </span><span>return</span> <span>$result</span><span>;
    }
    </span><span>$A</span>="common";<span>//</span><span>"hello";</span>
    <span>$B</span>="month";<span>//</span><span>"jeesite";</span>
    <span>$result</span>=getStringIntersect(<span>$A</span>, <span>$B</span><span>);
    </span><span>echo</span> <span>$result</span><span>;
</span>?><br />今天随便浏览网页的时候又发现了这篇文章(一次谷歌面试趣事)<br />我想起来了 思路二出自这篇文章。

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
2023年前端React面试题大汇总(收藏)2023年前端React面试题大汇总(收藏)Aug 04, 2020 pm 05:33 PM

php中文网作为知名编程学习网站,为您整理了一些React面试题,帮助前端开发人员准备和清除React面试障碍。

2023年精选Web前端面试题大全及答案(收藏)2023年精选Web前端面试题大全及答案(收藏)Apr 08, 2021 am 10:11 AM

本篇文章给大家总结一些值得收藏的精选Web前端面试题(附答案)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

五个常见的Go语言面试题及解答五个常见的Go语言面试题及解答Jun 01, 2023 pm 08:10 PM

作为近年来备受热捧的一门编程语言,Go语言已经成为众多公司与企业的面试热点。对于Go语言初学者而言,在面试过程中遇到相关问题时,如何回答是一个值得探讨的问题。下面列举五个常见的Go语言面试题及解答,供初学者参考。请介绍一下Go语言的垃圾回收机制是如何工作的?Go语言的垃圾回收机制基于标记-清除算法和三色标记算法。当Go程序中的内存空间不够用时,Go垃圾回收器

50个你必须掌握的Angular面试题(收藏)50个你必须掌握的Angular面试题(收藏)Jul 23, 2021 am 10:12 AM

本篇文章给大家分享50个必须掌握的Angular面试题,会从初学者-中级-高级三个部分来解析这50个面试题,带大家吃透它们!

面试官:你对高并发了解多少?我:emmm...面试官:你对高并发了解多少?我:emmm...Jul 26, 2023 pm 04:07 PM

高并发,几乎是每个程序员都想拥有的经验。原因很简单:随着流量变大,会遇到各种各样的技术问题,比如接口响应超时、CPU load升高、GC频繁、死锁、大数据量存储等等,这些问题能推动我们在技术深度上不断精进。

2023年vue高频面试题分享(附答案分析)2023年vue高频面试题分享(附答案分析)Aug 01, 2022 pm 08:08 PM

本篇文章给大家总结一些值得收藏的2023年精选vue高频面试题(附答案)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

总结一些前端常见面试题(附答案),带你巩固知识点!总结一些前端常见面试题(附答案),带你巩固知识点!Jul 29, 2022 am 09:49 AM

发布文章主要也是巩固自己的知识更加熟练,全凭自己的理解和网上查资料总结出来的,如有不对的地方还望多多指点。下面是我总结的一下常见面试题,为了督促自己还可以会继续更新

看看这些前端面试题,带你搞定高频知识点(四)看看这些前端面试题,带你搞定高频知识点(四)Feb 20, 2023 pm 07:19 PM

每天10道题,100天后,搞定所有前端面试的高频知识点,加油!!!,在看文章的同时,希望不要直接看答案,先思考一下自己会不会,如果会,自己的答案是什么?想过之后再与答案比对,是不是会更好一点,当然如果你有比我更好的答案,欢迎评论区留言,一起探讨技术之美。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.