1.移除元素
class Solution {
public int removeElement(int[] nums, int val) {
int i =0 ;
int j;
for (j=0;j<nums.length;j++){
if (nums[j]!=val){
nums[i]=nums[j];
i++;
}
}
return i;
}
}
2.实现 strStr
class Solution {
public static int strStr(String haystack, String needle) {
//当haystack的长度小于needle的长度时,直接返回false
if (haystack.length()==0&&needle.length()!=0||haystack.length()<needle.length()){
return -1;
}
if (haystack.length()==0&&needle.length()==0){
return 0;
}
if (needle.length()==0){
return 0;
}
int i ;
//找到haystack中与needle第一元素相同的位置,通过judge函数比较后面的是否一致。
for (i=0;i<haystack.length();i++){
if (needle.charAt(0)==haystack.charAt(i)){
//当haystack剩下的数量小于needle的长度时,直接返回false
if (haystack.length()-i<needle.length()){
return -1;
}else {
if(judge(haystack,needle,i)){
return i;
}
}
}
}
return -1;
}
private static boolean judge(String haystack, String needle, int j) {
int i;
for (i=0;i<needle.length();i++){
if (haystack.charAt(j+i)!=needle.charAt(i)){
return false;
}
}
return true;
}
}
3.搜索插入位置
//for循环
class Solution {
public int searchInsert(int[] nums, int target) {
int i;
int temp = 0;
for(i=nums.length-1;i>=0;i--){
if(nums[i]==target){
temp=i;
break;
}else if(nums[i]<target){
temp = i+1;
break;
}
}
return temp;
}
}
//二分搜索
class Solution {
public int searchInsert(int[] nums, int target) {
int left=0;
int right = nums.length-1;
while(left<=right){
int mid = (left+right)/2;
if(nums[mid]==target){
return mid;
}
else if(nums[mid]>target){
right = mid-1;
}
else{
left = mid+1;
}
}
return left;
}
}
4.外观数列
//利用递归
class Solution {
public String countAndSay(int n) {
//利用 StringBuilder来进行添加操作
StringBuilder s = new StringBuilder();
if (n == 1) {
return "1";
}
int i;
String num = countAndSay(n - 1);
int count = 1;
for (i = 0; i < num.length(); i++) {
if (i == num.length() - 1){
s.append(count).append(num.charAt(i));
return s.toString();
}
if (num.charAt(i) != num.charAt(i + 1) ) {
s.append(count).append(num.charAt(i));
count = 1;
} else {
count++;
}
}
return s.toString();
}
}
5.最大子序和
/*
利用两层for循环(菜鸡做法),其实此做法可应用于寻找最大子序列的起始位置,而寻找最大和就大可不必了。
执行用时 :6 ms, 在所有 Java 提交中击败了7.06%的用户
内存消耗 :39.8 MB, 在所有 Java 提交中击败了38.20%的用户
*/
class Solution {
public int maxSubArray(int[] nums) {
int sum = nums[0];
int temp = 0;
int i,j;
for (i=0;i<nums.length;i++){
temp=nums[i];
//如果当前值大于sum,则将其保存
if (nums[i]>sum){
sum = nums[i];
}
for (j=i+1;j<nums.length;j++){
temp+=nums[j];
//当加上num[j]小于0时,换下一个i
if (temp<0){
break;
}
if (temp>sum){
sum = temp;
}
}
}
return sum;
}
}
/*
一层for循环,贪心策略
执行用时 :1 ms, 在所有 Java 提交中击败了95.18%的用户
内存消耗 :39.8 MB, 在所有 Java 提交中击败了38.20%的用户
*/
class Solution {
public int maxSubArray(int[] nums) {
int sum = nums[0];
int temp = 0;
int i ;
for (i=0;i<nums.length;i++){
temp = Math.max(temp+nums[i],nums[i]);
sum = Math.max(temp,sum);
}
return sum;
}
}
6.最后一个单词的长度
/*
利用spilt分割
执行用时 :1 ms, 在所有 Java 提交中击败了40.41%的用户
内存消耗 :38.2 MB, 在所有 Java 提交中击败了6.38%的用户
*/
class Solution {
public int lengthOfLastWord(String s) {
String[] s1 = s.split(" ");
if(s1.length==0){
return 0;
}a
return s1[s1.length-1].length();
}
}
/*
从后面开始找
执行用时 :0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗 :38.1 MB, 在所有 Java 提交中击败了6.38%的用户
*/
class Solution {
public int lengthOfLastWord(String s) {
if(s.length()==0||s==null){
return 0;
}
int end = s.length() - 1;
int count=0;
int i;
for(i=end;i>=0;i--){
if(s.charAt(i)==' '){
if(count==0) continue;
break;
}
count++;
}
return count;
}
}
7.加一
/*
执行用时 :0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗 :38.4 MB, 在所有 Java 提交中击败了5.63%的用户
*/
class Solution {
public int[] plusOne(int[] digits) {
int i;
for (i=digits.length-1;i>=0;i--){
digits[i] = (digits[i]+1)%10;
if (digits[i]!=0) return digits;
}
//当循环完还没返回时,这说明最高为也进位,即现在数字都是0,创建一个比原数组长度大1的数组,设置第0位为1
int[] temp = new int[digits.length+1];
temp[0]=1;
return temp;
}
}
8.x的平方根
/*
利用数学公式来求解,将x的平方根,转变为e(1/2*lnx)
执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:37 MB, 在所有 Java 提交中击败了5.55%的用户
*/
class Solution {
public int mySqrt(int x) {
if (x == 0) {
return 0;
}
int num = (int)Math.exp(0.5*Math.log(x));
//因为浮点数在保存时会损失精度,我们这时能比较num+1和num
return (long)(num+1)*(num+1)<=x?num+1:num;
}
}
/*
二分法求解,利用temp不断保存满足条件的mid
执行用时:2 ms, 在所有 Java 提交中击败了59.21%的用户
内存消耗:37.1 MB, 在所有 Java 提交中击败了5.55%的用户
*/
class Solution {
public int mySqrt(int x) {
int left = 0;
int right = x;
int temp = 0;
while(left<=right){
int mid = (right+left)/2;
if((long)mid*mid<=x) {
temp = mid;left=mid+1;}
else right = mid-1;
}
return temp;
}
}
9.爬楼梯
/*
超过时间限制,龟速递归。
*/
class Solution {
public int climbStairs(int n) {
if (n==1){
return 1;
}
else if (n==2){
return 2;
}
else return climbStairs(n-1)+climbStairs(n-2);
}
}
/*
动态规划,利用a,b存储到前两阶台阶的方法数
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:36.4 MB, 在所有 Java 提交中击败了5.66%的用户
*/
class Solution {
public int climbStairs(int n) {
int i;
int a = 0 ,b = 1,temp=0;
if (n==0){
return 0;
}
for (i=1;i<=n;i++){
temp = a+b;
a = b;
b = temp;
}
return temp;
}
}
10.删除排序链表中的重复元素
/*
执行用时:1 ms, 在所有 Java 提交中击败了73.30%的用户
内存消耗:39.6 MB, 在所有 Java 提交中击败了5.97%的用户
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode temp = head;
while (temp!=null&&temp.next!=null){
if (temp.val!=temp.next.val){
temp = temp.next;
}else {
temp.next = temp.next.next;
}
}
return head;
}
}