27 December 2019
LeetCode(71) -- 24, 121, 62, 29
by Jerry Zhang
P24. Swap Nodes in Pairs (Medium)
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list’s nodes, only nodes itself may be changed.
Given 1->2->3->4, you should return the list as 2->1->4->3.
My Solution
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = dummy;
while (cur.next != null && cur.next.next != null) {
ListNode first = cur.next;
ListNode next = cur.next.next.next;
cur.next = cur.next.next;
cur.next.next = first;
cur.next.next.next = next;
cur = cur.next.next;
}
return dummy.next;
}
}
100%
P121. Best Time to Buy and Sell Stock (Easy)
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
My Solution
class Solution {
public int maxProfit(int[] prices) {
int diff = 0;
int min = Integer.MAX_VALUE;
for (int price : prices) {
min = Math.min(min, price);
diff = Math.max(price - min, diff);
}
return diff;
}
}
100%
P62. Unique Paths (Medium)
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
My Solution
public class P62_UniquePaths {
public int uniquePaths(int m, int n) {
int a, b;
if (m > n) {
a = m - 1;
b = n - 1;
} else {
a = n - 1;
b = m - 1;
}
long res = 1, sum = a + b;
while (sum > a) {
res *= sum;
sum--;
}
while (b > 1) {
res /= b;
b--;
}
return (int) res;
}
}
100%
The Best Solution
class Solution {
public int uniquePaths(int m, int n) {
if(m<=0 || n<=0){
return 0;
}
int[][] result = new int[m][n];
for(int i=0; i<n; i++){
result[0][i] = 1;
}
for(int i=0; i<m; i++){
result[i][0] = 1;
}
for(int i=1; i<m; i++){
for(int j=1; j<n; j++){
result[i][j] = result[i-1][j] + result[i][j-1];
}
}
return result[m-1][n-1];
}
}
P29. Divide Two Integers (Medium)
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero.
Input: dividend = 10, divisor = 3
Output: 3
Discuss
public class P29_DivideTwoIntegers {
public int divide(int dividend, int divisor) {
int sign = 1;
if ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0))
sign = -1;
long longDividend = Math.abs((long) dividend);
long longDivisor = Math.abs((long) divisor);
//Take care the edge cases.
if (longDivisor == 0) return Integer.MAX_VALUE;
if ((longDividend == 0) || (longDividend < longDivisor)) {
return 0;
}
long lans = ldivide(longDividend, longDivisor);
int ans;
if (lans > Integer.MAX_VALUE){ //Handle overflow.
ans = (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
} else {
ans = (int) (sign * lans);
}
return ans;
}
private long ldivide(long ldividend, long ldivisor) {
// Recursion exit condition
if (ldividend < ldivisor) return 0;
// Find the largest multiple so that (divisor * multiple <= dividend),
// whereas we are moving with stride 1, 2, 4, 8, 16...2^n for performance reason.
// Think this as a binary search.
long sum = ldivisor;
long multiple = 1;
while ((sum + sum) <= ldividend) {
sum += sum;
multiple += multiple;
}
//Look for additional value for the multiple from the reminder (dividend - sum) recursively.
return multiple + ldivide(ldividend - sum, ldivisor);
}
public static void main(String[] args) {
int divide = new P29_DivideTwoIntegers().divide(2147483647, 2);
System.out.println("divide = " + divide);
}
}
100%
tags: LeetCode