【LeetCode从零单排】No27.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.

代码

public class Solution {
    public int removeElement(int[] A, int elem) {
    int next = 0;
    for(int i = 0; i < A.length; i++) {
        if(A[i] != elem) {
            A[next++] = A[i];
        }
    }
    return next;
    }
}

代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

时间: 2024-08-10 02:11:19

【LeetCode从零单排】No27.Remove Element的相关文章

【LeetCode从零单排】No118 Pascal&#39;s Triangle

题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码 public class Solution { public List<List<Integer>> generate(int numRows) { List<List

【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

题目 题目要求:去除sort int数组中的重复项. 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 exam

【LeetCode从零单排】No.169 Majority Element(hashmap用法)

题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits:Special thanks to

【LeetCode从零单排】No83 Remove Duplicates from Sorted List

题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 代码 public static ListNode deleteDuplicates(ListNode head)

【LeetCode从零单排】No70.ClimbingStairs

题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这道题一共有三个方法实现(我想到的): 1.递归(对应代码climbStairs) 如果楼梯有n层,我们回退到n-

【LeetCode从零单刷】Majority Element

题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. 解答: 寻找主元素.主元素有几个性质: 主元素,

【LeetCode从零单排】No19.RemoveNthNodeFromEndofList

题目 这是道链表的简单应用题目,删除从结尾数第n个节点. Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1-&g

【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

题目 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 又

LeetCode解题思路:27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't matter w