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指向的堆 否则报错
}

  

												

LeetCode 2的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. 新概念英语(1-101)A Card From Jimmy

    Lesson 101 A card from Jimmy 吉米的明信片 Listen to the tape then answer this question. Does Grandmother s ...

  2. angular2 学习笔记 ( Rxjs, Promise, Async/Await 的区别 )

    Promise 是 ES 6 Async/Await 是 ES 7 Rxjs 是一个 js 库 在使用 angular 时,你会经常看见这 3 个东西. 它们都和异步编程有关,有些情况下你会觉得用它们 ...

  3. GIT入门笔记(8)-- 查看历史提交记录/根据版本号回到过去或未来

    在Git中,用HEAD表示当前版本,也就是最新的提交版本, 上一个版本就是HEAD^, 上上一个版本就是HEAD^^, 往上100个版本写100个^比较容易数不过来,所以写成HEAD~100. Git ...

  4. 消息队列的使用 RabbitMQ (二): Windows 环境下集群的实现

    一.RabbitMQ 集群的基本概念 一个 RabbitMQ 中间件(broker) 由一个或多个 erlang 节点组成,节点之间共享 用户名.虚拟目录.队列消息.运行参数 等, 这个 节点的集合被 ...

  5. python基础二(基础数据类型)

    一. 引子 1. 什么是数据 x=10,10是我们要存储的数据 2. 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类型 数字 字符串 列表 元组 字 ...

  6. Django Form组件 学生管理系统

    from django.db import models # Create your models here. class Classes(models.Model): title=models.Ch ...

  7. 基于OpenCV单目相机的快速标定--源码、工程、实现过程

    相机的标定是所有人走进视觉世界需要做的第一件事,辣么多的视觉标定原理解释你可以随便在网上找到,这里只讲到底如何去实现,也算是给刚入门的朋友做个简单的分享. 1.单目相机标定的工程源码 首先请到同性交友 ...

  8. CNN中减少网络的参数的三个思想

    CNN中减少网络的参数的三个思想: 1) 局部连接(Local Connectivity) 2) 权值共享(Shared Weights) 3) 池化(Pooling) 局部连接 局部连接是相对于全连 ...

  9. SQL SERVER之查询外键及索引

    --查询表或数据库中的所有外键 select A.name as 约束名, object_name(b.parent_object_id) as 外健表, c.name as 外键列, object_ ...

  10. LOJ #6119. 「2017 山东二轮集训 Day7」国王

    Description 在某个神奇的大陆上,有一个国家,这片大陆的所有城市间的道路网可以看做是一棵树,每个城市要么是工业城市,要么是农业城市,这个国家的人认为一条路径是 exciting 的,当且仅当 ...