- 博客(31)
- 收藏
- 关注
原创 【18】Sort a stack in ascending order
Question: Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to writ
2013-05-22 22:15:53
542
原创 【17】Implement a MyQueue class which implements a queue using two stacks
Question:Implement a MyQueue class which implements a queue using two stackspackage CareerCup;public class MyQueue { private Stack s1; private Stack s2; public MyQueue(){} public void enque
2013-05-22 22:13:29
545
原创 【16】Set of stacks
Qustion:Imagine a (literal) stack of plates. If the stack gets too high, it might topple. There-fore, in real life, we would likely start a new stack when the previous stack exceedssome threshold. I
2013-05-01 13:21:11
512
原创 【15】Design a stack which has push pop min max
Question:How would you design a stack which, in addition to push and pop, also has functions which return the minimum and maximum elements? Push, pop, min, max should all operate inO(1) time. 思路:
2013-04-30 11:38:15
516
原创 Java 实现堆栈和队列
1.堆栈package CareerCup;public class Stack { Node top; public Stack(){} public void push(int data) { Node node = new Node(data); node.next = top; top = node; } public Node pop() {
2013-04-27 22:39:17
503
原创 【14】Use a single array to implement three stacks
Question:Describe how you could use a single array to implement three stacks. package CareerCup;public class OneArrayThreeStack { int[] array; int top1; int top2; int top3Left; int top3R
2013-04-27 22:37:17
671
原创 【13】Find the start of loop
Question:Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.DEFINITION:Circular linked list: A (corrupt) linked list in which a node’s next pointer
2013-04-23 21:59:36
692
原创 【12】Adds two numbers represented by a linked list and returns the sum as a linked list
Question:You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write
2013-04-15 23:14:28
365
原创 【11】Delete a node in the middle of a single linked list
Question:Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e package Career
2013-04-15 23:11:12
434
原创 【10】Find nth to last element of a singly linked list
Question:Implement an algorithm to find the nth to last element of a singly linked list. package CareerCup;public class FindNthToLast{ public FindNthToLast(){} public Node find(LinkedList
2013-04-13 18:37:19
509
原创 Java 链表实现
先定义节点:package CareerCup;public class Node{ public Node next = null; public int data = 0; public Node(){} public Node(int data) { this.data = data; this.next = null; } public vo
2013-04-13 17:55:57
373
原创 【9】Delete duplicates from an unsorted linked list
Question: Write code to remove duplicates from an unsorted linked list. How would you solve this problem if a temporary buffer is not allowed?package CareerCup;import java.util.HashSet;public
2013-04-13 17:53:25
740
原创 【8】set matrix's elements to 0
Question:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.package CareerCup;public class setZero { public setZero(){} public int[][] set(i
2013-04-13 15:21:33
392
原创 【7】Rotate matrix by 90 degrees
Question:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?package CareerCup;publi
2013-04-13 15:18:48
809
原创 【6】Decide if one string is a rotation of another string or not
Question: Assume you have a method isSubstring which checks if one word is a substring ofanother. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 usingonly one call to is
2013-04-10 22:38:28
787
原创 【5】Replace all spaces in a string with ‘%20’
Question:Write a method to replace all spaces in a string with ‘%20’. package CareerCup;public class ReplaceSpace { public ReplaceSpace(){} public String replace(String str) { String strRepl
2013-04-10 22:13:21
524
原创 【4】Decide if two strings are anagrams or not
Question: Write a method to decide if two strings are anagrams or not.package CareerCup;public class Anagram { public Anagram(){} public boolean detect(String str1, String str2) { if(str1.le
2013-04-10 22:09:25
651
原创 【3】Remove the duplicate characters in a string
Question: Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine An extra copy of the a
2013-04-09 21:47:54
868
原创 【2】Reverse a C-Style String
Question: Write code to reverse a C-Style String (C-String means that “abcd” is represented as five characters, including the null character )package CareerCup;public class ReverseCStyleStr {
2013-04-09 21:44:22
535
原创 【1】Detect if a string has all unique characters
Question: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?package CareerCup;public class UniqueChar { public UniqueC
2013-04-09 21:40:39
472
原创 快速排序
快速排序是一种不稳定排序。平均时间复杂度0(nlogn)。升序(降序)排序:每次选取一个基准数,先从后遍历直至遇到比基准数小(大)的数,则将该数与基准数的位置对调,并将该数位置视为下次置换的位置。再从前遍历直至遇到比基准数大(小)的数,则将该数置换到上次置换数的位置。直至从前遍历和从后遍历都到达同一位置,即找到基准数的位置,再将基准数替换位置的数即可。再对基准数左右的两个子集迭代该方法,即可完成整
2013-04-08 21:05:39
312
原创 归并排序
归并排序是一种稳定排序,归并时需要两两比较,遇到等值时将位置在前的置前即保持相对顺序。时间复杂度是0(nlogn)。public class MergeSort { public MergeSort(){} public static void merge(int[] array,int start,int mid,int end,int[] arraySorted,String r
2013-04-07 21:52:19
371
原创 堆排序
堆排序可以看成是另一种希尔排序,但它的增量是以2^n,即从当前节点经过父节点再到根节点组成一个子集,再对子集内元素进行插入排序。堆排序是一种不稳定排序。平均时间复杂度是0(nlogn)。public class HeapSort { public HeapSort(){} private static void CreatMinHeap(int[] array,int start,i
2013-04-06 19:26:17
303
原创 希尔排序
希尔排序是不稳定排序。因为是分割成子集进行插入排序的,相同元素的相对位置可能在不同子集的插入排序中发生变化。时间复杂度最优情况是0(nlog2n)。public class ShellSort { public ShellSort(){} public static int[] sort(int[] array,String rule) { if(!rule.equalsIgn
2013-04-05 18:56:53
325
原创 直接插入排序
简单插入排序是稳定排序,每次都选取未排序序列中的元素与已排序序列比较,只有不等值时才会移动已排序序列,所以等值元素的相对顺序不变。时间复杂度0(n^2)。public class InsertSort { public InsertSort(){} public static int[] sort(int[] array,String rule) { if(!rule.equa
2013-04-01 22:39:27
316
原创 简单选择排序
选择排序是一种不稳定排序。每次都选取未排序的序列中最大值或最小值,与第i个数值交换。交换中有可能破坏等数值元素的相对位置。例如: 4 6 4 2 1 第一遍选择会选取‘1’和第一个‘4’交换,则原来第一位的‘4’会排在第三位的‘4’之后。时间复杂度0(n^2)。 算法中需要注意的地方:每次选择极值时先记录当前位置(即要排序的位置),再遍历未排序的序列如有比当前数值大(或小)记录该
2013-03-28 08:13:46
312
原创 冒泡排序
冒泡排序是一种稳定的排序算法。即相同的两个元素在排序之前和排序之后的相对顺序不变,因冒泡排序是两两比较,不等则对调,对于等值的元素不做变动。时间复杂度是0(n^2)。public class BubbleSort { public BubbleSort(){} public static int[] sort(int[] array,String rule) { if(!rule.
2013-03-26 22:46:03
333
原创 二分查找
二分查找:实现中采用左闭右闭区间。在findPos中,如果要查找的数在数组中有重复值,则返回的是最靠近数组中间位置的值。在findPosFirst中,如果有重复值返回位置最前的值。在findPosLast中,如果有重复值返回位置最后的值。public class Bsearch { public Bsearch(){} public static int[] array
2013-03-25 20:20:49
352
原创 Find the min jumps
Given an array of integers, each element represents the max number of jumps that you can move forward.Write a piece of code to find out the minimum number of elements you need to select to reach t
2013-03-25 11:30:27
856
原创 24点游戏
上一篇“求等式”问题是24点游戏的简化版。采用穷举法。import java.util.Arrays;import java.util.Random;public class Game24points { public static double Threshold = 1E-6; public static int CardMax = 13; public static int
2013-03-24 15:36:40
507
原创 Create equation
Question:Given an array with positive integers and another integer for example{7 2 4} 9, you are required to generate an equation, by inserting operator add ("+") and minus ("-") among the array
2013-03-24 12:33:15
897
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人