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. HTTP协议扫盲(二)HTTP协议的请求方法、请求头和响应头

    一.HTTP请求方法 Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET,POST,PUT,DELETE. 一个URL地址用于描述一个网络上的资源,而HTTP中的GET, POST ...

  2. python基础—函数装饰器

    python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...

  3. Scale

    Scale刻度组件. 当你希望用户输入某个范围内的一个数值,使用scale组件可以很好的代替Entry组件. 用法: 创建一个指定范围的Scale组件其实非常容易,你只需要指定from和to两个选项即 ...

  4. node.js使用node-xlsx读写数据

    下面是包含了读写的样例: var xlsx = require('node-xlsx'); var fs = require('fs'); // 读取文件内容 var data = xlsx.pars ...

  5. MySQL实现全关联 full outer join

    SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行. LEFT JOIN 关键 ...

  6. Pycharm节能模式

    如题,开启节能模式代码不会自动补全.

  7. STM32 - GPIO

    买了一个STM32F4的开发板,想把上面的东西重新学一下 快速过: 一.GPIO控制 void GPIO_DeInit(GPIO_TypeDef* GPIOx); //把某一个IO口恢复到默认值 /* ...

  8. [TJOI 2016&HEOI 2016]排序

    Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

  9. [HNOI2006]公路修建问题

    题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 输入输出样例 输入样例#1: 复制 4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1 3 ...

  10. [LSGDOJ1822]书架 Splay

    题目描述 Sally有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. Sally在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一 ...