`

算法2

阅读更多
两数之和,数组中两个数和等于固定值A,这两个数的位置?
暴力法:
class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i=0 ; i<nums.length; i++) {
            for(int j=i+1 ; j<nums.length; j++){
                if(nums[i]+nums[j] ==  target) {
                    return new int[]{ i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}

一致性哈希法
class Solution {
    public int[] twoSum(int[] nums, int target) {
        
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0 ; i<nums.length; i++) {
            int j = target-nums[i];
            if(map.containsKey(j)) {
                return new int[] {map.get(j),i};
            }
            map.put(nums[i],i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}



单链表反转
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        while(head!=null){
            ListNode temp = head.next;
            head.next = prev;
            prev = head;
            head = temp;
        }
        return prev;
    }
}


二叉树广度遍历
深度用栈
广度用队列
https://blog.csdn.net/m_sdn/article/details/84864645
  private static void bfs(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode peek = queue.poll();
            System.out.println(peek.value + "\t");
            if (peek.left != null) queue.offer(peek.left);
            if (peek.right != null) queue.offer(peek.right);
        }
    }

 private static void dfs(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode peek = stack.peek();
            System.out.println(peek.value + "\t");
            stack.pop();
 
            if (peek.right != null) stack.push(peek.right);
            if (peek.left != null) stack.push(peek.left);
        }
    }
 
Stack.peek()与Stack.pop()
peek():返回栈顶的值 ;不改变栈的值,查看栈顶的对象而不移除它。
pop():返回栈顶的值 ;会把栈顶的值删除。

poll与pop
poll:Queue(队列)的一个方法,获取并移除此队列的头,如果此队列为空,则返回null。
pop:Stack(栈)的方法,移除堆栈顶部的对象,并作为此函数的值返回该对象 。


裴波那契数列
第n个数值等于n-1 + n-2的数值和
public static void main(String[] args) {
		// TODO Auto-generated method stub
		 for(int i=0;i<10;i++){
			   System.out.println(getfibonacci(i));
			  }  
	}
	
	public static long getfibonacci(int n){
		if(n==0){
			return 0;
		}else if(n==1){
			return 1;
		}else{
			long fib[]=new long[n+1];
			fib[0]=0;
			fib[1]=1;
			for(int i=2;i<=n;i++){
				fib[i]=fib[i-1]+fib[i-2];
			}
			return fib[n];
		}






反转链表

    public static  ListNode reverseList(ListNode head) {
        //定义两个临时变量 
        ListNode prev = null; // 预返回变量
        ListNode curr = head; // 正在处理移位指向的变量
        while (curr != null) {
            ListNode nextTemp = curr.next; // ABCD 临时变量指向BCD
            curr.next = prev; // ABCD分为BCD和A 其中把A的指向指向到null变量 获取出A,下一次获取B+Anull= BA
            prev = curr; // 预返回变量设置为Anull 下一次设置为 BAnull
            curr = nextTemp; // 当前移位为BCD
        }
        return prev;
    }

    public static class ListNode {
     int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(2);
        ListNode head3 = new ListNode(3);
        ListNode head4 = new ListNode(4);
        ListNode head5 = new ListNode(5);
        head4.next = head5;
        head3.next = head4;
        head2.next = head3;
        head1.next = head2;

        System.out.println(reverseList(head1));
    }





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics