面试题总结-算法部分

  • 最大连续子序列和
  public void maxSum(int[] nums) {
        int start = 0;
        int end = 0;
        int max = 0;

        int temp = 0;
        int ts = 0;
        for(int i = 0; i < nums.length; i++) {
            temp += nums[i];
            if(temp < 0) {
                ts = i + 1;
                temp = 0;
            } else {
                if(temp > max) {
                    start = ts;
                    end = i;
                    max = temp;
                }
            }
        }

        System.out.println("maxSum = " + max + ", start : " + start + ", end = " + end);
    }
复制代码
  • 单链表逆序

  • 排序方法

  • 快排序算法
//时间复杂度:nlg(2n)
public void sort(int[] a,int low,int high){
         int start = low;
         int end = high;
         int key = a[low];
         
         
         while(end>start){
             //从后往前比较
             while(end>start&&a[end]>=key)  //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
                 end--;
             if(a[end]<=key){
                 int temp = a[end];
                 a[end] = a[start];
                 a[start] = temp;
             }
             //从前往后比较
             while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
                start++;
             if(a[start]>=key){
                 int temp = a[start];
                 a[start] = a[end];
                 a[end] = temp;
             }
         //此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
         }
         //递归
         if(start>low) sort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
         if(end<high) sort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
     }
复制代码
  • 冒泡排序
/**
     * 冒泡排序   时间复杂度:n^2
     * 比较相邻的元素。如果第一个比第二个大,就交换他们两个。  
     * 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。  
     * 针对所有的元素重复以上的步骤,除了最后一个。
     * 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 
     * @param numbers 需要排序的整型数组
     */
    public static void bubbleSort(int[] numbers)
    {
        int temp = 0;
        int size = numbers.length;
        for(int i = 0 ; i < size-1; i ++) {
            for(int j = 0 ;j < size-1-i ; j++) {
                //交换两数位置
                if(numbers[j] > numbers[j+1]) {
                    temp = numbers[j];
                    numbers[j] = numbers[j+1];
                    numbers[j+1] = temp;
                }
            }
        }
    }
复制代码
  • 选择排序
/**
     * 选择排序算法  时间复杂度:n^2
     * 在未排序序列中找到最小元素,存放到排序序列的起始位置  
     * 再从剩余未排序元素中继续寻找最小元素,然后放到排序序列末尾。 
     * 以此类推,直到所有元素均排序完毕。 
     * @param numbers
     */
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i; // 用来记录最小值的索引位置,默认值为i
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j; // 遍历 i+1~length 的值,找到其中最小值的位置
                }
            }
            // 交换当前索引 i 和最小值索引 minIndex 两处的值
            if (i != minIndex) {
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
            // 执行完一次循环,当前索引 i 处的值为最小值,直到循环结束即可完成排序
        }
    }
复制代码
  • 插入排序
/**  
     * 插入排序 时间复杂度:n^2
     * 
     * 从第一个元素开始,该元素可以认为已经被排序
     * 取出下一个元素,在已经排序的元素序列中从后向前扫描 
     * 如果该元素(已排序)大于新元素,将该元素移到下一位置  
     * 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置  
     * 将新元素插入到该位置中  
     * 重复步骤2  
     * @param numbers  待排序数组
     */  
    public static void insertSort(int[] numbers) {
        int size = numbers.length;
        int temp = 0 ;
        int j =  0;
        for(int i = 0 ; i < size ; i++) {
            temp = numbers[i];
            //假如temp比前面的值小,则将前面的值后移
            for(j = i ; j > 0 && temp < numbers[j-1] ; j --) {
                numbers[j] = numbers[j-1];
            }
            numbers[j] = temp;
        }
    }
复制代码
  • 将两个有序链表合并成一个链表
public class MyList {
    /**
     * 递归方式合并两个单链表
     *
     * @param head1 有序链表1
     * @param head2 有序链表2
     * @return 合并后的链表
     */
    public static Node mergeTwoList(Node head1, Node head2) {
        //递归结束条件
        if (head1 == null && head2 == null) {
            return null;
        }
        if (head1 == null) {
            return head2;
        }
        if (head2 == null) {
            return head1;
        }
        //合并后的链表
        Node head = null;
        if (head1.data > head2.data) {
            //把head较小的结点给头结点
            head = head2;
            //继续递归head2
            head.next = mergeTwoList(head1, head2.next);
        } else {
            head = head1;
            head.next = mergeTwoList(head1.next, head2);
        }
        return head;
    }
复制代码
  • 二分法查找
public int search(int[] arr, int key) {
       int start = 0;
       int end = arr.length - 1;
       while (start <= end) {
           int middle = (start + end) / 2;
           if (key < arr[middle]) {
               end = middle - 1;
           } else if (key > arr[middle]) {
               start = middle + 1;
           } else {
               return middle;
           }
       }
       return -1;
   }
复制代码
  • 二叉树的深度遍历和广度遍历
class ListNode{
      ListNode left;
      ListNode right;
      int val;
      public ListNode(int value){
            this.val=value;
      }
}
复制代码
//广度遍历
public void levelOrderTraversal(LsitNode node){
      if(node==null){
            System.out.print("empty tree"); 
            return;
      }
      ArrayDeque<ListNode> deque = new ArrayDeque<ListNode>();
      deque.add(node);
      while(!deque.isEmpty()){
            ListNode rnode = deque.remove();
            System.out.print(rnode.val+"  ");
            if(rnode.left!=null){
                  deque.add(rnode.left);
            }
            if(rnode.right!=null){
                  deque.add(rnode.right);
            }
      }
}
复制代码
//深度遍历
public void depthTraversal(ListNode node){
       if(node==null){
             System.out.print("empty tree");
             return;
       }
       Stack<ListNode> stack = new Stack<ListNode>();
       stack.push(node);
       while(!stack.isEmpty()){
             ListNode rnode = stack.pop();
             System.out.print(rnode.val);
             if(rnode.right!=null){
                   stack.push(rnode.right);
             }
             if(rnode.left!=null){
                   stack.push(rnode.left);
             }
       }
}
复制代码
  • java中集合删除元素
//利用for循环删除
for (int i = 0; i < aList.size(); i++) {
    if ("abc".equals(aList.get(i))) {
        // 索引回溯
        aList.remove(i--);
    }
}
复制代码
//利用迭代器的remove()方法删除
ListIterator<String> listIterator = aList.listIterator();
while(listIterator.hasNext()){
	String  str = listIterator.next();
	if ("abc".equals(str)) {
	    //迭代器的remove() 方法删除
	    listIterator.remove(); 
	}
}
复制代码
  • Android 遍历ViewGroup找出某种类型的所有子View
// 遍历viewGroup
public void traverseViewGroup(View view) {
	if(null == view) {
		return;
	}
	if(view instanceof ViewGroup) {
		ViewGroup viewGroup = (ViewGroup) view;
		LinkedList<ViewGroup> linkedList = new LinkedList<>();
		linkedList.add(viewGroup);
		while(!linkedList.isEmpty()) {
			ViewGroup current = linkedList.removeFirst();
			//dosomething
			for(int i = 0; i < current.getChildCount(); i ++) {
				if(current.getChildAt(i) instanceof ViewGroup) {
					linkedList.addLast((ViewGroup) current.getChildAt(i));
				}else {
					//dosomething
				}
			}
		}
	}else {
		//dosomething
	}
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值