随笔分类 -  leetcode-array

摘要:1 public class Solution { 2 public int maxPoints(Point[] points) { 3 HashMap map = new HashMap(); 4 int res = 0; 5 for(int i=0;i<points.length;i++){ 6 map.clear(); 7 int curMax = 1; 8 int sameP =0; 9 for(int j=i+1;j<points.... 阅读全文
posted @ 2014-02-24 04:06 krunning
摘要:There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting gas 阅读全文
posted @ 2014-02-24 03:45 krunning
摘要:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.Note: You 阅读全文
posted @ 2014-02-22 13:12 krunning
摘要:There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors. 1 public class Solution { 2 p... 阅读全文
posted @ 2014-02-22 13:05 krunning
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 1 public class Solution { 2 public int maxProfit(int[]... 阅读全文
posted @ 2014-02-19 05:33 krunning
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions. 1 public class Solution { 2 public int maxProfit(int[] prices) { 3 int len = prices.length; 4 if(len==0) re... 阅读全文
posted @ 2014-02-19 05:30 krunning
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文
posted @ 2014-02-19 05:27 krunning
摘要:Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to ... 阅读全文
posted @ 2014-02-19 00:30 krunning
摘要:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]] 1 public class Solution { 2 public ArrayList> generate(int numRows) { 3 ArrayList> res = new ArrayList>(); 4 if(numRows==0) retu... 阅读全文
posted @ 2014-02-19 00:25 krunning
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse. 1 阅读全文
posted @ 2014-02-13 04:51 krunning
摘要:1 public class Solution { 2 public ArrayList> fourSum(int[] num, int target) { 3 ArrayList> res = new ArrayList>(); 4 int len = num.length; 5 if(len temp = new ArrayList();15 temp.add(num[i]);16 temp.add(num[j]);17 ... 阅读全文
posted @ 2014-02-13 04:27 krunning
摘要:Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice? 1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 int len = A.length; 4 if(len<=2) return len; 5 int count = 1; 6 int p1 =1, p2=1; 7 while(p2<len){ 8 ... 阅读全文
posted @ 2014-02-13 04:26 krunning
摘要:1 public class Solution { 2 public ArrayList grayCode(int n) { 3 ArrayList res = new ArrayList(); 4 res.add(0); 5 for(int i=0;i=0;j--){ 9 res.add(highest+res.get(j));10 }11 }12 return res;13 }14 }View Code 阅读全文
posted @ 2014-02-13 04:25 krunning
摘要:1 public class Solution { 2 public String countAndSay(int n) { 3 String res = "1"; 4 if(n==0) return res; 5 for(int i=1;i1){22 temp = temp+count+last;23 count = 1;24 }25 else{26 temp = temp+c... 阅读全文
posted @ 2014-02-12 04:01 krunning
摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. 1 public class Solution { 2 publ... 阅读全文
posted @ 2014-02-12 03:56 krunning
摘要:Determine whether an integer is a palindrome. Do this without extra space. 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if(x=10){ 6 div *=10; 7 } 8 while(x>0){ 9 if(x/div!=x%10) return false;10 x = x%div/10;11 ... 阅读全文
posted @ 2014-02-07 03:49 krunning
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A =[2,3,1,1,4]The minimum n 阅读全文
posted @ 2014-02-07 03:47 krunning
摘要:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of 阅读全文
posted @ 2014-02-07 03:46 krunning
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity. 1 public class Solution { 2 public... 阅读全文
posted @ 2014-02-06 15:03 krunning
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array. 1 public class Solution { 2 public void merge(int A[], int m, int B[], int n) { 3 int p3 = m+n-1; 4 int p1 = m-1; 5 int p2 = n-1; 6 while(p1>=0 && p2>=0){ 7 if(A[p1]>=B[p2]){ ... 阅读全文
posted @ 2014-02-06 14:49 krunning