This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int searchInsert(int[] nums, int target) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
int i = 0, j = nums.length-1; | |
int index = -1; | |
while(i <= j) { | |
index = (i + j) / 2; | |
if(nums[index] == target) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def searchInsert(self, nums: 'List[int]', target: 'int') -> 'int': | |
lo, up = 0, len(nums) - 1 | |
mi = (lo + up) // 2 | |
while lo <= up: | |
if nums[mi] == target: | |
return mi | |
elif nums[mi] > target: | |
up = mi - 1 | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int maxSubArray(int[] nums) { | |
int res = nums[0]; | |
int curr = nums[0]; | |
for(int i = 1; i < nums.length; i++) { | |
curr += nums[i]; | |
if (curr < 0 || nums[i] > curr) | |
curr = nums[i]; | |
if (res < curr) | |
res = curr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def maxSubArray(self, nums: 'List[int]') -> 'int': | |
l = len(nums) | |
if l == 0: return 0 | |
res = now = nums[0] | |
for i in range(1, l): | |
if now > 0: | |
now += nums[i] | |
else: | |
now = nums[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solution from lx223 | |
public class Solution { | |
public String addBinary(String a, String b) { | |
StringBuilder sb = new StringBuilder(); | |
int i = a.length() - 1, j = b.length() - 1, carry = 0; | |
while (i >= 0 || j >= 0) { | |
int sum = carry; | |
if (j >= 0) sum += b.charAt(j--) - '0'; | |
if (i >= 0) sum += a.charAt(i--) - '0'; | |
sb.append(sum % 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def addBinary(self, a: 'str', b: 'str') -> 'str': | |
return bin(int(a, 2) + int(b, 2))[2:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int climbStairs(int n) { | |
if(n <= 2) return n; | |
int[] res = new int[n]; | |
res[1-1] = 1; | |
res[2-1] = 2; | |
for(int i = 3; i <= n; i++) { | |
res[i-1] = res[i-1-1] + res[i-2-1]; | |
} | |
return res[n-1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def climbStairs(self, n: 'int') -> 'int': | |
if n == 1: | |
return 1 | |
if n == 2: | |
return 2 | |
s1, s2 = 1, 2 | |
for _ in range(n - 2): | |
s1, s2 = s2, s1 + s2 | |
return s2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.next = None | |
class Solution: | |
def deleteDuplicates(self, head: 'ListNode') -> 'ListNode': | |
ite = head | |
while ite: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { val = x; } | |
* } | |
*/ | |
class Solution { | |
public ListNode deleteDuplicates(ListNode head) { |