leetcode 134 62 63 67 143

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bw_yyziq/article/details/79780206

【134】gas-station

public class Solution {
	public int canCompleteCircuit(int[] gas, int[] cost) {

		int len = gas.length;
		for (int i = 0; i < len; i++) {
			int total = 0;
			int j = i;
			while (total >= 0) {
				total = total + gas[j] - cost[j];
				j = (j + 1) % (len);
				if (total < 0)
					break;
				if (j == i) return i;
			}
		}

		return -1;
	}
}

【62】unique-paths

//递归,这种方式复杂度太高
public class Solution {
    public int uniquePaths(int m, int n) {
		
    	if(m == 1 || n == 1) return 1;
    	
    	return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
    }
}
//维护一个数组
public class Solution {

	public int uniquePaths(int m, int n) {

		int[][] arr = new int[m][n];

		for (int i = 0; i < n; i++) {
			arr[0][i] = 1;
		}
		for (int i = 0; i < m; i++) {
			arr[i][0] = 1;
		}
		for (int i = 1; i < m; i++) {
			for (int j = 1; j < n; j++) {
				arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
			}
		}
		return arr[m - 1][n - 1];
	}
}

【63】unique-paths II

public class Solution {

	public int uniquePathsWithObstacles(int[][] obstacleGrid) {

		int m = obstacleGrid.length, n = obstacleGrid[0].length;

		if(obstacleGrid[m-1][n-1] == 1) return 0;
		
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (obstacleGrid[i][j] == 1)
					obstacleGrid[i][j] = -1;
			}
		}

		for (int i = m - 1; i >= 0; i--) {

			if (obstacleGrid[i][n - 1] == -1) {
				for(int j = i; j >= 0; j--) {
					obstacleGrid[j][n - 1] = 0;
				}
				break;
			}
			obstacleGrid[i][n - 1] = 1;
		}
		for (int i = n - 1; i >= 0; i--) {

			if (obstacleGrid[m-1][i] == -1) {
				for(int j = i; j >= 0; j--) {
					obstacleGrid[m-1][j] = 0;
				}
				break;
			}
			obstacleGrid[m - 1][i] = 1;
		}

		for (int i = m - 2; i >= 0; i--) {
			for (int j = n - 2 ; j >= 0; j--) {
				if (obstacleGrid[i][j] == -1) {
					obstacleGrid[i][j] = 0;
					continue;
				}
				obstacleGrid[i][j] = obstacleGrid[i+1][j] + obstacleGrid[i][j+1];
			}
		}

		return obstacleGrid[0][0];
	}
}

【67】add-binary

public class Solution {
	public String addBinary(String a, String b) {

		int len = a.length() - b.length();
		StringBuilder sb = new StringBuilder();
		if (len != 0) {
			for (int i = 0; i < Math.abs(len); i++) {
				sb.append("0");
			}
		}
		if (len > 0) {
			b = sb.toString() + b;
		} else {
			a = sb.toString() + a;
		}
		sb = new StringBuilder();
		char carry = '0';
		for (int i = a.length() - 1; i >= 0; i--) {
			if (a.charAt(i) == '0' && b.charAt(i) == '0') {
				sb.insert(0, carry);
				carry = '0';
			}else if (a.charAt(i) == b.charAt(i)) {
				sb.insert(0, carry);
				carry = '1';
			}else if (a.charAt(i) != b.charAt(i) && carry == '0') {
				sb.insert(0, '1');
				carry = '0';
			}else if (a.charAt(i) != b.charAt(i) && carry == '1') {
				sb.insert(0, '0');
				carry = '1';
			}
		}
		if (carry == '1') sb.insert(0, '1');
		return sb.toString();
	}
}

【143】reorder-list

public class Solution {
    public void reorderList(ListNode head) {
        
        if(head == null || head.next == null) return;
        
        ListNode slow = head;
        ListNode fast = head;
        ListNode temp;
        //快慢指针找到中间节点
        while(fast != null && fast.next != null){
            
            slow = slow.next;
            fast = fast.next.next;
        }
        
        fast = slow.next;
        slow.next = null;
        //翻转后半部分链表
        slow = null;
        temp = fast;
        while(fast != null){
            
            fast = fast.next;
            temp.next = slow;
            slow = temp;
            temp = fast;
        }
        //合并前半部分链表与和翻转之后的后半部分链表
        temp = head;
        while(temp != null && slow != null){
            
            fast = slow;
            slow = slow.next;
            fast.next = temp.next;
            temp.next = fast;
            temp = temp.next.next;
        }
        
    }
}



猜你喜欢

转载自blog.csdn.net/bw_yyziq/article/details/79780206