Skip to content

Conversation

@jameszu
Copy link
Contributor

@jameszu jameszu commented Oct 17, 2020

Rotate Array & Single Number II

Description

Rotate Array:

Given an array, rotate the array to the right by k steps, where k is non-negative. Follow up: Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. Could you do it in-place with O(1) extra space? 

Idea:
One way is to append the first (length-k) elements to the back, and delete the first (length-k) elements in the array.

Single Number II:

Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. 

Idea:
One way is to use hashtable and count how many elements repeated and the one with 1 would be the return value.
Or bitwise way think a[i]b[i] as a binary number, to indicate (the sum of all i-th bit of num) % 3.
You can use num[0:2] as a, b just with slight changes. Finally after a loop the b would be our return value.

Put check marks:

Have you made changes in README file ?

  • Added problem & solution under correct topic.
  • Specified Space & Time complexity.
  • Specified difficulty level, tag & Note(if any).

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Please also note any relevant details for your test configuration.

Rotate Array:

  • Input: [1,2,3,4,5,6,7]
    3
    Expected output: [5,6,7,1,2,3,4]
  • Input: [-1,-100,3,99]
    2
    Expected output: [3,99,-1,-100]

Single Number II

  • Input: [2,2,3,2] Expected output: 3
  • Input: [0,1,0,1,0,1,99] Expected output: 99

Make sure all below guidelines are followed else PR will get Reject:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code so that it is easy to understand
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules
@GouravRusiya30
Copy link
Member

@jameszu We already have good collections on array problems.
Can you contribute to some other topic ?

@jameszu
Copy link
Contributor Author

jameszu commented Oct 17, 2020

Okay sure I will look into others

@GouravRusiya30
Copy link
Member

@jameszu Thanks !!!
I will wait for more solutions before merging this PR

@jameszu
Copy link
Contributor Author

jameszu commented Oct 17, 2020

@GouravRusiya30 Okay I've contributed another question

@jameszu jameszu changed the title Added python code for 189. Rotate Array Added python code for189. Rotate Array & 137, Single Number II Oct 17, 2020
@jameszu jameszu changed the title Added python code for189. Rotate Array & 137, Single Number II Added python code for 189. Rotate Array & 137. Single Number II Oct 17, 2020
@GouravRusiya30
Copy link
Member

Nice !!!
Merging this PR

@GouravRusiya30 GouravRusiya30 merged commit e758b5b into codedecks-in:master Oct 17, 2020
@welcome
Copy link

welcome bot commented Oct 17, 2020

Your code looks great! Congrats, I've gone ahead and merged your first pull request! Keep it up! alt text

tarunsinghdev pushed a commit to tarunsinghdev/LeetCode-Solutions that referenced this pull request Oct 29, 2020
Co-authored-by: Chi Hi Ong <ongch@seagroup.com> Co-authored-by: Gourav Rusiya <gouravrusiya.lnct@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants