LeetCode简单题汇总
给出的数组为 {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],
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。
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.请计算给出的数组(至少含有一个数字)中具有最大和的子数组(子数组要求在原数组中连续)
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步才能爬到楼梯顶部
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中,变成一个有序的数组
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简单题汇总的更多相关文章
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- leetcode简单题6
今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- Go: LeetCode简单题,简单做(sort.Search)
前言 正值端午佳节,LeetCode也很懂.这两天都是简单题,早点做完去包粽子. 故事从一道简单题说起 第一个错误的版本 简单题 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最 ...
- LeetCode好题汇总
最近开始刷LeetCode,准备按照专题来进行.所有的解题方案我都会放在GitHub上面,对于有价值的题目,我会重新在这里做记录,并且将解题方案贴出来,便于自己之后复习. Array 1. easy ...
- LeetCode简单题(三)
题目一: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股 ...
- LeetCode简单题(二)
题目一: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的 ...
- LeetCode简单题(一)
题目一: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
- Leetcode简单题
# Title Solution Acceptance Difficulty Frequency 1 Two Sum 44.5% Easy 2 Add Two Number ...
- LeetCode简单题(四)
题目一: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你 ...
随机推荐
- xcode 6 如何将 模拟器(simulator) for iphone/ipad 转变成 simulator for iphone
xcode 6默认模拟器是iphone/ipad通用的,如果想只针对iphone或者ipad可以进行如下设置: 1.修改模拟器大小(非必须) 模拟器->WIndow->scale-> ...
- Linux之文件传输
本文借鉴<Linux命令大全> 1. bye命令 功能:终端FTP连线并结束程序 语法:bye 补充:在ftp模式下,输入bye即可中断目前的连线作业,并结束ftp的执行. 2. ftp命 ...
- PHP 的 SAPI 是个什么东西(转)
SAPI,是 Server Application Programming Interface 的首字母缩写,意思是服务器端应用编程接口. 这是 PHP 内核提供给外部调用其服务的接口,即外部系统可 ...
- zabbix_get工具基础使用
zabbix_get工具基础使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.zabbix_get工具概述 我们在使用zabbix server监控zabbix agent端 ...
- Zabbix——自动监控
zabbix简介 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管 ...
- Python连载61-tkinter三种布局
一.pack布局举例 #pack布局案例 import tkinter baseFrame = tkinter.Tk() #以下代码都是创建一个组件,然后布局 btn1 = tkinter.Butto ...
- 028、Java中的关系运算符
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 吴裕雄--天生自然java开发常用类库学习笔记:线程常用的操作方法
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i< ...
- DevOps - 不适用
章节 DevOps – 为什么 DevOps – 与传统方式区别 DevOps – 优势 DevOps – 不适用 DevOps – 生命周期 DevOps – 与敏捷方法区别 DevOps – 实施 ...
- C# Winform使用线程,委托定时更新界面UI控件,解决界面卡顿问题(转载)
一.定时执行主界面控件值 1.利用定时器 Thread t = null; private void InitTSJK() { t = new Thread(new ThreadStart(GetDa ...