Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1460518

Browse files
add 3 random questions
1 parent8ffe904 commit1460518

File tree

3 files changed

+187
-50
lines changed

3 files changed

+187
-50
lines changed

‎build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ sourceCompatibility = 1.8
1515
targetCompatibility=1.8
1616

1717
repositories {
18-
maven { url"http://repo.maven.apache.org/maven2" }
18+
mavenCentral()
19+
maven { url"http://repo.maven.apache.org/maven2" }
1920
}
2021

2122
dependencies {
23+
compile'com.google.code.gson:gson:2.8.0'
2224
testCompilegroup:'junit',name:'junit',version:'4.12'
2325
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.google.gson.JsonArray;
4+
importcom.google.gson.JsonElement;
5+
importcom.google.gson.JsonObject;
6+
importcom.google.gson.JsonParser;
7+
8+
importjava.io.BufferedReader;
9+
importjava.io.InputStreamReader;
10+
importjava.net.HttpURLConnection;
11+
importjava.net.URL;
12+
importjava.util.ArrayList;
13+
importjava.util.Collections;
14+
importjava.util.List;
15+
importjava.util.Map;
16+
importjava.util.TreeMap;
17+
18+
publicclass_99999RandomQuestions {
19+
20+
publicstaticvoidmain(String...args) {
21+
int[]nums =newint[]{1,2,3,4,5, -1, -3, -6,3,2, -4};
22+
// int[] nums = new int[]{-1, -2, 1,2,3};
23+
// int[] nums = new int[]{-1, -2, 1,2,3,-1, -2};
24+
// List<int[]> result = subarraySum_v2(nums);
25+
26+
System.out.println(rollingString("abc",newString[]{"0 0 L","2 2 L","0 2 R"}));
27+
28+
GetMoviesgetMovies =newGetMovies();
29+
System.out.println(getMovies.getMovieTitles("spiderman"));
30+
31+
System.out.println(counting("00110"));
32+
33+
}
34+
35+
staticStringrollingString(Strings,String[]operations) {
36+
char[]chars =s.toCharArray();
37+
for (Stringoperation :operations) {
38+
String[]ops =operation.split(" ");
39+
for (inti =Integer.parseInt(ops[0]);i <=Integer.parseInt(ops[1]);i++) {
40+
if ("L".equals(ops[2])) {
41+
if (chars[i] =='a') {
42+
chars[i] ='z';
43+
}else {
44+
chars[i] -=1;
45+
}
46+
}elseif ("R".equals(ops[2])) {
47+
if (chars[i] =='z') {
48+
chars[i] ='a';
49+
}else {
50+
chars[i] +=1;
51+
}
52+
}
53+
}
54+
}
55+
returnnewString(chars);
56+
}
57+
58+
publicstaticclassGetMovies {
59+
staticString[]getMovieTitles(Stringsubstr) {
60+
finalStringurl ="https://jsonmock.hackerrank.com/api/movies/search/?Title=";
61+
List<String>movies =newArrayList<>();
62+
try {
63+
Stringresponse =getResponse(url +substr);
64+
JsonParserparser =newJsonParser();
65+
JsonElementrootNode =parser.parse(response);
66+
67+
JsonObjectdetails =rootNode.getAsJsonObject();
68+
69+
JsonElementtotalMovies =details.get("total");
70+
System.out.println(totalMovies.toString());
71+
72+
JsonElementtotalPages =details.get("total_pages");
73+
System.out.println(totalPages.toString());
74+
75+
intcurrentPage =0;
76+
while (currentPage++ <Integer.parseInt(totalPages.toString())) {
77+
nextPage(movies,currentPage,substr);
78+
}
79+
Collections.sort(movies);
80+
}catch (Exceptione) {
81+
e.printStackTrace();
82+
}
83+
String[]result =newString[movies.size()];
84+
returnmovies.toArray(result);
85+
}
86+
87+
staticvoidnextPage(List<String>movies,intcurrentPage,Stringsubstr)throwsException {
88+
finalStringurl ="https://jsonmock.hackerrank.com/api/movies/search/?Title=";
89+
Stringresponse =getResponse(url +substr +"&page=" +currentPage);
90+
JsonParserparser =newJsonParser();
91+
JsonElementrootNode =parser.parse(response);
92+
93+
JsonObjectdetails =rootNode.getAsJsonObject();
94+
JsonElementdata =details.get("data");
95+
JsonArrayjsonArray =data.getAsJsonArray();
96+
for (JsonElementeach :jsonArray) {
97+
JsonObjecttitleObject =each.getAsJsonObject();
98+
Stringtitle =titleObject.get("Title").getAsString();
99+
movies.add(title);
100+
}
101+
}
102+
103+
staticStringgetResponse(StringurlToRead)throwsException {
104+
StringBuilderresult =newStringBuilder();
105+
URLurl =newURL(urlToRead);
106+
HttpURLConnectionconn = (HttpURLConnection)url.openConnection();
107+
conn.setRequestMethod("GET");
108+
BufferedReaderrd =newBufferedReader(newInputStreamReader(conn.getInputStream()));
109+
Stringline;
110+
while ((line =rd.readLine()) !=null) {
111+
result.append(line);
112+
}
113+
rd.close();
114+
returnresult.toString();
115+
}
116+
}
117+
118+
/**Problem: count binary substrings:
119+
* The 0's and 1's are grouped consecutively and their numbers are equal
120+
* e.g.
121+
* 00110 => 3 because there are 3 substrings that have equal number of consecutive 1's and 0's: 0011, 01, 10
122+
* 10101 => 4, there are 4 substrings: 10, 01, 10, 01*/
123+
staticintcounting(Strings) {
124+
intn =s.length();
125+
/**a[i][0] denotes from most left up to i (inclusive), how many consecutive 0's
126+
* a[i][1] denotes from most left up to i (inclusive), how many consecutive 1's*/
127+
int[][]a =newint[n][2];
128+
/**a[i][0] denotes from i (inclusive) to the most right, how many consecutive 0's
129+
* b[i][0] denotes from i (inclusive) to the most right, how many consecutive 1's*/
130+
int[][]b =newint[n][2];
131+
for (inti =0;i <n;i++) {
132+
if (s.charAt(i) =='0') {
133+
a[i][0] =1 + (i -1 >=0 ?a[i -1][0] :0);
134+
}else {
135+
a[i][1] =1 + (i -1 >=0 ?a[i -1][1] :0);
136+
}
137+
}
138+
for (inti =n -1;i >=0;i--) {
139+
if (s.charAt(i) =='0') {
140+
b[i][0] =1 + (i +1 <n ?b[i +1][0] :0);
141+
}else {
142+
b[i][1] =1 + (i +1 <n ?b[i +1][1] :0);
143+
}
144+
145+
}
146+
longans =0;
147+
for (inti =0;i +1 <n;i++) {
148+
ans +=Math.min(a[i][0],b[i +1][1]);
149+
ans +=Math.min(a[i][1],b[i +1][0]);
150+
}
151+
return (int)ans;
152+
}
153+
154+
publicstaticclassSubArraySum {
155+
/**
156+
* Given an array, return the start/end indices of the contiguous subarray that have the largest sum.
157+
* Note:
158+
* 1. There could be multiple subarrays, return all of the indices.
159+
*/
160+
publicstaticList<int[]>subarraySum(int[]nums) {
161+
int[]preSums =newint[nums.length +1];
162+
for (inti =1;i <=nums.length;i++) {
163+
preSums[i] =preSums[i -1] +nums[i -1];
164+
}
165+
TreeMap<Integer,List<int[]>>preSum =newTreeMap(Collections.reverseOrder());
166+
for (inti =1;i <=nums.length;i++) {
167+
for (intj =0;j <i -1;j++) {
168+
intsum =preSums[i] -preSums[j];
169+
if (!preSum.containsKey(sum)) {
170+
List<int[]>value =newArrayList<>();
171+
value.add(newint[]{j,i -1});
172+
preSum.put(sum,value);
173+
}else {
174+
List<int[]>value =preSum.get(sum);
175+
value.add(newint[]{j,i -1});
176+
preSum.put(sum,value);
177+
}
178+
}
179+
}
180+
Map.Entry<Integer,List<int[]>>firstEntry =preSum.firstEntry();
181+
returnfirstEntry.getValue();
182+
}
183+
}
184+
}

‎src/main/java/com/fishercoder/solutions/_999MSOnlineAssessment.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp