Code Monkey home page Code Monkey logo

leetcode-testttttt's Introduction

LeetCode Testtttt

implementation by Swift

Product array

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[I]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6].

[ 1, a[0], a[0]*a[1], a[0]*a[1]*a[2], ] [ a[1]*a[2]*a[3], a[2]*a[3], a[3], 1, ]

   func productArray(inputArray: Array<Int>) -> Array<Int>  {
        guard !inputArray.isEmpty else {
           // throws
           return []
       }
           let arrayCount = inputArray.count
           var outPutArray = Array(repeating: 1, count: arrayCount)
           for n in 1...(arrayCount - 1) {
               outPutArray[n] = outPutArray[n - 1] * inputArray[n - 1]
           }
           var right = 1
           for n in 0...(arrayCount - 1) {
               let index = arrayCount - 1 - n
               outPutArray[index] *= right
               right *= inputArray[index]
           }
           debugPrint("inputArray ---- \(inputArray); outPutArray --- \(outPutArray);")
           return outPutArray
    }

Number first

Given an array nums, write a function to move all non-zero numbers to the head of it while maintaining the relative order of the non-zero numbers. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note: You must do this in-place without making a copy of the array. Minimize the total number of operations.

    // MARK: - 2. Number first
    func NumberFirst(input: Array<Int>) -> Array<Int> {
        var inputArray = input
         guard !inputArray.isEmpty else {
            // throws
            return []
        }
        let arrayCount = inputArray.count
        var num = 0
        var zeroCount = 0
        for n in 0...(arrayCount - 1) {
            if inputArray[n] == 0 {
                zeroCount += 1;
            } else {
                inputArray[num] = inputArray[n]
                num += 1
            }
         }
        for _ in 0...(zeroCount - 1) {
            inputArray[num] = 0
            num += 1
        }
        debugPrint("NumberFirst ---- \(inputArray)")
        return inputArray
    }

Power

Implement pow(x, n).

   func Power(num1:Double, num2:Int) -> Double {
       let x = num1
       var n = num2
       if x == 0 && n == 0 {
          // throws
           return 0
       }
       var isNegative = false
       if n < 0 {
           n *= -1
           isNegative = true
       }
       let result = pow(x: x, n: n)
       if isNegative {
           return 1.0 / result
       } else {
           return result
       }
   }
   
   func pow(x:Double, n:Int) -> Double {
       if n == 0 {
           return 1
       } else {
           let result = pow(x: x, n: n / 2)
           if n % 2 != 0 {
               return x * result * result
           } else {
               return result * result
           }
       }
   }

leetcode-testttttt's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.