IT討論區(116) 加拿大救生艇門已開

狼王托迪10

1001 回覆
0 Like 1 Dislike
手一黏便緊(UTC+9 2021-04-19 19:42:37
實驗羊 2021-04-19 19:51:22
狼王托迪10 2021-04-19 20:00:37
何需太高清 2021-04-19 20:04:33
實驗羊 2021-04-19 20:05:50
狼王托迪10 2021-04-19 20:07:02
Mike_Chan 2021-04-19 20:08:52
刷自信
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;

// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");

class Solution {
    public int solution(int N)
        {
            // write your code in C# 6.0 with .NET 4.5 (Mono)
            string binaryN = Convert.ToString(N, 2);
            List<int> pos = new List<int>();
            for (int i = 0; i < binaryN.Length; i++)
            {
                if (binaryN[i]=='1')
                {
                    pos.Add(i);
                }
            }
            int ans = 0;
            for (int i = 1; i < pos.Count; i++)
            {
                int r = pos[i] - pos[i - 1] - 1;
                if (r > ans)
                {
                    ans = r;
                }
            }
            return ans;
        }
}
蛾兒雪柳黃金縷 2021-04-19 20:12:59
[/quote]手巴宣布發動全行封殺蛾兒雪柳黃金縷。[/quote]
過去幫你引引戰
蛾兒雪柳黃金縷 2021-04-19 20:14:12
⋯⋯C#定.NET黎
Mike_Chan 2021-04-19 20:28:06
C#
Mike_Chan 2021-04-19 20:30:06
Task description
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
Write a function:
class Solution { public int[] solution(int[] A, int K); }
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.
For example, given
A = [3, 8, 9, 7, 6]
K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:
[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
[6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
[7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
For another example, given
A = [0, 0, 0]
K = 1
the function should return [0, 0, 0]
Given
A = [1, 2, 3, 4]
K = 4
the function should return [1, 2, 3, 4]
Assume that:
N and K are integers within the range [0..100];
each element of array A is an integer within the range [−1,000..1,000].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

考果陣有咁易就好
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;

// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");

class Solution {
    public int[] solution(int[] A, int K) {
        // write your code in C# 6.0 with .NET 4.5 (Mono)
            if (A.Length == 0)
            {
                return A;
            }
            Queue<int> que = new Queue<int>(A.Reverse());
            for (int i = 0; i < K; i++)
            {
                int picked = que.Dequeue();
                que.Enqueue(picked);
            }
            return que.ToArray().Reverse().ToArray();
    }
}
加奈美-Fight!!! 2021-04-19 20:33:45
心繫社會 2021-04-19 20:42:19
咁樣會唔會好d
public void rotate(int[] arr, int k) {
        int n = arr.length;
        int pre = arr[0], cur = 0;

        for (int i = 0; i < n; ++i) {
            int to = (cur + k) % n;
            int temp = arr[to];
            arr[to] = pre;
            pre = temp;
            cur = to;
        }
    }
Mike_Chan 2021-04-19 20:46:05
我一唔用data structure就會hang機
目標只係correctness 100%
performance隨緣啦邊撚識寫靚code
心繫社會 2021-04-19 20:49:16
我都係操左好耐 先有入門水平
Mike_Chan 2021-04-19 21:27:17
問題: array搵單丁果粒數
100%, 100%, 100%
using System;
using System.Collections.Generic;
using System.Linq;
class Solution {
    public int solution(int[] A)
        {
            HashSet<int> hash = new HashSet<int>();
            foreach (int i in A)
            {
                if (hash.Contains(i))
                {
                    hash.Remove(i);
                }
                else
                {
                    hash.Add(i);
                }
            }
            return hash.First();
        }
}
手一黏便緊(UTC+9 2021-04-19 22:14:00
xor
Code4Food 2021-04-19 23:31:03
XOR
膠の呼吸 2021-04-19 23:35:18
小貓咪 2021-04-20 00:11:30
data analyst 白紙
識python shell script SQL
市價幾多?
Mike_Chan 2021-04-20 00:46:09
唔明
手一黏便緊(UTC+9 2021-04-20 00:55:14
p xor q xor q = p

Btw手巴個reply被忽略了 果然大家都block柒柒手
Code4Food 2021-04-20 00:56:33
將所有數全部XOR晒,重複嘅會變0,單丁個會留返響度。

in C++:

int solution(const std::vector<int>& A) {
  int answer = 0;
  for (int ai : A) {
    answer ^= ai;
  }
  return answer;
}
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