
笔试题
zyilove34
计算机相关专业
展开
-
链表的头插法与尾插法
一、头插法头插法建立的表,得到的链表的顺序和实际的插入顺序是相反的,比如插入的顺序是1,2,3,4,5,那么得到的顺序是5,4,3,2,1。整个插入过程入下图所示,二、尾插法尾插法得到的链表数值顺序是按照插入顺序的,比如插入的顺序是1,2,3,4,5,得到的顺序也是1,2,3,4,5,具体的操作过程如下图:...原创 2020-02-26 10:32:01 · 1209 阅读 · 0 评论 -
路灯最短距离
题目:一条长l的笔直的街道上有n个路灯,若这条街的起点为0,终点为l,第i个路灯坐标为ai ,每盏灯可以覆盖到的最远距离为d,为了照明需求,所有灯的灯光必须覆盖整条街,但是为了省电,要是这个d最小,请找到这个最小的d。import java.util.*;import java.text.DecimalFormat;import java.math.BigDecimal;publi原创 2017-07-04 13:57:28 · 439 阅读 · 0 评论 -
Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.大意是:给定n对圆括号,要求将所有合法的圆括号序列找出来/* 解题思路:采用递归,当左括号的个数小于n的时候,就产生一个左括号,当右括号个数 小于左括号的原创 2017-06-25 21:30:44 · 177 阅读 · 0 评论 -
找出链表中是否存在环
/* 解题思路:采用两个指针,一个指针每一次值移动一次,两位一个指针每一次移动两步。如果存在环,则他们必定会存在 相遇的那一刻 */ public boolean hasCycle(ListNode head) { if(head==null){//注意考虑头结点为空的情况 return false;原创 2017-06-26 11:40:00 · 249 阅读 · 0 评论 -
数组顺时针选择90度
题目:有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵,保证N小于等于300。 /* 解题思路:以对角线为界限,交换两边的数组元素, 之后交换数组的列 */ public int[][] rotateMatrix(int[][] mat, int原创 2017-07-05 10:30:52 · 354 阅读 · 0 评论 -
移除掉数组里面的重复元素, 但是对于每种元素保留最后出现的那个
public class Main { /* 解题思路:从后往前遍历数组,通过ArrayList中的contains方法判断ArrayList是否包含该 函数,如果包含则不添加,否则添加,最后把集合逆序输出 */public static void main(String[] args) {Scanner sc=new Scanner原创 2017-06-15 15:26:02 · 517 阅读 · 0 评论 -
寻找数组的峰值
题目:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple pea原创 2017-07-07 13:09:27 · 513 阅读 · 0 评论 -
116. Populating Next Right Pointers in Each Node
题目:Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL. /*原创 2017-07-07 14:36:48 · 206 阅读 · 0 评论 -
回文
题目:对于一个字符串,我们想通过添加字符的方式使得新的字符串整体变成回文串,但是只能在原串的结尾添加字符,请返回在结尾添加的最短字符串。给定原字符串A及它的长度n,请返回添加的字符串。保证原串不是回文串。 public String addToPalindrome(String A, int n) { /* 解题思原创 2017-07-26 16:34:04 · 255 阅读 · 0 评论 -
风口之下,猪都能飞。当今中国股市牛市,真可谓“错过等七年”。 给你一个回顾历史的机会,已知一支股票连续n天的价格走势,以长度为n的整数数组表示,数组中第i个元素(prices[i])代表该股票第i天
/** *解题思路:因为最多可以交易两次,所以可以将价格分为两部分,(0,i)和(i+1,n); 用两个数组prefix和suffix分别表示从前往后,和从后往前的最大差值,最后遍历这两个预处理 的数组,求出解即可 * * @param prices Prices[i]即第i天的股价 * @return 整型 *原创 2017-08-18 11:26:47 · 1722 阅读 · 1 评论 -
72. Edit Distance 编辑距离
题目:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on原创 2017-09-13 13:29:22 · 223 阅读 · 0 评论 -
两个链表相加的和445. Add Two Numbers II
题目:You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and r原创 2017-09-23 17:07:04 · 284 阅读 · 0 评论 -
49. Group Anagrams
题目:Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]class S原创 2017-08-30 19:16:11 · 316 阅读 · 0 评论 -
513. Find Bottom Left Tree Value
题目:找出二叉树最后一层,最左边的孩子,返回该节点的值 /* 解题思路:采用层次遍历,用一个nlast指向每一层中最右边的孩子,last一开始指向根节点,当出队列的节点temp=last的时候, 更新last的值为nlast,用result表示每一次最左边的孩子,当last==temp的时候,更新result的值为下一次出队列的节点,原创 2017-09-20 01:32:05 · 295 阅读 · 0 评论 -
Add to List 198. House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent原创 2017-09-13 00:27:47 · 283 阅读 · 0 评论 -
二进制位数的比较
题目:世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?解题思路:将m和n分别与1相与,如果得到的两个结果不相同,则说明该位是不相同的,将结果加1;然后分别将m与n右移一位,如此重复,直到m和n两者当中有一个为0,则退出循环。如果m或者n不为0,则将不为0的那个数与1相与,如果不为0,则将结果加1.如此直到该数为0为止原创 2017-07-03 14:45:31 · 1189 阅读 · 0 评论 -
最长的公共子序列
题目:对于一个数字序列,请设计一个复杂度为O(nlogn)的算法,返回该序列的最长上升子序列的长度,这里的子序列定义为这样一个序列U1,U2...,其中Ui 给定一个数字序列A及序列的长度n,请返回最长上升子序列的长度。 public int findLongest(int[] A, int n) { // write code原创 2017-07-12 10:46:40 · 229 阅读 · 0 评论 -
leetcode15. 3Sum
/* 解题思路:通过转化为两数求和,a+b+c=0转化为a+b=-c,并且把求得的结果存入的set集合中 以方便去重;使用该方法首先必须先把数组排序 */ public List> threeSum(int[] nums) { //将数组排序 Arrays.sort(nums); List> result原创 2017-06-17 15:43:17 · 238 阅读 · 0 评论 -
双栈排序练习
题目:请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。给定一个int[] numbers(C++中为vector<int>),其中第一个元素为栈顶,请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只能访问到第一个元素。/*解题思路:遍历数组,如果集合为空,则把数组中的元素直接加入原创 2017-06-19 09:54:28 · 262 阅读 · 0 评论 -
滑动窗口问题
题目:有一个整型数组 arr 和一个大小为 w 的窗口从数组的最左边滑到最右边,窗口每次向右边滑一个位置。 返回一个长度为n-w+1的数组res,res[i]表示每一种窗口状态下的最大值。 以数组为[4,3,5,4,3,3,6,7],w=3为例。因为第一个窗口[4,3,5]的最大值为5,第二个窗口[3,5,4]的最大值为5,第三个窗口[5,4,3]的最大值为5。第四个窗口[4,3,3]的最大值为4。第五个窗口[3,3,6]的最大值为6。第六个窗口[3,6,7]的最大值为7。所以最终返回[5,5,5,4,6原创 2017-06-19 12:29:44 · 300 阅读 · 0 评论 -
回溯法,深度优先遍历
/*题目:地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?*/public class原创 2017-06-07 11:35:05 · 783 阅读 · 0 评论 -
卡特兰公式的应用
/*假设有n对左右括号,请求出合法的排列有多少个?合法是指每一个括号都可以找到与之配对的括号,比如n=1时,()是合法的,但是)(为不合法。给定一个整数n,请返回所求的合法排列数。保证结果在int范围内。*/public class Parenthesis { /* 使用卡特兰公式:(2*n)!除以n!*n!,再除以(n+1) 注意数的范围原创 2017-06-06 23:13:02 · 414 阅读 · 0 评论 -
数组旋转
/*题目:Rotate an array of n elements to the right by k steps.*/解法一:暴力求解,即每次通过移动数组的位置,进行旋转,但是最后一个测试用例超时public void rotate(int[] nums, int k) { for(int i=0;i int temp=nums[原创 2017-06-05 12:31:28 · 525 阅读 · 0 评论 -
125. Valid Palindrome
/*Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.判断给定的字符串是否是回文*/解法一:先将字符串转化为只有小写字母和数字的字符串,然后通过双指针即可,但是这样最后一个测试用例超时public boole原创 2017-06-05 10:56:28 · 177 阅读 · 0 评论 -
完成二叉树的判断
/*题目:有一棵二叉树,请设计一个算法判断它是否是完全二叉树。给定二叉树的根结点root,请返回一个bool值代表它是否为完全二叉树。树的结点个数小于等于500。*/ public boolean chk(TreeNode root) { /* 解题思路:采用层次遍历,在遍历过程成考虑3种情况, 1节点的左右孩子原创 2017-06-04 14:29:41 · 268 阅读 · 0 评论 -
平衡二叉树的判断
public boolean isBlance(TreeNode root) { boolean []result=new boolean[1]; result[0]=true; int level=1; getHeight(root,level,result); return resu原创 2017-06-04 13:32:47 · 271 阅读 · 0 评论 -
二分查找
题目:对于一个有序数组arr,再给定一个整数num,请在arr中找到num这个数出现的最左边的位置。给定一个数组arr及它的大小n,同时给定num。请返回所求位置。若该元素在数组中未出现,请返回-1。import java.util.*;public class LeftMostAppearance { public int findPos(int[] a原创 2017-06-03 20:12:26 · 180 阅读 · 0 评论 -
重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树 /* 解题思路:使用递归的思想,根据前序序列的节点,找出该节点在中序中的位置,并记录下 该位置,然后生成节点,继续递归,直到前序序列的起始节点大于末尾节点时退出递归 */ public TreeNode reConstructBinaryTree(int [] pre,int [] in)原创 2017-06-20 11:34:09 · 506 阅读 · 0 评论 -
找出二叉搜索树第k大的节点
题目:给定一颗二叉搜索树,请找出其中的第k大的结点。 /* 解题思路:通过中序遍历二叉搜索树,找到第k大的节点的值temp,然后通过层次遍历二叉搜索树,找到值为 temp的节点,并且返回该节点 注意考虑:1 二叉搜索树为空,或者k为0则直接返回null; 2 k的值大于二叉搜索树的节点个数,则返回null */ Tre原创 2017-06-20 14:56:34 · 2182 阅读 · 0 评论 -
380. Insert Delete GetRandom O(1)
题目:Design a data structure that supports all following operations in average O(1) time.insert(val): Inserts an item val to the set if not already present.remove(val): Removes an item val f原创 2017-07-01 00:19:09 · 192 阅读 · 0 评论 -
删除链表中的重复元素
题目:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5public ListNode deleteDuplication(ListNode pHead) { if(pHead==null){ return原创 2017-06-22 10:16:06 · 276 阅读 · 0 评论 -
521. Longest Uncommon Subsequence I
/* 解题思路:将字符串转化为字符数字,判断两个字符中哪个字符的长度比较大,然后遍历教小的字符 数组,通过较长的字符串判断是否包含较小字符串 */ public int findLUSlength(String a, String b) { int result=-1; char[] A=a.toCharArray();原创 2017-06-17 14:38:56 · 196 阅读 · 0 评论