1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because num[0] + nums[1] = 2 + 7 = 9,
return [0, 1]
Solution:
/*
利用HashMap的特性,把时间复杂度由O(n^2)降到O(n),敲黑板重点
*/
public class Solution{
public int[] twoSum(int[] nums, int target){
HashMap<Integer,Inteeger> tracker = new HashMap<Integer, Integer>();
int len = nums.length;
for(int i = 0; i < len; i++){
if(tracker.containsKey(nums[i])){
int left = tracker.get(nums[i]);
return new int[]{left, i};
}else{
tracker.put(target - nums[i], i);
}
}
return new int[2];
}
}
2. 3Sum
Given an array s of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: the solution set must not contain duplicate triplets.
Example:
S = [-1, 0, 1, 2, -1, -4]
A solution set is:
[
[-1, 0, 1],
[-1, -1 ,2]
]
Solution:
/*
平均时间复杂度为O(n^2)
以递增的顺序对数组进行排序,遍历除最后两个元素的所有元素作为三元祖的第一个数,然后两头遍历数组,求的复合要求的第二个和第三个元素。
特别注意重复元素的剔除
*/
public class Solution{
public List<List<Integer>> threeSum(int[] nums){
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
if(nums[i]>0) break;
if(i==0||(i>0&&nums[i-1]!=nums[i])){
int j=i+1;
int k=nums.length-1;
int num=-nums[i];
while(j<k){
if(nums[j]+nums[k]==num){
res.add(Arrays.asList(nums[i],nums[j],nums[k]));
while(j<k&&nums[j]==nums[j+1]) j++;
while(j<k&&nums[k]==nums[k-1]) k--;
j++;
k--;
}else{
if(nums[j]+nums[k]<num) j++;
else k--;
}
}
}
}
return res;
}
}
3. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a square The coordinate (x,y) of apoint is represented by an integer array with two integers.
Example
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4[0,1]
Output: True
Solution
/*
注意4个点位置相同的情况
*/
public class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
int s[]=new int[6];
int k=0;
List<Integer[]> list=new ArrayList<>();
list.add(new Integer[]{p1[0], p1[1]});
list.add(new Integer[]{p2[0], p2[1]});
list.add(new Integer[]{p3[0], p3[1]});
list.add(new Integer[]{p4[0], p4[1]});
for(int i=0;i<3;i++){
for(int j=i+1;j<4;j++){
Integer[] a=list.get(i);
Integer[] b=list.get(j);
int tmp= (int) (Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));
s[k]=tmp;
int f=1;
while(k-f>=0&&tmp<s[k-f]){
tmp=s[k-f+1];
s[k-f+1]=s[k-f];
s[k-f]=tmp;
f++;
}
k++;
}
}
if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]&&s[4]==s[5]&&s[3]!=s[4])
return true;
else return false;
}
}
3. Add Two Numbers
You are given two non-empty liked lists representing two non-negative integers. 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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Solution:
/*
此题解题思路比较常规,但是需要注意链表空值状态的判断,以及如何使得代码逻辑规整。
class Solution为我的解决方案,class Solution1为别人的解决方案
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1,ListNode l2){
if(l1==null) return l2;
if(l1!=null&&l2==null) return l1;
return add(l1,l2,0);
}
public ListNode add(ListNode l1,ListNode l2,int carry){
int sum=l1.val+l2.val+carry;
int c=0;
if(sum>=0){
c=sum/10;
sum=sum%10;
}
ListNode node = new ListNode(sum);
if(l1.next==null) {
if(l2.next!=null){
if(c==0)
node.next=l2.next;
else
node.next=add(new ListNode(0),l2.next,c);
}else
if(c!=0)
node.next=add(new ListNode(0),new ListNode(0),c);
return node;
}
else{
if(l2.next==null) {
if(c==0)
node.next = l1.next;
else
node.next = add(l1.next, new ListNode(0), c);
}else node.next=add(l1.next,l2.next,c);
return node;
}
}
}
public class Solution1 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode prev = new ListNode(0);
ListNode head = prev;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
ListNode cur = new ListNode(0);
int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry;
cur.val = sum % 10;
carry = sum / 10;
prev.next = cur;
prev = cur;
l1 = (l1 == null) ? l1 : l1.next;
l2 = (l2 == null) ? l2 : l2.next;
}
return head.next;
}
}
4. Longest Substring Without Repeating Characters
Given a string,find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke",with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
Solution:
\*
利用HashMap减少时间复杂度,涉及到一组数据找一个数据时,应多思考HashMap来优化算法
*\
public class Solution {
public int lengthOfLongestSubstring(String s) {
int len=s.length();
if(len==0) return 0;
int max=1;
HashMap<Character,Integer> map=new HashMap<>();
for(int i=0,j=0;i<len;i++){
if(map.containsKey(s.charAt(i))){
j=Math.max(map.get(s.charAt(i))+1,j);
}
map.put(s.charAt(i),i);
max=Math.max(max,i-j+1);
}
return max;
}
}
5.
import sun.swing.SwingUtilities2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
/**
* Created by 01sr on 2017/6/26.
*/
public class Sum {
public static void main(String []args){
new Sum().findMedianSortedArrays(new int[]{},new int[]{1});
}
/*
* left | right
* A[0],A[1]...,A[i-1] | A[i],A[i+1]...,A[m-1]
* B[0],B[1]...,B[j-1] | B[j],B[j+1]...,B[n-1]
* 1)(i+j)*2=m+n+1;
* 2)A[i-1]<=A[i],B[j-1]<=B[j](数组已排序,所以肯定满足);
* 3)A[i-1]<=B[j],B[j-1]<=A[i];
*
* 由条件1)可以知道,当i确定时,j必然可以确定,所以控制i的变化就可以形成新的方案。设 i=0~m,则 j=(m+n+1)/2-i;
* 条件3)可作为方案终止的判断条件;
* */
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m=nums1.length;
int n=nums2.length;
if(m>n){
int tmp=m;
m=n;
n=tmp;
int[] tmps=nums1;
nums1=nums2;
nums2=tmps;
}
if(n==0)return -1;
int mini=0,maxi=m,i=(mini+maxi)/2,j=(m+n+1)/2-i;
while(mini<=maxi){
i=(mini+maxi)/2;
j=(m+n+1)/2-i;
if(i>0&&nums1[i-1]>nums2[j]) maxi=i-1;
else if(i<m&&nums2[j-1]>nums1[i]) mini=i+1;
else{
int left=-1,right;
if(i==0) left=nums2[j-1];
else if(j==0) left=nums1[i-1];
else left=nums1[i-1]>nums2[j-1]?nums1[i-1]:nums2[j-1];
if((m+n)%2!=0) return left;
if(i==m) right=nums2[j];
else if(j==n) right=nums1[i];
else right = nums1[i] < nums2[j] ? nums1[i] : nums2[j];
return (left+right)/2.0;
}
}
return -1;
}
}