# LeetCode 67,69,83,88,100

Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".

### 解法

class Solution {
public String addBinary(String a, String b) {
int aLength = a.trim().length();
int bLength = b.trim().length();
if (aLength == 0 && bLength == 0) return "0";
if (aLength == 0) return b;
if (bLength == 0) return a;
if (aLength < 10 && bLength < 10)
return Integer.toBinaryString(Integer.valueOf(a, 2) + Integer.valueOf(b, 2));

char[] maxChars, minChars;
int maxLength = 0, minLength = 0;
if (aLength >= bLength) {
maxLength = aLength;
minLength = bLength;
maxChars = a.toCharArray();
minChars = b.toCharArray();
} else {
maxLength = bLength;
minLength = aLength;
maxChars = b.toCharArray();
minChars = a.toCharArray();
}
boolean plusOne = false;
for (int i = 0; i < minLength; i++) {
if (minChars[minLength - 1 - i] == '0' && maxChars[maxLength - 1 - i] == '0') {
maxChars[maxLength - 1 - i] = plusOne ? '1' : '0';
plusOne = false;
} else if ((minChars[minLength - 1 - i] == '0' && maxChars[maxLength - 1 - i] == '1')
|| (minChars[minLength - 1 - i] == '1' && maxChars[maxLength - 1 - i] == '0')) {
maxChars[maxLength - 1 - i] = plusOne ? '0' : '1';
} else {
maxChars[maxLength - 1 - i] = plusOne ? '1' : '0';
plusOne = true;
}
}
if (plusOne) {
if (maxLength == minLength) {
return "1" + new String(maxChars);
} else {
for (int i = 0; i < maxLength - minLength; i++) {
int index = maxLength - minLength - 1 - i;
if (index > 0) {
if (maxChars[index] == '0') {
maxChars[index] = plusOne ? '1' : '0';
plusOne = false;
} else if (maxChars[index] == '1') {
maxChars[index] = plusOne ? '0' : '1';
}
} else {
if (plusOne) {
if (maxChars[0] == '0') {
maxChars[0] = '1';
} else {
maxChars[0] = '0';
return "1" + new String(maxChars);
}
}
}
}
}
}
return new String(maxChars);
}
}

## 69. Sqrt(x)

// Time Limit Exceeded:
class Solution {
public int mySqrt(int x) {
if (x == 0) return 0;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
int product = i * i;
if (product == x) {
return i;
} else if (product > x) {
return i - 1;
}
}
return 0;
}
}

P.s：

// Accepted
class Solution {
public int mySqrt(int x) {
long r = x;
while (r * r > x)
r = (r + x / r) / 2;
return (int) r;
}
}

## 83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

### 解法

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
class Solution {
}
}

## 88. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

## 解法

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
for(int i = 0; i < n; i++)
nums1[m + i] = nums2[i];
for (int i = 0; i < m + n; i++) {
for (int j = i + 1; j < m + n; j++) {
if(nums1[i]>nums1[j]){
int temp = nums1[i];
nums1[i] = nums1[j];
nums1[j] = temp;
}
}
}
}
}

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
for(int i = 0; i < n; i++)
nums1[m + i] = nums2[i];
Arrays.sort(nums1);
}
}

## 100. Same Tree

Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

### 解法

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} else if (p == null || q == null) {
return false;
} else {
return (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
}

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
return (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120