
笔试题目
Ellis_hym
这个作者很懒,什么都没留下…
展开
-
剑指offer-python代码-第二章
开始看剑指offer,粗略的捋一捋里面的面试题:以下代码大多用python3写的,少部分会用C++ python代码看上去更加简洁明了,里面也穿插和补充了我的部分总结。p38:二维数组中的查找:从右上角或者左下角开始:def searchMatrix( matrix, target): """ :type matrix: List[List[int]] :type targ原创 2017-03-09 15:48:46 · 460 阅读 · 0 评论 -
频数相关的leetcode:451 Sort Characters By Frequency& 347 Top K Frequent Elements
利用字典,zip函数,再排序,则这两个题投机取消的很简单。347 Top K Frequent Elementsclass Solution(object): def topKFrequent(self, nums, k): d={} for i in nums: if i in d: d[i]+=1原创 2017-03-06 17:22:46 · 355 阅读 · 0 评论 -
二叉树回顾
遍历:前序遍历: 节点-左-右使用一个栈来实现,非递归:class Solution(object): def preorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if not root:return []原创 2017-03-05 15:10:09 · 331 阅读 · 0 评论 -
445. Add Two Numbers II
回顾一下,链表: 445. Add Two Numbers II 这个题目,最开始就考虑反转链表,但是题目不允许,我采取了用 栈 把链表里的数取出来的做法。。 后面的做法和 add two numbers 1 差不多了class ListNode(object): def __init__(self, x): self.val = x self.n原创 2017-03-04 13:04:09 · 392 阅读 · 0 评论 -
动态规划leetcode 回顾
Counting Bits看看数 0 0 dp[0]=0 1 1 dp[1]=dp[1-1]+1 2 10 dp[2]=dp[2-2]+14 100 dp[4]=dp[4-4]+18 1000 dp[8]=dp[8-8]+1动态规划:dp[i]=dp[i-offset]+1 代码:class Solution(object): def countBits(self, n原创 2017-03-02 16:28:35 · 331 阅读 · 0 评论 -
20分钟回顾基础排序与二分查找
来来,复习基础的数据结构吧~~知乎上有人讲,如果只是应付面试,重要程度: 数据结构:二叉树>散列表>栈>队列>堆>一般树>图>其他 算法:二分法>冒泡>快速排序>加密算法>其他排序算法>随机数的算法>其他下面需要20分钟就能 浏览完,最最基础的:基本排序与二分查找~~排序#插入排序def insertion_sort(nums): for i in range(1,len(nums))原创 2017-03-02 14:00:15 · 421 阅读 · 0 评论 -
485 Max Consecutive Ones
485 Max Consecutive Ones 求连续的1的个数 第一个想到的老办法,用两个指针:start来记录开始与结束class Solution(object): def findMaxConsecutiveOnes(self, nums): """ :type nums: List[int] :rtype: int原创 2017-03-01 21:40:52 · 301 阅读 · 0 评论 -
263.Ugly Number||202 happy number||476 Number Complement||136 Single Number
leetcode原创 2017-03-01 21:21:30 · 313 阅读 · 0 评论 -
78. Subsets && 90. Subsets II
这个题目就是求子集,如【1,2】的子集就是【1】【2】【1,2】【】,别忘了空集。我想到的最容易的做法就是分治,用递归的方法求,求完之后再加上空集。 比如说:求【1,2】的子集。 1. 先求【1】的子集【1】 2. 再求【2】的子集【2】 3. 把子集”加”起来 [a+b for a in l1 for b in l2]+l1+l2 1. 最后再加上空集整体代码如下:class Solut原创 2016-12-10 14:13:33 · 281 阅读 · 0 评论 -
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", the answer is "b", with the le原创 2016-12-10 13:18:56 · 257 阅读 · 0 评论 -
15.3Sum &16.3Sum Closest
第16题是找一个和target最接近的数,两个指针双层遍历即可:class Solution(object): def threeSumClosest(self, nums, target): result=nums[0]+nums[1]+nums[2] nums.sort() for i in range(len(nums)-2):原创 2016-12-08 16:37:09 · 246 阅读 · 0 评论 -
89. Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray原创 2016-11-30 21:09:58 · 249 阅读 · 0 评论 -
377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.Example:_**nums**_ = [1, 2, 3]_**target**_ = 4The原创 2016-11-30 11:07:49 · 284 阅读 · 0 评论 -
319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off i原创 2016-11-29 22:00:38 · 290 阅读 · 0 评论 -
400. Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231).Example 1:**Input:**3**O原创 2016-11-29 21:36:36 · 252 阅读 · 0 评论 -
445. Add Two Numbers II
You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linke原创 2016-11-28 22:10:37 · 350 阅读 · 0 评论 -
463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely s原创 2016-11-28 17:33:09 · 399 阅读 · 0 评论 -
435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.Note:You may assume the interval’s end point is always bigger原创 2016-11-22 16:10:44 · 330 阅读 · 0 评论 -
86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the原创 2016-11-22 15:30:26 · 267 阅读 · 0 评论 -
423. Reconstruct Original Digits from English
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.Note:Input contains only lowercase English letters.Input is guaranteed t原创 2016-11-21 22:37:47 · 372 阅读 · 0 评论 -
python bisect模块的应用方法与leetcode:436. Find Right Interval
这个模块只有几个函数,一旦决定使用二分搜索时,立马要想到使用这个模块. >>>import bisect >>>dir(bisect) [‘builtins‘, ‘cached‘, ‘doc‘, ‘file‘, ‘loader‘, ‘name‘, ‘package‘, ‘spec‘, ‘bisect’, ‘bisect_left’, ‘bisect_right’, ‘ins原创 2016-11-21 17:07:54 · 699 阅读 · 0 评论 -
438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be larger t原创 2016-11-17 11:31:39 · 394 阅读 · 0 评论 -
451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. 就是按频数来排序呗!! 我的做法是先写成字典,然后用sorted和zip函数一起对字典的value进行排序。class Solution(object): def frequencySort(self, s):原创 2016-11-17 10:35:13 · 255 阅读 · 0 评论 -
437. Path Sum III
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \3 -2 1Return 3\. The paths that sum to 8 are:1\. 5 -> 32\. 5 -> 2 -> 13\. -3 -> 1原创 2016-11-16 21:34:19 · 269 阅读 · 0 评论 -
459. Repeated Substring Pattern
代码比较简单,分为奇数偶数讨论。 如果有第19行,运行时间35ms;没有的话 39ms.class Solution(object): def repeatedSubstringPattern(self, str): """ :type str: str :rtype: bool """ n=len(str)原创 2016-11-16 20:36:31 · 232 阅读 · 0 评论 -
455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cook原创 2016-11-16 17:07:31 · 350 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index原创 2016-11-16 16:53:57 · 224 阅读 · 0 评论 -
153和154. Find Minimum in Rotated Sorted Array II
Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unk原创 2016-11-16 15:27:52 · 303 阅读 · 0 评论 -
leetcode:447. Number of Boomerangs
原题的输入输出:**Input:**[[0,0],[1,0],[2,0]]**Output:**2**Explanation:**The two boomerangs are **[[1,0],[0,0],[2,0]]** and **[[1,0],[2,0],[0,0]]**直译是找回旋镖的个数。 我将其理解为 :先确定点 i,找到点k,j使得点i、k距离与点ij的距离相等。class S原创 2016-11-15 22:31:14 · 394 阅读 · 0 评论 -
leetcode:11. Container With Most Water
原题: Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find tw原创 2016-11-14 21:49:33 · 327 阅读 · 0 评论 -
c++中main 函数执行完后还能执行其它语句吗?
此问题来自:C和C++程序员面试秘笈 p15 方法:用atexit()函数来注册 程序正常终止时要被调用的函数用法:用 法: int atexit(void (*func)(void));其参数是一个指针函数,指向一个没有参数也没有返回值的函数例子:#include <stdio.h>#include<stdlib.h> //atexit包含在此void fn1(void);void fn2原创 2016-11-14 20:54:20 · 665 阅读 · 0 评论 -
leetcode:216. Combination Sum III
216. Combination Sum III大意是说在1-9中选k个不同的数,使他们的和是n,返回是所有的选择。 Example 1: 输入: k = 3, n = 7 输出:[[1,2,4]] Example 2: 输入: k = 3, n = 9 输出:[[1,2,6], [1,3,5], [2,3,4]]思路: 1. 输入的k 肯定在必须在 1-9原创 2016-11-11 11:03:30 · 252 阅读 · 0 评论 -
leetcode:453. Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.Example:**Input:**[1,2,3]**Outp原创 2016-11-09 21:45:04 · 294 阅读 · 0 评论 -
leetcode:17. Letter Combinations of a Phone Number
输入输出示例 Input:Digit string “23” Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].我采用了将输入拆成两部分,然后再把结果加起来的方法。class Solution(object): def letterCombinations(self, digits): "原创 2016-11-09 21:18:45 · 302 阅读 · 0 评论 -
leetcode:448. Find All Numbers Disappeared in an Array
这个题目简单,求题设条件中没有的数,即集合的差集,题目要求不能有额外的空间,用列表生成式来写。class Solution(object): def findDisappearedNumbers(self, nums): """ :type nums: List[int] :rtype: List[int] """原创 2016-11-09 20:53:05 · 481 阅读 · 0 评论 -
leetcode:442. Find All Duplicates in an Array
即找出数组中的重复的数字,题目咋一看比较简单,但是要求不允许使用额外的空间,复杂度为o(n)class Solution(object): def findDuplicates(self, nums): """ :type nums: List[int] :rtype: List[int] """ return原创 2016-11-09 20:48:37 · 281 阅读 · 0 评论 -
2016年 秋招 hua为 上机题目:CD key 生成
题目很简单,把题目读懂最重要,笔者随手写点。。原题:#include #include using namespace std; class cd_key{ private: string mystring="23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; char mychar='-'; public: stri原创 2016-11-01 20:21:22 · 301 阅读 · 0 评论 -
2016hua为校招:笔试上机题目:游程编码、解码
之前同学上笔试的时候拍下来的,自己随便写了一点,水平较低,仅供参考。原题:原题只需要解码,我把编码、解码都写了一下:#include#includeusing namespace std;class Youcheng{ public: //解码 string decode(string & s){原创 2016-10-31 21:56:20 · 825 阅读 · 0 评论