1.两个数之和
给出一个整数数组,请在数组中找出两个加起来等于目标值的数,
你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index2.。注意:下标是从1开始的
假设给出的数组中只存在唯一解
例如:

给出的数组为 {2, 7, 11, 15},目标值为9
输出 ndex1=1, index2=2  (所有的代码在牛客网的LeetCode板块运行)

 public class Solution {
public int[] twoSum(int[] nums, int target) { int index1 = 0,index2 =0;
int sum = target; for(int i = 0; i < nums.length; i++){
for(int j = i+1; j < nums.length; j++){
if (sum == nums[i]+ nums[j]){
index1 = i;
index2 = j;
}
}
}
int [] b ={index1+1,index2+1};
return b;
}
}

2.反转整数

将给出的整数x翻转。
例1:x=123,返回321
例2:x=-123,返回-321

你有思考过下面的这些问题么?

如果整数的最后一位是0,那么输出应该是什么?比如10,100
你注意到翻转后的整数可能溢出吗?假设输入是32位整数,则将翻转10000000003就会溢出,你该怎么处理这样的样例?抛出异常?这样做很好,但是如果不允许抛出异常呢?这样的话你必须重新设计函数(比如添加一个额外的参数)。

 public class Solution {
public int reverse(int x) {
if(x == 0){
return 0;
}else if(x < 0){
String str = String.valueOf(x);
StringBuffer bf = new StringBuffer(str.substring(1,str.length()));
String s = new String(bf.reverse());
return Integer.parseInt("-"+s);
}else {
StringBuffer bf = new StringBuffer(String.valueOf(x));
String s = new String(bf.reverse());
return Integer.parseInt(s);
}
}
}

14.编写一个函数来查找字符串数组中的最长公共前缀。

 public class Solution {
//水平
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0){
return "";
}
int max = strs[0].length() -1;
for(int i = 1; i < strs.length; i++){
int s = -1;
while(s < max && s < strs[i].length()-1){
if(strs[0].charAt(s+1) == strs[i].charAt(s+1)){
s++;
}else{
break;
}
}
if(s == -1){
return "";
}
max = s;
}
return strs[0].substring(0,max+1);
}
}

20.给出一个仅包含字符'(',')','{','}','['和']',的字符串,判断给出的字符串是否是合法的括号序列
括号必须以正确的顺序关闭,"()"和"()[]{}"都是合法的括号序列,但"(]"和"([)]"不合法。

 import java.util.*;
public class Solution {
public boolean isValid(String s) {
if(s == null || s.length() == 0){
return false;
}
Stack<Character> stack = new Stack<>();
char [] c = s.toCharArray();
for(int i = 0; i < s.length(); i++){
if(c[i] == '('){
stack.push(')');
}else if(c[i] == '{'){
stack.push('}');
}else if(c[i] == '['){
stack.push(']');
}else if(stack.empty() || c[i] != stack.pop()){
return false;
}
}
return stack.empty();
}
}

21.将两个有序的链表合并为一个新链表,要求新的链表是通过拼接两个链表的节点来生成的。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode heap = new ListNode(0);
ListNode p = heap;
while(l1 != null && l2 != null){
if(l1.val <= l2.val){
p.next = l1;
l1 = l1.next;
}else{
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if(l1 !=null){
p.next = l1;
}
if(l2 != null){
p.next = l2;
}
return heap.next;
}
}

 26.给定一个已排序的数组,使用就地算法将重复的数字移除,使数组中的每个元素只出现一次,返回新数组的长度。

不能为数组分配额外的空间,你必须使用常熟级空间复杂度的就地算法。

例如,
给定输入数组 A=[1,1,2],

你给出的函数应该返回length=2,A数组现在是[1,2]。
 public class Solution {
public int removeDuplicates(int[] A) {
if(A.length == 0 || A.length == 1){
return A.length;
}
int k = 0;
for(int i = 1; i < A.length; i++){
if(A[k] != A[i]){
A[++k]= A[i];
}
}
return k+1;
}
}

28.实现函数 strStr。

函数声明如下:
char *strStr(char *haystack, char *needle)
返回一个指针,指向needle第一次在haystack中出现的位置,如果needle不是haystack的子串,则返回null。
 public class Solution {
public String strStr(String haystack, String needle) {
int index = haystack.indexOf(needle);
if(index < 0){
return null;
}else{
return haystack.substring(index,haystack.length());
}
}
}

53.请计算给出的数组(至少含有一个数字)中具有最大和的子数组(子数组要求在原数组中连续)

例如:给出的数组为[−2,1,−3,4,−1,2,1,−5,4],
子数组[−2,1,−3,4,−1,2,1,−5,4],具有最大的和:6.
 public class Solution {
public int maxSubArray(int[] array) {
if(array.length == 0){
return 0;
}
int sum = 0;
//不能等于0,存在负数
int max = array[0];
for(int i=0; i < array.length; i++){
if(sum >= 0){
sum = sum+array[i];
}else{
//抛弃,重新计算
sum = array[i];
}
if(sum > max){
max = sum;
}
}
return max;
}
}

