# leetcode 第二天

2017年12月27日

#### 4.（118）Pascal's Triangle

##### JAVA
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
//当numRows = 0 的情况
if(numRows == 0) return triangle;
//当numRows != 0 的情况

for(int i = 1; i <numRows;i++){
List<Integer> curRow = new ArrayList<Integer>();
List<Integer> preRow = triangle.get(i-1);
//first element

for(int j = 0 ;j<preRow.size()-1;j++){
}
}
return triangle;
}
}

##### Python
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
triangle = []

if numRows == 0:
return triangle

triangle.append([1])

for i in range(1,numRows):
curRow = []
preRow = triangle[i-1]

curRow.append(1)

for j in range(len(preRow)-1):
curRow.append(preRow[j]+preRow[j+1])

curRow.append(1)
triangle.append(curRow)
return triangle


# leetcode 第三天

2018年1月3日

#### 5.（217）Contains Duplicate

##### JAVA
// T:O(nlogn)  S:O(1)
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == nums[i + 1]) return true;
}
return false;
}

T:O(n)  S:O(n)
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>(nums.length);
for (int x: nums) {
if (set.contains(x)) return true;
}
return false;
}


#### 6.（717）1-bit and 2-bit Characters

##### JAVA
class Solution {
public boolean isOneBitCharacter(int[] bits) {
int i = 0;
while (i<bits.length - 1){
i += bits[i]+1;
}
return i == bits.length-1;
}
}

class Solution {
public boolean isOneBitCharacter(int[] bits) {
int i = bits.length - 2;
while (i >= 0 && bits[i] > 0) i--;
return (bits.length - i) % 2 == 0;
}
}

##### Python
class Solution(object):
def isOneBitCharacter(self, bits):
i = 0
while i < len(bits) - 1:
i += bits[i] + 1
return i == len(bits) - 1

class Solution(object):
def isOneBitCharacter(self, bits):
parity = bits.pop()
while bits and bits.pop(): parity ^= 1
return parity == 0


#### 7.（119）Pascal's Triangle II

##### Java
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>();
if(rowIndex < 0) return result;
for(int i = 0 ;i <= rowIndex;i++){
for(int j = i-1 ; j > 0 ; j--){
result.set(j,result.get(j)+result.get(j-1));
}
}
return result;
}
}


#### 8.（695）Max Area of Island

##### JAVA

class Solution {
int[][] grid;
boolean[][] seen;
public int maxAreaOfIsland(int[][] grid) {
this.grid = grid;
int result = 0;
seen = new boolean[grid.length][grid[0].length];
for(int r = 0;r<grid.length;r++)
for(int c = 0 ;c<grid[0].length;c++)
result = Math.max(result,area(r,c));
return result;
}

public int area(int r,int c){
if(r<0||r>=grid.length||c<0||c>=grid[0].length||seen[r][c]||grid[r][c]==0) return 0;
seen[r][c] = true;
return 1+area(r-1,c)+area(r,c-1)+area(r+1,c)+area(r,c+1);
}

}


#### 9.（26）Remove Duplicates from Sorted Array

##### JAVA
class Solution {
public int removeDuplicates(int[] nums) {
int newLength = 1;
if(nums.length == 0) return 0;
for(int i = 0;i<nums.length;i++)
if(nums[newLength-1] != nums[i]){
nums[newLength]= nums[i];
newLength++;
}
return newLength;
}
}


#### 10.（27）Remove Element

##### JAVA
class Solution {
public int removeElement(int[] nums, int val) {
int newLength = 0;
if(nums.length ==0) return newLength;
for(int i = 0;i<nums.length;i++)
if(nums[i]!=val)
nums[newLength++] = nums[i];
return newLength;
}
}


#### 11.（121）Best Time to Buy and Sell Stock

##### JAVA
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
int min = Integer.MAX_VALUE;
for(int i = 0;i<prices.length;i++){
min = Math.min(prices[i],min);
profit = Math.max(prices[i]-min,profit);
}
return profit;
}
}


#### 12.（122）Best Time to Buy and Sell Stock II

##### JAVA
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for(int i =0;i<prices.length-1;i++)
if(prices[i+1]>prices[i])
profit += prices[i+1]-prices[i];
return profit;
}
}


#### 13.（624）Maximum Distance in Arrays

Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.

Example 1:

Input:
[[1,2,3],
[4,5],
[1,2,3]]
Output: 4
Explanation:
One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.

Note:

1. Each given array will have at least 1 number. There will be at least two non-empty arrays.
2. The total number of the integers in all the m arrays will be in the range of [2, 10000].
3. The integers in the m arrays will be in the range of [-10000, 10000].
##### JAVA
public class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int res = 0;
int min = arrays.get(0).get(0);
int max = arrays.get(0).get(arrays.get(0).size() - 1);
for (int i = 1; i < arrays.size(); i++) {
List<Integer> array = arrays.get(i);
res = Math.max(Math.abs(min - array.get(array.size() - 1)), Math.max(Math.abs(array.get(0) - max), res));
min = Math.min(min, array.get(0));
max = Math.max(max, array.get(array.size() - 1));
}
return res;
}
}


#### 14.（35）Search Insert Position

##### JAVA
class Solution {
public int searchInsert(int[] nums, int target) {
for(int i =0;i<nums.length;i++){
if(nums[i] == target)
return i;
if(nums[i]>target)
return i;
}
return nums.length;
}
}


05-07 84

08-31 25

02-09 84

04-15 23

09-11 135

07-24 19

08-07 577

02-21 21

04-01 422

02-19 10

07-30 138

03-24 9

04-21 18

08-24 71

04-14 60万+

03-13 15万+

02-19 18万+

03-01 13万+

03-04 14万+

03-06 3723

03-08 7万+

04-25 7万+

03-10 13万+

03-10 19万+

03-12 11万+

03-13 11万+

03-16 2871

03-18 1万+

03-19 8万+

03-19 4万+

03-20 1万+

03-22 5万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-30 4万+

03-26 3022

03-29 22万+

04-10 1953

03-29 10万+

03-30 17万+

04-17 5万+

04-01 3385

03-23 1万+

04-02 1万+

04-02 4万+

06-01 862

04-06 7万+

04-06 5027

04-09 8万+

04-09 3万+

05-17 9797

04-11 4万+

04-15 6万+

04-18 5万+

04-20 5万+

05-03 1万+

05-16 5万+

05-06 2万+

05-07 1万+

05-08 4万+

05-12 1万+

05-14 7504

05-16 2476

05-16 1万+

05-16 2万+

05-30 2762

05-18 9322

05-18 6036

05-18 4372

05-21 9001

05-21 9169

05-21 622

05-30 2065

05-23 7297

05-28 3367

#### 因为无聊，我做出了一个能下载电影、动漫、电视剧的小程序，从此告别过去只能看部分视频简介的历史

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客