LeetCode 2

No1

  

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

个人:

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

  优秀代码: public int searchInsert(int[] A, int target) {        int low = 0, high = A.length-1;

        while(low<=high){
int mid = (low+high)/2;
if(A[mid] == target) return mid;
else if(A[mid] > target) high = mid-1;
else low = mid+1;
}
return low;
} No2

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.



  public void merge(int[] nums1, int m, int[] nums2, int n) {
int length=m+n-1,x=m-1,y=n-1;
while(x>-1 && y>-1){
nums1[length--] = (nums1[x]>nums2[y]) ? nums1[x--] : nums2[y--];
}
while(y>-1){
nums1[length--]=nums2[y--];
}
}

  

No3 

  Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

  For example,
  "A man, a plan, a canal: Panama" is a palindrome.
  "race a car" is not a palindrome.

  Note:
  Have you consider that the string might be empty? This is a good question to ask during an interview.

  For the purpose of this problem, we define empty string as valid palindrome.

  

  

public static boolean isPalindrome(String s) {
char[] str=s.toLowerCase().toCharArray();
StringBuilder stringBuilder=new StringBuilder();
for(int i=0;i<str.length;i++){
if(Character.isLetterOrDigit(str[i])&&str[i]!=' '){
stringBuilder.append(str[i]);
}
}
str=stringBuilder.toString().toCharArray();
if(str.length<=1)return true;
int start=0;
int length=str.length-1; while(start<length){
if(str[start]!=str[length]){
return false;
}else {
start++;
length--;
}
}
if(start>=length) return true;
return false;
}

  

public static boolean isPalindrome(String s) {
String str=s.replaceAll("[^A-Za-z0-9]","").toLowerCase();
String stringBUffer=new StringBuffer(str).reverse().toString();
return str.equals(stringBUffer);
}

  

No3

  Given a binary tree, find its maximum depth.

  The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}

  小心得:在求有一定规律结构时 可用递归去实现  而不用每个分支都储存 然后求size  。很屎的想法

 
No4 

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.


You may assume the two numbers do not contain any leading zero, except the number 0 itself.


Example


Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
 
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode p1 = l1, p2 = l2;
ListNode result = new ListNode(0);
ListNode pk=result;
int c = 0;
while (p1 != null || p2 != null || c == 1) {
int num1 =( p1 == null ? 0 : p1.val);
int num2 =( p2 == null ? 0 : p2.val);
int k = num1 + num2 + c;
c = k / 10;
pk.next = new ListNode(k % 10);
pk = pk.next;
if (p1 != null) {p1 = p1.next;}
if (p2 != null) {p2 = p2.next;}
}
return result.next;//不能反回pk.next; pk仅将指针只想result的堆 所以数据还存在result指向的堆 否则报错
}

  

上一篇:ORACLE 存储过程异常捕获并抛出


下一篇:企业和个人都需要的终极跨平台全端解决方案 UniApp