这几天把之前的设计模式回顾了一遍,整理了一点以前的项目。同学说,打算刷leetcode题目,也勾起了我的兴趣,索性也刷一些题目,再提高一些内功。刚开始进去,leetcode随机分配的题目,直接也就做了,在后来,按顺序做,现在做到了第五题。大概做了如下题目:
1. Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
这个题目简单,直接上代码如下:
public int[] twoSum(int []nums,int target){ if(nums==null||nums.length==0){ return null; } int re[]=new int[2]; for(int i=0;i<nums.length;i++){ for(int j=i+1;j<nums.length;j++){ if(nums[i]+nums[j]==target){ re[0]=i+1; re[1]=j+1; } } } return re; }
2. Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
这个题也不难,主要考虑好进位就行,稍微费点功夫,代码如下:
private ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode re=null,head=re; int tmp; int flag=0; while(l1!=null||l2!=null){ if(l1==null&&l2!=null){ tmp=l2.val+flag; }else if(l1!=null&&l2==null){ tmp=l1.val+flag; }else{ tmp=l1.val+l2.val+flag; } if(tmp/10!=0){//进位 ListNode lt=new ListNode(tmp%10); flag=1; if(l1==null&&l2!=null){ l1=null; l2=l2.next; }else if(l1!=null&&l2==null){ l1=l1.next; l2=null; }else{ l1=l1.next; l2=l2.next; } if(re!=null){ re.next=lt; re=re.next; }else{ re=lt; head=re; } //最后进位 if(l1==null&&null==l2){ ListNode lt1=new ListNode(tmp/10); re.next=lt1; re=re.next; } }else{ ListNode lt=new ListNode(tmp%10); flag=0; if(l1==null&&l2!=null){ l1=null; l2=l2.next; }else if(l1!=null&&l2==null){ l2=null; l1=l1.next; }else{ l1=l1.next; l2=l2.next; } if(re!=null){ re.next=lt; re=re.next; }else{ re=lt; head=re; } } } return head; }
3.Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
这道题也不是太难,主要需要考虑到各种情况,刚开始的时候,没有考虑是一种包含元素的关系,直接用队列删除首元素,因此,AC不了。调整代码如下:
public int lengthOfLongestSubstring(String s) { if(s.isEmpty()){ return 0; } int max=0,left=0; Queue<Character> q=new LinkedList<Character>(); for(int i=0;i<s.length();i++){ if(q.contains(s.charAt(i))){ while(left<i&&s.charAt(left)!=s.charAt(i)){ q.remove(s.charAt(left)); left++; } left++; }else{ q.offer(s.charAt(i)); max=Math.max(max,i-left+1); } } return max; }
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
这个题初一看,很简单的样子,但是,确实一个难度很大的题目,想到了前几天用到的二路归并的思想。代码如下:
public double findMedianSortedArrays(int nums1[], int nums2[]) { if((nums1.length+nums2.length)%2==1) return merge(nums1,nums2,0,nums1.length-1,0,nums2.length-1,(nums1.length+nums2.length)/2+1); else return (merge(nums1,nums2,0,nums1.length-1,0,nums2.length-1,(nums1.length+nums2.length)/2) +merge(nums1,nums2,0,nums1.length-1,0,nums2.length-1,(nums1.length+nums2.length)/2+1))/2.0; } private int merge(int A[], int B[], int i, int i2, int j, int j2, int k){ int m = i2-i+1; int n = j2-j+1; if(m>n) return merge(B,A,j,j2,i,i2,k); if(m==0) return B[j+k-1]; if(k==1) return Math.min(A[i],B[j]); int posA = Math.min(k/2,m); int posB = k-posA; if(A[i+posA-1]==B[j+posB-1]) return A[i+posA-1]; else if(A[i+posA-1]<B[j+posB-1]) return merge(A,B,i+posA,i2,j,j+posB-1,k-posA); else return merge(A,B,i,i+posA-1,j+posB,j2,k-posB); }
130. Surrounded Regions
Given a 2D board containing 'X'
and 'O'
, capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
For example,
X X X X X O O X X X O X X O X X
After running your function, the board should be:
X X X X X X X X X X X X X O X X
这个题主要是图的深度优先和广度优先遍历问题,在深度优先的时候,使用递归实现的时候,是不能AC的,会报栈溢出的错误,因此修改为了非递归的方式:
public void solve(char[][] board) { if(board==null||board.length==0){ return; } int m=board.length; int n=board[0].length; for(int i=0;i<m;i++){ if(board[i][0]=='O'){ dfs(board,i,0); } if(board[i][n-1]=='O'){ dfs(board,i,n-1); } } for(int j=0;j<n;j++){ if(board[0][j]=='O'){ dfs(board,0,j); } if(board[m-1][j]=='O'){ dfs(board,m-1,j); } } print(board); System.out.println("==============="); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(board[i][j]=='O'){ board[i][j]='X'; }else if(board[i][j]=='#'){ board[i][j]='O'; } } } print(board); } /** * @Title dfs * @Description 采用递归会报java.lang.StackOverflowError错误。非递归实现的时候,在if中加入continue。之后得不到正确结果。 * @param board * @param i * @param j * @Return void * @Throws * @user Administrator * @Date 2016年1月24日 */ public void dfs(char[][]board,int i,int j){ //非递归实现 Stack<Pos> s=new Stack<Pos>(); Pos pos=new Pos(i,j); s.push(pos); board[i][j]='#'; int m=board.length; int n=board[0].length; while(!s.isEmpty()){ Pos top=s.pop(); if(top.x>0&&board[top.x-1][top.y]=='O'){ Pos up=new Pos(top.x-1,top.y); s.push(up); board[up.x][up.y]='#'; } if(top.x<m-1&&board[top.x+1][top.y]=='O'){ Pos down=new Pos(top.x+1,top.y); s.push(down); board[down.x][down.y]='#'; } if(top.y>0&&board[top.x][top.y-1]=='O'){ Pos left=new Pos(top.x,top.y-1); s.push(left); board[left.x][left.y]='#'; } if(top.y<n-1&&board[top.x][top.y+1]=='O'){ Pos right=new Pos(top.x,top.y+1); s.push(right); board[right.x][right.y]='#'; } } } private class Pos{ int x,y; public Pos(int x,int y){ this.x=x; this.y=y; } };
206. Reverse Linked List
Reverse a singly linked list.
这个题很简单:
public ListNode reverseList(ListNode head) { ListNode re=null; Stack<ListNode> s=new Stack<ListNode>(); while(head!=null){ ListNode tmp=new ListNode(head.val); s.push(tmp); head=head.next; } ListNode st=null,h=re; while(!s.isEmpty()){ st=s.pop(); if(re==null){ re=st; h=re; }else{ re.next=st; re=re.next; } } return h; }
328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example:
Given 1->2->3->4->5->NULL
,
return 1->3->5->2->4->NULL
.
这个问题也不算太难:
public ListNode oddEvenList(ListNode head) { if(head==null)return head; ListNode odd=head,even=head.next,evenHead=even; while(odd.next!=null&&even.next!=null){ odd.next=even.next; odd=odd.next; even.next=odd.next; even=even.next; } odd.next=evenHead; return head; }
未完待续……