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. java使用zookeeper实现的分布式锁示例

    java使用zookeeper实现的分布式锁示例 作者: 字体:[增加 减小] 类型:转载 时间:2014-05-07我要评论 这篇文章主要介绍了java使用zookeeper实现的分布式锁示例,需要 ...

  2. 动态、指针field-symbols初探

    DATA: BEGIN OF STRUC, COMP1 VALUE ', COMP2 VALUE ', COMP3 TYPE STRING VALUE 'bruce king', END OF STR ...

  3. 在React中随机生成图形验证码

    各个方法 在输入框中定义一个位置存放图形 完整代码 方便复制粘贴 import React, { Component } from 'react'; import styles from './lef ...

  4. 干货 | RDS For SQL Server单库上云

    数据库作为核心数据的重要存储,很多时候都会面临数据迁移的需求,例如:业务从本地迁移上云.数据中心故障需要切换至灾备中心.混合云或多云部署下的数据同步.流量突增导致数据库性能瓶颈需要拆分-- 本文将会一 ...

  5. 设备树DTS 学习: uboot 传递 dtb 给 内核

    背景 得到 dtb 文件以后,我们需要想办法下载到 板子中,并给 Linux 内核使用. (高级版本的 uboot也有了 自己使用设备树支持,我们这里不讨论 uboot 使用的设备树) Linux 内 ...

  6. HDU 4960 Handling the past 2014 多校9 线段树

    首先确定的基本思想是按时间离散化后来建线段树,对于每个操作插入到相应的时间点上 但是难就难在那个pop操作,我之前对pop操作的处理是找到离他最近的那个点删掉,但是这样对于后面的peak操作,如果时间 ...

  7. UVA 12510/CSU 1119 Collecting Coins DFS

    前年的省赛题,难点在于这个石头的推移不太好处理 后来还是看了阳神当年的省赛总结,发现这个石头这里,因为就四五个子,就暴力dfs处理即可.先把石头当做普通障碍,进行一遍全图的dfs或者bfs,找到可以找 ...

  8. 云服务器CentOS7系统环境配置(jdk和tomcat)

    配置jdk 下载与安装 如果没有安装wget,首先安装wget工具 yum -y install wget 安装完成后,在网上找到jdk的下载链接,可以在官网找点击此处到官网下载,如果官网下载慢也可以 ...

  9. S7-300数据处理基本知识(结尾以MW8+1 ADD指令实训仿真,并用状态表监控及刷写变量)

    数据处理基本知识汇总 STEP7 的数据类型包括什么? 基本数据类型 复杂数据类型 用于FB(功能块)的输入,输出参数类型 用于FC(功能)的输入,输出参数类型 基本数据类型是什么? 先列举12种数据 ...

  10. GNS3 模拟icmp端口不可达

    R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 no ip routing end R2 f0/0: conf t ...