leetcode题目解答报告(1)
Remove Element
题目:
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
题意:从数组中去除某个值,返回新数组的长度;注意在去除完毕后数组中的元素应该变化了,即出现value的位置都被替换掉了,最后一句的意思是 新长度后面的元素值任意,也就是说,新数组的总长度可以保持不变,但是新长度之前的元素要正确,后面的无所谓。
思路:
设置下标i,j,i表示遍历数组的下标,j表示当前比较的元素下标。初始j=0,从i=0开始遍历数组。如果a[i]==value,不执行任何操作,进行下一次遍历。,直到找到一个a[i]不等于value,这时将将a[i]赋给a[j],然后j自加一。直到遍历结束,此时j的值就是新的数组长度
public class RemoveElement {
static public int quchu(int[] a,int value){
int i=0,j=0;
for(i=0;i<a.length;i++){//遍历数组
if(a[i]==value)//如果a[i]等于value,跳过继续执行
continue;
a[j]=a[i];//如果不相等,赋值给a[j]
j++;//j表示数组中与value不相等的元素个数
}
return j;
}
public static void main(String[] args){
int[] a={1,2,3,3,4,5,3,6,3,7,3};
int length=0;
length=quchu(a,3);
System.out.println(length);
}
}
Remove Duplicates from Sorted Array
题目:
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2,
and A is now [1,2].
题意:
public class RemoveDuplicatesfromSortedArray {
public static int removeDuplicates(int A[]) {
if(A.length == 0) {
return 0;
}
int j = 0;
/*
* 去重的数组A[0]不变,
* 查看A[1]是不是等于A[0],若等于,则继续查看A[2]是否等于A[0],直到找到一个不相等的A[i]
* 因为A[0]已被赋值,所以将A[i]的值赋给A[++j]
* 将新找到的数组元素作为比较对象继续遍历数组,直到结束
* 最后去重后的数组长度是j+1(A[0]-A[j]共j+1个)
* */
for(int i = 1; i < A.length; i++) {
if(A[j] != A[i]) {
j++;//下标加一
A[j] = A[i];
}
}
return j + 1;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int length=0;
int[] a={1,2,3,3,4,5,5,6,6,7,8,8,9};
length=removeDuplicates(a);
System.out.println(length);
}
}
Remove Duplicates from Sorted Array II
题目:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5,
and A is now [1,1,2,2,3].
题意:
每个数只允许重复一次,输出去重后的数字个数,依旧上一题的要求,常数空间也就是只能在原数组上操作。
思路:设置下标i,j,i表示遍历数组的下标,j表示当前比较的元素下标。初始j=0,从i=1开始遍历数组。如果a[i]==a[j],执行num加1操作,。之后判断num是否小于2,如果小于,则j自加1并把a[i]赋值给a[j]。若大于等于2,则不执行任何操作。如果a[i]!=a[j],则j自加1并把a[i]赋值给a[j],同时将num清零,进行下一次遍历,a[j]作为下一次比较的对象。直到遍历结束,此时j+1的值就是新的数组长度。
public class RemoveDuplicatesfromSortedArrayII {
public static int removeDuplicates(int A[]) {
if(A.length == 0) {
return 0;
}
int j = 0;
int num=0;//重复次数
/*
* 去重的数组A[0]不变,
* 查看A[1]是不是等于A[0],若等于,num自加1,此时num=1,满足条件。继续查看A[2]是否等于A[0],若等于,则num=2,不满足 *条件,之后所有重复的元素都被剔除。直到找到一个不相等的A[i]
* 因为A[0]已被赋值,所以将A[i]的值赋给A[++j],这时将num清零,用于统计新的元素相等的个数
* 将新找到的数组元素作为比较对象继续遍历数组,直到结束
* 最后去重后的数组长度是j+1(A[0]-A[j]共j+1个)
* */
for(int i = 1; i < A.length; i++) {
if(A[j]==A[i]) {
num++;
if(num<2) {
j++;
A[j]=A[i];
}
}
else {
j++;//下标加一
A[j] = A[i];
num=0;//清零,用于统计下一个元素的重复数
}
}
return j + 1;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int length=0;
int[] a={1,2,3,3,3,4,5,5,5,6,6,6,7,8,8,9};
length=removeDuplicates(a);
System.out.println(length);
}
}
Plus One
题目:
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
题意:
给定一个用数组表示的一个数,对它进行加一操作。 每一个数位都存储在数组的一个位置上。数组下标从大到小表示数位从低位到高位。
思路:
直接求解,设置一个进位标志carry,初值为1,表示加1,从最低位开始tmp
= a[x] + carry, a[x] = tmp%10,carry = tmp/10,如果carry不为0对下一位再进行操作,直到所有的数位处理完或者carray为0就退出,如果最后还有carray不为0说明整个数组要扩展一个数位。
public class PlusOne {
public static int[] mytets(int[] a) {
int carry=1;//进位值,初始为1,表示加1操作
int temp;
int i;
for(i=a.length-1;i>=0;i--) {
temp=a[i]+carry;
carry=temp/10;//向下一位的进位值
a[i]=temp%10;//当前为的结果值
if(carry==0) {//无进位则退出
break;
}
}
if(carry==1) {//分析最后产生的进位,例如999的特殊情况
int[] result=new int[a.length+1];
System.arraycopy(a, 0, result, 1, result.length-1);
result[0]=carry;
return result;
}
else {
return a;
} }
public static void main(String[] args) {
int []a= {9,9,9,9,9};
int result[]=mytets(a);
for (int i = 0; i <result.length; i++) {
System.out.print(result[i]);
} } }
leetcode题目解答报告(1)的更多相关文章
- leetcode题目解答报告(2)
Pascal's Triangle 题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- Leetcode题目practice
目录 Leetcode题目解答 1. 删除最外层的括号 2. 两数之和 3. 宝石与石头 4. 移除元素 5.删除排序数组中的重复项 6.寻找两个有序数组的中位数 7.盛最多水的容器 8.存在重复元素 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- LeetCode题目答案及理解汇总(持续更新)
面试算法题 dfs相关 全排列 #include<bits/stdc++.h> using namespace std; const int N = 10; //用一个path数组来存储每 ...
- leetcode题目清单
2016-09-24,开始刷leetcode上的算法题,下面整理一下leetcode题目清单.Github-leetcode 1.基本数学 Two Sum Palindrome Number Cont ...
- LeetCode 题目总结/分类
LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...
随机推荐
- SharepPoint 2013安装体会
SharePoint 2013出来了,一直没有找到合适的机器来安装.前天腾出来一台内存8G的机器,决定在Hyper-V上安装在一台虚机,然后装个Windows 2012,再装SharePoint 20 ...
- UVa11424 GCD - Extreme (I)
直接两重循环O(n^2)算gcd……未免太耗时 枚举因数a和a的倍数n,考虑gcd(i,n)==a的i数量(i<=n) 由于gcd(i,n)==a等价于gcd(i/a,n/a)==1,所以满足g ...
- 47深入理解C指针之---指针与硬件
一.size_t:用于安全表示长度,所有平台和系统都会解析成自己对应的长度 1.定义:size_t类型表示C中任何对象所能表示的最大长度,是个无符号整数:常常定义在stdio.h或stdlib.h中 ...
- 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- k8s入门简介
1.docker的三种编排工具 Docker的第一类编排工具: a.docker compose(docker原生):只能对一个主机上的容器进行编排,无法编排多个主机上的容器; b.docker sw ...
- POI2004
11th Polish Olympiad in Informatics(POI2004) <br > 填坑计划第二弹......把这个没填完的坑搬过来啦~ 上次勉强填完NEERC的坑... ...
- HTML5 一些有用的 APIs
Animation Timing Window.requestAnimationFrame(callback): 告诉浏览器希望执行一个动画,让浏览器在下一个动画帧安排一次网页重绘(类似于 setTi ...
- 前端必备性能知识 - http2.0
前端开发中,性能是一定绕不开的,今天就来说一下前后台通信中最重要的一个通道--HTTP2.0 最开始的通讯协议叫http1.0,作为始祖级的它,定义了最基本的数据结构,请求头和请求体,以及每一个字段的 ...
- KVC技巧二则
说两个与KVC相关的技巧. 1.KVC与字典 有时候我们需要取出嵌套字典中的某个键的值.例如某个嵌套字典: NSDictionary *dict = @{@"subDict":@{ ...
- windows10系统下安装nginx的安装步骤
打开nginx的官网:http://nginx.org/,下载最新的稳定版本. 下载完成后,解压到你想要解压的文件路径中,我解压到了D盘中,并把文件名改为nginx:进入文件内,打开nginx.e ...