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指向的堆 否则报错
}
LeetCode 2的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Python内置函数(42)——hash
英文文档: hash(object)Return the hash value of the object (if it has one). Hash values are integers. The ...
- Spring入门(3-1)Spring的标签命名空间
1.标签命名空间声明: 2.标签命名空间使用 标签默认的命名空间是 security:,可以不用带 security:,直接写标签,如: <http <authentication-ma ...
- mybatis配置多数据源(利用spring的AbstractRoutingDataSource)
主要是利用了spring的AbstractRoutingDataSource. 直接上配置了: spring-mybatis.xml <bean name="dataSource&qu ...
- uva 10870
https://vjudge.net/problem/UVA-10870 题意: f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n ...
- 04、NetCore2.0下Web应用之Startup源码解析
04.NetCore2.0Web应用之Startup源码解析 通过分析Asp.Net Core 2.0的Startup部分源码,来理解插件框架的运行机制,以及掌握Startup注册的最优姿势. - ...
- 基于 MySQL 的数据库实践(自然连接)
在基本查询一节的示例中,我们有从 instructor 和 teaches 表组合信息,匹配条件是 instructor.ID 等于 teaches.ID 的查询,ID 属性是两个表中具有相同名称的所 ...
- [LeetCode] Dota2 Senate 刀塔二参议院
In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of ...
- php array_multisort函数实现按某一字段对二维数组进行排序
在工作中碰到一个页面如表格似的展示多条数据,要求根据其中的修改时间对数据进行排序, 数据格式类似于 $a = array( 0=>array( editTime=>'' addTime=& ...
- [BZOJ 4403]序列统计
Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input 输入第一行包含一个整数T,表示数据组 ...
- [SCOI2009]windy数
题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? 输入输出格式 输 ...