69.实现函数 int sqrt(int x). 计算并返回x的平方根

 public class Solution {
public int sqrt(int x) {
return (int)Math.sqrt(x);
}
}

 70.你在爬楼梯,需要n步才能爬到楼梯顶部

每次你只能向上爬1步或者2步。有多少种方法可以爬到楼梯顶部?
 public class Solution {
public int climbStairs(int n) {
if( n <= 2 ){
return n;
}
int [] s = new int[n+1];
s[0] = 0;
s[1] = 1;
s[2] = 2;
for(int i = 3; i < s.length;i++){
s[i] = s[i-1]+s[i-2];
}
return s[n];
}
}

88.给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组

注意:可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和n
 import java.util.*;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int i = m -1, j = n - 1, index = m+n-1;
while(i >= 0 && j >= 0){
A[index--] = A[i] >= B[j] ? A[i--] : B[j--];
}
while(j >=0){
A[index--] = B[j--];
}
}
}

104.求给定二叉树的最大深度,

最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。
 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int count = 0, depth = 0, nextCount = 1; Queue<TreeNode> queue = new LinkedList<TreeNode>(); //将根节点添加到队列中
queue.add(root); //第一次循环时队列的长度为1
while(queue.size()!=0){
count++;
//先进先出,取出队列的第一个元素
TreeNode top = queue.poll();
//如果根节点的左子树不为空,则将左子树的根节点添加到队列中
if(top.left!=null){
queue.add(top.left);
}
//如果根节点的右子树不为空,则将右子树的根节点添加到队列中
if(top.right!=null){
queue.add(top.right);
}
//当同一层的节点全部添加到队列中时,count与nextCount相等,deph+1
if(count==nextCount){
nextCount = queue.size();
depth++;
count=0;
}
}
return depth;
}
}

136.现在有一个整数类型的数组,数组中素只有一个元素只出现一次,其余的元素都出现两次。

注意:你需要给出一个线性时间复杂度的算法,你能在不使用额外内存空间的情况下解决这个问题么?
 public class Solution {
public int singleNumber(int[] A) {
//思路:两个相同的数异或为0
//例子:1^2^3^4^4^3^2 = 2^2^3^3^4^4^1 = 1
int result = A[0];
for(int i = 1; i < A.length; i++){
result = result ^ A[i];
}
return result;
}
}

LeetCode简单题汇总的更多相关文章

  1. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  2. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  3. Go: LeetCode简单题,简单做(sort.Search)

    前言 正值端午佳节,LeetCode也很懂.这两天都是简单题,早点做完去包粽子. 故事从一道简单题说起 第一个错误的版本 简单题 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最 ...

  4. LeetCode好题汇总

    最近开始刷LeetCode,准备按照专题来进行.所有的解题方案我都会放在GitHub上面,对于有价值的题目,我会重新在这里做记录,并且将解题方案贴出来,便于自己之后复习. Array 1. easy ...

  5. LeetCode简单题(三)

    题目一: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股 ...

  6. LeetCode简单题(二)

    题目一: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的 ...

  7. LeetCode简单题(一)

    题目一: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...

  8. Leetcode简单题

    # Title Solution Acceptance Difficulty Frequency     1 Two Sum       44.5% Easy     2 Add Two Number ...

  9. LeetCode简单题(四)

    题目一: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你 ...

随机推荐

  1. python异常处理(学习)

    异常处理可以保证程序在恶劣的环境也能运行,或者给用户相关的提示 如下是运行异常的情况 如无异常 也可以创建异常处理类型

  2. 095、Java中String类之不自动保存对象池操作

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. super關鍵字

    super 关键字 我们知道,this关键字总是指向函数所在的当前对象,ES6 又新增了另一个类似的关键字super,指向当前对象的原型对象. const proto = { foo: 'hello' ...

  4. 二十四 Redis消息订阅&事务&持久化

    Redis数据类型: Redis控制5种数据类型:String,list,hash,set,sorted-set 添加数据,删除数据,获取数据,查看有多少个元素,判断元素是否存在 key通用操作 JR ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-primary" 类的文本样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. prepareRefresh方法源码跟踪

    看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...

  7. tools.quartz.about

    官方网站,中文文档,demo,  参考零, 参考一, 参考二, 参考三, 参考四 , 参考五 ,文档下载 .

  8. spark aggregate算子

    spark aggregate源代码 /** * Aggregate the elements of each partition, and then the results for all the ...

  9. JAVAWEB limit 分页 (转载)

    原文来自于      https://www.jianshu.com/p/553fc76bb8eb  作者写的很不错 只是为了自己方便学习转载的  代码我就不贴了 我是 Oracle 要改一些代码 原 ...

  10. flask 常用数据模型模板

    1.一对多关系模型 示例代码 class Role(db.Model): """角色表""" __tablename__ = 'roles' ...